1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
5 * Copyright (c) 2009-2013 LSI Corporation
6 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
8 *
9 * FILE: megaraid_sas_fusion.c
10 *
11 * Authors: Broadcom Inc.
12 * Sumant Patro
13 * Adam Radford
14 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
16 *
17 * Send feedback to: megaraidlinux.pdl@broadcom.com
18 */
19
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/uaccess.h>
31 #include <linux/fs.h>
32 #include <linux/compat.h>
33 #include <linux/blkdev.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/workqueue.h>
38 #include <linux/irq_poll.h>
39
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_dbg.h>
45 #include <linux/dmi.h>
46
47 #include "megaraid_sas_fusion.h"
48 #include "megaraid_sas.h"
49
50
51 extern void
52 megasas_complete_cmd(struct megasas_instance *instance,
53 struct megasas_cmd *cmd, u8 alt_status);
54 int
55 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
56 int seconds);
57
58 int
59 megasas_clear_intr_fusion(struct megasas_instance *instance);
60
61 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
62
63 extern u32 megasas_dbg_lvl;
64 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
65 int initial);
66 extern struct megasas_mgmt_info megasas_mgmt_info;
67 extern unsigned int resetwaittime;
68 extern unsigned int dual_qdepth_disable;
69 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
70 static void megasas_free_reply_fusion(struct megasas_instance *instance);
71 static inline
72 void megasas_configure_queue_sizes(struct megasas_instance *instance);
73 static void megasas_fusion_crash_dump(struct megasas_instance *instance);
74
75 /**
76 * megasas_adp_reset_wait_for_ready - initiate chip reset and wait for
77 * controller to come to ready state
78 * @instance: adapter's soft state
79 * @do_adp_reset: If true, do a chip reset
80 * @ocr_context: If called from OCR context this will
81 * be set to 1, else 0
82 *
83 * This function initiates a chip reset followed by a wait for controller to
84 * transition to ready state.
85 * During this, driver will block all access to PCI config space from userspace
86 */
87 int
megasas_adp_reset_wait_for_ready(struct megasas_instance * instance,bool do_adp_reset,int ocr_context)88 megasas_adp_reset_wait_for_ready(struct megasas_instance *instance,
89 bool do_adp_reset,
90 int ocr_context)
91 {
92 int ret = FAILED;
93
94 /*
95 * Block access to PCI config space from userspace
96 * when diag reset is initiated from driver
97 */
98 if (megasas_dbg_lvl & OCR_DEBUG)
99 dev_info(&instance->pdev->dev,
100 "Block access to PCI config space %s %d\n",
101 __func__, __LINE__);
102
103 pci_cfg_access_lock(instance->pdev);
104
105 if (do_adp_reset) {
106 if (instance->instancet->adp_reset
107 (instance, instance->reg_set))
108 goto out;
109 }
110
111 /* Wait for FW to become ready */
112 if (megasas_transition_to_ready(instance, ocr_context)) {
113 dev_warn(&instance->pdev->dev,
114 "Failed to transition controller to ready for scsi%d.\n",
115 instance->host->host_no);
116 goto out;
117 }
118
119 ret = SUCCESS;
120 out:
121 if (megasas_dbg_lvl & OCR_DEBUG)
122 dev_info(&instance->pdev->dev,
123 "Unlock access to PCI config space %s %d\n",
124 __func__, __LINE__);
125
126 pci_cfg_access_unlock(instance->pdev);
127
128 return ret;
129 }
130
131 /**
132 * megasas_check_same_4gb_region - check if allocation
133 * crosses same 4GB boundary or not
134 * @instance: adapter's soft instance
135 * @start_addr: start address of DMA allocation
136 * @size: size of allocation in bytes
137 * @return: true : allocation does not cross same
138 * 4GB boundary
139 * false: allocation crosses same
140 * 4GB boundary
141 */
megasas_check_same_4gb_region(struct megasas_instance * instance,dma_addr_t start_addr,size_t size)142 static inline bool megasas_check_same_4gb_region
143 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
144 {
145 dma_addr_t end_addr;
146
147 end_addr = start_addr + size;
148
149 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
150 dev_err(&instance->pdev->dev,
151 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
152 (unsigned long long)start_addr,
153 (unsigned long long)end_addr);
154 return false;
155 }
156
157 return true;
158 }
159
160 /**
161 * megasas_enable_intr_fusion - Enables interrupts
162 * @instance: adapter's soft instance
163 */
164 static void
megasas_enable_intr_fusion(struct megasas_instance * instance)165 megasas_enable_intr_fusion(struct megasas_instance *instance)
166 {
167 struct megasas_register_set __iomem *regs;
168 regs = instance->reg_set;
169
170 instance->mask_interrupts = 0;
171 /* For Thunderbolt/Invader also clear intr on enable */
172 writel(~0, ®s->outbound_intr_status);
173 readl(®s->outbound_intr_status);
174
175 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
176
177 /* Dummy readl to force pci flush */
178 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
179 __func__, readl(®s->outbound_intr_mask));
180 }
181
182 /**
183 * megasas_disable_intr_fusion - Disables interrupt
184 * @instance: adapter's soft instance
185 */
186 static void
megasas_disable_intr_fusion(struct megasas_instance * instance)187 megasas_disable_intr_fusion(struct megasas_instance *instance)
188 {
189 u32 mask = 0xFFFFFFFF;
190 struct megasas_register_set __iomem *regs;
191 regs = instance->reg_set;
192 instance->mask_interrupts = 1;
193
194 writel(mask, ®s->outbound_intr_mask);
195 /* Dummy readl to force pci flush */
196 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
197 __func__, readl(®s->outbound_intr_mask));
198 }
199
200 int
megasas_clear_intr_fusion(struct megasas_instance * instance)201 megasas_clear_intr_fusion(struct megasas_instance *instance)
202 {
203 u32 status;
204 struct megasas_register_set __iomem *regs;
205 regs = instance->reg_set;
206 /*
207 * Check if it is our interrupt
208 */
209 status = megasas_readl(instance,
210 ®s->outbound_intr_status);
211
212 if (status & 1) {
213 writel(status, ®s->outbound_intr_status);
214 readl(®s->outbound_intr_status);
215 return 1;
216 }
217 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
218 return 0;
219
220 return 1;
221 }
222
223 static inline void
megasas_sdev_busy_inc(struct megasas_instance * instance,struct scsi_cmnd * scmd)224 megasas_sdev_busy_inc(struct megasas_instance *instance,
225 struct scsi_cmnd *scmd)
226 {
227 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
228 struct MR_PRIV_DEVICE *mr_device_priv_data =
229 scmd->device->hostdata;
230 atomic_inc(&mr_device_priv_data->sdev_priv_busy);
231 }
232 }
233
234 static inline void
megasas_sdev_busy_dec(struct megasas_instance * instance,struct scsi_cmnd * scmd)235 megasas_sdev_busy_dec(struct megasas_instance *instance,
236 struct scsi_cmnd *scmd)
237 {
238 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
239 struct MR_PRIV_DEVICE *mr_device_priv_data =
240 scmd->device->hostdata;
241 atomic_dec(&mr_device_priv_data->sdev_priv_busy);
242 }
243 }
244
245 static inline int
megasas_sdev_busy_read(struct megasas_instance * instance,struct scsi_cmnd * scmd)246 megasas_sdev_busy_read(struct megasas_instance *instance,
247 struct scsi_cmnd *scmd)
248 {
249 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
250 struct MR_PRIV_DEVICE *mr_device_priv_data =
251 scmd->device->hostdata;
252 return atomic_read(&mr_device_priv_data->sdev_priv_busy);
253 }
254 return 0;
255 }
256
257 /**
258 * megasas_get_cmd_fusion - Get a command from the free pool
259 * @instance: Adapter soft state
260 * @blk_tag: Command tag
261 *
262 * Returns a blk_tag indexed mpt frame
263 */
megasas_get_cmd_fusion(struct megasas_instance * instance,u32 blk_tag)264 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
265 *instance, u32 blk_tag)
266 {
267 struct fusion_context *fusion;
268
269 fusion = instance->ctrl_context;
270 return fusion->cmd_list[blk_tag];
271 }
272
273 /**
274 * megasas_return_cmd_fusion - Return a cmd to free command pool
275 * @instance: Adapter soft state
276 * @cmd: Command packet to be returned to free command pool
277 */
megasas_return_cmd_fusion(struct megasas_instance * instance,struct megasas_cmd_fusion * cmd)278 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
279 struct megasas_cmd_fusion *cmd)
280 {
281 cmd->scmd = NULL;
282 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
283 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
284 cmd->cmd_completed = false;
285 }
286
287 /**
288 * megasas_write_64bit_req_desc - PCI writes 64bit request descriptor
289 * @instance: Adapter soft state
290 * @req_desc: 64bit Request descriptor
291 */
292 static void
megasas_write_64bit_req_desc(struct megasas_instance * instance,union MEGASAS_REQUEST_DESCRIPTOR_UNION * req_desc)293 megasas_write_64bit_req_desc(struct megasas_instance *instance,
294 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
295 {
296 #if defined(writeq) && defined(CONFIG_64BIT)
297 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
298 le32_to_cpu(req_desc->u.low));
299 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
300 #else
301 unsigned long flags;
302 spin_lock_irqsave(&instance->hba_lock, flags);
303 writel(le32_to_cpu(req_desc->u.low),
304 &instance->reg_set->inbound_low_queue_port);
305 writel(le32_to_cpu(req_desc->u.high),
306 &instance->reg_set->inbound_high_queue_port);
307 spin_unlock_irqrestore(&instance->hba_lock, flags);
308 #endif
309 }
310
311 /**
312 * megasas_fire_cmd_fusion - Sends command to the FW
313 * @instance: Adapter soft state
314 * @req_desc: 32bit or 64bit Request descriptor
315 *
316 * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
317 * Prior to AERO_SERIES support 64 bit Descriptor.
318 */
319 static void
megasas_fire_cmd_fusion(struct megasas_instance * instance,union MEGASAS_REQUEST_DESCRIPTOR_UNION * req_desc)320 megasas_fire_cmd_fusion(struct megasas_instance *instance,
321 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
322 {
323 if (instance->atomic_desc_support)
324 writel(le32_to_cpu(req_desc->u.low),
325 &instance->reg_set->inbound_single_queue_port);
326 else
327 megasas_write_64bit_req_desc(instance, req_desc);
328 }
329
330 /**
331 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
332 * @instance: Adapter soft state
333 * @fw_boot_context: Whether this function called during probe or after OCR
334 *
335 * This function is only for fusion controllers.
336 * Update host can queue, if firmware downgrade max supported firmware commands.
337 * Firmware upgrade case will be skipped because underlying firmware has
338 * more resource than exposed to the OS.
339 *
340 */
341 static void
megasas_fusion_update_can_queue(struct megasas_instance * instance,int fw_boot_context)342 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
343 {
344 u16 cur_max_fw_cmds = 0;
345 u16 ldio_threshold = 0;
346
347 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
348 if (instance->adapter_type < VENTURA_SERIES)
349 cur_max_fw_cmds =
350 megasas_readl(instance,
351 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
352
353 if (dual_qdepth_disable || !cur_max_fw_cmds)
354 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
355 else
356 ldio_threshold =
357 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
358
359 dev_info(&instance->pdev->dev,
360 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
361 cur_max_fw_cmds, ldio_threshold);
362
363 if (fw_boot_context == OCR_CONTEXT) {
364 cur_max_fw_cmds = cur_max_fw_cmds - 1;
365 if (cur_max_fw_cmds < instance->max_fw_cmds) {
366 instance->cur_can_queue =
367 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
368 MEGASAS_FUSION_IOCTL_CMDS);
369 instance->host->can_queue = instance->cur_can_queue;
370 instance->ldio_threshold = ldio_threshold;
371 }
372 } else {
373 instance->max_fw_cmds = cur_max_fw_cmds;
374 instance->ldio_threshold = ldio_threshold;
375
376 if (reset_devices)
377 instance->max_fw_cmds = min(instance->max_fw_cmds,
378 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
379 /*
380 * Reduce the max supported cmds by 1. This is to ensure that the
381 * reply_q_sz (1 more than the max cmd that driver may send)
382 * does not exceed max cmds that the FW can support
383 */
384 instance->max_fw_cmds = instance->max_fw_cmds-1;
385 }
386 }
387
388 static inline void
megasas_get_msix_index(struct megasas_instance * instance,struct scsi_cmnd * scmd,struct megasas_cmd_fusion * cmd,u8 data_arms)389 megasas_get_msix_index(struct megasas_instance *instance,
390 struct scsi_cmnd *scmd,
391 struct megasas_cmd_fusion *cmd,
392 u8 data_arms)
393 {
394 if (instance->perf_mode == MR_BALANCED_PERF_MODE &&
395 (megasas_sdev_busy_read(instance, scmd) >
396 (data_arms * MR_DEVICE_HIGH_IOPS_DEPTH))) {
397 cmd->request_desc->SCSIIO.MSIxIndex =
398 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
399 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
400 } else if (instance->msix_load_balance) {
401 cmd->request_desc->SCSIIO.MSIxIndex =
402 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
403 instance->msix_vectors));
404 } else if (instance->host->nr_hw_queues > 1) {
405 u32 tag = blk_mq_unique_tag(scsi_cmd_to_rq(scmd));
406
407 cmd->request_desc->SCSIIO.MSIxIndex = blk_mq_unique_tag_to_hwq(tag) +
408 instance->low_latency_index_start;
409 } else {
410 cmd->request_desc->SCSIIO.MSIxIndex =
411 instance->reply_map[raw_smp_processor_id()];
412 }
413 }
414
415 /**
416 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
417 * @instance: Adapter soft state
418 */
419 void
megasas_free_cmds_fusion(struct megasas_instance * instance)420 megasas_free_cmds_fusion(struct megasas_instance *instance)
421 {
422 int i;
423 struct fusion_context *fusion = instance->ctrl_context;
424 struct megasas_cmd_fusion *cmd;
425
426 if (fusion->sense)
427 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
428 fusion->sense_phys_addr);
429
430 /* SG */
431 if (fusion->cmd_list) {
432 for (i = 0; i < instance->max_mpt_cmds; i++) {
433 cmd = fusion->cmd_list[i];
434 if (cmd) {
435 if (cmd->sg_frame)
436 dma_pool_free(fusion->sg_dma_pool,
437 cmd->sg_frame,
438 cmd->sg_frame_phys_addr);
439 }
440 kfree(cmd);
441 }
442 kfree(fusion->cmd_list);
443 }
444
445 if (fusion->sg_dma_pool) {
446 dma_pool_destroy(fusion->sg_dma_pool);
447 fusion->sg_dma_pool = NULL;
448 }
449 if (fusion->sense_dma_pool) {
450 dma_pool_destroy(fusion->sense_dma_pool);
451 fusion->sense_dma_pool = NULL;
452 }
453
454
455 /* Reply Frame, Desc*/
456 if (instance->is_rdpq)
457 megasas_free_rdpq_fusion(instance);
458 else
459 megasas_free_reply_fusion(instance);
460
461 /* Request Frame, Desc*/
462 if (fusion->req_frames_desc)
463 dma_free_coherent(&instance->pdev->dev,
464 fusion->request_alloc_sz, fusion->req_frames_desc,
465 fusion->req_frames_desc_phys);
466 if (fusion->io_request_frames)
467 dma_pool_free(fusion->io_request_frames_pool,
468 fusion->io_request_frames,
469 fusion->io_request_frames_phys);
470 if (fusion->io_request_frames_pool) {
471 dma_pool_destroy(fusion->io_request_frames_pool);
472 fusion->io_request_frames_pool = NULL;
473 }
474 }
475
476 /**
477 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
478 * @instance: Adapter soft state
479 *
480 */
megasas_create_sg_sense_fusion(struct megasas_instance * instance)481 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
482 {
483 int i;
484 u16 max_cmd;
485 struct fusion_context *fusion;
486 struct megasas_cmd_fusion *cmd;
487 int sense_sz;
488 u32 offset;
489
490 fusion = instance->ctrl_context;
491 max_cmd = instance->max_fw_cmds;
492 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
493
494 fusion->sg_dma_pool =
495 dma_pool_create("mr_sg", &instance->pdev->dev,
496 instance->max_chain_frame_sz,
497 MR_DEFAULT_NVME_PAGE_SIZE, 0);
498 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
499 fusion->sense_dma_pool =
500 dma_pool_create("mr_sense", &instance->pdev->dev,
501 sense_sz, 64, 0);
502
503 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
504 dev_err(&instance->pdev->dev,
505 "Failed from %s %d\n", __func__, __LINE__);
506 return -ENOMEM;
507 }
508
509 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
510 GFP_KERNEL, &fusion->sense_phys_addr);
511 if (!fusion->sense) {
512 dev_err(&instance->pdev->dev,
513 "failed from %s %d\n", __func__, __LINE__);
514 return -ENOMEM;
515 }
516
517 /* sense buffer, request frame and reply desc pool requires to be in
518 * same 4 gb region. Below function will check this.
519 * In case of failure, new pci pool will be created with updated
520 * alignment.
521 * Older allocation and pool will be destroyed.
522 * Alignment will be used such a way that next allocation if success,
523 * will always meet same 4gb region requirement.
524 * Actual requirement is not alignment, but we need start and end of
525 * DMA address must have same upper 32 bit address.
526 */
527
528 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
529 sense_sz)) {
530 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
531 fusion->sense_phys_addr);
532 fusion->sense = NULL;
533 dma_pool_destroy(fusion->sense_dma_pool);
534
535 fusion->sense_dma_pool =
536 dma_pool_create("mr_sense_align", &instance->pdev->dev,
537 sense_sz, roundup_pow_of_two(sense_sz),
538 0);
539 if (!fusion->sense_dma_pool) {
540 dev_err(&instance->pdev->dev,
541 "Failed from %s %d\n", __func__, __LINE__);
542 return -ENOMEM;
543 }
544 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
545 GFP_KERNEL,
546 &fusion->sense_phys_addr);
547 if (!fusion->sense) {
548 dev_err(&instance->pdev->dev,
549 "failed from %s %d\n", __func__, __LINE__);
550 return -ENOMEM;
551 }
552 }
553
554 /*
555 * Allocate and attach a frame to each of the commands in cmd_list
556 */
557 for (i = 0; i < max_cmd; i++) {
558 cmd = fusion->cmd_list[i];
559 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
560 GFP_KERNEL, &cmd->sg_frame_phys_addr);
561
562 offset = SCSI_SENSE_BUFFERSIZE * i;
563 cmd->sense = (u8 *)fusion->sense + offset;
564 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
565
566 if (!cmd->sg_frame) {
567 dev_err(&instance->pdev->dev,
568 "Failed from %s %d\n", __func__, __LINE__);
569 return -ENOMEM;
570 }
571 }
572
573 /* create sense buffer for the raid 1/10 fp */
574 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
575 cmd = fusion->cmd_list[i];
576 offset = SCSI_SENSE_BUFFERSIZE * i;
577 cmd->sense = (u8 *)fusion->sense + offset;
578 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
579
580 }
581
582 return 0;
583 }
584
585 static int
megasas_alloc_cmdlist_fusion(struct megasas_instance * instance)586 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
587 {
588 u32 max_mpt_cmd, i, j;
589 struct fusion_context *fusion;
590
591 fusion = instance->ctrl_context;
592
593 max_mpt_cmd = instance->max_mpt_cmds;
594
595 /*
596 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
597 * Allocate the dynamic array first and then allocate individual
598 * commands.
599 */
600 fusion->cmd_list =
601 kzalloc_objs(struct megasas_cmd_fusion *, max_mpt_cmd);
602 if (!fusion->cmd_list) {
603 dev_err(&instance->pdev->dev,
604 "Failed from %s %d\n", __func__, __LINE__);
605 return -ENOMEM;
606 }
607
608 for (i = 0; i < max_mpt_cmd; i++) {
609 fusion->cmd_list[i] = kzalloc_obj(struct megasas_cmd_fusion);
610 if (!fusion->cmd_list[i]) {
611 for (j = 0; j < i; j++)
612 kfree(fusion->cmd_list[j]);
613 kfree(fusion->cmd_list);
614 dev_err(&instance->pdev->dev,
615 "Failed from %s %d\n", __func__, __LINE__);
616 return -ENOMEM;
617 }
618 }
619
620 return 0;
621 }
622
623 static int
megasas_alloc_request_fusion(struct megasas_instance * instance)624 megasas_alloc_request_fusion(struct megasas_instance *instance)
625 {
626 struct fusion_context *fusion;
627
628 fusion = instance->ctrl_context;
629
630 retry_alloc:
631 fusion->io_request_frames_pool =
632 dma_pool_create("mr_ioreq", &instance->pdev->dev,
633 fusion->io_frames_alloc_sz, 16, 0);
634
635 if (!fusion->io_request_frames_pool) {
636 dev_err(&instance->pdev->dev,
637 "Failed from %s %d\n", __func__, __LINE__);
638 return -ENOMEM;
639 }
640
641 fusion->io_request_frames =
642 dma_pool_alloc(fusion->io_request_frames_pool,
643 GFP_KERNEL | __GFP_NOWARN,
644 &fusion->io_request_frames_phys);
645 if (!fusion->io_request_frames) {
646 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
647 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
648 dma_pool_destroy(fusion->io_request_frames_pool);
649 megasas_configure_queue_sizes(instance);
650 goto retry_alloc;
651 } else {
652 dev_err(&instance->pdev->dev,
653 "Failed from %s %d\n", __func__, __LINE__);
654 return -ENOMEM;
655 }
656 }
657
658 if (!megasas_check_same_4gb_region(instance,
659 fusion->io_request_frames_phys,
660 fusion->io_frames_alloc_sz)) {
661 dma_pool_free(fusion->io_request_frames_pool,
662 fusion->io_request_frames,
663 fusion->io_request_frames_phys);
664 fusion->io_request_frames = NULL;
665 dma_pool_destroy(fusion->io_request_frames_pool);
666
667 fusion->io_request_frames_pool =
668 dma_pool_create("mr_ioreq_align",
669 &instance->pdev->dev,
670 fusion->io_frames_alloc_sz,
671 roundup_pow_of_two(fusion->io_frames_alloc_sz),
672 0);
673
674 if (!fusion->io_request_frames_pool) {
675 dev_err(&instance->pdev->dev,
676 "Failed from %s %d\n", __func__, __LINE__);
677 return -ENOMEM;
678 }
679
680 fusion->io_request_frames =
681 dma_pool_alloc(fusion->io_request_frames_pool,
682 GFP_KERNEL | __GFP_NOWARN,
683 &fusion->io_request_frames_phys);
684
685 if (!fusion->io_request_frames) {
686 dev_err(&instance->pdev->dev,
687 "Failed from %s %d\n", __func__, __LINE__);
688 return -ENOMEM;
689 }
690 }
691
692 fusion->req_frames_desc =
693 dma_alloc_coherent(&instance->pdev->dev,
694 fusion->request_alloc_sz,
695 &fusion->req_frames_desc_phys, GFP_KERNEL);
696 if (!fusion->req_frames_desc) {
697 dev_err(&instance->pdev->dev,
698 "Failed from %s %d\n", __func__, __LINE__);
699 return -ENOMEM;
700 }
701
702 return 0;
703 }
704
705 static int
megasas_alloc_reply_fusion(struct megasas_instance * instance)706 megasas_alloc_reply_fusion(struct megasas_instance *instance)
707 {
708 int i, count;
709 struct fusion_context *fusion;
710 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
711 fusion = instance->ctrl_context;
712
713 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
714 count += instance->iopoll_q_count;
715
716 fusion->reply_frames_desc_pool =
717 dma_pool_create("mr_reply", &instance->pdev->dev,
718 fusion->reply_alloc_sz * count, 16, 0);
719
720 if (!fusion->reply_frames_desc_pool) {
721 dev_err(&instance->pdev->dev,
722 "Failed from %s %d\n", __func__, __LINE__);
723 return -ENOMEM;
724 }
725
726 fusion->reply_frames_desc[0] =
727 dma_pool_alloc(fusion->reply_frames_desc_pool,
728 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
729 if (!fusion->reply_frames_desc[0]) {
730 dev_err(&instance->pdev->dev,
731 "Failed from %s %d\n", __func__, __LINE__);
732 return -ENOMEM;
733 }
734
735 if (!megasas_check_same_4gb_region(instance,
736 fusion->reply_frames_desc_phys[0],
737 (fusion->reply_alloc_sz * count))) {
738 dma_pool_free(fusion->reply_frames_desc_pool,
739 fusion->reply_frames_desc[0],
740 fusion->reply_frames_desc_phys[0]);
741 fusion->reply_frames_desc[0] = NULL;
742 dma_pool_destroy(fusion->reply_frames_desc_pool);
743
744 fusion->reply_frames_desc_pool =
745 dma_pool_create("mr_reply_align",
746 &instance->pdev->dev,
747 fusion->reply_alloc_sz * count,
748 roundup_pow_of_two(fusion->reply_alloc_sz * count),
749 0);
750
751 if (!fusion->reply_frames_desc_pool) {
752 dev_err(&instance->pdev->dev,
753 "Failed from %s %d\n", __func__, __LINE__);
754 return -ENOMEM;
755 }
756
757 fusion->reply_frames_desc[0] =
758 dma_pool_alloc(fusion->reply_frames_desc_pool,
759 GFP_KERNEL,
760 &fusion->reply_frames_desc_phys[0]);
761
762 if (!fusion->reply_frames_desc[0]) {
763 dev_err(&instance->pdev->dev,
764 "Failed from %s %d\n", __func__, __LINE__);
765 return -ENOMEM;
766 }
767 }
768
769 reply_desc = fusion->reply_frames_desc[0];
770 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
771 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
772
773 /* This is not a rdpq mode, but driver still populate
774 * reply_frame_desc array to use same msix index in ISR path.
775 */
776 for (i = 0; i < (count - 1); i++)
777 fusion->reply_frames_desc[i + 1] =
778 fusion->reply_frames_desc[i] +
779 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
780
781 return 0;
782 }
783
784 static int
megasas_alloc_rdpq_fusion(struct megasas_instance * instance)785 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
786 {
787 int i, j, k, msix_count;
788 struct fusion_context *fusion;
789 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
790 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
791 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
792 u8 dma_alloc_count, abs_index;
793 u32 chunk_size, array_size, offset;
794
795 fusion = instance->ctrl_context;
796 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
797 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
798 MAX_MSIX_QUEUES_FUSION;
799
800 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
801 array_size, &fusion->rdpq_phys,
802 GFP_KERNEL);
803 if (!fusion->rdpq_virt) {
804 dev_err(&instance->pdev->dev,
805 "Failed from %s %d\n", __func__, __LINE__);
806 return -ENOMEM;
807 }
808
809 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
810 msix_count += instance->iopoll_q_count;
811
812 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
813 &instance->pdev->dev,
814 chunk_size, 16, 0);
815 fusion->reply_frames_desc_pool_align =
816 dma_pool_create("mr_rdpq_align",
817 &instance->pdev->dev,
818 chunk_size,
819 roundup_pow_of_two(chunk_size),
820 0);
821
822 if (!fusion->reply_frames_desc_pool ||
823 !fusion->reply_frames_desc_pool_align) {
824 dev_err(&instance->pdev->dev,
825 "Failed from %s %d\n", __func__, __LINE__);
826 return -ENOMEM;
827 }
828
829 /*
830 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
831 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
832 * within 4GB boundary and also reply queues in a set must have same
833 * upper 32-bits in their memory address. so here driver is allocating the
834 * DMA'able memory for reply queues according. Driver uses limitation of
835 * VENTURA_SERIES to manage INVADER_SERIES as well.
836 */
837 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
838
839 for (i = 0; i < dma_alloc_count; i++) {
840 rdpq_chunk_virt[i] =
841 dma_pool_alloc(fusion->reply_frames_desc_pool,
842 GFP_KERNEL, &rdpq_chunk_phys[i]);
843 if (!rdpq_chunk_virt[i]) {
844 dev_err(&instance->pdev->dev,
845 "Failed from %s %d\n", __func__, __LINE__);
846 return -ENOMEM;
847 }
848 /* reply desc pool requires to be in same 4 gb region.
849 * Below function will check this.
850 * In case of failure, new pci pool will be created with updated
851 * alignment.
852 * For RDPQ buffers, driver always allocate two separate pci pool.
853 * Alignment will be used such a way that next allocation if
854 * success, will always meet same 4gb region requirement.
855 * rdpq_tracker keep track of each buffer's physical,
856 * virtual address and pci pool descriptor. It will help driver
857 * while freeing the resources.
858 *
859 */
860 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
861 chunk_size)) {
862 dma_pool_free(fusion->reply_frames_desc_pool,
863 rdpq_chunk_virt[i],
864 rdpq_chunk_phys[i]);
865
866 rdpq_chunk_virt[i] =
867 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
868 GFP_KERNEL, &rdpq_chunk_phys[i]);
869 if (!rdpq_chunk_virt[i]) {
870 dev_err(&instance->pdev->dev,
871 "Failed from %s %d\n",
872 __func__, __LINE__);
873 return -ENOMEM;
874 }
875 fusion->rdpq_tracker[i].dma_pool_ptr =
876 fusion->reply_frames_desc_pool_align;
877 } else {
878 fusion->rdpq_tracker[i].dma_pool_ptr =
879 fusion->reply_frames_desc_pool;
880 }
881
882 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
883 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
884 }
885
886 for (k = 0; k < dma_alloc_count; k++) {
887 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
888 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
889
890 if (abs_index == msix_count)
891 break;
892 offset = fusion->reply_alloc_sz * i;
893 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
894 cpu_to_le64(rdpq_chunk_phys[k] + offset);
895 fusion->reply_frames_desc_phys[abs_index] =
896 rdpq_chunk_phys[k] + offset;
897 fusion->reply_frames_desc[abs_index] =
898 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
899
900 reply_desc = fusion->reply_frames_desc[abs_index];
901 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
902 reply_desc->Words = ULLONG_MAX;
903 }
904 }
905
906 return 0;
907 }
908
909 static void
megasas_free_rdpq_fusion(struct megasas_instance * instance)910 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
911
912 int i;
913 struct fusion_context *fusion;
914
915 fusion = instance->ctrl_context;
916
917 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
918 if (fusion->rdpq_tracker[i].pool_entry_virt)
919 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
920 fusion->rdpq_tracker[i].pool_entry_virt,
921 fusion->rdpq_tracker[i].pool_entry_phys);
922
923 }
924
925 dma_pool_destroy(fusion->reply_frames_desc_pool);
926 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
927
928 if (fusion->rdpq_virt)
929 dma_free_coherent(&instance->pdev->dev,
930 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
931 fusion->rdpq_virt, fusion->rdpq_phys);
932 }
933
934 static void
megasas_free_reply_fusion(struct megasas_instance * instance)935 megasas_free_reply_fusion(struct megasas_instance *instance) {
936
937 struct fusion_context *fusion;
938
939 fusion = instance->ctrl_context;
940
941 if (fusion->reply_frames_desc[0])
942 dma_pool_free(fusion->reply_frames_desc_pool,
943 fusion->reply_frames_desc[0],
944 fusion->reply_frames_desc_phys[0]);
945
946 dma_pool_destroy(fusion->reply_frames_desc_pool);
947
948 }
949
950
951 /**
952 * megasas_alloc_cmds_fusion - Allocates the command packets
953 * @instance: Adapter soft state
954 *
955 *
956 * Each frame has a 32-bit field called context. This context is used to get
957 * back the megasas_cmd_fusion from the frame when a frame gets completed
958 * In this driver, the 32 bit values are the indices into an array cmd_list.
959 * This array is used only to look up the megasas_cmd_fusion given the context.
960 * The free commands themselves are maintained in a linked list called cmd_pool.
961 *
962 * cmds are formed in the io_request and sg_frame members of the
963 * megasas_cmd_fusion. The context field is used to get a request descriptor
964 * and is used as SMID of the cmd.
965 * SMID value range is from 1 to max_fw_cmds.
966 */
967 static int
megasas_alloc_cmds_fusion(struct megasas_instance * instance)968 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
969 {
970 int i;
971 struct fusion_context *fusion;
972 struct megasas_cmd_fusion *cmd;
973 u32 offset;
974 dma_addr_t io_req_base_phys;
975 u8 *io_req_base;
976
977
978 fusion = instance->ctrl_context;
979
980 if (megasas_alloc_request_fusion(instance))
981 goto fail_exit;
982
983 if (instance->is_rdpq) {
984 if (megasas_alloc_rdpq_fusion(instance))
985 goto fail_exit;
986 } else
987 if (megasas_alloc_reply_fusion(instance))
988 goto fail_exit;
989
990 if (megasas_alloc_cmdlist_fusion(instance))
991 goto fail_exit;
992
993 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
994 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
995 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
996
997 /*
998 * Add all the commands to command pool (fusion->cmd_pool)
999 */
1000
1001 /* SMID 0 is reserved. Set SMID/index from 1 */
1002 for (i = 0; i < instance->max_mpt_cmds; i++) {
1003 cmd = fusion->cmd_list[i];
1004 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
1005 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
1006 cmd->index = i + 1;
1007 cmd->scmd = NULL;
1008 cmd->sync_cmd_idx =
1009 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
1010 (i - instance->max_scsi_cmds) :
1011 (u32)ULONG_MAX; /* Set to Invalid */
1012 cmd->instance = instance;
1013 cmd->io_request =
1014 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1015 (io_req_base + offset);
1016 memset(cmd->io_request, 0,
1017 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
1018 cmd->io_request_phys_addr = io_req_base_phys + offset;
1019 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
1020 }
1021
1022 if (megasas_create_sg_sense_fusion(instance))
1023 goto fail_exit;
1024
1025 return 0;
1026
1027 fail_exit:
1028 megasas_free_cmds_fusion(instance);
1029 return -ENOMEM;
1030 }
1031
1032 /**
1033 * wait_and_poll - Issues a polling command
1034 * @instance: Adapter soft state
1035 * @cmd: Command packet to be issued
1036 * @seconds: Maximum poll time
1037 *
1038 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
1039 */
1040 int
wait_and_poll(struct megasas_instance * instance,struct megasas_cmd * cmd,int seconds)1041 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
1042 int seconds)
1043 {
1044 int i;
1045 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1046 u32 status_reg;
1047
1048 u32 msecs = seconds * 1000;
1049
1050 /*
1051 * Wait for cmd_status to change
1052 */
1053 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
1054 rmb();
1055 msleep(20);
1056 if (!(i % 5000)) {
1057 status_reg = instance->instancet->read_fw_status_reg(instance)
1058 & MFI_STATE_MASK;
1059 if (status_reg == MFI_STATE_FAULT)
1060 break;
1061 }
1062 }
1063
1064 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
1065 return DCMD_TIMEOUT;
1066 else if (frame_hdr->cmd_status == MFI_STAT_OK)
1067 return DCMD_SUCCESS;
1068 else
1069 return DCMD_FAILED;
1070 }
1071
1072 /**
1073 * megasas_ioc_init_fusion - Initializes the FW
1074 * @instance: Adapter soft state
1075 *
1076 * Issues the IOC Init cmd
1077 */
1078 int
megasas_ioc_init_fusion(struct megasas_instance * instance)1079 megasas_ioc_init_fusion(struct megasas_instance *instance)
1080 {
1081 struct megasas_init_frame *init_frame;
1082 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
1083 dma_addr_t ioc_init_handle;
1084 struct megasas_cmd *cmd;
1085 u8 ret, cur_rdpq_mode;
1086 struct fusion_context *fusion;
1087 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
1088 int i;
1089 struct megasas_header *frame_hdr;
1090 const char *sys_info;
1091 MFI_CAPABILITIES *drv_ops;
1092 u32 scratch_pad_1;
1093 ktime_t time;
1094 bool cur_fw_64bit_dma_capable;
1095 bool cur_intr_coalescing;
1096
1097 fusion = instance->ctrl_context;
1098
1099 ioc_init_handle = fusion->ioc_init_request_phys;
1100 IOCInitMessage = fusion->ioc_init_request;
1101
1102 cmd = fusion->ioc_init_cmd;
1103
1104 scratch_pad_1 = megasas_readl
1105 (instance, &instance->reg_set->outbound_scratch_pad_1);
1106
1107 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
1108
1109 if (instance->adapter_type == INVADER_SERIES) {
1110 cur_fw_64bit_dma_capable =
1111 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
1112
1113 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1114 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1115 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1116 megaraid_sas_kill_hba(instance);
1117 ret = 1;
1118 goto fail_fw_init;
1119 }
1120 }
1121
1122 if (instance->is_rdpq && !cur_rdpq_mode) {
1123 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1124 " from RDPQ mode to non RDPQ mode\n");
1125 ret = 1;
1126 goto fail_fw_init;
1127 }
1128
1129 cur_intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
1130 true : false;
1131
1132 if ((instance->low_latency_index_start ==
1133 MR_HIGH_IOPS_QUEUE_COUNT) && cur_intr_coalescing)
1134 instance->perf_mode = MR_BALANCED_PERF_MODE;
1135
1136 dev_info(&instance->pdev->dev, "Performance mode :%s (latency index = %d)\n",
1137 MEGASAS_PERF_MODE_2STR(instance->perf_mode),
1138 instance->low_latency_index_start);
1139
1140 instance->fw_sync_cache_support = (scratch_pad_1 &
1141 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1142 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1143 instance->fw_sync_cache_support ? "Yes" : "No");
1144
1145 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1146
1147 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1148 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1149 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1150 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1151 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1152
1153 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1154 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1155 cpu_to_le64(fusion->rdpq_phys) :
1156 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1157 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1158 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1159 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1160 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1161 IOCInitMessage->HostMSIxVectors = instance->msix_vectors + instance->iopoll_q_count;
1162 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1163
1164 time = ktime_get_real();
1165 /* Convert to milliseconds as per FW requirement */
1166 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1167
1168 init_frame = (struct megasas_init_frame *)cmd->frame;
1169 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1170
1171 frame_hdr = &cmd->frame->hdr;
1172 frame_hdr->cmd_status = 0xFF;
1173 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1174
1175 init_frame->cmd = MFI_CMD_INIT;
1176 init_frame->cmd_status = 0xFF;
1177
1178 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1179
1180 /* driver support Extended MSIX */
1181 if (instance->adapter_type >= INVADER_SERIES)
1182 drv_ops->mfi_capabilities.support_additional_msix = 1;
1183 /* driver supports HA / Remote LUN over Fast Path interface */
1184 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1185
1186 drv_ops->mfi_capabilities.support_max_255lds = 1;
1187 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1188 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1189
1190 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1191 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1192
1193 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1194 if (!dual_qdepth_disable)
1195 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1196
1197 drv_ops->mfi_capabilities.support_qd_throttling = 1;
1198 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1199 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1200 drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
1201
1202 if (reset_devices)
1203 drv_ops->mfi_capabilities.support_memdump = 1;
1204
1205 if (instance->consistent_mask_64bit)
1206 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1207
1208 /* Convert capability to LE32 */
1209 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1210
1211 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1212 if (instance->system_info_buf && sys_info) {
1213 memcpy(instance->system_info_buf->systemId, sys_info,
1214 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1215 instance->system_info_buf->systemIdLength =
1216 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1217 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1218 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1219 }
1220
1221 init_frame->queue_info_new_phys_addr_hi =
1222 cpu_to_le32(upper_32_bits(ioc_init_handle));
1223 init_frame->queue_info_new_phys_addr_lo =
1224 cpu_to_le32(lower_32_bits(ioc_init_handle));
1225 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1226
1227 /*
1228 * Each bit in replyqueue_mask represents one group of MSI-x vectors
1229 * (each group has 8 vectors)
1230 */
1231 switch (instance->perf_mode) {
1232 case MR_BALANCED_PERF_MODE:
1233 init_frame->replyqueue_mask =
1234 cpu_to_le16(~(~0 << instance->low_latency_index_start/8));
1235 break;
1236 case MR_IOPS_PERF_MODE:
1237 init_frame->replyqueue_mask =
1238 cpu_to_le16(~(~0 << instance->msix_vectors/8));
1239 break;
1240 }
1241
1242
1243 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1244 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1245 req_desc.MFAIo.RequestFlags =
1246 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1247 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1248
1249 /*
1250 * disable the intr before firing the init frame
1251 */
1252 instance->instancet->disable_intr(instance);
1253
1254 for (i = 0; i < (10 * 1000); i += 20) {
1255 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1256 msleep(20);
1257 else
1258 break;
1259 }
1260
1261 /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1262 megasas_write_64bit_req_desc(instance, &req_desc);
1263
1264 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1265
1266 frame_hdr = &cmd->frame->hdr;
1267 if (frame_hdr->cmd_status != 0) {
1268 ret = 1;
1269 goto fail_fw_init;
1270 }
1271
1272 if (instance->adapter_type >= AERO_SERIES) {
1273 scratch_pad_1 = megasas_readl
1274 (instance, &instance->reg_set->outbound_scratch_pad_1);
1275
1276 instance->atomic_desc_support =
1277 (scratch_pad_1 & MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
1278
1279 dev_info(&instance->pdev->dev, "FW supports atomic descriptor\t: %s\n",
1280 instance->atomic_desc_support ? "Yes" : "No");
1281 }
1282
1283 return 0;
1284
1285 fail_fw_init:
1286 dev_err(&instance->pdev->dev,
1287 "Init cmd return status FAILED for SCSI host %d\n",
1288 instance->host->host_no);
1289
1290 return ret;
1291 }
1292
1293 /**
1294 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1295 * @instance: Adapter soft state
1296 * @pend: set to 1, if it is pended jbod map.
1297 *
1298 * Issue Jbod map to the firmware. If it is pended command,
1299 * issue command and return. If it is first instance of jbod map
1300 * issue and receive command.
1301 */
1302 int
megasas_sync_pd_seq_num(struct megasas_instance * instance,bool pend)1303 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1304 int ret = 0;
1305 size_t pd_seq_map_sz;
1306 struct megasas_cmd *cmd;
1307 struct megasas_dcmd_frame *dcmd;
1308 struct fusion_context *fusion = instance->ctrl_context;
1309 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1310 dma_addr_t pd_seq_h;
1311
1312 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1313 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1314 pd_seq_map_sz = struct_size(pd_sync, seq, MAX_PHYSICAL_DEVICES);
1315
1316 cmd = megasas_get_cmd(instance);
1317 if (!cmd) {
1318 dev_err(&instance->pdev->dev,
1319 "Could not get mfi cmd. Fail from %s %d\n",
1320 __func__, __LINE__);
1321 return -ENOMEM;
1322 }
1323
1324 dcmd = &cmd->frame->dcmd;
1325
1326 memset(pd_sync, 0, pd_seq_map_sz);
1327 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1328
1329 if (pend) {
1330 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1331 dcmd->flags = MFI_FRAME_DIR_WRITE;
1332 instance->jbod_seq_cmd = cmd;
1333 } else {
1334 dcmd->flags = MFI_FRAME_DIR_READ;
1335 }
1336
1337 dcmd->cmd = MFI_CMD_DCMD;
1338 dcmd->cmd_status = 0xFF;
1339 dcmd->sge_count = 1;
1340 dcmd->timeout = 0;
1341 dcmd->pad_0 = 0;
1342 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1343 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1344
1345 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1346
1347 if (pend) {
1348 instance->instancet->issue_dcmd(instance, cmd);
1349 return 0;
1350 }
1351
1352 /* Below code is only for non pended DCMD */
1353 if (!instance->mask_interrupts)
1354 ret = megasas_issue_blocked_cmd(instance, cmd,
1355 MFI_IO_TIMEOUT_SECS);
1356 else
1357 ret = megasas_issue_polled(instance, cmd);
1358
1359 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1360 dev_warn(&instance->pdev->dev,
1361 "driver supports max %d JBOD, but FW reports %d\n",
1362 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1363 ret = -EINVAL;
1364 }
1365
1366 if (ret == DCMD_TIMEOUT)
1367 dev_warn(&instance->pdev->dev,
1368 "%s DCMD timed out, continue without JBOD sequence map\n",
1369 __func__);
1370
1371 if (ret == DCMD_SUCCESS)
1372 instance->pd_seq_map_id++;
1373
1374 megasas_return_cmd(instance, cmd);
1375 return ret;
1376 }
1377
1378 /*
1379 * megasas_get_ld_map_info - Returns FW's ld_map structure
1380 * @instance: Adapter soft state
1381 * @pend: Pend the command or not
1382 * Issues an internal command (DCMD) to get the FW's controller PD
1383 * list structure. This information is mainly used to find out SYSTEM
1384 * supported by the FW.
1385 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1386 * dcmd.mbox.b[0] - number of LDs being sync'd
1387 * dcmd.mbox.b[1] - 0 - complete command immediately.
1388 * - 1 - pend till config change
1389 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1390 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1391 * uses extended struct MR_FW_RAID_MAP_EXT
1392 */
1393 static int
megasas_get_ld_map_info(struct megasas_instance * instance)1394 megasas_get_ld_map_info(struct megasas_instance *instance)
1395 {
1396 int ret = 0;
1397 struct megasas_cmd *cmd;
1398 struct megasas_dcmd_frame *dcmd;
1399 void *ci;
1400 dma_addr_t ci_h = 0;
1401 u32 size_map_info;
1402 struct fusion_context *fusion;
1403
1404 cmd = megasas_get_cmd(instance);
1405
1406 if (!cmd) {
1407 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1408 return -ENOMEM;
1409 }
1410
1411 fusion = instance->ctrl_context;
1412
1413 if (!fusion) {
1414 megasas_return_cmd(instance, cmd);
1415 return -ENXIO;
1416 }
1417
1418 dcmd = &cmd->frame->dcmd;
1419
1420 size_map_info = fusion->current_map_sz;
1421
1422 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1423 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1424
1425 if (!ci) {
1426 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1427 megasas_return_cmd(instance, cmd);
1428 return -ENOMEM;
1429 }
1430
1431 memset(ci, 0, fusion->max_map_sz);
1432 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1433 dcmd->cmd = MFI_CMD_DCMD;
1434 dcmd->cmd_status = 0xFF;
1435 dcmd->sge_count = 1;
1436 dcmd->flags = MFI_FRAME_DIR_READ;
1437 dcmd->timeout = 0;
1438 dcmd->pad_0 = 0;
1439 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1440 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1441
1442 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1443
1444 if (!instance->mask_interrupts)
1445 ret = megasas_issue_blocked_cmd(instance, cmd,
1446 MFI_IO_TIMEOUT_SECS);
1447 else
1448 ret = megasas_issue_polled(instance, cmd);
1449
1450 if (ret == DCMD_TIMEOUT)
1451 dev_warn(&instance->pdev->dev,
1452 "%s DCMD timed out, RAID map is disabled\n",
1453 __func__);
1454
1455 megasas_return_cmd(instance, cmd);
1456
1457 return ret;
1458 }
1459
1460 u8
megasas_get_map_info(struct megasas_instance * instance)1461 megasas_get_map_info(struct megasas_instance *instance)
1462 {
1463 struct fusion_context *fusion = instance->ctrl_context;
1464
1465 fusion->fast_path_io = 0;
1466 if (!megasas_get_ld_map_info(instance)) {
1467 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1468 fusion->fast_path_io = 1;
1469 return 0;
1470 }
1471 }
1472 return 1;
1473 }
1474
1475 /*
1476 * megasas_sync_map_info - Returns FW's ld_map structure
1477 * @instance: Adapter soft state
1478 *
1479 * Issues an internal command (DCMD) to get the FW's controller PD
1480 * list structure. This information is mainly used to find out SYSTEM
1481 * supported by the FW.
1482 */
1483 int
megasas_sync_map_info(struct megasas_instance * instance)1484 megasas_sync_map_info(struct megasas_instance *instance)
1485 {
1486 int i;
1487 struct megasas_cmd *cmd;
1488 struct megasas_dcmd_frame *dcmd;
1489 u16 num_lds;
1490 struct fusion_context *fusion;
1491 struct MR_LD_TARGET_SYNC *ci = NULL;
1492 struct MR_DRV_RAID_MAP_ALL *map;
1493 struct MR_LD_RAID *raid;
1494 struct MR_LD_TARGET_SYNC *ld_sync;
1495 dma_addr_t ci_h = 0;
1496 u32 size_map_info;
1497
1498 cmd = megasas_get_cmd(instance);
1499
1500 if (!cmd) {
1501 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1502 return -ENOMEM;
1503 }
1504
1505 fusion = instance->ctrl_context;
1506
1507 if (!fusion) {
1508 megasas_return_cmd(instance, cmd);
1509 return 1;
1510 }
1511
1512 map = fusion->ld_drv_map[instance->map_id & 1];
1513
1514 num_lds = le16_to_cpu(map->raidMap.ldCount);
1515
1516 dcmd = &cmd->frame->dcmd;
1517
1518 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1519
1520 ci = (struct MR_LD_TARGET_SYNC *)
1521 fusion->ld_map[(instance->map_id - 1) & 1];
1522 memset(ci, 0, fusion->max_map_sz);
1523
1524 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1525
1526 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1527
1528 for (i = 0; i < num_lds; i++, ld_sync++) {
1529 raid = MR_LdRaidGet(i, map);
1530 ld_sync->targetId = MR_GetLDTgtId(i, map);
1531 ld_sync->seqNum = raid->seqNum;
1532 }
1533
1534 size_map_info = fusion->current_map_sz;
1535
1536 dcmd->cmd = MFI_CMD_DCMD;
1537 dcmd->cmd_status = 0xFF;
1538 dcmd->sge_count = 1;
1539 dcmd->flags = MFI_FRAME_DIR_WRITE;
1540 dcmd->timeout = 0;
1541 dcmd->pad_0 = 0;
1542 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1543 dcmd->mbox.b[0] = num_lds;
1544 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1545 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1546
1547 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1548
1549 instance->map_update_cmd = cmd;
1550
1551 instance->instancet->issue_dcmd(instance, cmd);
1552
1553 return 0;
1554 }
1555
1556 /*
1557 * meagasas_display_intel_branding - Display branding string
1558 * @instance: per adapter object
1559 *
1560 * Return nothing.
1561 */
1562 static void
megasas_display_intel_branding(struct megasas_instance * instance)1563 megasas_display_intel_branding(struct megasas_instance *instance)
1564 {
1565 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1566 return;
1567
1568 switch (instance->pdev->device) {
1569 case PCI_DEVICE_ID_LSI_INVADER:
1570 switch (instance->pdev->subsystem_device) {
1571 case MEGARAID_INTEL_RS3DC080_SSDID:
1572 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1573 instance->host->host_no,
1574 MEGARAID_INTEL_RS3DC080_BRANDING);
1575 break;
1576 case MEGARAID_INTEL_RS3DC040_SSDID:
1577 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1578 instance->host->host_no,
1579 MEGARAID_INTEL_RS3DC040_BRANDING);
1580 break;
1581 case MEGARAID_INTEL_RS3SC008_SSDID:
1582 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1583 instance->host->host_no,
1584 MEGARAID_INTEL_RS3SC008_BRANDING);
1585 break;
1586 case MEGARAID_INTEL_RS3MC044_SSDID:
1587 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1588 instance->host->host_no,
1589 MEGARAID_INTEL_RS3MC044_BRANDING);
1590 break;
1591 default:
1592 break;
1593 }
1594 break;
1595 case PCI_DEVICE_ID_LSI_FURY:
1596 switch (instance->pdev->subsystem_device) {
1597 case MEGARAID_INTEL_RS3WC080_SSDID:
1598 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1599 instance->host->host_no,
1600 MEGARAID_INTEL_RS3WC080_BRANDING);
1601 break;
1602 case MEGARAID_INTEL_RS3WC040_SSDID:
1603 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1604 instance->host->host_no,
1605 MEGARAID_INTEL_RS3WC040_BRANDING);
1606 break;
1607 default:
1608 break;
1609 }
1610 break;
1611 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1612 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1613 switch (instance->pdev->subsystem_device) {
1614 case MEGARAID_INTEL_RMS3BC160_SSDID:
1615 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1616 instance->host->host_no,
1617 MEGARAID_INTEL_RMS3BC160_BRANDING);
1618 break;
1619 default:
1620 break;
1621 }
1622 break;
1623 default:
1624 break;
1625 }
1626 }
1627
1628 /**
1629 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1630 * @instance: Adapter soft state
1631 *
1632 * return: if success: return 0
1633 * failed: return -ENOMEM
1634 */
megasas_allocate_raid_maps(struct megasas_instance * instance)1635 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1636 {
1637 struct fusion_context *fusion;
1638 int i = 0;
1639
1640 fusion = instance->ctrl_context;
1641
1642 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1643
1644 for (i = 0; i < 2; i++) {
1645 fusion->ld_map[i] = NULL;
1646
1647 fusion->ld_drv_map[i] = (void *)
1648 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1649 fusion->drv_map_pages);
1650
1651 if (!fusion->ld_drv_map[i]) {
1652 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1653
1654 if (!fusion->ld_drv_map[i]) {
1655 dev_err(&instance->pdev->dev,
1656 "Could not allocate memory for local map"
1657 " size requested: %d\n",
1658 fusion->drv_map_sz);
1659 goto ld_drv_map_alloc_fail;
1660 }
1661 }
1662 }
1663
1664 for (i = 0; i < 2; i++) {
1665 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1666 fusion->max_map_sz,
1667 &fusion->ld_map_phys[i],
1668 GFP_KERNEL);
1669 if (!fusion->ld_map[i]) {
1670 dev_err(&instance->pdev->dev,
1671 "Could not allocate memory for map info %s:%d\n",
1672 __func__, __LINE__);
1673 goto ld_map_alloc_fail;
1674 }
1675 }
1676
1677 return 0;
1678
1679 ld_map_alloc_fail:
1680 for (i = 0; i < 2; i++) {
1681 if (fusion->ld_map[i])
1682 dma_free_coherent(&instance->pdev->dev,
1683 fusion->max_map_sz,
1684 fusion->ld_map[i],
1685 fusion->ld_map_phys[i]);
1686 }
1687
1688 ld_drv_map_alloc_fail:
1689 for (i = 0; i < 2; i++) {
1690 if (fusion->ld_drv_map[i]) {
1691 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1692 vfree(fusion->ld_drv_map[i]);
1693 else
1694 free_pages((ulong)fusion->ld_drv_map[i],
1695 fusion->drv_map_pages);
1696 }
1697 }
1698
1699 return -ENOMEM;
1700 }
1701
1702 /**
1703 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1704 * reply desc queue,
1705 * IO request frame queue, set can_queue.
1706 * @instance: Adapter soft state
1707 * @return: void
1708 */
1709 static inline
megasas_configure_queue_sizes(struct megasas_instance * instance)1710 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1711 {
1712 struct fusion_context *fusion;
1713 u16 max_cmd;
1714
1715 fusion = instance->ctrl_context;
1716 max_cmd = instance->max_fw_cmds;
1717
1718 if (instance->adapter_type >= VENTURA_SERIES)
1719 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1720 else
1721 instance->max_mpt_cmds = instance->max_fw_cmds;
1722
1723 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1724 instance->cur_can_queue = instance->max_scsi_cmds;
1725 instance->host->can_queue = instance->cur_can_queue;
1726
1727 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1728
1729 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1730 instance->max_mpt_cmds;
1731 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1732 (fusion->reply_q_depth);
1733 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1734 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1735 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1736 }
1737
megasas_alloc_ioc_init_frame(struct megasas_instance * instance)1738 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1739 {
1740 struct fusion_context *fusion;
1741 struct megasas_cmd *cmd;
1742
1743 fusion = instance->ctrl_context;
1744
1745 cmd = kzalloc_obj(struct megasas_cmd);
1746
1747 if (!cmd) {
1748 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1749 __func__, __LINE__);
1750 return -ENOMEM;
1751 }
1752
1753 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1754 IOC_INIT_FRAME_SIZE,
1755 &cmd->frame_phys_addr, GFP_KERNEL);
1756
1757 if (!cmd->frame) {
1758 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1759 __func__, __LINE__);
1760 kfree(cmd);
1761 return -ENOMEM;
1762 }
1763
1764 fusion->ioc_init_cmd = cmd;
1765 return 0;
1766 }
1767
1768 /**
1769 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1770 * @instance: Adapter soft state
1771 */
megasas_free_ioc_init_cmd(struct megasas_instance * instance)1772 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1773 {
1774 struct fusion_context *fusion;
1775
1776 fusion = instance->ctrl_context;
1777
1778 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1779 dma_free_coherent(&instance->pdev->dev,
1780 IOC_INIT_FRAME_SIZE,
1781 fusion->ioc_init_cmd->frame,
1782 fusion->ioc_init_cmd->frame_phys_addr);
1783
1784 kfree(fusion->ioc_init_cmd);
1785 }
1786
1787 /**
1788 * megasas_init_adapter_fusion - Initializes the FW
1789 * @instance: Adapter soft state
1790 *
1791 * This is the main function for initializing firmware.
1792 */
1793 static u32
megasas_init_adapter_fusion(struct megasas_instance * instance)1794 megasas_init_adapter_fusion(struct megasas_instance *instance)
1795 {
1796 struct fusion_context *fusion;
1797 u32 scratch_pad_1;
1798 int i = 0, count;
1799 u32 status_reg;
1800
1801 fusion = instance->ctrl_context;
1802
1803 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1804
1805 /*
1806 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1807 */
1808 instance->max_mfi_cmds =
1809 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1810
1811 megasas_configure_queue_sizes(instance);
1812
1813 scratch_pad_1 = megasas_readl(instance,
1814 &instance->reg_set->outbound_scratch_pad_1);
1815 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1816 * Firmware support extended IO chain frame which is 4 times more than
1817 * legacy Firmware.
1818 * Legacy Firmware - Frame size is (8 * 128) = 1K
1819 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1820 */
1821 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1822 instance->max_chain_frame_sz =
1823 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1824 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1825 else
1826 instance->max_chain_frame_sz =
1827 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1828 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1829
1830 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1831 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1832 instance->max_chain_frame_sz,
1833 MEGASAS_CHAIN_FRAME_SZ_MIN);
1834 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1835 }
1836
1837 fusion->max_sge_in_main_msg =
1838 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1839 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1840
1841 fusion->max_sge_in_chain =
1842 instance->max_chain_frame_sz
1843 / sizeof(union MPI2_SGE_IO_UNION);
1844
1845 instance->max_num_sge =
1846 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1847 + fusion->max_sge_in_chain - 2);
1848
1849 /* Used for pass thru MFI frame (DCMD) */
1850 fusion->chain_offset_mfi_pthru =
1851 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1852
1853 fusion->chain_offset_io_request =
1854 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1855 sizeof(union MPI2_SGE_IO_UNION))/16;
1856
1857 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1858 count += instance->iopoll_q_count;
1859
1860 for (i = 0 ; i < count; i++)
1861 fusion->last_reply_idx[i] = 0;
1862
1863 /*
1864 * For fusion adapters, 3 commands for IOCTL and 8 commands
1865 * for driver's internal DCMDs.
1866 */
1867 instance->max_scsi_cmds = instance->max_fw_cmds -
1868 (MEGASAS_FUSION_INTERNAL_CMDS +
1869 MEGASAS_FUSION_IOCTL_CMDS);
1870 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1871
1872 for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++)
1873 atomic_set(&fusion->busy_mq_poll[i], 0);
1874
1875 if (megasas_alloc_ioc_init_frame(instance))
1876 return 1;
1877
1878 /*
1879 * Allocate memory for descriptors
1880 * Create a pool of commands
1881 */
1882 if (megasas_alloc_cmds(instance))
1883 goto fail_alloc_mfi_cmds;
1884 if (megasas_alloc_cmds_fusion(instance))
1885 goto fail_alloc_cmds;
1886
1887 if (megasas_ioc_init_fusion(instance)) {
1888 status_reg = instance->instancet->read_fw_status_reg(instance);
1889 if (((status_reg & MFI_STATE_MASK) == MFI_STATE_FAULT) &&
1890 (status_reg & MFI_RESET_ADAPTER)) {
1891 /* Do a chip reset and then retry IOC INIT once */
1892 if (megasas_adp_reset_wait_for_ready
1893 (instance, true, 0) == FAILED)
1894 goto fail_ioc_init;
1895
1896 if (megasas_ioc_init_fusion(instance))
1897 goto fail_ioc_init;
1898 } else {
1899 goto fail_ioc_init;
1900 }
1901 }
1902
1903 megasas_display_intel_branding(instance);
1904 if (megasas_get_ctrl_info(instance)) {
1905 dev_err(&instance->pdev->dev,
1906 "Could not get controller info. Fail from %s %d\n",
1907 __func__, __LINE__);
1908 goto fail_ioc_init;
1909 }
1910
1911 instance->flag_ieee = 1;
1912 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1913 instance->threshold_reply_count = instance->max_fw_cmds / 4;
1914 fusion->fast_path_io = 0;
1915
1916 if (megasas_allocate_raid_maps(instance))
1917 goto fail_ioc_init;
1918
1919 if (!megasas_get_map_info(instance))
1920 megasas_sync_map_info(instance);
1921
1922 return 0;
1923
1924 fail_ioc_init:
1925 megasas_free_cmds_fusion(instance);
1926 fail_alloc_cmds:
1927 megasas_free_cmds(instance);
1928 fail_alloc_mfi_cmds:
1929 megasas_free_ioc_init_cmd(instance);
1930 return 1;
1931 }
1932
1933 /**
1934 * megasas_fault_detect_work - Worker function of
1935 * FW fault handling workqueue.
1936 * @work: FW fault work struct
1937 */
1938 static void
megasas_fault_detect_work(struct work_struct * work)1939 megasas_fault_detect_work(struct work_struct *work)
1940 {
1941 struct megasas_instance *instance =
1942 container_of(work, struct megasas_instance,
1943 fw_fault_work.work);
1944 u32 fw_state, dma_state, status;
1945
1946 /* Check the fw state */
1947 fw_state = instance->instancet->read_fw_status_reg(instance) &
1948 MFI_STATE_MASK;
1949
1950 if (fw_state == MFI_STATE_FAULT) {
1951 dma_state = instance->instancet->read_fw_status_reg(instance) &
1952 MFI_STATE_DMADONE;
1953 /* Start collecting crash, if DMA bit is done */
1954 if (instance->crash_dump_drv_support &&
1955 instance->crash_dump_app_support && dma_state) {
1956 megasas_fusion_crash_dump(instance);
1957 } else {
1958 if (instance->unload == 0) {
1959 status = megasas_reset_fusion(instance->host, 0);
1960 if (status != SUCCESS) {
1961 dev_err(&instance->pdev->dev,
1962 "Failed from %s %d, do not re-arm timer\n",
1963 __func__, __LINE__);
1964 return;
1965 }
1966 }
1967 }
1968 }
1969
1970 if (instance->fw_fault_work_q)
1971 queue_delayed_work(instance->fw_fault_work_q,
1972 &instance->fw_fault_work,
1973 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1974 }
1975
1976 int
megasas_fusion_start_watchdog(struct megasas_instance * instance)1977 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1978 {
1979 /* Check if the Fault WQ is already started */
1980 if (instance->fw_fault_work_q)
1981 return SUCCESS;
1982
1983 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1984
1985 snprintf(instance->fault_handler_work_q_name,
1986 sizeof(instance->fault_handler_work_q_name),
1987 "poll_megasas%d_status", instance->host->host_no);
1988
1989 instance->fw_fault_work_q = alloc_ordered_workqueue(
1990 "%s", WQ_MEM_RECLAIM, instance->fault_handler_work_q_name);
1991 if (!instance->fw_fault_work_q) {
1992 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1993 __func__, __LINE__);
1994 return FAILED;
1995 }
1996
1997 queue_delayed_work(instance->fw_fault_work_q,
1998 &instance->fw_fault_work,
1999 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
2000
2001 return SUCCESS;
2002 }
2003
2004 void
megasas_fusion_stop_watchdog(struct megasas_instance * instance)2005 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
2006 {
2007 struct workqueue_struct *wq;
2008
2009 if (instance->fw_fault_work_q) {
2010 wq = instance->fw_fault_work_q;
2011 instance->fw_fault_work_q = NULL;
2012 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
2013 flush_workqueue(wq);
2014 destroy_workqueue(wq);
2015 }
2016 }
2017
2018 /**
2019 * map_cmd_status - Maps FW cmd status to OS cmd status
2020 * @fusion: fusion context
2021 * @scmd: Pointer to cmd
2022 * @status: status of cmd returned by FW
2023 * @ext_status: ext status of cmd returned by FW
2024 * @data_length: command data length
2025 * @sense: command sense data
2026 */
2027 static void
map_cmd_status(struct fusion_context * fusion,struct scsi_cmnd * scmd,u8 status,u8 ext_status,u32 data_length,u8 * sense)2028 map_cmd_status(struct fusion_context *fusion,
2029 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
2030 u32 data_length, u8 *sense)
2031 {
2032 u8 cmd_type;
2033 int resid;
2034
2035 cmd_type = megasas_cmd_type(scmd);
2036 switch (status) {
2037
2038 case MFI_STAT_OK:
2039 scmd->result = DID_OK << 16;
2040 break;
2041
2042 case MFI_STAT_SCSI_IO_FAILED:
2043 case MFI_STAT_LD_INIT_IN_PROGRESS:
2044 if (ext_status == 0xf0)
2045 scmd->result = (DID_ERROR << 16) | SAM_STAT_CHECK_CONDITION;
2046 else
2047 scmd->result = (DID_ERROR << 16) | ext_status;
2048 break;
2049
2050 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2051
2052 scmd->result = (DID_OK << 16) | ext_status;
2053 if (ext_status == SAM_STAT_CHECK_CONDITION) {
2054 memcpy(scmd->sense_buffer, sense,
2055 SCSI_SENSE_BUFFERSIZE);
2056 }
2057
2058 /*
2059 * If the IO request is partially completed, then MR FW will
2060 * update "io_request->DataLength" field with actual number of
2061 * bytes transferred.Driver will set residual bytes count in
2062 * SCSI command structure.
2063 */
2064 resid = (scsi_bufflen(scmd) - data_length);
2065 scsi_set_resid(scmd, resid);
2066
2067 if (resid &&
2068 ((cmd_type == READ_WRITE_LDIO) ||
2069 (cmd_type == READ_WRITE_SYSPDIO)))
2070 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
2071 " requested/completed 0x%x/0x%x\n",
2072 status, scsi_bufflen(scmd), data_length);
2073 break;
2074
2075 case MFI_STAT_LD_OFFLINE:
2076 case MFI_STAT_DEVICE_NOT_FOUND:
2077 scmd->result = DID_BAD_TARGET << 16;
2078 break;
2079 case MFI_STAT_CONFIG_SEQ_MISMATCH:
2080 scmd->result = DID_IMM_RETRY << 16;
2081 break;
2082 default:
2083 scmd->result = DID_ERROR << 16;
2084 break;
2085 }
2086 }
2087
2088 /**
2089 * megasas_is_prp_possible -
2090 * Checks if native NVMe PRPs can be built for the IO
2091 *
2092 * @instance: Adapter soft state
2093 * @scmd: SCSI command from the mid-layer
2094 * @sge_count: scatter gather element count.
2095 *
2096 * Returns: true: PRPs can be built
2097 * false: IEEE SGLs needs to be built
2098 */
2099 static bool
megasas_is_prp_possible(struct megasas_instance * instance,struct scsi_cmnd * scmd,int sge_count)2100 megasas_is_prp_possible(struct megasas_instance *instance,
2101 struct scsi_cmnd *scmd, int sge_count)
2102 {
2103 u32 data_length = 0;
2104 struct scatterlist *sg_scmd;
2105 bool build_prp = false;
2106 u32 mr_nvme_pg_size;
2107
2108 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2109 MR_DEFAULT_NVME_PAGE_SIZE);
2110 data_length = scsi_bufflen(scmd);
2111 sg_scmd = scsi_sglist(scmd);
2112
2113 /*
2114 * NVMe uses one PRP for each page (or part of a page)
2115 * look at the data length - if 4 pages or less then IEEE is OK
2116 * if > 5 pages then we need to build a native SGL
2117 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2118 * if this first size in the page is >= the residual beyond 4 pages
2119 * then use IEEE, otherwise use native SGL
2120 */
2121
2122 if (data_length > (mr_nvme_pg_size * 5)) {
2123 build_prp = true;
2124 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
2125 (data_length <= (mr_nvme_pg_size * 5))) {
2126 /* check if 1st SG entry size is < residual beyond 4 pages */
2127 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
2128 build_prp = true;
2129 }
2130
2131 return build_prp;
2132 }
2133
2134 /**
2135 * megasas_make_prp_nvme -
2136 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2137 *
2138 * @instance: Adapter soft state
2139 * @scmd: SCSI command from the mid-layer
2140 * @sgl_ptr: SGL to be filled in
2141 * @cmd: Fusion command frame
2142 * @sge_count: scatter gather element count.
2143 *
2144 * Returns: true: PRPs are built
2145 * false: IEEE SGLs needs to be built
2146 */
2147 static bool
megasas_make_prp_nvme(struct megasas_instance * instance,struct scsi_cmnd * scmd,struct MPI25_IEEE_SGE_CHAIN64 * sgl_ptr,struct megasas_cmd_fusion * cmd,int sge_count)2148 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2149 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2150 struct megasas_cmd_fusion *cmd, int sge_count)
2151 {
2152 int sge_len, offset, num_prp_in_chain = 0;
2153 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2154 u64 *ptr_sgl;
2155 dma_addr_t ptr_sgl_phys;
2156 u64 sge_addr;
2157 u32 page_mask, page_mask_result;
2158 struct scatterlist *sg_scmd;
2159 u32 first_prp_len;
2160 bool build_prp = false;
2161 int data_len = scsi_bufflen(scmd);
2162 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2163 MR_DEFAULT_NVME_PAGE_SIZE);
2164
2165 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2166
2167 if (!build_prp)
2168 return false;
2169
2170 /*
2171 * Nvme has a very convoluted prp format. One prp is required
2172 * for each page or partial page. Driver need to split up OS sg_list
2173 * entries if it is longer than one page or cross a page
2174 * boundary. Driver also have to insert a PRP list pointer entry as
2175 * the last entry in each physical page of the PRP list.
2176 *
2177 * NOTE: The first PRP "entry" is actually placed in the first
2178 * SGL entry in the main message as IEEE 64 format. The 2nd
2179 * entry in the main message is the chain element, and the rest
2180 * of the PRP entries are built in the contiguous pcie buffer.
2181 */
2182 page_mask = mr_nvme_pg_size - 1;
2183 ptr_sgl = (u64 *)cmd->sg_frame;
2184 ptr_sgl_phys = cmd->sg_frame_phys_addr;
2185 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2186
2187 /* Build chain frame element which holds all prps except first*/
2188 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2189 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2190
2191 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2192 main_chain_element->NextChainOffset = 0;
2193 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2194 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2195 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2196
2197 /* Build first prp, sge need not to be page aligned*/
2198 ptr_first_sgl = sgl_ptr;
2199 sg_scmd = scsi_sglist(scmd);
2200 sge_addr = sg_dma_address(sg_scmd);
2201 sge_len = sg_dma_len(sg_scmd);
2202
2203 offset = (u32)(sge_addr & page_mask);
2204 first_prp_len = mr_nvme_pg_size - offset;
2205
2206 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2207 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2208
2209 data_len -= first_prp_len;
2210
2211 if (sge_len > first_prp_len) {
2212 sge_addr += first_prp_len;
2213 sge_len -= first_prp_len;
2214 } else if (sge_len == first_prp_len) {
2215 sg_scmd = sg_next(sg_scmd);
2216 sge_addr = sg_dma_address(sg_scmd);
2217 sge_len = sg_dma_len(sg_scmd);
2218 }
2219
2220 for (;;) {
2221 offset = (u32)(sge_addr & page_mask);
2222
2223 /* Put PRP pointer due to page boundary*/
2224 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2225 if (unlikely(!page_mask_result)) {
2226 scmd_printk(KERN_NOTICE,
2227 scmd, "page boundary ptr_sgl: 0x%p\n",
2228 ptr_sgl);
2229 ptr_sgl_phys += 8;
2230 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2231 ptr_sgl++;
2232 num_prp_in_chain++;
2233 }
2234
2235 *ptr_sgl = cpu_to_le64(sge_addr);
2236 ptr_sgl++;
2237 ptr_sgl_phys += 8;
2238 num_prp_in_chain++;
2239
2240 sge_addr += mr_nvme_pg_size;
2241 sge_len -= mr_nvme_pg_size;
2242 data_len -= mr_nvme_pg_size;
2243
2244 if (data_len <= 0)
2245 break;
2246
2247 if (sge_len > 0)
2248 continue;
2249
2250 sg_scmd = sg_next(sg_scmd);
2251 sge_addr = sg_dma_address(sg_scmd);
2252 sge_len = sg_dma_len(sg_scmd);
2253 }
2254
2255 main_chain_element->Length =
2256 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2257
2258 return build_prp;
2259 }
2260
2261 /**
2262 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2263 * @instance: Adapter soft state
2264 * @scp: SCSI command from the mid-layer
2265 * @sgl_ptr: SGL to be filled in
2266 * @cmd: cmd we are working on
2267 * @sge_count: sge count
2268 *
2269 */
2270 static void
megasas_make_sgl_fusion(struct megasas_instance * instance,struct scsi_cmnd * scp,struct MPI25_IEEE_SGE_CHAIN64 * sgl_ptr,struct megasas_cmd_fusion * cmd,int sge_count)2271 megasas_make_sgl_fusion(struct megasas_instance *instance,
2272 struct scsi_cmnd *scp,
2273 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2274 struct megasas_cmd_fusion *cmd, int sge_count)
2275 {
2276 int i, sg_processed;
2277 struct scatterlist *os_sgl;
2278 struct fusion_context *fusion;
2279
2280 fusion = instance->ctrl_context;
2281
2282 if (instance->adapter_type >= INVADER_SERIES) {
2283 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2284 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2285 sgl_ptr_end->Flags = 0;
2286 }
2287
2288 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2289 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2290 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2291 sgl_ptr->Flags = 0;
2292 if (instance->adapter_type >= INVADER_SERIES)
2293 if (i == sge_count - 1)
2294 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2295 sgl_ptr++;
2296 sg_processed = i + 1;
2297
2298 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2299 (sge_count > fusion->max_sge_in_main_msg)) {
2300
2301 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2302 if (instance->adapter_type >= INVADER_SERIES) {
2303 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2304 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2305 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2306 cmd->io_request->ChainOffset =
2307 fusion->
2308 chain_offset_io_request;
2309 else
2310 cmd->io_request->ChainOffset = 0;
2311 } else
2312 cmd->io_request->ChainOffset =
2313 fusion->chain_offset_io_request;
2314
2315 sg_chain = sgl_ptr;
2316 /* Prepare chain element */
2317 sg_chain->NextChainOffset = 0;
2318 if (instance->adapter_type >= INVADER_SERIES)
2319 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2320 else
2321 sg_chain->Flags =
2322 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2323 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2324 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2325 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2326
2327 sgl_ptr =
2328 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2329 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2330 }
2331 }
2332 }
2333
2334 /**
2335 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2336 * @scp: SCSI command pointer
2337 * @instance: Soft instance of controller
2338 * @cmd: Fusion command pointer
2339 *
2340 * This function will build sgls based on device type.
2341 * For nvme drives, there is different way of building sgls in nvme native
2342 * format- PRPs(Physical Region Page).
2343 *
2344 * Returns the number of sg lists actually used, zero if the sg lists
2345 * is NULL, or -ENOMEM if the mapping failed
2346 */
2347 static
megasas_make_sgl(struct megasas_instance * instance,struct scsi_cmnd * scp,struct megasas_cmd_fusion * cmd)2348 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2349 struct megasas_cmd_fusion *cmd)
2350 {
2351 int sge_count;
2352 bool build_prp = false;
2353 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2354
2355 sge_count = scsi_dma_map(scp);
2356
2357 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2358 return sge_count;
2359
2360 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2361 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2362 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2363 (cmd->pd_interface == NVME_PD))
2364 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2365 cmd, sge_count);
2366
2367 if (!build_prp)
2368 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2369 cmd, sge_count);
2370
2371 return sge_count;
2372 }
2373
2374 /**
2375 * megasas_set_pd_lba - Sets PD LBA
2376 * @io_request: IO request
2377 * @cdb_len: cdb length
2378 * @io_info: IO information
2379 * @scp: SCSI command
2380 * @local_map_ptr: Raid map
2381 * @ref_tag: Primary reference tag
2382 *
2383 * Used to set the PD LBA in CDB for FP IOs
2384 */
2385 static void
megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST * io_request,u8 cdb_len,struct IO_REQUEST_INFO * io_info,struct scsi_cmnd * scp,struct MR_DRV_RAID_MAP_ALL * local_map_ptr,u32 ref_tag)2386 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2387 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2388 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2389 {
2390 struct MR_LD_RAID *raid;
2391 u16 ld;
2392 u64 start_blk = io_info->pdBlock;
2393 u8 *cdb = io_request->CDB.CDB32;
2394 u32 num_blocks = io_info->numBlocks;
2395 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2396
2397 /* Check if T10 PI (DIF) is enabled for this LD */
2398 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2399 raid = MR_LdRaidGet(ld, local_map_ptr);
2400 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2401 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2402 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2403 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2404
2405 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2406 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2407 else
2408 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2409 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2410
2411 /* LBA */
2412 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2413 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2414 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2415 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2416 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2417 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2418 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2419 cdb[19] = (u8)(start_blk & 0xff);
2420
2421 /* Logical block reference tag */
2422 io_request->CDB.EEDP32.PrimaryReferenceTag =
2423 cpu_to_be32(ref_tag);
2424 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2425 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2426
2427 /* Transfer length */
2428 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2429 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2430 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2431 cdb[31] = (u8)(num_blocks & 0xff);
2432
2433 /* set SCSI IO EEDPFlags */
2434 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2435 io_request->EEDPFlags = cpu_to_le16(
2436 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2437 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2438 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2439 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2440 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2441 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2442 } else {
2443 io_request->EEDPFlags = cpu_to_le16(
2444 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2445 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2446 }
2447 io_request->Control |= cpu_to_le32((0x4 << 26));
2448 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2449 } else {
2450 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2451 if (((cdb_len == 12) || (cdb_len == 16)) &&
2452 (start_blk <= 0xffffffff)) {
2453 if (cdb_len == 16) {
2454 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2455 flagvals = cdb[1];
2456 groupnum = cdb[14];
2457 control = cdb[15];
2458 } else {
2459 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2460 flagvals = cdb[1];
2461 groupnum = cdb[10];
2462 control = cdb[11];
2463 }
2464
2465 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2466
2467 cdb[0] = opcode;
2468 cdb[1] = flagvals;
2469 cdb[6] = groupnum;
2470 cdb[9] = control;
2471
2472 /* Transfer length */
2473 cdb[8] = (u8)(num_blocks & 0xff);
2474 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2475
2476 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2477 cdb_len = 10;
2478 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2479 /* Convert to 16 byte CDB for large LBA's */
2480 switch (cdb_len) {
2481 case 6:
2482 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2483 control = cdb[5];
2484 break;
2485 case 10:
2486 opcode =
2487 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2488 flagvals = cdb[1];
2489 groupnum = cdb[6];
2490 control = cdb[9];
2491 break;
2492 case 12:
2493 opcode =
2494 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2495 flagvals = cdb[1];
2496 groupnum = cdb[10];
2497 control = cdb[11];
2498 break;
2499 }
2500
2501 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2502
2503 cdb[0] = opcode;
2504 cdb[1] = flagvals;
2505 cdb[14] = groupnum;
2506 cdb[15] = control;
2507
2508 /* Transfer length */
2509 cdb[13] = (u8)(num_blocks & 0xff);
2510 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2511 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2512 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2513
2514 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2515 cdb_len = 16;
2516 }
2517
2518 /* Normal case, just load LBA here */
2519 switch (cdb_len) {
2520 case 6:
2521 {
2522 u8 val = cdb[1] & 0xE0;
2523 cdb[3] = (u8)(start_blk & 0xff);
2524 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2525 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2526 break;
2527 }
2528 case 10:
2529 cdb[5] = (u8)(start_blk & 0xff);
2530 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2531 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2532 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2533 break;
2534 case 12:
2535 cdb[5] = (u8)(start_blk & 0xff);
2536 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2537 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2538 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2539 break;
2540 case 16:
2541 cdb[9] = (u8)(start_blk & 0xff);
2542 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2543 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2544 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2545 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2546 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2547 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2548 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2549 break;
2550 }
2551 }
2552 }
2553
2554 /**
2555 * megasas_stream_detect - stream detection on read and and write IOs
2556 * @instance: Adapter soft state
2557 * @cmd: Command to be prepared
2558 * @io_info: IO Request info
2559 *
2560 */
2561
2562 /** stream detection on read and and write IOs */
megasas_stream_detect(struct megasas_instance * instance,struct megasas_cmd_fusion * cmd,struct IO_REQUEST_INFO * io_info)2563 static void megasas_stream_detect(struct megasas_instance *instance,
2564 struct megasas_cmd_fusion *cmd,
2565 struct IO_REQUEST_INFO *io_info)
2566 {
2567 struct fusion_context *fusion = instance->ctrl_context;
2568 u32 device_id = io_info->ldTgtId;
2569 struct LD_STREAM_DETECT *current_ld_sd
2570 = fusion->stream_detect_by_ld[device_id];
2571 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2572 u32 shifted_values, unshifted_values;
2573 u32 index_value_mask, shifted_values_mask;
2574 int i;
2575 bool is_read_ahead = false;
2576 struct STREAM_DETECT *current_sd;
2577 /* find possible stream */
2578 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2579 stream_num = (*track_stream >>
2580 (i * BITS_PER_INDEX_STREAM)) &
2581 STREAM_MASK;
2582 current_sd = ¤t_ld_sd->stream_track[stream_num];
2583 /* if we found a stream, update the raid
2584 * context and also update the mruBitMap
2585 */
2586 /* boundary condition */
2587 if ((current_sd->next_seq_lba) &&
2588 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2589 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2590 (current_sd->is_read == io_info->isRead)) {
2591
2592 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2593 ((!io_info->isRead) || (!is_read_ahead)))
2594 /*
2595 * Once the API is available we need to change this.
2596 * At this point we are not allowing any gap
2597 */
2598 continue;
2599
2600 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2601 current_sd->next_seq_lba =
2602 io_info->ldStartBlock + io_info->numBlocks;
2603 /*
2604 * update the mruBitMap LRU
2605 */
2606 shifted_values_mask =
2607 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2608 shifted_values = ((*track_stream & shifted_values_mask)
2609 << BITS_PER_INDEX_STREAM);
2610 index_value_mask =
2611 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2612 unshifted_values =
2613 *track_stream & ~(shifted_values_mask |
2614 index_value_mask);
2615 *track_stream =
2616 unshifted_values | shifted_values | stream_num;
2617 return;
2618 }
2619 }
2620 /*
2621 * if we did not find any stream, create a new one
2622 * from the least recently used
2623 */
2624 stream_num = (*track_stream >>
2625 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2626 STREAM_MASK;
2627 current_sd = ¤t_ld_sd->stream_track[stream_num];
2628 current_sd->is_read = io_info->isRead;
2629 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2630 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2631 return;
2632 }
2633
2634 /**
2635 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2636 * affinity (cpu of the controller) and raid_flags in the raid context
2637 * based on IO type.
2638 *
2639 * @fusion: Fusion context
2640 * @praid_context: IO RAID context
2641 * @raid: LD raid map
2642 * @fp_possible: Is fast path possible?
2643 * @is_read: Is read IO?
2644 * @scsi_buff_len: SCSI command buffer length
2645 *
2646 */
2647 static void
megasas_set_raidflag_cpu_affinity(struct fusion_context * fusion,union RAID_CONTEXT_UNION * praid_context,struct MR_LD_RAID * raid,bool fp_possible,u8 is_read,u32 scsi_buff_len)2648 megasas_set_raidflag_cpu_affinity(struct fusion_context *fusion,
2649 union RAID_CONTEXT_UNION *praid_context,
2650 struct MR_LD_RAID *raid, bool fp_possible,
2651 u8 is_read, u32 scsi_buff_len)
2652 {
2653 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2654 struct RAID_CONTEXT_G35 *rctx_g35;
2655
2656 rctx_g35 = &praid_context->raid_context_g35;
2657 if (fp_possible) {
2658 if (is_read) {
2659 if ((raid->cpuAffinity.pdRead.cpu0) &&
2660 (raid->cpuAffinity.pdRead.cpu1))
2661 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2662 else if (raid->cpuAffinity.pdRead.cpu1)
2663 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2664 } else {
2665 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2666 (raid->cpuAffinity.pdWrite.cpu1))
2667 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2668 else if (raid->cpuAffinity.pdWrite.cpu1)
2669 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2670 /* Fast path cache by pass capable R0/R1 VD */
2671 if ((raid->level <= 1) &&
2672 (raid->capability.fp_cache_bypass_capable)) {
2673 rctx_g35->routing_flags |=
2674 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2675 rctx_g35->raid_flags =
2676 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2677 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2678 }
2679 }
2680 } else {
2681 if (is_read) {
2682 if ((raid->cpuAffinity.ldRead.cpu0) &&
2683 (raid->cpuAffinity.ldRead.cpu1))
2684 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2685 else if (raid->cpuAffinity.ldRead.cpu1)
2686 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2687 } else {
2688 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2689 (raid->cpuAffinity.ldWrite.cpu1))
2690 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2691 else if (raid->cpuAffinity.ldWrite.cpu1)
2692 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2693
2694 if (is_stream_detected(rctx_g35) &&
2695 ((raid->level == 5) || (raid->level == 6)) &&
2696 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2697 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2698 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2699 }
2700 }
2701
2702 rctx_g35->routing_flags |=
2703 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2704
2705 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2706 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2707 * IO Subtype is not bitmap.
2708 */
2709 if ((fusion->pcie_bw_limitation) && (raid->level == 1) && (!is_read) &&
2710 (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)) {
2711 praid_context->raid_context_g35.raid_flags =
2712 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2713 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2714 }
2715 }
2716
2717 /**
2718 * megasas_build_ldio_fusion - Prepares IOs to devices
2719 * @instance: Adapter soft state
2720 * @scp: SCSI command
2721 * @cmd: Command to be prepared
2722 *
2723 * Prepares the io_request and chain elements (sg_frame) for IO
2724 * The IO can be for PD (Fast Path) or LD
2725 */
2726 static void
megasas_build_ldio_fusion(struct megasas_instance * instance,struct scsi_cmnd * scp,struct megasas_cmd_fusion * cmd)2727 megasas_build_ldio_fusion(struct megasas_instance *instance,
2728 struct scsi_cmnd *scp,
2729 struct megasas_cmd_fusion *cmd)
2730 {
2731 bool fp_possible;
2732 u16 ld;
2733 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2734 u32 scsi_buff_len;
2735 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2736 struct IO_REQUEST_INFO io_info;
2737 struct fusion_context *fusion;
2738 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2739 u8 *raidLUN;
2740 unsigned long spinlock_flags;
2741 struct MR_LD_RAID *raid = NULL;
2742 struct MR_PRIV_DEVICE *mrdev_priv;
2743 struct RAID_CONTEXT *rctx;
2744 struct RAID_CONTEXT_G35 *rctx_g35;
2745
2746 device_id = MEGASAS_DEV_INDEX(scp);
2747
2748 fusion = instance->ctrl_context;
2749
2750 io_request = cmd->io_request;
2751 rctx = &io_request->RaidContext.raid_context;
2752 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2753
2754 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2755 rctx->status = 0;
2756 rctx->ex_status = 0;
2757
2758 start_lba_lo = 0;
2759 start_lba_hi = 0;
2760 fp_possible = false;
2761
2762 /*
2763 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2764 */
2765 if (scp->cmd_len == 6) {
2766 datalength = (u32) scp->cmnd[4];
2767 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2768 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2769
2770 start_lba_lo &= 0x1FFFFF;
2771 }
2772
2773 /*
2774 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2775 */
2776 else if (scp->cmd_len == 10) {
2777 datalength = (u32) scp->cmnd[8] |
2778 ((u32) scp->cmnd[7] << 8);
2779 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2780 ((u32) scp->cmnd[3] << 16) |
2781 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2782 }
2783
2784 /*
2785 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2786 */
2787 else if (scp->cmd_len == 12) {
2788 datalength = ((u32) scp->cmnd[6] << 24) |
2789 ((u32) scp->cmnd[7] << 16) |
2790 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2791 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2792 ((u32) scp->cmnd[3] << 16) |
2793 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2794 }
2795
2796 /*
2797 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2798 */
2799 else if (scp->cmd_len == 16) {
2800 datalength = ((u32) scp->cmnd[10] << 24) |
2801 ((u32) scp->cmnd[11] << 16) |
2802 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2803 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2804 ((u32) scp->cmnd[7] << 16) |
2805 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2806
2807 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2808 ((u32) scp->cmnd[3] << 16) |
2809 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2810 }
2811
2812 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2813 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2814 io_info.numBlocks = datalength;
2815 io_info.ldTgtId = device_id;
2816 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2817 scsi_buff_len = scsi_bufflen(scp);
2818 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2819 io_info.data_arms = 1;
2820
2821 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2822 io_info.isRead = 1;
2823
2824 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2825 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2826
2827 if (ld < instance->fw_supported_vd_count)
2828 raid = MR_LdRaidGet(ld, local_map_ptr);
2829
2830 if (!raid || (!fusion->fast_path_io)) {
2831 rctx->reg_lock_flags = 0;
2832 fp_possible = false;
2833 } else {
2834 if (MR_BuildRaidContext(instance, &io_info, rctx,
2835 local_map_ptr, &raidLUN))
2836 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2837 }
2838
2839 megasas_get_msix_index(instance, scp, cmd, io_info.data_arms);
2840
2841 if (instance->adapter_type >= VENTURA_SERIES) {
2842 /* FP for Optimal raid level 1.
2843 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2844 * are built by the driver as LD I/Os.
2845 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2846 * (there is never a reason to process these as buffered writes)
2847 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2848 * with the SLD bit asserted.
2849 */
2850 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2851 mrdev_priv = scp->device->hostdata;
2852
2853 if (atomic_inc_return(&instance->fw_outstanding) >
2854 (instance->host->can_queue)) {
2855 fp_possible = false;
2856 atomic_dec(&instance->fw_outstanding);
2857 } else if (fusion->pcie_bw_limitation &&
2858 ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2859 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0))) {
2860 fp_possible = false;
2861 atomic_dec(&instance->fw_outstanding);
2862 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2863 atomic_set(&mrdev_priv->r1_ldio_hint,
2864 instance->r1_ldio_hint_default);
2865 }
2866 }
2867
2868 if (!fp_possible ||
2869 (io_info.isRead && io_info.ra_capable)) {
2870 spin_lock_irqsave(&instance->stream_lock,
2871 spinlock_flags);
2872 megasas_stream_detect(instance, cmd, &io_info);
2873 spin_unlock_irqrestore(&instance->stream_lock,
2874 spinlock_flags);
2875 /* In ventura if stream detected for a read and it is
2876 * read ahead capable make this IO as LDIO
2877 */
2878 if (is_stream_detected(rctx_g35))
2879 fp_possible = false;
2880 }
2881
2882 /* If raid is NULL, set CPU affinity to default CPU0 */
2883 if (raid)
2884 megasas_set_raidflag_cpu_affinity(fusion, &io_request->RaidContext,
2885 raid, fp_possible, io_info.isRead,
2886 scsi_buff_len);
2887 else
2888 rctx_g35->routing_flags |=
2889 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2890 }
2891
2892 if (fp_possible) {
2893 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2894 local_map_ptr, start_lba_lo);
2895 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2896 cmd->request_desc->SCSIIO.RequestFlags =
2897 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2898 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2899 if (instance->adapter_type == INVADER_SERIES) {
2900 rctx->type = MPI2_TYPE_CUDA;
2901 rctx->nseg = 0x1;
2902 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2903 rctx->reg_lock_flags |=
2904 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2905 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2906 } else if (instance->adapter_type >= VENTURA_SERIES) {
2907 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2908 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2909 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2910 io_request->IoFlags |=
2911 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2912 }
2913 if (fusion->load_balance_info &&
2914 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2915 (io_info.isRead)) {
2916 io_info.devHandle =
2917 get_updated_dev_handle(instance,
2918 &fusion->load_balance_info[device_id],
2919 &io_info, local_map_ptr);
2920 megasas_priv(scp)->status |= MEGASAS_LOAD_BALANCE_FLAG;
2921 cmd->pd_r1_lb = io_info.pd_after_lb;
2922 if (instance->adapter_type >= VENTURA_SERIES)
2923 rctx_g35->span_arm = io_info.span_arm;
2924 else
2925 rctx->span_arm = io_info.span_arm;
2926
2927 } else
2928 megasas_priv(scp)->status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2929
2930 if (instance->adapter_type >= VENTURA_SERIES)
2931 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2932 else
2933 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2934
2935 if ((raidLUN[0] == 1) &&
2936 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2937 instance->dev_handle = !(instance->dev_handle);
2938 io_info.devHandle =
2939 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2940 }
2941
2942 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2943 io_request->DevHandle = io_info.devHandle;
2944 cmd->pd_interface = io_info.pd_interface;
2945 /* populate the LUN field */
2946 memcpy(io_request->LUN, raidLUN, 8);
2947 } else {
2948 rctx->timeout_value =
2949 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2950 cmd->request_desc->SCSIIO.RequestFlags =
2951 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2952 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2953 if (instance->adapter_type == INVADER_SERIES) {
2954 if (io_info.do_fp_rlbypass ||
2955 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2956 cmd->request_desc->SCSIIO.RequestFlags =
2957 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2958 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2959 rctx->type = MPI2_TYPE_CUDA;
2960 rctx->reg_lock_flags |=
2961 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2962 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2963 rctx->nseg = 0x1;
2964 } else if (instance->adapter_type >= VENTURA_SERIES) {
2965 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2966 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2967 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2968 }
2969 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2970 io_request->DevHandle = cpu_to_le16(device_id);
2971
2972 } /* Not FP */
2973 }
2974
2975 /**
2976 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2977 * @instance: Adapter soft state
2978 * @scmd: SCSI command
2979 * @cmd: Command to be prepared
2980 *
2981 * Prepares the io_request frame for non-rw io cmds for vd.
2982 */
megasas_build_ld_nonrw_fusion(struct megasas_instance * instance,struct scsi_cmnd * scmd,struct megasas_cmd_fusion * cmd)2983 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2984 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2985 {
2986 u32 device_id;
2987 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2988 u16 ld;
2989 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2990 struct fusion_context *fusion = instance->ctrl_context;
2991 u8 span, physArm;
2992 __le16 devHandle;
2993 u32 arRef, pd;
2994 struct MR_LD_RAID *raid;
2995 struct RAID_CONTEXT *pRAID_Context;
2996 u8 fp_possible = 1;
2997
2998 io_request = cmd->io_request;
2999 device_id = MEGASAS_DEV_INDEX(scmd);
3000 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
3001 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3002 /* get RAID_Context pointer */
3003 pRAID_Context = &io_request->RaidContext.raid_context;
3004 /* Check with FW team */
3005 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3006 pRAID_Context->reg_lock_row_lba = 0;
3007 pRAID_Context->reg_lock_length = 0;
3008
3009 if (fusion->fast_path_io && (
3010 device_id < instance->fw_supported_vd_count)) {
3011
3012 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
3013 if (ld >= instance->fw_supported_vd_count - 1)
3014 fp_possible = 0;
3015 else {
3016 raid = MR_LdRaidGet(ld, local_map_ptr);
3017 if (!(raid->capability.fpNonRWCapable))
3018 fp_possible = 0;
3019 }
3020 } else
3021 fp_possible = 0;
3022
3023 if (!fp_possible) {
3024 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3025 io_request->DevHandle = cpu_to_le16(device_id);
3026 io_request->LUN[1] = scmd->device->lun;
3027 pRAID_Context->timeout_value =
3028 cpu_to_le16(scsi_cmd_to_rq(scmd)->timeout / HZ);
3029 cmd->request_desc->SCSIIO.RequestFlags =
3030 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3031 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3032 } else {
3033
3034 /* set RAID context values */
3035 pRAID_Context->config_seq_num = raid->seqNum;
3036 if (instance->adapter_type < VENTURA_SERIES)
3037 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
3038 pRAID_Context->timeout_value =
3039 cpu_to_le16(raid->fpIoTimeoutForLd);
3040
3041 /* get the DevHandle for the PD (since this is
3042 fpNonRWCapable, this is a single disk RAID0) */
3043 span = physArm = 0;
3044 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
3045 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
3046 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
3047
3048 /* build request descriptor */
3049 cmd->request_desc->SCSIIO.RequestFlags =
3050 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3051 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3052 cmd->request_desc->SCSIIO.DevHandle = devHandle;
3053
3054 /* populate the LUN field */
3055 memcpy(io_request->LUN, raid->LUN, 8);
3056
3057 /* build the raidScsiIO structure */
3058 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3059 io_request->DevHandle = devHandle;
3060 }
3061 }
3062
3063 /**
3064 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3065 * @instance: Adapter soft state
3066 * @scmd: SCSI command
3067 * @cmd: Command to be prepared
3068 * @fp_possible: parameter to detect fast path or firmware path io.
3069 *
3070 * Prepares the io_request frame for rw/non-rw io cmds for syspds
3071 */
3072 static void
megasas_build_syspd_fusion(struct megasas_instance * instance,struct scsi_cmnd * scmd,struct megasas_cmd_fusion * cmd,bool fp_possible)3073 megasas_build_syspd_fusion(struct megasas_instance *instance,
3074 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
3075 bool fp_possible)
3076 {
3077 u32 device_id;
3078 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
3079 u16 pd_index = 0;
3080 u16 os_timeout_value;
3081 u16 timeout_limit;
3082 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
3083 struct RAID_CONTEXT *pRAID_Context;
3084 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3085 struct MR_PRIV_DEVICE *mr_device_priv_data;
3086 struct fusion_context *fusion = instance->ctrl_context;
3087 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
3088
3089 device_id = MEGASAS_DEV_INDEX(scmd);
3090 pd_index = MEGASAS_PD_INDEX(scmd);
3091 os_timeout_value = scsi_cmd_to_rq(scmd)->timeout / HZ;
3092 mr_device_priv_data = scmd->device->hostdata;
3093 cmd->pd_interface = mr_device_priv_data->interface_type;
3094
3095 io_request = cmd->io_request;
3096 /* get RAID_Context pointer */
3097 pRAID_Context = &io_request->RaidContext.raid_context;
3098 pRAID_Context->reg_lock_flags = 0;
3099 pRAID_Context->reg_lock_row_lba = 0;
3100 pRAID_Context->reg_lock_length = 0;
3101 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3102 io_request->LUN[1] = scmd->device->lun;
3103 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
3104 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
3105
3106 /* If FW supports PD sequence number */
3107 if (instance->support_seqnum_jbod_fp) {
3108 if (instance->use_seqnum_jbod_fp &&
3109 instance->pd_list[pd_index].driveType == TYPE_DISK) {
3110
3111 /* More than 256 PD/JBOD support for Ventura */
3112 if (instance->support_morethan256jbod)
3113 pRAID_Context->virtual_disk_tgt_id =
3114 pd_sync->seq[pd_index].pd_target_id;
3115 else
3116 pRAID_Context->virtual_disk_tgt_id =
3117 cpu_to_le16(device_id +
3118 (MAX_PHYSICAL_DEVICES - 1));
3119 pRAID_Context->config_seq_num =
3120 pd_sync->seq[pd_index].seqNum;
3121 io_request->DevHandle =
3122 pd_sync->seq[pd_index].devHandle;
3123 if (instance->adapter_type >= VENTURA_SERIES) {
3124 io_request->RaidContext.raid_context_g35.routing_flags |=
3125 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3126 io_request->RaidContext.raid_context_g35.nseg_type |=
3127 (1 << RAID_CONTEXT_NSEG_SHIFT);
3128 io_request->RaidContext.raid_context_g35.nseg_type |=
3129 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3130 } else {
3131 pRAID_Context->type = MPI2_TYPE_CUDA;
3132 pRAID_Context->nseg = 0x1;
3133 pRAID_Context->reg_lock_flags |=
3134 (MR_RL_FLAGS_SEQ_NUM_ENABLE |
3135 MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3136 }
3137 } else {
3138 pRAID_Context->virtual_disk_tgt_id =
3139 cpu_to_le16(device_id +
3140 (MAX_PHYSICAL_DEVICES - 1));
3141 pRAID_Context->config_seq_num = 0;
3142 io_request->DevHandle = cpu_to_le16(0xFFFF);
3143 }
3144 } else {
3145 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3146 pRAID_Context->config_seq_num = 0;
3147
3148 if (fusion->fast_path_io) {
3149 local_map_ptr =
3150 fusion->ld_drv_map[(instance->map_id & 1)];
3151 io_request->DevHandle =
3152 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3153 } else {
3154 io_request->DevHandle = cpu_to_le16(0xFFFF);
3155 }
3156 }
3157
3158 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3159
3160 megasas_get_msix_index(instance, scmd, cmd, 1);
3161
3162 if (!fp_possible) {
3163 /* system pd firmware path */
3164 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3165 cmd->request_desc->SCSIIO.RequestFlags =
3166 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3167 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3168 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3169 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3170 } else {
3171 if (os_timeout_value)
3172 os_timeout_value++;
3173
3174 /* system pd Fast Path */
3175 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3176 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3177 255 : 0xFFFF;
3178 pRAID_Context->timeout_value =
3179 cpu_to_le16((os_timeout_value > timeout_limit) ?
3180 timeout_limit : os_timeout_value);
3181 if (instance->adapter_type >= INVADER_SERIES)
3182 io_request->IoFlags |=
3183 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3184
3185 cmd->request_desc->SCSIIO.RequestFlags =
3186 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3187 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3188 }
3189 }
3190
3191 /**
3192 * megasas_build_io_fusion - Prepares IOs to devices
3193 * @instance: Adapter soft state
3194 * @scp: SCSI command
3195 * @cmd: Command to be prepared
3196 *
3197 * Invokes helper functions to prepare request frames
3198 * and sets flags appropriate for IO/Non-IO cmd
3199 */
3200 static int
megasas_build_io_fusion(struct megasas_instance * instance,struct scsi_cmnd * scp,struct megasas_cmd_fusion * cmd)3201 megasas_build_io_fusion(struct megasas_instance *instance,
3202 struct scsi_cmnd *scp,
3203 struct megasas_cmd_fusion *cmd)
3204 {
3205 int sge_count;
3206 u16 pd_index = 0;
3207 u8 drive_type = 0;
3208 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3209 struct MR_PRIV_DEVICE *mr_device_priv_data;
3210 mr_device_priv_data = scp->device->hostdata;
3211
3212 /* Zero out some fields so they don't get reused */
3213 memset(io_request->LUN, 0x0, 8);
3214 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3215 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3216 io_request->EEDPFlags = 0;
3217 io_request->Control = 0;
3218 io_request->EEDPBlockSize = 0;
3219 io_request->ChainOffset = 0;
3220 io_request->RaidContext.raid_context.raid_flags = 0;
3221 io_request->RaidContext.raid_context.type = 0;
3222 io_request->RaidContext.raid_context.nseg = 0;
3223
3224 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3225 /*
3226 * Just the CDB length,rest of the Flags are zero
3227 * This will be modified for FP in build_ldio_fusion
3228 */
3229 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3230
3231 switch (megasas_cmd_type(scp)) {
3232 case READ_WRITE_LDIO:
3233 megasas_build_ldio_fusion(instance, scp, cmd);
3234 break;
3235 case NON_READ_WRITE_LDIO:
3236 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3237 break;
3238 case READ_WRITE_SYSPDIO:
3239 megasas_build_syspd_fusion(instance, scp, cmd, true);
3240 break;
3241 case NON_READ_WRITE_SYSPDIO:
3242 pd_index = MEGASAS_PD_INDEX(scp);
3243 drive_type = instance->pd_list[pd_index].driveType;
3244 if ((instance->secure_jbod_support ||
3245 mr_device_priv_data->is_tm_capable) ||
3246 (instance->adapter_type >= VENTURA_SERIES &&
3247 drive_type == TYPE_ENCLOSURE))
3248 megasas_build_syspd_fusion(instance, scp, cmd, false);
3249 else
3250 megasas_build_syspd_fusion(instance, scp, cmd, true);
3251 break;
3252 default:
3253 break;
3254 }
3255
3256 /*
3257 * Construct SGL
3258 */
3259
3260 sge_count = megasas_make_sgl(instance, scp, cmd);
3261
3262 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3263 dev_err(&instance->pdev->dev,
3264 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3265 __func__, __LINE__, sge_count, instance->max_num_sge);
3266 return 1;
3267 }
3268
3269 if (instance->adapter_type >= VENTURA_SERIES) {
3270 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3271 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3272 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3273 } else {
3274 /* numSGE store lower 8 bit of sge_count.
3275 * numSGEExt store higher 8 bit of sge_count
3276 */
3277 io_request->RaidContext.raid_context.num_sge = sge_count;
3278 io_request->RaidContext.raid_context.num_sge_ext =
3279 (u8)(sge_count >> 8);
3280 }
3281
3282 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3283
3284 if (scp->sc_data_direction == DMA_TO_DEVICE)
3285 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3286 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3287 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3288
3289 io_request->SGLOffset0 =
3290 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3291
3292 io_request->SenseBufferLowAddress =
3293 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3294 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3295
3296 cmd->scmd = scp;
3297 megasas_priv(scp)->cmd_priv = cmd;
3298
3299 return 0;
3300 }
3301
3302 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
megasas_get_request_descriptor(struct megasas_instance * instance,u16 index)3303 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3304 {
3305 u8 *p;
3306 struct fusion_context *fusion;
3307
3308 fusion = instance->ctrl_context;
3309 p = fusion->req_frames_desc +
3310 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3311
3312 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3313 }
3314
3315
3316 /* megasas_prepate_secondRaid1_IO
3317 * It prepares the raid 1 second IO
3318 */
megasas_prepare_secondRaid1_IO(struct megasas_instance * instance,struct megasas_cmd_fusion * cmd,struct megasas_cmd_fusion * r1_cmd)3319 static void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3320 struct megasas_cmd_fusion *cmd,
3321 struct megasas_cmd_fusion *r1_cmd)
3322 {
3323 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3324 struct fusion_context *fusion;
3325 fusion = instance->ctrl_context;
3326 req_desc = cmd->request_desc;
3327 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3328 memcpy(r1_cmd->io_request, cmd->io_request,
3329 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3330 memcpy(r1_cmd->io_request->SGLs, cmd->io_request->SGLs,
3331 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3332 /*sense buffer is different for r1 command*/
3333 r1_cmd->io_request->SenseBufferLowAddress =
3334 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3335 r1_cmd->scmd = cmd->scmd;
3336 req_desc2 = megasas_get_request_descriptor(instance,
3337 (r1_cmd->index - 1));
3338 req_desc2->Words = 0;
3339 r1_cmd->request_desc = req_desc2;
3340 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3341 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3342 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3343 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3344 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3345 cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3346 cpu_to_le16(r1_cmd->index);
3347 r1_cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3348 cpu_to_le16(cmd->index);
3349 /*MSIxIndex of both commands request descriptors should be same*/
3350 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3351 cmd->request_desc->SCSIIO.MSIxIndex;
3352 /*span arm is different for r1 cmd*/
3353 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3354 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3355 }
3356
3357 /**
3358 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3359 * issuing non IOCTL cmd
3360 * @instance: Adapter soft state
3361 * @scmd: pointer to scsi cmd from OS
3362 */
3363 static u32
megasas_build_and_issue_cmd_fusion(struct megasas_instance * instance,struct scsi_cmnd * scmd)3364 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3365 struct scsi_cmnd *scmd)
3366 {
3367 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3368 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3369 u32 index;
3370
3371 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3372 instance->ldio_threshold &&
3373 (atomic_inc_return(&instance->ldio_outstanding) >
3374 instance->ldio_threshold)) {
3375 atomic_dec(&instance->ldio_outstanding);
3376 return SCSI_MLQUEUE_DEVICE_BUSY;
3377 }
3378
3379 if (atomic_inc_return(&instance->fw_outstanding) >
3380 instance->host->can_queue) {
3381 atomic_dec(&instance->fw_outstanding);
3382 return SCSI_MLQUEUE_HOST_BUSY;
3383 }
3384
3385 cmd = megasas_get_cmd_fusion(instance, scsi_cmd_to_rq(scmd)->tag);
3386
3387 if (!cmd) {
3388 atomic_dec(&instance->fw_outstanding);
3389 return SCSI_MLQUEUE_HOST_BUSY;
3390 }
3391
3392 index = cmd->index;
3393
3394 req_desc = megasas_get_request_descriptor(instance, index-1);
3395
3396 req_desc->Words = 0;
3397 cmd->request_desc = req_desc;
3398
3399 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3400 megasas_return_cmd_fusion(instance, cmd);
3401 dev_err(&instance->pdev->dev, "Error building command\n");
3402 cmd->request_desc = NULL;
3403 atomic_dec(&instance->fw_outstanding);
3404 return SCSI_MLQUEUE_HOST_BUSY;
3405 }
3406
3407 req_desc = cmd->request_desc;
3408 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3409
3410 if (cmd->io_request->ChainOffset != 0 &&
3411 cmd->io_request->ChainOffset != 0xF)
3412 dev_err(&instance->pdev->dev, "The chain offset value is not "
3413 "correct : %x\n", cmd->io_request->ChainOffset);
3414 /*
3415 * if it is raid 1/10 fp write capable.
3416 * try to get second command from pool and construct it.
3417 * From FW, it has confirmed that lba values of two PDs
3418 * corresponds to single R1/10 LD are always same
3419 *
3420 */
3421 /* driver side count always should be less than max_fw_cmds
3422 * to get new command
3423 */
3424 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3425 r1_cmd = megasas_get_cmd_fusion(instance,
3426 scsi_cmd_to_rq(scmd)->tag + instance->max_fw_cmds);
3427 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3428 }
3429
3430
3431 /*
3432 * Issue the command to the FW
3433 */
3434
3435 megasas_sdev_busy_inc(instance, scmd);
3436 megasas_fire_cmd_fusion(instance, req_desc);
3437
3438 if (r1_cmd)
3439 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3440
3441
3442 return 0;
3443 }
3444
3445 /**
3446 * megasas_complete_r1_command -
3447 * completes R1 FP write commands which has valid peer smid
3448 * @instance: Adapter soft state
3449 * @cmd: MPT command frame
3450 *
3451 */
3452 static inline void
megasas_complete_r1_command(struct megasas_instance * instance,struct megasas_cmd_fusion * cmd)3453 megasas_complete_r1_command(struct megasas_instance *instance,
3454 struct megasas_cmd_fusion *cmd)
3455 {
3456 u8 *sense, status, ex_status;
3457 u32 data_length;
3458 u16 peer_smid;
3459 struct fusion_context *fusion;
3460 struct megasas_cmd_fusion *r1_cmd = NULL;
3461 struct scsi_cmnd *scmd_local = NULL;
3462 struct RAID_CONTEXT_G35 *rctx_g35;
3463
3464 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3465 fusion = instance->ctrl_context;
3466 peer_smid = le16_to_cpu(rctx_g35->flow_specific.peer_smid);
3467
3468 r1_cmd = fusion->cmd_list[peer_smid - 1];
3469 scmd_local = cmd->scmd;
3470 status = rctx_g35->status;
3471 ex_status = rctx_g35->ex_status;
3472 data_length = cmd->io_request->DataLength;
3473 sense = cmd->sense;
3474
3475 cmd->cmd_completed = true;
3476
3477 /* Check if peer command is completed or not*/
3478 if (r1_cmd->cmd_completed) {
3479 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3480 if (rctx_g35->status != MFI_STAT_OK) {
3481 status = rctx_g35->status;
3482 ex_status = rctx_g35->ex_status;
3483 data_length = r1_cmd->io_request->DataLength;
3484 sense = r1_cmd->sense;
3485 }
3486
3487 megasas_return_cmd_fusion(instance, r1_cmd);
3488 map_cmd_status(fusion, scmd_local, status, ex_status,
3489 le32_to_cpu(data_length), sense);
3490 if (instance->ldio_threshold &&
3491 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3492 atomic_dec(&instance->ldio_outstanding);
3493 megasas_priv(scmd_local)->cmd_priv = NULL;
3494 megasas_return_cmd_fusion(instance, cmd);
3495 scsi_dma_unmap(scmd_local);
3496 megasas_sdev_busy_dec(instance, scmd_local);
3497 scsi_done(scmd_local);
3498 }
3499 }
3500
3501 /**
3502 * access_irq_context: Access to reply processing
3503 * @irq_context: IRQ context
3504 *
3505 * Synchronize access to reply processing.
3506 *
3507 * Return: true on success, false on failure.
3508 */
3509 static inline
access_irq_context(struct megasas_irq_context * irq_context)3510 bool access_irq_context(struct megasas_irq_context *irq_context)
3511 {
3512 if (!irq_context)
3513 return true;
3514
3515 if (atomic_add_unless(&irq_context->in_used, 1, 1))
3516 return true;
3517
3518 return false;
3519 }
3520
3521 /**
3522 * release_irq_context: Release reply processing
3523 * @irq_context: IRQ context
3524 *
3525 * Release access of reply processing.
3526 *
3527 * Return: Nothing.
3528 */
3529 static inline
release_irq_context(struct megasas_irq_context * irq_context)3530 void release_irq_context(struct megasas_irq_context *irq_context)
3531 {
3532 if (irq_context)
3533 atomic_dec(&irq_context->in_used);
3534 }
3535
3536 /**
3537 * complete_cmd_fusion - Completes command
3538 * @instance: Adapter soft state
3539 * @MSIxIndex: MSI number
3540 * @irq_context: IRQ context
3541 *
3542 * Completes all commands that is in reply descriptor queue
3543 */
3544 static int
complete_cmd_fusion(struct megasas_instance * instance,u32 MSIxIndex,struct megasas_irq_context * irq_context)3545 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex,
3546 struct megasas_irq_context *irq_context)
3547 {
3548 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3549 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3550 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3551 struct fusion_context *fusion;
3552 struct megasas_cmd *cmd_mfi;
3553 struct megasas_cmd_fusion *cmd_fusion;
3554 u16 smid, num_completed;
3555 u8 reply_descript_type, *sense, status, extStatus;
3556 u32 device_id, data_length;
3557 union desc_value d_val;
3558 struct LD_LOAD_BALANCE_INFO *lbinfo;
3559 int threshold_reply_count = 0;
3560 struct scsi_cmnd *scmd_local = NULL;
3561 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3562 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3563
3564 fusion = instance->ctrl_context;
3565
3566 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3567 return IRQ_HANDLED;
3568
3569 if (!access_irq_context(irq_context))
3570 return 0;
3571
3572 desc = fusion->reply_frames_desc[MSIxIndex] +
3573 fusion->last_reply_idx[MSIxIndex];
3574
3575 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3576
3577 d_val.word = desc->Words;
3578
3579 reply_descript_type = reply_desc->ReplyFlags &
3580 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3581
3582 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
3583 release_irq_context(irq_context);
3584 return IRQ_NONE;
3585 }
3586
3587 num_completed = 0;
3588
3589 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3590 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3591
3592 smid = le16_to_cpu(reply_desc->SMID);
3593 cmd_fusion = fusion->cmd_list[smid - 1];
3594 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3595 cmd_fusion->io_request;
3596
3597 scmd_local = cmd_fusion->scmd;
3598 status = scsi_io_req->RaidContext.raid_context.status;
3599 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3600 sense = cmd_fusion->sense;
3601 data_length = scsi_io_req->DataLength;
3602
3603 switch (scsi_io_req->Function) {
3604 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3605 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3606 cmd_fusion->io_request;
3607 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3608 &mr_tm_req->TmRequest;
3609 dev_dbg(&instance->pdev->dev, "TM completion:"
3610 "type: 0x%x TaskMID: 0x%x\n",
3611 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3612 complete(&cmd_fusion->done);
3613 break;
3614 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3615 /*
3616 * Firmware can send stale/duplicate completions for
3617 * commands already returned to the pool. scmd_local
3618 * would be NULL for such cases. Skip processing to
3619 * avoid NULL pointer access.
3620 */
3621 if (!scmd_local)
3622 break;
3623
3624 /* Update load balancing info */
3625 if (fusion->load_balance_info &&
3626 (megasas_priv(cmd_fusion->scmd)->status &
3627 MEGASAS_LOAD_BALANCE_FLAG)) {
3628 device_id = MEGASAS_DEV_INDEX(scmd_local);
3629 lbinfo = &fusion->load_balance_info[device_id];
3630 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3631 megasas_priv(cmd_fusion->scmd)->status &=
3632 ~MEGASAS_LOAD_BALANCE_FLAG;
3633 }
3634 fallthrough; /* and complete IO */
3635 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3636 atomic_dec(&instance->fw_outstanding);
3637 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3638 map_cmd_status(fusion, scmd_local, status,
3639 extStatus, le32_to_cpu(data_length),
3640 sense);
3641 if (instance->ldio_threshold &&
3642 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3643 atomic_dec(&instance->ldio_outstanding);
3644 megasas_priv(scmd_local)->cmd_priv = NULL;
3645 megasas_return_cmd_fusion(instance, cmd_fusion);
3646 scsi_dma_unmap(scmd_local);
3647 megasas_sdev_busy_dec(instance, scmd_local);
3648 scsi_done(scmd_local);
3649 } else /* Optimal VD - R1 FP command completion. */
3650 megasas_complete_r1_command(instance, cmd_fusion);
3651 break;
3652 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3653 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3654 /* Poll mode. Dummy free.
3655 * In case of Interrupt mode, caller has reverse check.
3656 */
3657 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3658 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3659 megasas_return_cmd(instance, cmd_mfi);
3660 } else
3661 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3662 break;
3663 }
3664
3665 fusion->last_reply_idx[MSIxIndex]++;
3666 if (fusion->last_reply_idx[MSIxIndex] >=
3667 fusion->reply_q_depth)
3668 fusion->last_reply_idx[MSIxIndex] = 0;
3669
3670 desc->Words = cpu_to_le64(ULLONG_MAX);
3671 num_completed++;
3672 threshold_reply_count++;
3673
3674 /* Get the next reply descriptor */
3675 if (!fusion->last_reply_idx[MSIxIndex])
3676 desc = fusion->reply_frames_desc[MSIxIndex];
3677 else
3678 desc++;
3679
3680 reply_desc =
3681 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3682
3683 d_val.word = desc->Words;
3684
3685 reply_descript_type = reply_desc->ReplyFlags &
3686 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3687
3688 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3689 break;
3690 /*
3691 * Write to reply post host index register after completing threshold
3692 * number of reply counts and still there are more replies in reply queue
3693 * pending to be completed
3694 */
3695 if (threshold_reply_count >= instance->threshold_reply_count) {
3696 if (instance->msix_combined)
3697 writel(((MSIxIndex & 0x7) << 24) |
3698 fusion->last_reply_idx[MSIxIndex],
3699 instance->reply_post_host_index_addr[MSIxIndex/8]);
3700 else
3701 writel((MSIxIndex << 24) |
3702 fusion->last_reply_idx[MSIxIndex],
3703 instance->reply_post_host_index_addr[0]);
3704 threshold_reply_count = 0;
3705 if (irq_context) {
3706 if (!irq_context->irq_poll_scheduled) {
3707 irq_context->irq_poll_scheduled = true;
3708 irq_context->irq_line_enable = true;
3709 irq_poll_sched(&irq_context->irqpoll);
3710 }
3711 release_irq_context(irq_context);
3712 return num_completed;
3713 }
3714 }
3715 }
3716
3717 if (num_completed) {
3718 wmb();
3719 if (instance->msix_combined)
3720 writel(((MSIxIndex & 0x7) << 24) |
3721 fusion->last_reply_idx[MSIxIndex],
3722 instance->reply_post_host_index_addr[MSIxIndex/8]);
3723 else
3724 writel((MSIxIndex << 24) |
3725 fusion->last_reply_idx[MSIxIndex],
3726 instance->reply_post_host_index_addr[0]);
3727 megasas_check_and_restore_queue_depth(instance);
3728 }
3729
3730 release_irq_context(irq_context);
3731
3732 return num_completed;
3733 }
3734
megasas_blk_mq_poll(struct Scsi_Host * shost,unsigned int queue_num)3735 int megasas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
3736 {
3737
3738 struct megasas_instance *instance;
3739 int num_entries = 0;
3740 struct fusion_context *fusion;
3741
3742 instance = (struct megasas_instance *)shost->hostdata;
3743
3744 fusion = instance->ctrl_context;
3745
3746 queue_num = queue_num + instance->low_latency_index_start;
3747
3748 if (!atomic_add_unless(&fusion->busy_mq_poll[queue_num], 1, 1))
3749 return 0;
3750
3751 num_entries = complete_cmd_fusion(instance, queue_num, NULL);
3752 atomic_dec(&fusion->busy_mq_poll[queue_num]);
3753
3754 return num_entries;
3755 }
3756
3757 /**
3758 * megasas_enable_irq_poll() - enable irqpoll
3759 * @instance: Adapter soft state
3760 */
megasas_enable_irq_poll(struct megasas_instance * instance)3761 static void megasas_enable_irq_poll(struct megasas_instance *instance)
3762 {
3763 u32 count, i;
3764 struct megasas_irq_context *irq_ctx;
3765
3766 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3767
3768 for (i = 0; i < count; i++) {
3769 irq_ctx = &instance->irq_context[i];
3770 irq_poll_enable(&irq_ctx->irqpoll);
3771 }
3772 }
3773
3774 /**
3775 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3776 * @instance_addr: Adapter soft state address
3777 */
megasas_sync_irqs(unsigned long instance_addr)3778 static void megasas_sync_irqs(unsigned long instance_addr)
3779 {
3780 u32 count, i;
3781 struct megasas_instance *instance =
3782 (struct megasas_instance *)instance_addr;
3783 struct megasas_irq_context *irq_ctx;
3784
3785 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3786
3787 for (i = 0; i < count; i++) {
3788 synchronize_irq(pci_irq_vector(instance->pdev, i));
3789 irq_ctx = &instance->irq_context[i];
3790 irq_poll_disable(&irq_ctx->irqpoll);
3791 if (irq_ctx->irq_poll_scheduled) {
3792 irq_ctx->irq_poll_scheduled = false;
3793 enable_irq(irq_ctx->os_irq);
3794 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3795 }
3796 }
3797 }
3798
3799 /**
3800 * megasas_irqpoll() - process a queue for completed reply descriptors
3801 * @irqpoll: IRQ poll structure associated with queue to poll.
3802 * @budget: Threshold of reply descriptors to process per poll.
3803 *
3804 * Return: The number of entries processed.
3805 */
3806
megasas_irqpoll(struct irq_poll * irqpoll,int budget)3807 int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3808 {
3809 struct megasas_irq_context *irq_ctx;
3810 struct megasas_instance *instance;
3811 int num_entries;
3812
3813 irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3814 instance = irq_ctx->instance;
3815
3816 if (irq_ctx->irq_line_enable) {
3817 disable_irq_nosync(irq_ctx->os_irq);
3818 irq_ctx->irq_line_enable = false;
3819 }
3820
3821 num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3822 if (num_entries < budget) {
3823 irq_poll_complete(irqpoll);
3824 irq_ctx->irq_poll_scheduled = false;
3825 enable_irq(irq_ctx->os_irq);
3826 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3827 }
3828
3829 return num_entries;
3830 }
3831
3832 /**
3833 * megasas_complete_cmd_dpc_fusion - Completes command
3834 * @instance_addr: Adapter soft state address
3835 *
3836 * Tasklet to complete cmds
3837 */
3838 static void
megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)3839 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3840 {
3841 struct megasas_instance *instance =
3842 (struct megasas_instance *)instance_addr;
3843 struct megasas_irq_context *irq_ctx = NULL;
3844 u32 count, MSIxIndex;
3845
3846 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3847
3848 /* If we have already declared adapter dead, donot complete cmds */
3849 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3850 return;
3851
3852 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) {
3853 irq_ctx = &instance->irq_context[MSIxIndex];
3854 complete_cmd_fusion(instance, MSIxIndex, irq_ctx);
3855 }
3856 }
3857
3858 /**
3859 * megasas_isr_fusion - isr entry point
3860 * @irq: IRQ number
3861 * @devp: IRQ context
3862 */
megasas_isr_fusion(int irq,void * devp)3863 static irqreturn_t megasas_isr_fusion(int irq, void *devp)
3864 {
3865 struct megasas_irq_context *irq_context = devp;
3866 struct megasas_instance *instance = irq_context->instance;
3867 u32 mfiStatus;
3868
3869 if (instance->mask_interrupts)
3870 return IRQ_NONE;
3871
3872 if (irq_context->irq_poll_scheduled)
3873 return IRQ_HANDLED;
3874
3875 if (!instance->msix_vectors) {
3876 mfiStatus = instance->instancet->clear_intr(instance);
3877 if (!mfiStatus)
3878 return IRQ_NONE;
3879 }
3880
3881 /* If we are resetting, bail */
3882 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3883 instance->instancet->clear_intr(instance);
3884 return IRQ_HANDLED;
3885 }
3886
3887 return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3888 ? IRQ_HANDLED : IRQ_NONE;
3889 }
3890
3891 /**
3892 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3893 * @instance: Adapter soft state
3894 * @mfi_cmd: megasas_cmd pointer
3895 *
3896 */
3897 static void
build_mpt_mfi_pass_thru(struct megasas_instance * instance,struct megasas_cmd * mfi_cmd)3898 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3899 struct megasas_cmd *mfi_cmd)
3900 {
3901 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3902 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3903 struct megasas_cmd_fusion *cmd;
3904 struct fusion_context *fusion;
3905 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3906
3907 fusion = instance->ctrl_context;
3908
3909 cmd = megasas_get_cmd_fusion(instance,
3910 instance->max_scsi_cmds + mfi_cmd->index);
3911
3912 /* Save the smid. To be used for returning the cmd */
3913 mfi_cmd->context.smid = cmd->index;
3914
3915 /*
3916 * For cmds where the flag is set, store the flag and check
3917 * on completion. For cmds with this flag, don't call
3918 * megasas_complete_cmd
3919 */
3920
3921 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3922 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3923
3924 io_req = cmd->io_request;
3925
3926 if (instance->adapter_type >= INVADER_SERIES) {
3927 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3928 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3929 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3930 sgl_ptr_end->Flags = 0;
3931 }
3932
3933 mpi25_ieee_chain =
3934 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3935
3936 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3937 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3938 SGL) / 4;
3939 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3940
3941 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3942
3943 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3944 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3945
3946 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3947 }
3948
3949 /**
3950 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3951 * @instance: Adapter soft state
3952 * @cmd: mfi cmd to build
3953 *
3954 */
3955 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
build_mpt_cmd(struct megasas_instance * instance,struct megasas_cmd * cmd)3956 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3957 {
3958 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3959 u16 index;
3960
3961 build_mpt_mfi_pass_thru(instance, cmd);
3962 index = cmd->context.smid;
3963
3964 req_desc = megasas_get_request_descriptor(instance, index - 1);
3965
3966 req_desc->Words = 0;
3967 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3968 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3969
3970 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3971
3972 return req_desc;
3973 }
3974
3975 /**
3976 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3977 * @instance: Adapter soft state
3978 * @cmd: mfi cmd pointer
3979 *
3980 */
3981 static void
megasas_issue_dcmd_fusion(struct megasas_instance * instance,struct megasas_cmd * cmd)3982 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3983 struct megasas_cmd *cmd)
3984 {
3985 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3986
3987 req_desc = build_mpt_cmd(instance, cmd);
3988
3989 megasas_fire_cmd_fusion(instance, req_desc);
3990 return;
3991 }
3992
3993 /**
3994 * megasas_release_fusion - Reverses the FW initialization
3995 * @instance: Adapter soft state
3996 */
3997 void
megasas_release_fusion(struct megasas_instance * instance)3998 megasas_release_fusion(struct megasas_instance *instance)
3999 {
4000 megasas_free_ioc_init_cmd(instance);
4001 megasas_free_cmds(instance);
4002 megasas_free_cmds_fusion(instance);
4003
4004 iounmap(instance->reg_set);
4005
4006 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
4007 }
4008
4009 /**
4010 * megasas_read_fw_status_reg_fusion - returns the current FW status value
4011 * @instance: Adapter soft state
4012 */
4013 static u32
megasas_read_fw_status_reg_fusion(struct megasas_instance * instance)4014 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
4015 {
4016 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
4017 }
4018
4019 /**
4020 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4021 * @instance: Controller's soft instance
4022 * @return: Number of allocated host crash buffers
4023 */
4024 static void
megasas_alloc_host_crash_buffer(struct megasas_instance * instance)4025 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
4026 {
4027 unsigned int i;
4028
4029 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
4030 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
4031 if (!instance->crash_buf[i]) {
4032 dev_info(&instance->pdev->dev, "Firmware crash dump "
4033 "memory allocation failed at index %d\n", i);
4034 break;
4035 }
4036 }
4037 instance->drv_buf_alloc = i;
4038 }
4039
4040 /**
4041 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4042 * @instance: Controller's soft instance
4043 */
4044 void
megasas_free_host_crash_buffer(struct megasas_instance * instance)4045 megasas_free_host_crash_buffer(struct megasas_instance *instance)
4046 {
4047 unsigned int i;
4048 for (i = 0; i < instance->drv_buf_alloc; i++) {
4049 vfree(instance->crash_buf[i]);
4050 }
4051 instance->drv_buf_index = 0;
4052 instance->drv_buf_alloc = 0;
4053 instance->fw_crash_state = UNAVAILABLE;
4054 instance->fw_crash_buffer_size = 0;
4055 }
4056
4057 /**
4058 * megasas_adp_reset_fusion - For controller reset
4059 * @instance: Controller's soft instance
4060 * @regs: MFI register set
4061 */
4062 static int
megasas_adp_reset_fusion(struct megasas_instance * instance,struct megasas_register_set __iomem * regs)4063 megasas_adp_reset_fusion(struct megasas_instance *instance,
4064 struct megasas_register_set __iomem *regs)
4065 {
4066 u32 host_diag, abs_state, retry;
4067
4068 /* Now try to reset the chip */
4069 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4070 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4071 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4072 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4073 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4074 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4075 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4076
4077 /* Check that the diag write enable (DRWE) bit is on */
4078 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4079 retry = 0;
4080 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
4081 msleep(100);
4082 host_diag = megasas_readl(instance,
4083 &instance->reg_set->fusion_host_diag);
4084 if (retry++ == 100) {
4085 dev_warn(&instance->pdev->dev,
4086 "Host diag unlock failed from %s %d\n",
4087 __func__, __LINE__);
4088 break;
4089 }
4090 }
4091 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
4092 return -1;
4093
4094 /* Send chip reset command */
4095 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
4096 &instance->reg_set->fusion_host_diag);
4097 msleep(3000);
4098
4099 /* Make sure reset adapter bit is cleared */
4100 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4101 retry = 0;
4102 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
4103 msleep(100);
4104 host_diag = megasas_readl(instance,
4105 &instance->reg_set->fusion_host_diag);
4106 if (retry++ == 1000) {
4107 dev_warn(&instance->pdev->dev,
4108 "Diag reset adapter never cleared %s %d\n",
4109 __func__, __LINE__);
4110 break;
4111 }
4112 }
4113 if (host_diag & HOST_DIAG_RESET_ADAPTER)
4114 return -1;
4115
4116 abs_state = instance->instancet->read_fw_status_reg(instance)
4117 & MFI_STATE_MASK;
4118 retry = 0;
4119
4120 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
4121 msleep(100);
4122 abs_state = instance->instancet->
4123 read_fw_status_reg(instance) & MFI_STATE_MASK;
4124 }
4125 if (abs_state <= MFI_STATE_FW_INIT) {
4126 dev_warn(&instance->pdev->dev,
4127 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4128 abs_state, __func__, __LINE__);
4129 return -1;
4130 }
4131
4132 return 0;
4133 }
4134
4135 /**
4136 * megasas_check_reset_fusion - For controller reset check
4137 * @instance: Controller's soft instance
4138 * @regs: MFI register set
4139 */
4140 static int
megasas_check_reset_fusion(struct megasas_instance * instance,struct megasas_register_set __iomem * regs)4141 megasas_check_reset_fusion(struct megasas_instance *instance,
4142 struct megasas_register_set __iomem *regs)
4143 {
4144 return 0;
4145 }
4146
4147 /**
4148 * megasas_trigger_snap_dump - Trigger snap dump in FW
4149 * @instance: Soft instance of adapter
4150 */
megasas_trigger_snap_dump(struct megasas_instance * instance)4151 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4152 {
4153 int j;
4154 u32 fw_state, abs_state;
4155
4156 if (!instance->disableOnlineCtrlReset) {
4157 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4158 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4159 &instance->reg_set->doorbell);
4160 readl(&instance->reg_set->doorbell);
4161 }
4162
4163 for (j = 0; j < instance->snapdump_wait_time; j++) {
4164 abs_state = instance->instancet->read_fw_status_reg(instance);
4165 fw_state = abs_state & MFI_STATE_MASK;
4166 if (fw_state == MFI_STATE_FAULT) {
4167 dev_printk(KERN_ERR, &instance->pdev->dev,
4168 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4169 abs_state & MFI_STATE_FAULT_CODE,
4170 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4171 return;
4172 }
4173 msleep(1000);
4174 }
4175 }
4176
4177 /* This function waits for outstanding commands on fusion to complete */
4178 static int
megasas_wait_for_outstanding_fusion(struct megasas_instance * instance,int reason,int * convert)4179 megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
4180 int reason, int *convert)
4181 {
4182 int i, outstanding, retval = 0, hb_seconds_missed = 0;
4183 u32 fw_state, abs_state;
4184 u32 waittime_for_io_completion;
4185
4186 waittime_for_io_completion =
4187 min_t(u32, resetwaittime,
4188 (resetwaittime - instance->snapdump_wait_time));
4189
4190 if (reason == MFI_IO_TIMEOUT_OCR) {
4191 dev_info(&instance->pdev->dev,
4192 "MFI command is timed out\n");
4193 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4194 if (instance->snapdump_wait_time)
4195 megasas_trigger_snap_dump(instance);
4196 retval = 1;
4197 goto out;
4198 }
4199
4200 for (i = 0; i < waittime_for_io_completion; i++) {
4201 /* Check if firmware is in fault state */
4202 abs_state = instance->instancet->read_fw_status_reg(instance);
4203 fw_state = abs_state & MFI_STATE_MASK;
4204 if (fw_state == MFI_STATE_FAULT) {
4205 dev_printk(KERN_ERR, &instance->pdev->dev,
4206 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4207 abs_state & MFI_STATE_FAULT_CODE,
4208 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4209 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4210 if (instance->requestorId && reason) {
4211 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4212 " state while polling during"
4213 " I/O timeout handling for %d\n",
4214 instance->host->host_no);
4215 *convert = 1;
4216 }
4217
4218 retval = 1;
4219 goto out;
4220 }
4221
4222
4223 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4224 if (instance->requestorId && !reason) {
4225 retval = 1;
4226 goto out;
4227 }
4228
4229 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4230 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
4231 if (instance->hb_host_mem->HB.fwCounter !=
4232 instance->hb_host_mem->HB.driverCounter) {
4233 instance->hb_host_mem->HB.driverCounter =
4234 instance->hb_host_mem->HB.fwCounter;
4235 hb_seconds_missed = 0;
4236 } else {
4237 hb_seconds_missed++;
4238 if (hb_seconds_missed ==
4239 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
4240 dev_warn(&instance->pdev->dev, "SR-IOV:"
4241 " Heartbeat never completed "
4242 " while polling during I/O "
4243 " timeout handling for "
4244 "scsi%d.\n",
4245 instance->host->host_no);
4246 *convert = 1;
4247 retval = 1;
4248 goto out;
4249 }
4250 }
4251 }
4252
4253 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4254 outstanding = atomic_read(&instance->fw_outstanding);
4255 if (!outstanding)
4256 goto out;
4257
4258 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4259 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
4260 "commands to complete for scsi%d\n", i,
4261 outstanding, instance->host->host_no);
4262 }
4263 msleep(1000);
4264 }
4265
4266 if (instance->snapdump_wait_time) {
4267 megasas_trigger_snap_dump(instance);
4268 retval = 1;
4269 goto out;
4270 }
4271
4272 if (atomic_read(&instance->fw_outstanding)) {
4273 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
4274 "will reset adapter scsi%d.\n",
4275 instance->host->host_no);
4276 *convert = 1;
4277 retval = 1;
4278 }
4279
4280 out:
4281 if (!retval && reason == SCSIIO_TIMEOUT_OCR)
4282 dev_info(&instance->pdev->dev, "IO is completed, no OCR is required\n");
4283
4284 return retval;
4285 }
4286
megasas_reset_reply_desc(struct megasas_instance * instance)4287 void megasas_reset_reply_desc(struct megasas_instance *instance)
4288 {
4289 int i, j, count;
4290 struct fusion_context *fusion;
4291 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4292
4293 fusion = instance->ctrl_context;
4294 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4295 count += instance->iopoll_q_count;
4296
4297 for (i = 0 ; i < count ; i++) {
4298 fusion->last_reply_idx[i] = 0;
4299 reply_desc = fusion->reply_frames_desc[i];
4300 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4301 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4302 }
4303 }
4304
4305 /*
4306 * megasas_refire_mgmt_cmd : Re-fire management commands
4307 * @instance: Controller's soft instance
4308 */
megasas_refire_mgmt_cmd(struct megasas_instance * instance,bool return_ioctl)4309 static void megasas_refire_mgmt_cmd(struct megasas_instance *instance,
4310 bool return_ioctl)
4311 {
4312 int j;
4313 struct megasas_cmd_fusion *cmd_fusion;
4314 struct fusion_context *fusion;
4315 struct megasas_cmd *cmd_mfi;
4316 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4317 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
4318 u16 smid;
4319 bool refire_cmd = false;
4320 u8 result;
4321 u32 opcode = 0;
4322
4323 fusion = instance->ctrl_context;
4324
4325 /* Re-fire management commands.
4326 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4327 */
4328 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4329 cmd_fusion = fusion->cmd_list[j];
4330 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4331 smid = le16_to_cpu(cmd_mfi->context.smid);
4332 result = REFIRE_CMD;
4333
4334 if (!smid)
4335 continue;
4336
4337 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4338
4339 switch (cmd_mfi->frame->hdr.cmd) {
4340 case MFI_CMD_DCMD:
4341 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4342 /* Do not refire shutdown command */
4343 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4344 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4345 result = COMPLETE_CMD;
4346 break;
4347 }
4348
4349 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4350 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4351 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4352
4353 if (!refire_cmd)
4354 result = RETURN_CMD;
4355
4356 break;
4357 case MFI_CMD_NVME:
4358 if (!instance->support_nvme_passthru) {
4359 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4360 result = COMPLETE_CMD;
4361 }
4362
4363 break;
4364 case MFI_CMD_TOOLBOX:
4365 if (!instance->support_pci_lane_margining) {
4366 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4367 result = COMPLETE_CMD;
4368 }
4369
4370 break;
4371 default:
4372 break;
4373 }
4374
4375 if (return_ioctl && cmd_mfi->sync_cmd &&
4376 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT) {
4377 dev_err(&instance->pdev->dev,
4378 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x\n",
4379 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4380 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4381 cmd_mfi->cmd_status_drv = DCMD_BUSY;
4382 result = COMPLETE_CMD;
4383 }
4384
4385 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
4386 cmd_fusion->io_request;
4387 if (scsi_io_req->Function == MPI2_FUNCTION_SCSI_TASK_MGMT)
4388 result = RETURN_CMD;
4389
4390 switch (result) {
4391 case REFIRE_CMD:
4392 megasas_fire_cmd_fusion(instance, req_desc);
4393 break;
4394 case RETURN_CMD:
4395 megasas_return_cmd(instance, cmd_mfi);
4396 break;
4397 case COMPLETE_CMD:
4398 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4399 break;
4400 }
4401 }
4402 }
4403
4404 /*
4405 * megasas_return_polled_cmds: Return polled mode commands back to the pool
4406 * before initiating an OCR.
4407 * @instance: Controller's soft instance
4408 */
4409 static void
megasas_return_polled_cmds(struct megasas_instance * instance)4410 megasas_return_polled_cmds(struct megasas_instance *instance)
4411 {
4412 int i;
4413 struct megasas_cmd_fusion *cmd_fusion;
4414 struct fusion_context *fusion;
4415 struct megasas_cmd *cmd_mfi;
4416
4417 fusion = instance->ctrl_context;
4418
4419 for (i = instance->max_scsi_cmds; i < instance->max_fw_cmds; i++) {
4420 cmd_fusion = fusion->cmd_list[i];
4421 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4422
4423 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
4424 if (megasas_dbg_lvl & OCR_DEBUG)
4425 dev_info(&instance->pdev->dev,
4426 "%s %d return cmd 0x%x opcode 0x%x\n",
4427 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4428 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4429 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
4430 megasas_return_cmd(instance, cmd_mfi);
4431 }
4432 }
4433 }
4434
4435 /*
4436 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4437 * @instance: per adapter struct
4438 * @channel: the channel assigned by the OS
4439 * @id: the id assigned by the OS
4440 *
4441 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4442 */
4443
megasas_track_scsiio(struct megasas_instance * instance,int id,int channel)4444 static int megasas_track_scsiio(struct megasas_instance *instance,
4445 int id, int channel)
4446 {
4447 int i, found = 0;
4448 struct megasas_cmd_fusion *cmd_fusion;
4449 struct fusion_context *fusion;
4450 fusion = instance->ctrl_context;
4451
4452 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4453 cmd_fusion = fusion->cmd_list[i];
4454 if (cmd_fusion->scmd &&
4455 (cmd_fusion->scmd->device->id == id &&
4456 cmd_fusion->scmd->device->channel == channel)) {
4457 dev_info(&instance->pdev->dev,
4458 "SCSI commands pending to target"
4459 "channel %d id %d \tSMID: 0x%x\n",
4460 channel, id, cmd_fusion->index);
4461 scsi_print_command(cmd_fusion->scmd);
4462 found = 1;
4463 break;
4464 }
4465 }
4466
4467 return found ? FAILED : SUCCESS;
4468 }
4469
4470 /**
4471 * megasas_tm_response_code - translation of device response code
4472 * @instance: Controller's soft instance
4473 * @mpi_reply: MPI reply returned by firmware
4474 *
4475 * Return nothing.
4476 */
4477 static void
megasas_tm_response_code(struct megasas_instance * instance,struct MPI2_SCSI_TASK_MANAGE_REPLY * mpi_reply)4478 megasas_tm_response_code(struct megasas_instance *instance,
4479 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4480 {
4481 char *desc;
4482
4483 switch (mpi_reply->ResponseCode) {
4484 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4485 desc = "task management request completed";
4486 break;
4487 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4488 desc = "invalid frame";
4489 break;
4490 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4491 desc = "task management request not supported";
4492 break;
4493 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4494 desc = "task management request failed";
4495 break;
4496 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4497 desc = "task management request succeeded";
4498 break;
4499 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4500 desc = "invalid lun";
4501 break;
4502 case 0xA:
4503 desc = "overlapped tag attempted";
4504 break;
4505 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4506 desc = "task queued, however not sent to target";
4507 break;
4508 default:
4509 desc = "unknown";
4510 break;
4511 }
4512 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4513 mpi_reply->ResponseCode, desc);
4514 dev_dbg(&instance->pdev->dev,
4515 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4516 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4517 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4518 mpi_reply->Function, mpi_reply->TaskType,
4519 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4520 }
4521
4522 /**
4523 * megasas_issue_tm - main routine for sending tm requests
4524 * @instance: per adapter struct
4525 * @device_handle: device handle
4526 * @channel: the channel assigned by the OS
4527 * @id: the id assigned by the OS
4528 * @smid_task: smid assigned to the task
4529 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4530 * @mr_device_priv_data: private data
4531 * Context: user
4532 *
4533 * MegaRaid use MPT interface for Task Magement request.
4534 * A generic API for sending task management requests to firmware.
4535 *
4536 * Return SUCCESS or FAILED.
4537 */
4538 static int
megasas_issue_tm(struct megasas_instance * instance,u16 device_handle,uint channel,uint id,u16 smid_task,u8 type,struct MR_PRIV_DEVICE * mr_device_priv_data)4539 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4540 uint channel, uint id, u16 smid_task, u8 type,
4541 struct MR_PRIV_DEVICE *mr_device_priv_data)
4542 {
4543 struct MR_TASK_MANAGE_REQUEST *mr_request;
4544 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4545 unsigned long timeleft;
4546 struct megasas_cmd_fusion *cmd_fusion;
4547 struct megasas_cmd *cmd_mfi;
4548 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4549 struct fusion_context *fusion = NULL;
4550 struct megasas_cmd_fusion *scsi_lookup;
4551 int rc;
4552 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4553 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4554
4555 fusion = instance->ctrl_context;
4556
4557 cmd_mfi = megasas_get_cmd(instance);
4558
4559 if (!cmd_mfi) {
4560 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4561 __func__, __LINE__);
4562 return -ENOMEM;
4563 }
4564
4565 cmd_fusion = megasas_get_cmd_fusion(instance,
4566 instance->max_scsi_cmds + cmd_mfi->index);
4567
4568 /* Save the smid. To be used for returning the cmd */
4569 cmd_mfi->context.smid = cmd_fusion->index;
4570
4571 req_desc = megasas_get_request_descriptor(instance,
4572 (cmd_fusion->index - 1));
4573
4574 cmd_fusion->request_desc = req_desc;
4575 req_desc->Words = 0;
4576
4577 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4578 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4579 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4580 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4581 mpi_request->DevHandle = cpu_to_le16(device_handle);
4582 mpi_request->TaskType = type;
4583 mpi_request->TaskMID = cpu_to_le16(smid_task);
4584 mpi_request->LUN[1] = 0;
4585
4586
4587 req_desc = cmd_fusion->request_desc;
4588 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4589 req_desc->HighPriority.RequestFlags =
4590 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4591 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4592 req_desc->HighPriority.MSIxIndex = 0;
4593 req_desc->HighPriority.LMID = 0;
4594 req_desc->HighPriority.Reserved1 = 0;
4595
4596 if (channel < MEGASAS_MAX_PD_CHANNELS)
4597 mr_request->tmReqFlags.isTMForPD = 1;
4598 else
4599 mr_request->tmReqFlags.isTMForLD = 1;
4600
4601 init_completion(&cmd_fusion->done);
4602 megasas_fire_cmd_fusion(instance, req_desc);
4603
4604 switch (type) {
4605 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4606 timeout = mr_device_priv_data->task_abort_tmo;
4607 break;
4608 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4609 timeout = mr_device_priv_data->target_reset_tmo;
4610 break;
4611 }
4612
4613 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4614
4615 if (!timeleft) {
4616 dev_err(&instance->pdev->dev,
4617 "task mgmt type 0x%x timed out\n", type);
4618 mutex_unlock(&instance->reset_mutex);
4619 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4620 mutex_lock(&instance->reset_mutex);
4621 return rc;
4622 }
4623
4624 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4625 megasas_tm_response_code(instance, mpi_reply);
4626
4627 megasas_return_cmd(instance, cmd_mfi);
4628 rc = SUCCESS;
4629 switch (type) {
4630 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4631 scsi_lookup = fusion->cmd_list[smid_task - 1];
4632
4633 if (scsi_lookup->scmd == NULL)
4634 break;
4635 else {
4636 instance->instancet->disable_intr(instance);
4637 megasas_sync_irqs((unsigned long)instance);
4638 instance->instancet->enable_intr(instance);
4639 megasas_enable_irq_poll(instance);
4640 if (scsi_lookup->scmd == NULL)
4641 break;
4642 }
4643 rc = FAILED;
4644 break;
4645
4646 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4647 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4648 break;
4649 instance->instancet->disable_intr(instance);
4650 megasas_sync_irqs((unsigned long)instance);
4651 rc = megasas_track_scsiio(instance, id, channel);
4652 instance->instancet->enable_intr(instance);
4653 megasas_enable_irq_poll(instance);
4654
4655 break;
4656 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4657 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4658 break;
4659 default:
4660 rc = FAILED;
4661 break;
4662 }
4663
4664 return rc;
4665
4666 }
4667
4668 /*
4669 * megasas_fusion_smid_lookup : Look for fusion command corresponding to SCSI
4670 * @instance: per adapter struct
4671 *
4672 * Return Non Zero index, if SMID found in outstanding commands
4673 */
megasas_fusion_smid_lookup(struct scsi_cmnd * scmd)4674 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4675 {
4676 int i, ret = 0;
4677 struct megasas_instance *instance;
4678 struct megasas_cmd_fusion *cmd_fusion;
4679 struct fusion_context *fusion;
4680
4681 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4682
4683 fusion = instance->ctrl_context;
4684
4685 for (i = 0; i < instance->max_scsi_cmds; i++) {
4686 cmd_fusion = fusion->cmd_list[i];
4687 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4688 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4689 " SMID: %d\n", cmd_fusion->index);
4690 ret = cmd_fusion->index;
4691 break;
4692 }
4693 }
4694
4695 return ret;
4696 }
4697
4698 /*
4699 * megasas_get_tm_devhandle - Get devhandle for TM request
4700 * @sdev- OS provided scsi device
4701 *
4702 * Returns- devhandle/targetID of SCSI device
4703 */
megasas_get_tm_devhandle(struct scsi_device * sdev)4704 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4705 {
4706 u16 pd_index = 0;
4707 u32 device_id;
4708 struct megasas_instance *instance;
4709 struct fusion_context *fusion;
4710 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4711 u16 devhandle = (u16)ULONG_MAX;
4712
4713 instance = (struct megasas_instance *)sdev->host->hostdata;
4714 fusion = instance->ctrl_context;
4715
4716 if (!MEGASAS_IS_LOGICAL(sdev)) {
4717 if (instance->use_seqnum_jbod_fp) {
4718 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4719 + sdev->id;
4720 pd_sync = (void *)fusion->pd_seq_sync
4721 [(instance->pd_seq_map_id - 1) & 1];
4722 devhandle = pd_sync->seq[pd_index].devHandle;
4723 } else
4724 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4725 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4726 } else {
4727 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4728 + sdev->id;
4729 devhandle = device_id;
4730 }
4731
4732 return devhandle;
4733 }
4734
4735 /*
4736 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4737 * @scmd : pointer to scsi command object
4738 *
4739 * Return SUCCESS, if command aborted else FAILED
4740 */
4741
megasas_task_abort_fusion(struct scsi_cmnd * scmd)4742 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4743 {
4744 struct megasas_instance *instance;
4745 u16 smid, devhandle;
4746 int ret;
4747 struct MR_PRIV_DEVICE *mr_device_priv_data;
4748 mr_device_priv_data = scmd->device->hostdata;
4749
4750 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4751
4752 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4753 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4754 "SCSI host:%d\n", instance->host->host_no);
4755 ret = FAILED;
4756 return ret;
4757 }
4758
4759 if (!mr_device_priv_data) {
4760 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4761 "scmd(%p)\n", scmd);
4762 scmd->result = DID_NO_CONNECT << 16;
4763 ret = SUCCESS;
4764 goto out;
4765 }
4766
4767 if (!mr_device_priv_data->is_tm_capable) {
4768 ret = FAILED;
4769 goto out;
4770 }
4771
4772 mutex_lock(&instance->reset_mutex);
4773
4774 smid = megasas_fusion_smid_lookup(scmd);
4775
4776 if (!smid) {
4777 ret = SUCCESS;
4778 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4779 " issued is not found in outstanding commands\n");
4780 mutex_unlock(&instance->reset_mutex);
4781 goto out;
4782 }
4783
4784 devhandle = megasas_get_tm_devhandle(scmd->device);
4785
4786 if (devhandle == (u16)ULONG_MAX) {
4787 ret = FAILED;
4788 sdev_printk(KERN_INFO, scmd->device,
4789 "task abort issued for invalid devhandle\n");
4790 mutex_unlock(&instance->reset_mutex);
4791 goto out;
4792 }
4793 sdev_printk(KERN_INFO, scmd->device,
4794 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4795 scmd, devhandle);
4796
4797 mr_device_priv_data->tm_busy = true;
4798 ret = megasas_issue_tm(instance, devhandle,
4799 scmd->device->channel, scmd->device->id, smid,
4800 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4801 mr_device_priv_data);
4802 mr_device_priv_data->tm_busy = false;
4803
4804 mutex_unlock(&instance->reset_mutex);
4805 scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
4806 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4807 out:
4808 scsi_print_command(scmd);
4809 if (megasas_dbg_lvl & TM_DEBUG)
4810 megasas_dump_fusion_io(scmd);
4811
4812 return ret;
4813 }
4814
4815 /*
4816 * megasas_reset_target_fusion : target reset function for fusion adapters
4817 * scmd: SCSI command pointer
4818 *
4819 * Returns SUCCESS if all commands associated with target aborted else FAILED
4820 */
4821
megasas_reset_target_fusion(struct scsi_cmnd * scmd)4822 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4823 {
4824
4825 struct megasas_instance *instance;
4826 int ret = FAILED;
4827 u16 devhandle;
4828 struct MR_PRIV_DEVICE *mr_device_priv_data;
4829 mr_device_priv_data = scmd->device->hostdata;
4830
4831 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4832
4833 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4834 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4835 "SCSI host:%d\n", instance->host->host_no);
4836 ret = FAILED;
4837 return ret;
4838 }
4839
4840 if (!mr_device_priv_data) {
4841 sdev_printk(KERN_INFO, scmd->device,
4842 "device been deleted! scmd: (0x%p)\n", scmd);
4843 scmd->result = DID_NO_CONNECT << 16;
4844 ret = SUCCESS;
4845 goto out;
4846 }
4847
4848 if (!mr_device_priv_data->is_tm_capable) {
4849 ret = FAILED;
4850 goto out;
4851 }
4852
4853 mutex_lock(&instance->reset_mutex);
4854 devhandle = megasas_get_tm_devhandle(scmd->device);
4855
4856 if (devhandle == (u16)ULONG_MAX) {
4857 ret = FAILED;
4858 sdev_printk(KERN_INFO, scmd->device,
4859 "target reset issued for invalid devhandle\n");
4860 mutex_unlock(&instance->reset_mutex);
4861 goto out;
4862 }
4863
4864 sdev_printk(KERN_INFO, scmd->device,
4865 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4866 scmd, devhandle);
4867 mr_device_priv_data->tm_busy = true;
4868 ret = megasas_issue_tm(instance, devhandle,
4869 scmd->device->channel, scmd->device->id, 0,
4870 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4871 mr_device_priv_data);
4872 mr_device_priv_data->tm_busy = false;
4873 mutex_unlock(&instance->reset_mutex);
4874 scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
4875 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4876
4877 out:
4878 return ret;
4879 }
4880
4881 /*SRIOV get other instance in cluster if any*/
4882 static struct
megasas_get_peer_instance(struct megasas_instance * instance)4883 megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4884 {
4885 int i;
4886
4887 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4888 if (megasas_mgmt_info.instance[i] &&
4889 (megasas_mgmt_info.instance[i] != instance) &&
4890 megasas_mgmt_info.instance[i]->requestorId &&
4891 megasas_mgmt_info.instance[i]->peerIsPresent &&
4892 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4893 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4894 return megasas_mgmt_info.instance[i];
4895 }
4896 return NULL;
4897 }
4898
4899 /* Check for a second path that is currently UP */
megasas_check_mpio_paths(struct megasas_instance * instance,struct scsi_cmnd * scmd)4900 int megasas_check_mpio_paths(struct megasas_instance *instance,
4901 struct scsi_cmnd *scmd)
4902 {
4903 struct megasas_instance *peer_instance = NULL;
4904 int retval = (DID_REQUEUE << 16);
4905
4906 if (instance->peerIsPresent) {
4907 peer_instance = megasas_get_peer_instance(instance);
4908 if ((peer_instance) &&
4909 (atomic_read(&peer_instance->adprecovery) ==
4910 MEGASAS_HBA_OPERATIONAL))
4911 retval = (DID_NO_CONNECT << 16);
4912 }
4913 return retval;
4914 }
4915
4916 /* Core fusion reset function */
megasas_reset_fusion(struct Scsi_Host * shost,int reason)4917 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4918 {
4919 int retval = SUCCESS, i, j, convert = 0;
4920 struct megasas_instance *instance;
4921 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4922 struct fusion_context *fusion;
4923 u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
4924 u32 io_timeout_in_crash_mode = 0;
4925 struct scsi_cmnd *scmd_local = NULL;
4926 struct scsi_device *sdev;
4927 int ret_target_prop = DCMD_FAILED;
4928 bool is_target_prop = false;
4929 bool do_adp_reset = true;
4930 int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
4931
4932 instance = (struct megasas_instance *)shost->hostdata;
4933 fusion = instance->ctrl_context;
4934
4935 mutex_lock(&instance->reset_mutex);
4936
4937 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4938 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4939 "returning FAILED for scsi%d.\n",
4940 instance->host->host_no);
4941 mutex_unlock(&instance->reset_mutex);
4942 return FAILED;
4943 }
4944 status_reg = instance->instancet->read_fw_status_reg(instance);
4945 abs_state = status_reg & MFI_STATE_MASK;
4946
4947 /* IO timeout detected, forcibly put FW in FAULT state */
4948 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4949 instance->crash_dump_app_support && reason) {
4950 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4951 "forcibly FAULT Firmware\n");
4952 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4953 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4954 writel(status_reg | MFI_STATE_FORCE_OCR,
4955 &instance->reg_set->doorbell);
4956 readl(&instance->reg_set->doorbell);
4957 mutex_unlock(&instance->reset_mutex);
4958 do {
4959 ssleep(3);
4960 io_timeout_in_crash_mode++;
4961 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4962 "seconds for crash dump collection and OCR "
4963 "to be done\n", (io_timeout_in_crash_mode * 3));
4964 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4965 (io_timeout_in_crash_mode < 80));
4966
4967 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4968 dev_info(&instance->pdev->dev, "OCR done for IO "
4969 "timeout case\n");
4970 retval = SUCCESS;
4971 } else {
4972 dev_info(&instance->pdev->dev, "Controller is not "
4973 "operational after 240 seconds wait for IO "
4974 "timeout case in FW crash dump mode\n do "
4975 "OCR/kill adapter\n");
4976 retval = megasas_reset_fusion(shost, 0);
4977 }
4978 return retval;
4979 }
4980
4981 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4982 timer_delete_sync(&instance->sriov_heartbeat_timer);
4983 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4984 set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4985 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4986 instance->instancet->disable_intr(instance);
4987 megasas_sync_irqs((unsigned long)instance);
4988
4989 /* First try waiting for commands to complete */
4990 if (megasas_wait_for_outstanding_fusion(instance, reason,
4991 &convert)) {
4992 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4993 dev_warn(&instance->pdev->dev, "resetting fusion "
4994 "adapter scsi%d.\n", instance->host->host_no);
4995 if (convert)
4996 reason = 0;
4997
4998 if (megasas_dbg_lvl & OCR_DEBUG)
4999 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
5000
5001 /* Now return commands back to the OS */
5002 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
5003 cmd_fusion = fusion->cmd_list[i];
5004 /*check for extra commands issued by driver*/
5005 if (instance->adapter_type >= VENTURA_SERIES) {
5006 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
5007 megasas_return_cmd_fusion(instance, r1_cmd);
5008 }
5009 scmd_local = cmd_fusion->scmd;
5010 if (cmd_fusion->scmd) {
5011 if (megasas_dbg_lvl & OCR_DEBUG) {
5012 sdev_printk(KERN_INFO,
5013 cmd_fusion->scmd->device, "SMID: 0x%x\n",
5014 cmd_fusion->index);
5015 megasas_dump_fusion_io(cmd_fusion->scmd);
5016 }
5017
5018 if (cmd_fusion->io_request->Function ==
5019 MPI2_FUNCTION_SCSI_IO_REQUEST)
5020 fpio_count++;
5021
5022 scmd_local->result =
5023 megasas_check_mpio_paths(instance,
5024 scmd_local);
5025 if (instance->ldio_threshold &&
5026 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
5027 atomic_dec(&instance->ldio_outstanding);
5028 megasas_return_cmd_fusion(instance, cmd_fusion);
5029 scsi_dma_unmap(scmd_local);
5030 scsi_done(scmd_local);
5031 }
5032 }
5033
5034 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
5035 fpio_count);
5036
5037 atomic_set(&instance->fw_outstanding, 0);
5038
5039 status_reg = instance->instancet->read_fw_status_reg(instance);
5040 abs_state = status_reg & MFI_STATE_MASK;
5041 reset_adapter = status_reg & MFI_RESET_ADAPTER;
5042 if (instance->disableOnlineCtrlReset ||
5043 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
5044 /* Reset not supported, kill adapter */
5045 dev_warn(&instance->pdev->dev, "Reset not supported"
5046 ", killing adapter scsi%d.\n",
5047 instance->host->host_no);
5048 goto kill_hba;
5049 }
5050
5051 /* Let SR-IOV VF & PF sync up if there was a HB failure */
5052 if (instance->requestorId && !reason) {
5053 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
5054 do_adp_reset = false;
5055 max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
5056 }
5057
5058 /* Now try to reset the chip */
5059 for (i = 0; i < max_reset_tries; i++) {
5060 /*
5061 * Do adp reset and wait for
5062 * controller to transition to ready
5063 */
5064 if (megasas_adp_reset_wait_for_ready(instance,
5065 do_adp_reset, 1) == FAILED)
5066 continue;
5067
5068 /* Wait for FW to become ready */
5069 if (megasas_transition_to_ready(instance, 1)) {
5070 dev_warn(&instance->pdev->dev,
5071 "Failed to transition controller to ready for "
5072 "scsi%d.\n", instance->host->host_no);
5073 continue;
5074 }
5075 megasas_reset_reply_desc(instance);
5076 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
5077
5078 if (megasas_ioc_init_fusion(instance)) {
5079 continue;
5080 }
5081
5082 if (megasas_get_ctrl_info(instance)) {
5083 dev_info(&instance->pdev->dev,
5084 "Failed from %s %d\n",
5085 __func__, __LINE__);
5086 goto kill_hba;
5087 }
5088
5089 megasas_refire_mgmt_cmd(instance,
5090 (i == (MEGASAS_FUSION_MAX_RESET_TRIES - 1)
5091 ? 1 : 0));
5092
5093 /* Reset load balance info */
5094 if (fusion->load_balance_info)
5095 memset(fusion->load_balance_info, 0,
5096 (sizeof(struct LD_LOAD_BALANCE_INFO) *
5097 MAX_LOGICAL_DRIVES_EXT));
5098
5099 if (!megasas_get_map_info(instance)) {
5100 megasas_sync_map_info(instance);
5101 } else {
5102 /*
5103 * Return pending polled mode cmds before
5104 * retrying OCR
5105 */
5106 megasas_return_polled_cmds(instance);
5107 continue;
5108 }
5109
5110 megasas_setup_jbod_map(instance);
5111
5112 /* reset stream detection array */
5113 if (instance->adapter_type >= VENTURA_SERIES) {
5114 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
5115 memset(fusion->stream_detect_by_ld[j],
5116 0, sizeof(struct LD_STREAM_DETECT));
5117 fusion->stream_detect_by_ld[j]->mru_bit_map
5118 = MR_STREAM_BITMAP;
5119 }
5120 }
5121
5122 clear_bit(MEGASAS_FUSION_IN_RESET,
5123 &instance->reset_flags);
5124 instance->instancet->enable_intr(instance);
5125 megasas_enable_irq_poll(instance);
5126 shost_for_each_device(sdev, shost) {
5127 if ((instance->tgt_prop) &&
5128 (instance->nvme_page_size))
5129 ret_target_prop = megasas_get_target_prop(instance, sdev);
5130
5131 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
5132 megasas_set_dynamic_target_properties(sdev, NULL,
5133 is_target_prop);
5134 }
5135
5136 status_reg = instance->instancet->read_fw_status_reg
5137 (instance);
5138 abs_state = status_reg & MFI_STATE_MASK;
5139 if (abs_state != MFI_STATE_OPERATIONAL) {
5140 dev_info(&instance->pdev->dev,
5141 "Adapter is not OPERATIONAL, state 0x%x for scsi:%d\n",
5142 abs_state, instance->host->host_no);
5143 goto out;
5144 }
5145 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5146
5147 dev_info(&instance->pdev->dev,
5148 "Adapter is OPERATIONAL for scsi:%d\n",
5149 instance->host->host_no);
5150
5151 /* Restart SR-IOV heartbeat */
5152 if (instance->requestorId) {
5153 if (!megasas_sriov_start_heartbeat(instance, 0))
5154 megasas_start_timer(instance);
5155 else
5156 instance->skip_heartbeat_timer_del = 1;
5157 }
5158
5159 if (instance->crash_dump_drv_support &&
5160 instance->crash_dump_app_support)
5161 megasas_set_crash_dump_params(instance,
5162 MR_CRASH_BUF_TURN_ON);
5163 else
5164 megasas_set_crash_dump_params(instance,
5165 MR_CRASH_BUF_TURN_OFF);
5166
5167 if (instance->snapdump_wait_time) {
5168 megasas_get_snapdump_properties(instance);
5169 dev_info(&instance->pdev->dev,
5170 "Snap dump wait time\t: %d\n",
5171 instance->snapdump_wait_time);
5172 }
5173
5174 retval = SUCCESS;
5175
5176 /* Adapter reset completed successfully */
5177 dev_warn(&instance->pdev->dev,
5178 "Reset successful for scsi%d.\n",
5179 instance->host->host_no);
5180
5181 goto out;
5182 }
5183 /* Reset failed, kill the adapter */
5184 dev_warn(&instance->pdev->dev, "Reset failed, killing "
5185 "adapter scsi%d.\n", instance->host->host_no);
5186 goto kill_hba;
5187 } else {
5188 /* For VF: Restart HB timer if we didn't OCR */
5189 if (instance->requestorId) {
5190 megasas_start_timer(instance);
5191 }
5192 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5193 instance->instancet->enable_intr(instance);
5194 megasas_enable_irq_poll(instance);
5195 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5196 goto out;
5197 }
5198 kill_hba:
5199 megaraid_sas_kill_hba(instance);
5200 megasas_enable_irq_poll(instance);
5201 instance->skip_heartbeat_timer_del = 1;
5202 retval = FAILED;
5203 out:
5204 clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
5205 mutex_unlock(&instance->reset_mutex);
5206 return retval;
5207 }
5208
5209 /* Fusion Crash dump collection */
megasas_fusion_crash_dump(struct megasas_instance * instance)5210 static void megasas_fusion_crash_dump(struct megasas_instance *instance)
5211 {
5212 u32 status_reg;
5213 u8 partial_copy = 0;
5214 int wait = 0;
5215
5216
5217 status_reg = instance->instancet->read_fw_status_reg(instance);
5218
5219 /*
5220 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5221 * to host crash buffers
5222 */
5223 if (instance->drv_buf_index == 0) {
5224 /* Buffer is already allocated for old Crash dump.
5225 * Do OCR and do not wait for crash dump collection
5226 */
5227 if (instance->drv_buf_alloc) {
5228 dev_info(&instance->pdev->dev, "earlier crash dump is "
5229 "not yet copied by application, ignoring this "
5230 "crash dump and initiating OCR\n");
5231 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5232 writel(status_reg,
5233 &instance->reg_set->outbound_scratch_pad_0);
5234 readl(&instance->reg_set->outbound_scratch_pad_0);
5235 return;
5236 }
5237 megasas_alloc_host_crash_buffer(instance);
5238 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5239 "allocated: %d\n", instance->drv_buf_alloc);
5240 }
5241
5242 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5243 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5244 if (!(status_reg & MFI_STATE_DMADONE)) {
5245 /*
5246 * Next crash dump buffer is not yet DMA'd by FW
5247 * Check after 10ms. Wait for 1 second for FW to
5248 * post the next buffer. If not bail out.
5249 */
5250 wait++;
5251 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5252 status_reg = instance->instancet->read_fw_status_reg(
5253 instance);
5254 continue;
5255 }
5256
5257 wait = 0;
5258 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5259 dev_info(&instance->pdev->dev,
5260 "Driver is done copying the buffer: %d\n",
5261 instance->drv_buf_alloc);
5262 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5263 partial_copy = 1;
5264 break;
5265 } else {
5266 memcpy(instance->crash_buf[instance->drv_buf_index],
5267 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5268 instance->drv_buf_index++;
5269 status_reg &= ~MFI_STATE_DMADONE;
5270 }
5271
5272 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5273 readl(&instance->reg_set->outbound_scratch_pad_0);
5274
5275 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5276 status_reg = instance->instancet->read_fw_status_reg(instance);
5277 }
5278
5279 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5280 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5281 "of copied buffers: %d\n", instance->drv_buf_index);
5282 instance->fw_crash_buffer_size = instance->drv_buf_index;
5283 instance->fw_crash_state = AVAILABLE;
5284 instance->drv_buf_index = 0;
5285 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5286 readl(&instance->reg_set->outbound_scratch_pad_0);
5287 if (!partial_copy)
5288 megasas_reset_fusion(instance->host, 0);
5289 }
5290 }
5291
5292
5293 /* Fusion OCR work queue */
megasas_fusion_ocr_wq(struct work_struct * work)5294 void megasas_fusion_ocr_wq(struct work_struct *work)
5295 {
5296 struct megasas_instance *instance =
5297 container_of(work, struct megasas_instance, work_init);
5298
5299 megasas_reset_fusion(instance->host, 0);
5300 }
5301
5302 /* Allocate fusion context */
5303 int
megasas_alloc_fusion_context(struct megasas_instance * instance)5304 megasas_alloc_fusion_context(struct megasas_instance *instance)
5305 {
5306 struct fusion_context *fusion;
5307
5308 instance->ctrl_context = kzalloc_obj(struct fusion_context);
5309 if (!instance->ctrl_context) {
5310 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5311 __func__, __LINE__);
5312 return -ENOMEM;
5313 }
5314
5315 fusion = instance->ctrl_context;
5316
5317 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5318 sizeof(LD_SPAN_INFO));
5319 fusion->log_to_span =
5320 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5321 fusion->log_to_span_pages);
5322 if (!fusion->log_to_span) {
5323 fusion->log_to_span =
5324 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5325 sizeof(LD_SPAN_INFO)));
5326 if (!fusion->log_to_span) {
5327 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5328 __func__, __LINE__);
5329 return -ENOMEM;
5330 }
5331 }
5332
5333 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5334 sizeof(struct LD_LOAD_BALANCE_INFO));
5335 fusion->load_balance_info =
5336 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5337 fusion->load_balance_info_pages);
5338 if (!fusion->load_balance_info) {
5339 fusion->load_balance_info =
5340 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5341 sizeof(struct LD_LOAD_BALANCE_INFO)));
5342 if (!fusion->load_balance_info)
5343 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5344 "continuing without Load Balance support\n");
5345 }
5346
5347 return 0;
5348 }
5349
5350 void
megasas_free_fusion_context(struct megasas_instance * instance)5351 megasas_free_fusion_context(struct megasas_instance *instance)
5352 {
5353 struct fusion_context *fusion = instance->ctrl_context;
5354
5355 if (fusion) {
5356 if (fusion->load_balance_info) {
5357 if (is_vmalloc_addr(fusion->load_balance_info))
5358 vfree(fusion->load_balance_info);
5359 else
5360 free_pages((ulong)fusion->load_balance_info,
5361 fusion->load_balance_info_pages);
5362 }
5363
5364 if (fusion->log_to_span) {
5365 if (is_vmalloc_addr(fusion->log_to_span))
5366 vfree(fusion->log_to_span);
5367 else
5368 free_pages((ulong)fusion->log_to_span,
5369 fusion->log_to_span_pages);
5370 }
5371
5372 kfree(fusion);
5373 }
5374 }
5375
5376 struct megasas_instance_template megasas_instance_template_fusion = {
5377 .enable_intr = megasas_enable_intr_fusion,
5378 .disable_intr = megasas_disable_intr_fusion,
5379 .clear_intr = megasas_clear_intr_fusion,
5380 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5381 .adp_reset = megasas_adp_reset_fusion,
5382 .check_reset = megasas_check_reset_fusion,
5383 .service_isr = megasas_isr_fusion,
5384 .tasklet = megasas_complete_cmd_dpc_fusion,
5385 .init_adapter = megasas_init_adapter_fusion,
5386 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5387 .issue_dcmd = megasas_issue_dcmd_fusion,
5388 };
5389