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 /* Update load balancing info */
3616 if (fusion->load_balance_info &&
3617 (megasas_priv(cmd_fusion->scmd)->status &
3618 MEGASAS_LOAD_BALANCE_FLAG)) {
3619 device_id = MEGASAS_DEV_INDEX(scmd_local);
3620 lbinfo = &fusion->load_balance_info[device_id];
3621 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3622 megasas_priv(cmd_fusion->scmd)->status &=
3623 ~MEGASAS_LOAD_BALANCE_FLAG;
3624 }
3625 fallthrough; /* and complete IO */
3626 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3627 atomic_dec(&instance->fw_outstanding);
3628 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3629 map_cmd_status(fusion, scmd_local, status,
3630 extStatus, le32_to_cpu(data_length),
3631 sense);
3632 if (instance->ldio_threshold &&
3633 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3634 atomic_dec(&instance->ldio_outstanding);
3635 megasas_priv(scmd_local)->cmd_priv = NULL;
3636 megasas_return_cmd_fusion(instance, cmd_fusion);
3637 scsi_dma_unmap(scmd_local);
3638 megasas_sdev_busy_dec(instance, scmd_local);
3639 scsi_done(scmd_local);
3640 } else /* Optimal VD - R1 FP command completion. */
3641 megasas_complete_r1_command(instance, cmd_fusion);
3642 break;
3643 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3644 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3645 /* Poll mode. Dummy free.
3646 * In case of Interrupt mode, caller has reverse check.
3647 */
3648 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3649 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3650 megasas_return_cmd(instance, cmd_mfi);
3651 } else
3652 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3653 break;
3654 }
3655
3656 fusion->last_reply_idx[MSIxIndex]++;
3657 if (fusion->last_reply_idx[MSIxIndex] >=
3658 fusion->reply_q_depth)
3659 fusion->last_reply_idx[MSIxIndex] = 0;
3660
3661 desc->Words = cpu_to_le64(ULLONG_MAX);
3662 num_completed++;
3663 threshold_reply_count++;
3664
3665 /* Get the next reply descriptor */
3666 if (!fusion->last_reply_idx[MSIxIndex])
3667 desc = fusion->reply_frames_desc[MSIxIndex];
3668 else
3669 desc++;
3670
3671 reply_desc =
3672 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3673
3674 d_val.word = desc->Words;
3675
3676 reply_descript_type = reply_desc->ReplyFlags &
3677 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3678
3679 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3680 break;
3681 /*
3682 * Write to reply post host index register after completing threshold
3683 * number of reply counts and still there are more replies in reply queue
3684 * pending to be completed
3685 */
3686 if (threshold_reply_count >= instance->threshold_reply_count) {
3687 if (instance->msix_combined)
3688 writel(((MSIxIndex & 0x7) << 24) |
3689 fusion->last_reply_idx[MSIxIndex],
3690 instance->reply_post_host_index_addr[MSIxIndex/8]);
3691 else
3692 writel((MSIxIndex << 24) |
3693 fusion->last_reply_idx[MSIxIndex],
3694 instance->reply_post_host_index_addr[0]);
3695 threshold_reply_count = 0;
3696 if (irq_context) {
3697 if (!irq_context->irq_poll_scheduled) {
3698 irq_context->irq_poll_scheduled = true;
3699 irq_context->irq_line_enable = true;
3700 irq_poll_sched(&irq_context->irqpoll);
3701 }
3702 release_irq_context(irq_context);
3703 return num_completed;
3704 }
3705 }
3706 }
3707
3708 if (num_completed) {
3709 wmb();
3710 if (instance->msix_combined)
3711 writel(((MSIxIndex & 0x7) << 24) |
3712 fusion->last_reply_idx[MSIxIndex],
3713 instance->reply_post_host_index_addr[MSIxIndex/8]);
3714 else
3715 writel((MSIxIndex << 24) |
3716 fusion->last_reply_idx[MSIxIndex],
3717 instance->reply_post_host_index_addr[0]);
3718 megasas_check_and_restore_queue_depth(instance);
3719 }
3720
3721 release_irq_context(irq_context);
3722
3723 return num_completed;
3724 }
3725
megasas_blk_mq_poll(struct Scsi_Host * shost,unsigned int queue_num)3726 int megasas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
3727 {
3728
3729 struct megasas_instance *instance;
3730 int num_entries = 0;
3731 struct fusion_context *fusion;
3732
3733 instance = (struct megasas_instance *)shost->hostdata;
3734
3735 fusion = instance->ctrl_context;
3736
3737 queue_num = queue_num + instance->low_latency_index_start;
3738
3739 if (!atomic_add_unless(&fusion->busy_mq_poll[queue_num], 1, 1))
3740 return 0;
3741
3742 num_entries = complete_cmd_fusion(instance, queue_num, NULL);
3743 atomic_dec(&fusion->busy_mq_poll[queue_num]);
3744
3745 return num_entries;
3746 }
3747
3748 /**
3749 * megasas_enable_irq_poll() - enable irqpoll
3750 * @instance: Adapter soft state
3751 */
megasas_enable_irq_poll(struct megasas_instance * instance)3752 static void megasas_enable_irq_poll(struct megasas_instance *instance)
3753 {
3754 u32 count, i;
3755 struct megasas_irq_context *irq_ctx;
3756
3757 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3758
3759 for (i = 0; i < count; i++) {
3760 irq_ctx = &instance->irq_context[i];
3761 irq_poll_enable(&irq_ctx->irqpoll);
3762 }
3763 }
3764
3765 /**
3766 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3767 * @instance_addr: Adapter soft state address
3768 */
megasas_sync_irqs(unsigned long instance_addr)3769 static void megasas_sync_irqs(unsigned long instance_addr)
3770 {
3771 u32 count, i;
3772 struct megasas_instance *instance =
3773 (struct megasas_instance *)instance_addr;
3774 struct megasas_irq_context *irq_ctx;
3775
3776 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3777
3778 for (i = 0; i < count; i++) {
3779 synchronize_irq(pci_irq_vector(instance->pdev, i));
3780 irq_ctx = &instance->irq_context[i];
3781 irq_poll_disable(&irq_ctx->irqpoll);
3782 if (irq_ctx->irq_poll_scheduled) {
3783 irq_ctx->irq_poll_scheduled = false;
3784 enable_irq(irq_ctx->os_irq);
3785 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3786 }
3787 }
3788 }
3789
3790 /**
3791 * megasas_irqpoll() - process a queue for completed reply descriptors
3792 * @irqpoll: IRQ poll structure associated with queue to poll.
3793 * @budget: Threshold of reply descriptors to process per poll.
3794 *
3795 * Return: The number of entries processed.
3796 */
3797
megasas_irqpoll(struct irq_poll * irqpoll,int budget)3798 int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3799 {
3800 struct megasas_irq_context *irq_ctx;
3801 struct megasas_instance *instance;
3802 int num_entries;
3803
3804 irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3805 instance = irq_ctx->instance;
3806
3807 if (irq_ctx->irq_line_enable) {
3808 disable_irq_nosync(irq_ctx->os_irq);
3809 irq_ctx->irq_line_enable = false;
3810 }
3811
3812 num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3813 if (num_entries < budget) {
3814 irq_poll_complete(irqpoll);
3815 irq_ctx->irq_poll_scheduled = false;
3816 enable_irq(irq_ctx->os_irq);
3817 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3818 }
3819
3820 return num_entries;
3821 }
3822
3823 /**
3824 * megasas_complete_cmd_dpc_fusion - Completes command
3825 * @instance_addr: Adapter soft state address
3826 *
3827 * Tasklet to complete cmds
3828 */
3829 static void
megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)3830 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3831 {
3832 struct megasas_instance *instance =
3833 (struct megasas_instance *)instance_addr;
3834 struct megasas_irq_context *irq_ctx = NULL;
3835 u32 count, MSIxIndex;
3836
3837 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3838
3839 /* If we have already declared adapter dead, donot complete cmds */
3840 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3841 return;
3842
3843 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) {
3844 irq_ctx = &instance->irq_context[MSIxIndex];
3845 complete_cmd_fusion(instance, MSIxIndex, irq_ctx);
3846 }
3847 }
3848
3849 /**
3850 * megasas_isr_fusion - isr entry point
3851 * @irq: IRQ number
3852 * @devp: IRQ context
3853 */
megasas_isr_fusion(int irq,void * devp)3854 static irqreturn_t megasas_isr_fusion(int irq, void *devp)
3855 {
3856 struct megasas_irq_context *irq_context = devp;
3857 struct megasas_instance *instance = irq_context->instance;
3858 u32 mfiStatus;
3859
3860 if (instance->mask_interrupts)
3861 return IRQ_NONE;
3862
3863 if (irq_context->irq_poll_scheduled)
3864 return IRQ_HANDLED;
3865
3866 if (!instance->msix_vectors) {
3867 mfiStatus = instance->instancet->clear_intr(instance);
3868 if (!mfiStatus)
3869 return IRQ_NONE;
3870 }
3871
3872 /* If we are resetting, bail */
3873 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3874 instance->instancet->clear_intr(instance);
3875 return IRQ_HANDLED;
3876 }
3877
3878 return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3879 ? IRQ_HANDLED : IRQ_NONE;
3880 }
3881
3882 /**
3883 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3884 * @instance: Adapter soft state
3885 * @mfi_cmd: megasas_cmd pointer
3886 *
3887 */
3888 static void
build_mpt_mfi_pass_thru(struct megasas_instance * instance,struct megasas_cmd * mfi_cmd)3889 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3890 struct megasas_cmd *mfi_cmd)
3891 {
3892 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3893 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3894 struct megasas_cmd_fusion *cmd;
3895 struct fusion_context *fusion;
3896 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3897
3898 fusion = instance->ctrl_context;
3899
3900 cmd = megasas_get_cmd_fusion(instance,
3901 instance->max_scsi_cmds + mfi_cmd->index);
3902
3903 /* Save the smid. To be used for returning the cmd */
3904 mfi_cmd->context.smid = cmd->index;
3905
3906 /*
3907 * For cmds where the flag is set, store the flag and check
3908 * on completion. For cmds with this flag, don't call
3909 * megasas_complete_cmd
3910 */
3911
3912 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3913 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3914
3915 io_req = cmd->io_request;
3916
3917 if (instance->adapter_type >= INVADER_SERIES) {
3918 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3919 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3920 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3921 sgl_ptr_end->Flags = 0;
3922 }
3923
3924 mpi25_ieee_chain =
3925 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3926
3927 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3928 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3929 SGL) / 4;
3930 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3931
3932 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3933
3934 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3935 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3936
3937 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3938 }
3939
3940 /**
3941 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3942 * @instance: Adapter soft state
3943 * @cmd: mfi cmd to build
3944 *
3945 */
3946 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
build_mpt_cmd(struct megasas_instance * instance,struct megasas_cmd * cmd)3947 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3948 {
3949 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3950 u16 index;
3951
3952 build_mpt_mfi_pass_thru(instance, cmd);
3953 index = cmd->context.smid;
3954
3955 req_desc = megasas_get_request_descriptor(instance, index - 1);
3956
3957 req_desc->Words = 0;
3958 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3959 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3960
3961 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3962
3963 return req_desc;
3964 }
3965
3966 /**
3967 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3968 * @instance: Adapter soft state
3969 * @cmd: mfi cmd pointer
3970 *
3971 */
3972 static void
megasas_issue_dcmd_fusion(struct megasas_instance * instance,struct megasas_cmd * cmd)3973 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3974 struct megasas_cmd *cmd)
3975 {
3976 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3977
3978 req_desc = build_mpt_cmd(instance, cmd);
3979
3980 megasas_fire_cmd_fusion(instance, req_desc);
3981 return;
3982 }
3983
3984 /**
3985 * megasas_release_fusion - Reverses the FW initialization
3986 * @instance: Adapter soft state
3987 */
3988 void
megasas_release_fusion(struct megasas_instance * instance)3989 megasas_release_fusion(struct megasas_instance *instance)
3990 {
3991 megasas_free_ioc_init_cmd(instance);
3992 megasas_free_cmds(instance);
3993 megasas_free_cmds_fusion(instance);
3994
3995 iounmap(instance->reg_set);
3996
3997 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3998 }
3999
4000 /**
4001 * megasas_read_fw_status_reg_fusion - returns the current FW status value
4002 * @instance: Adapter soft state
4003 */
4004 static u32
megasas_read_fw_status_reg_fusion(struct megasas_instance * instance)4005 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
4006 {
4007 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
4008 }
4009
4010 /**
4011 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4012 * @instance: Controller's soft instance
4013 * @return: Number of allocated host crash buffers
4014 */
4015 static void
megasas_alloc_host_crash_buffer(struct megasas_instance * instance)4016 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
4017 {
4018 unsigned int i;
4019
4020 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
4021 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
4022 if (!instance->crash_buf[i]) {
4023 dev_info(&instance->pdev->dev, "Firmware crash dump "
4024 "memory allocation failed at index %d\n", i);
4025 break;
4026 }
4027 }
4028 instance->drv_buf_alloc = i;
4029 }
4030
4031 /**
4032 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4033 * @instance: Controller's soft instance
4034 */
4035 void
megasas_free_host_crash_buffer(struct megasas_instance * instance)4036 megasas_free_host_crash_buffer(struct megasas_instance *instance)
4037 {
4038 unsigned int i;
4039 for (i = 0; i < instance->drv_buf_alloc; i++) {
4040 vfree(instance->crash_buf[i]);
4041 }
4042 instance->drv_buf_index = 0;
4043 instance->drv_buf_alloc = 0;
4044 instance->fw_crash_state = UNAVAILABLE;
4045 instance->fw_crash_buffer_size = 0;
4046 }
4047
4048 /**
4049 * megasas_adp_reset_fusion - For controller reset
4050 * @instance: Controller's soft instance
4051 * @regs: MFI register set
4052 */
4053 static int
megasas_adp_reset_fusion(struct megasas_instance * instance,struct megasas_register_set __iomem * regs)4054 megasas_adp_reset_fusion(struct megasas_instance *instance,
4055 struct megasas_register_set __iomem *regs)
4056 {
4057 u32 host_diag, abs_state, retry;
4058
4059 /* Now try to reset the chip */
4060 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4061 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4062 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4063 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4064 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4065 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4066 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
4067
4068 /* Check that the diag write enable (DRWE) bit is on */
4069 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4070 retry = 0;
4071 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
4072 msleep(100);
4073 host_diag = megasas_readl(instance,
4074 &instance->reg_set->fusion_host_diag);
4075 if (retry++ == 100) {
4076 dev_warn(&instance->pdev->dev,
4077 "Host diag unlock failed from %s %d\n",
4078 __func__, __LINE__);
4079 break;
4080 }
4081 }
4082 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
4083 return -1;
4084
4085 /* Send chip reset command */
4086 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
4087 &instance->reg_set->fusion_host_diag);
4088 msleep(3000);
4089
4090 /* Make sure reset adapter bit is cleared */
4091 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4092 retry = 0;
4093 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
4094 msleep(100);
4095 host_diag = megasas_readl(instance,
4096 &instance->reg_set->fusion_host_diag);
4097 if (retry++ == 1000) {
4098 dev_warn(&instance->pdev->dev,
4099 "Diag reset adapter never cleared %s %d\n",
4100 __func__, __LINE__);
4101 break;
4102 }
4103 }
4104 if (host_diag & HOST_DIAG_RESET_ADAPTER)
4105 return -1;
4106
4107 abs_state = instance->instancet->read_fw_status_reg(instance)
4108 & MFI_STATE_MASK;
4109 retry = 0;
4110
4111 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
4112 msleep(100);
4113 abs_state = instance->instancet->
4114 read_fw_status_reg(instance) & MFI_STATE_MASK;
4115 }
4116 if (abs_state <= MFI_STATE_FW_INIT) {
4117 dev_warn(&instance->pdev->dev,
4118 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4119 abs_state, __func__, __LINE__);
4120 return -1;
4121 }
4122
4123 return 0;
4124 }
4125
4126 /**
4127 * megasas_check_reset_fusion - For controller reset check
4128 * @instance: Controller's soft instance
4129 * @regs: MFI register set
4130 */
4131 static int
megasas_check_reset_fusion(struct megasas_instance * instance,struct megasas_register_set __iomem * regs)4132 megasas_check_reset_fusion(struct megasas_instance *instance,
4133 struct megasas_register_set __iomem *regs)
4134 {
4135 return 0;
4136 }
4137
4138 /**
4139 * megasas_trigger_snap_dump - Trigger snap dump in FW
4140 * @instance: Soft instance of adapter
4141 */
megasas_trigger_snap_dump(struct megasas_instance * instance)4142 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4143 {
4144 int j;
4145 u32 fw_state, abs_state;
4146
4147 if (!instance->disableOnlineCtrlReset) {
4148 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4149 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4150 &instance->reg_set->doorbell);
4151 readl(&instance->reg_set->doorbell);
4152 }
4153
4154 for (j = 0; j < instance->snapdump_wait_time; j++) {
4155 abs_state = instance->instancet->read_fw_status_reg(instance);
4156 fw_state = abs_state & MFI_STATE_MASK;
4157 if (fw_state == MFI_STATE_FAULT) {
4158 dev_printk(KERN_ERR, &instance->pdev->dev,
4159 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4160 abs_state & MFI_STATE_FAULT_CODE,
4161 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4162 return;
4163 }
4164 msleep(1000);
4165 }
4166 }
4167
4168 /* This function waits for outstanding commands on fusion to complete */
4169 static int
megasas_wait_for_outstanding_fusion(struct megasas_instance * instance,int reason,int * convert)4170 megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
4171 int reason, int *convert)
4172 {
4173 int i, outstanding, retval = 0, hb_seconds_missed = 0;
4174 u32 fw_state, abs_state;
4175 u32 waittime_for_io_completion;
4176
4177 waittime_for_io_completion =
4178 min_t(u32, resetwaittime,
4179 (resetwaittime - instance->snapdump_wait_time));
4180
4181 if (reason == MFI_IO_TIMEOUT_OCR) {
4182 dev_info(&instance->pdev->dev,
4183 "MFI command is timed out\n");
4184 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4185 if (instance->snapdump_wait_time)
4186 megasas_trigger_snap_dump(instance);
4187 retval = 1;
4188 goto out;
4189 }
4190
4191 for (i = 0; i < waittime_for_io_completion; i++) {
4192 /* Check if firmware is in fault state */
4193 abs_state = instance->instancet->read_fw_status_reg(instance);
4194 fw_state = abs_state & MFI_STATE_MASK;
4195 if (fw_state == MFI_STATE_FAULT) {
4196 dev_printk(KERN_ERR, &instance->pdev->dev,
4197 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4198 abs_state & MFI_STATE_FAULT_CODE,
4199 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4200 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4201 if (instance->requestorId && reason) {
4202 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4203 " state while polling during"
4204 " I/O timeout handling for %d\n",
4205 instance->host->host_no);
4206 *convert = 1;
4207 }
4208
4209 retval = 1;
4210 goto out;
4211 }
4212
4213
4214 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4215 if (instance->requestorId && !reason) {
4216 retval = 1;
4217 goto out;
4218 }
4219
4220 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4221 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
4222 if (instance->hb_host_mem->HB.fwCounter !=
4223 instance->hb_host_mem->HB.driverCounter) {
4224 instance->hb_host_mem->HB.driverCounter =
4225 instance->hb_host_mem->HB.fwCounter;
4226 hb_seconds_missed = 0;
4227 } else {
4228 hb_seconds_missed++;
4229 if (hb_seconds_missed ==
4230 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
4231 dev_warn(&instance->pdev->dev, "SR-IOV:"
4232 " Heartbeat never completed "
4233 " while polling during I/O "
4234 " timeout handling for "
4235 "scsi%d.\n",
4236 instance->host->host_no);
4237 *convert = 1;
4238 retval = 1;
4239 goto out;
4240 }
4241 }
4242 }
4243
4244 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4245 outstanding = atomic_read(&instance->fw_outstanding);
4246 if (!outstanding)
4247 goto out;
4248
4249 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4250 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
4251 "commands to complete for scsi%d\n", i,
4252 outstanding, instance->host->host_no);
4253 }
4254 msleep(1000);
4255 }
4256
4257 if (instance->snapdump_wait_time) {
4258 megasas_trigger_snap_dump(instance);
4259 retval = 1;
4260 goto out;
4261 }
4262
4263 if (atomic_read(&instance->fw_outstanding)) {
4264 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
4265 "will reset adapter scsi%d.\n",
4266 instance->host->host_no);
4267 *convert = 1;
4268 retval = 1;
4269 }
4270
4271 out:
4272 if (!retval && reason == SCSIIO_TIMEOUT_OCR)
4273 dev_info(&instance->pdev->dev, "IO is completed, no OCR is required\n");
4274
4275 return retval;
4276 }
4277
megasas_reset_reply_desc(struct megasas_instance * instance)4278 void megasas_reset_reply_desc(struct megasas_instance *instance)
4279 {
4280 int i, j, count;
4281 struct fusion_context *fusion;
4282 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4283
4284 fusion = instance->ctrl_context;
4285 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4286 count += instance->iopoll_q_count;
4287
4288 for (i = 0 ; i < count ; i++) {
4289 fusion->last_reply_idx[i] = 0;
4290 reply_desc = fusion->reply_frames_desc[i];
4291 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4292 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4293 }
4294 }
4295
4296 /*
4297 * megasas_refire_mgmt_cmd : Re-fire management commands
4298 * @instance: Controller's soft instance
4299 */
megasas_refire_mgmt_cmd(struct megasas_instance * instance,bool return_ioctl)4300 static void megasas_refire_mgmt_cmd(struct megasas_instance *instance,
4301 bool return_ioctl)
4302 {
4303 int j;
4304 struct megasas_cmd_fusion *cmd_fusion;
4305 struct fusion_context *fusion;
4306 struct megasas_cmd *cmd_mfi;
4307 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4308 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
4309 u16 smid;
4310 bool refire_cmd = false;
4311 u8 result;
4312 u32 opcode = 0;
4313
4314 fusion = instance->ctrl_context;
4315
4316 /* Re-fire management commands.
4317 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4318 */
4319 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4320 cmd_fusion = fusion->cmd_list[j];
4321 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4322 smid = le16_to_cpu(cmd_mfi->context.smid);
4323 result = REFIRE_CMD;
4324
4325 if (!smid)
4326 continue;
4327
4328 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4329
4330 switch (cmd_mfi->frame->hdr.cmd) {
4331 case MFI_CMD_DCMD:
4332 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4333 /* Do not refire shutdown command */
4334 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4335 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4336 result = COMPLETE_CMD;
4337 break;
4338 }
4339
4340 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4341 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4342 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4343
4344 if (!refire_cmd)
4345 result = RETURN_CMD;
4346
4347 break;
4348 case MFI_CMD_NVME:
4349 if (!instance->support_nvme_passthru) {
4350 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4351 result = COMPLETE_CMD;
4352 }
4353
4354 break;
4355 case MFI_CMD_TOOLBOX:
4356 if (!instance->support_pci_lane_margining) {
4357 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4358 result = COMPLETE_CMD;
4359 }
4360
4361 break;
4362 default:
4363 break;
4364 }
4365
4366 if (return_ioctl && cmd_mfi->sync_cmd &&
4367 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT) {
4368 dev_err(&instance->pdev->dev,
4369 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x\n",
4370 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4371 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4372 cmd_mfi->cmd_status_drv = DCMD_BUSY;
4373 result = COMPLETE_CMD;
4374 }
4375
4376 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
4377 cmd_fusion->io_request;
4378 if (scsi_io_req->Function == MPI2_FUNCTION_SCSI_TASK_MGMT)
4379 result = RETURN_CMD;
4380
4381 switch (result) {
4382 case REFIRE_CMD:
4383 megasas_fire_cmd_fusion(instance, req_desc);
4384 break;
4385 case RETURN_CMD:
4386 megasas_return_cmd(instance, cmd_mfi);
4387 break;
4388 case COMPLETE_CMD:
4389 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4390 break;
4391 }
4392 }
4393 }
4394
4395 /*
4396 * megasas_return_polled_cmds: Return polled mode commands back to the pool
4397 * before initiating an OCR.
4398 * @instance: Controller's soft instance
4399 */
4400 static void
megasas_return_polled_cmds(struct megasas_instance * instance)4401 megasas_return_polled_cmds(struct megasas_instance *instance)
4402 {
4403 int i;
4404 struct megasas_cmd_fusion *cmd_fusion;
4405 struct fusion_context *fusion;
4406 struct megasas_cmd *cmd_mfi;
4407
4408 fusion = instance->ctrl_context;
4409
4410 for (i = instance->max_scsi_cmds; i < instance->max_fw_cmds; i++) {
4411 cmd_fusion = fusion->cmd_list[i];
4412 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4413
4414 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
4415 if (megasas_dbg_lvl & OCR_DEBUG)
4416 dev_info(&instance->pdev->dev,
4417 "%s %d return cmd 0x%x opcode 0x%x\n",
4418 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4419 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4420 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
4421 megasas_return_cmd(instance, cmd_mfi);
4422 }
4423 }
4424 }
4425
4426 /*
4427 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4428 * @instance: per adapter struct
4429 * @channel: the channel assigned by the OS
4430 * @id: the id assigned by the OS
4431 *
4432 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4433 */
4434
megasas_track_scsiio(struct megasas_instance * instance,int id,int channel)4435 static int megasas_track_scsiio(struct megasas_instance *instance,
4436 int id, int channel)
4437 {
4438 int i, found = 0;
4439 struct megasas_cmd_fusion *cmd_fusion;
4440 struct fusion_context *fusion;
4441 fusion = instance->ctrl_context;
4442
4443 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4444 cmd_fusion = fusion->cmd_list[i];
4445 if (cmd_fusion->scmd &&
4446 (cmd_fusion->scmd->device->id == id &&
4447 cmd_fusion->scmd->device->channel == channel)) {
4448 dev_info(&instance->pdev->dev,
4449 "SCSI commands pending to target"
4450 "channel %d id %d \tSMID: 0x%x\n",
4451 channel, id, cmd_fusion->index);
4452 scsi_print_command(cmd_fusion->scmd);
4453 found = 1;
4454 break;
4455 }
4456 }
4457
4458 return found ? FAILED : SUCCESS;
4459 }
4460
4461 /**
4462 * megasas_tm_response_code - translation of device response code
4463 * @instance: Controller's soft instance
4464 * @mpi_reply: MPI reply returned by firmware
4465 *
4466 * Return nothing.
4467 */
4468 static void
megasas_tm_response_code(struct megasas_instance * instance,struct MPI2_SCSI_TASK_MANAGE_REPLY * mpi_reply)4469 megasas_tm_response_code(struct megasas_instance *instance,
4470 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4471 {
4472 char *desc;
4473
4474 switch (mpi_reply->ResponseCode) {
4475 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4476 desc = "task management request completed";
4477 break;
4478 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4479 desc = "invalid frame";
4480 break;
4481 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4482 desc = "task management request not supported";
4483 break;
4484 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4485 desc = "task management request failed";
4486 break;
4487 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4488 desc = "task management request succeeded";
4489 break;
4490 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4491 desc = "invalid lun";
4492 break;
4493 case 0xA:
4494 desc = "overlapped tag attempted";
4495 break;
4496 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4497 desc = "task queued, however not sent to target";
4498 break;
4499 default:
4500 desc = "unknown";
4501 break;
4502 }
4503 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4504 mpi_reply->ResponseCode, desc);
4505 dev_dbg(&instance->pdev->dev,
4506 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4507 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4508 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4509 mpi_reply->Function, mpi_reply->TaskType,
4510 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4511 }
4512
4513 /**
4514 * megasas_issue_tm - main routine for sending tm requests
4515 * @instance: per adapter struct
4516 * @device_handle: device handle
4517 * @channel: the channel assigned by the OS
4518 * @id: the id assigned by the OS
4519 * @smid_task: smid assigned to the task
4520 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4521 * @mr_device_priv_data: private data
4522 * Context: user
4523 *
4524 * MegaRaid use MPT interface for Task Magement request.
4525 * A generic API for sending task management requests to firmware.
4526 *
4527 * Return SUCCESS or FAILED.
4528 */
4529 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)4530 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4531 uint channel, uint id, u16 smid_task, u8 type,
4532 struct MR_PRIV_DEVICE *mr_device_priv_data)
4533 {
4534 struct MR_TASK_MANAGE_REQUEST *mr_request;
4535 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4536 unsigned long timeleft;
4537 struct megasas_cmd_fusion *cmd_fusion;
4538 struct megasas_cmd *cmd_mfi;
4539 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4540 struct fusion_context *fusion = NULL;
4541 struct megasas_cmd_fusion *scsi_lookup;
4542 int rc;
4543 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4544 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4545
4546 fusion = instance->ctrl_context;
4547
4548 cmd_mfi = megasas_get_cmd(instance);
4549
4550 if (!cmd_mfi) {
4551 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4552 __func__, __LINE__);
4553 return -ENOMEM;
4554 }
4555
4556 cmd_fusion = megasas_get_cmd_fusion(instance,
4557 instance->max_scsi_cmds + cmd_mfi->index);
4558
4559 /* Save the smid. To be used for returning the cmd */
4560 cmd_mfi->context.smid = cmd_fusion->index;
4561
4562 req_desc = megasas_get_request_descriptor(instance,
4563 (cmd_fusion->index - 1));
4564
4565 cmd_fusion->request_desc = req_desc;
4566 req_desc->Words = 0;
4567
4568 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4569 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4570 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4571 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4572 mpi_request->DevHandle = cpu_to_le16(device_handle);
4573 mpi_request->TaskType = type;
4574 mpi_request->TaskMID = cpu_to_le16(smid_task);
4575 mpi_request->LUN[1] = 0;
4576
4577
4578 req_desc = cmd_fusion->request_desc;
4579 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4580 req_desc->HighPriority.RequestFlags =
4581 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4582 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4583 req_desc->HighPriority.MSIxIndex = 0;
4584 req_desc->HighPriority.LMID = 0;
4585 req_desc->HighPriority.Reserved1 = 0;
4586
4587 if (channel < MEGASAS_MAX_PD_CHANNELS)
4588 mr_request->tmReqFlags.isTMForPD = 1;
4589 else
4590 mr_request->tmReqFlags.isTMForLD = 1;
4591
4592 init_completion(&cmd_fusion->done);
4593 megasas_fire_cmd_fusion(instance, req_desc);
4594
4595 switch (type) {
4596 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4597 timeout = mr_device_priv_data->task_abort_tmo;
4598 break;
4599 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4600 timeout = mr_device_priv_data->target_reset_tmo;
4601 break;
4602 }
4603
4604 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4605
4606 if (!timeleft) {
4607 dev_err(&instance->pdev->dev,
4608 "task mgmt type 0x%x timed out\n", type);
4609 mutex_unlock(&instance->reset_mutex);
4610 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4611 mutex_lock(&instance->reset_mutex);
4612 return rc;
4613 }
4614
4615 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4616 megasas_tm_response_code(instance, mpi_reply);
4617
4618 megasas_return_cmd(instance, cmd_mfi);
4619 rc = SUCCESS;
4620 switch (type) {
4621 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4622 scsi_lookup = fusion->cmd_list[smid_task - 1];
4623
4624 if (scsi_lookup->scmd == NULL)
4625 break;
4626 else {
4627 instance->instancet->disable_intr(instance);
4628 megasas_sync_irqs((unsigned long)instance);
4629 instance->instancet->enable_intr(instance);
4630 megasas_enable_irq_poll(instance);
4631 if (scsi_lookup->scmd == NULL)
4632 break;
4633 }
4634 rc = FAILED;
4635 break;
4636
4637 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4638 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4639 break;
4640 instance->instancet->disable_intr(instance);
4641 megasas_sync_irqs((unsigned long)instance);
4642 rc = megasas_track_scsiio(instance, id, channel);
4643 instance->instancet->enable_intr(instance);
4644 megasas_enable_irq_poll(instance);
4645
4646 break;
4647 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4648 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4649 break;
4650 default:
4651 rc = FAILED;
4652 break;
4653 }
4654
4655 return rc;
4656
4657 }
4658
4659 /*
4660 * megasas_fusion_smid_lookup : Look for fusion command corresponding to SCSI
4661 * @instance: per adapter struct
4662 *
4663 * Return Non Zero index, if SMID found in outstanding commands
4664 */
megasas_fusion_smid_lookup(struct scsi_cmnd * scmd)4665 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4666 {
4667 int i, ret = 0;
4668 struct megasas_instance *instance;
4669 struct megasas_cmd_fusion *cmd_fusion;
4670 struct fusion_context *fusion;
4671
4672 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4673
4674 fusion = instance->ctrl_context;
4675
4676 for (i = 0; i < instance->max_scsi_cmds; i++) {
4677 cmd_fusion = fusion->cmd_list[i];
4678 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4679 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4680 " SMID: %d\n", cmd_fusion->index);
4681 ret = cmd_fusion->index;
4682 break;
4683 }
4684 }
4685
4686 return ret;
4687 }
4688
4689 /*
4690 * megasas_get_tm_devhandle - Get devhandle for TM request
4691 * @sdev- OS provided scsi device
4692 *
4693 * Returns- devhandle/targetID of SCSI device
4694 */
megasas_get_tm_devhandle(struct scsi_device * sdev)4695 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4696 {
4697 u16 pd_index = 0;
4698 u32 device_id;
4699 struct megasas_instance *instance;
4700 struct fusion_context *fusion;
4701 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4702 u16 devhandle = (u16)ULONG_MAX;
4703
4704 instance = (struct megasas_instance *)sdev->host->hostdata;
4705 fusion = instance->ctrl_context;
4706
4707 if (!MEGASAS_IS_LOGICAL(sdev)) {
4708 if (instance->use_seqnum_jbod_fp) {
4709 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4710 + sdev->id;
4711 pd_sync = (void *)fusion->pd_seq_sync
4712 [(instance->pd_seq_map_id - 1) & 1];
4713 devhandle = pd_sync->seq[pd_index].devHandle;
4714 } else
4715 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4716 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4717 } else {
4718 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4719 + sdev->id;
4720 devhandle = device_id;
4721 }
4722
4723 return devhandle;
4724 }
4725
4726 /*
4727 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4728 * @scmd : pointer to scsi command object
4729 *
4730 * Return SUCCESS, if command aborted else FAILED
4731 */
4732
megasas_task_abort_fusion(struct scsi_cmnd * scmd)4733 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4734 {
4735 struct megasas_instance *instance;
4736 u16 smid, devhandle;
4737 int ret;
4738 struct MR_PRIV_DEVICE *mr_device_priv_data;
4739 mr_device_priv_data = scmd->device->hostdata;
4740
4741 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4742
4743 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4744 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4745 "SCSI host:%d\n", instance->host->host_no);
4746 ret = FAILED;
4747 return ret;
4748 }
4749
4750 if (!mr_device_priv_data) {
4751 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4752 "scmd(%p)\n", scmd);
4753 scmd->result = DID_NO_CONNECT << 16;
4754 ret = SUCCESS;
4755 goto out;
4756 }
4757
4758 if (!mr_device_priv_data->is_tm_capable) {
4759 ret = FAILED;
4760 goto out;
4761 }
4762
4763 mutex_lock(&instance->reset_mutex);
4764
4765 smid = megasas_fusion_smid_lookup(scmd);
4766
4767 if (!smid) {
4768 ret = SUCCESS;
4769 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4770 " issued is not found in outstanding commands\n");
4771 mutex_unlock(&instance->reset_mutex);
4772 goto out;
4773 }
4774
4775 devhandle = megasas_get_tm_devhandle(scmd->device);
4776
4777 if (devhandle == (u16)ULONG_MAX) {
4778 ret = FAILED;
4779 sdev_printk(KERN_INFO, scmd->device,
4780 "task abort issued for invalid devhandle\n");
4781 mutex_unlock(&instance->reset_mutex);
4782 goto out;
4783 }
4784 sdev_printk(KERN_INFO, scmd->device,
4785 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4786 scmd, devhandle);
4787
4788 mr_device_priv_data->tm_busy = true;
4789 ret = megasas_issue_tm(instance, devhandle,
4790 scmd->device->channel, scmd->device->id, smid,
4791 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4792 mr_device_priv_data);
4793 mr_device_priv_data->tm_busy = false;
4794
4795 mutex_unlock(&instance->reset_mutex);
4796 scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
4797 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4798 out:
4799 scsi_print_command(scmd);
4800 if (megasas_dbg_lvl & TM_DEBUG)
4801 megasas_dump_fusion_io(scmd);
4802
4803 return ret;
4804 }
4805
4806 /*
4807 * megasas_reset_target_fusion : target reset function for fusion adapters
4808 * scmd: SCSI command pointer
4809 *
4810 * Returns SUCCESS if all commands associated with target aborted else FAILED
4811 */
4812
megasas_reset_target_fusion(struct scsi_cmnd * scmd)4813 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4814 {
4815
4816 struct megasas_instance *instance;
4817 int ret = FAILED;
4818 u16 devhandle;
4819 struct MR_PRIV_DEVICE *mr_device_priv_data;
4820 mr_device_priv_data = scmd->device->hostdata;
4821
4822 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4823
4824 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4825 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4826 "SCSI host:%d\n", instance->host->host_no);
4827 ret = FAILED;
4828 return ret;
4829 }
4830
4831 if (!mr_device_priv_data) {
4832 sdev_printk(KERN_INFO, scmd->device,
4833 "device been deleted! scmd: (0x%p)\n", scmd);
4834 scmd->result = DID_NO_CONNECT << 16;
4835 ret = SUCCESS;
4836 goto out;
4837 }
4838
4839 if (!mr_device_priv_data->is_tm_capable) {
4840 ret = FAILED;
4841 goto out;
4842 }
4843
4844 mutex_lock(&instance->reset_mutex);
4845 devhandle = megasas_get_tm_devhandle(scmd->device);
4846
4847 if (devhandle == (u16)ULONG_MAX) {
4848 ret = FAILED;
4849 sdev_printk(KERN_INFO, scmd->device,
4850 "target reset issued for invalid devhandle\n");
4851 mutex_unlock(&instance->reset_mutex);
4852 goto out;
4853 }
4854
4855 sdev_printk(KERN_INFO, scmd->device,
4856 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4857 scmd, devhandle);
4858 mr_device_priv_data->tm_busy = true;
4859 ret = megasas_issue_tm(instance, devhandle,
4860 scmd->device->channel, scmd->device->id, 0,
4861 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4862 mr_device_priv_data);
4863 mr_device_priv_data->tm_busy = false;
4864 mutex_unlock(&instance->reset_mutex);
4865 scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
4866 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4867
4868 out:
4869 return ret;
4870 }
4871
4872 /*SRIOV get other instance in cluster if any*/
4873 static struct
megasas_get_peer_instance(struct megasas_instance * instance)4874 megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4875 {
4876 int i;
4877
4878 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4879 if (megasas_mgmt_info.instance[i] &&
4880 (megasas_mgmt_info.instance[i] != instance) &&
4881 megasas_mgmt_info.instance[i]->requestorId &&
4882 megasas_mgmt_info.instance[i]->peerIsPresent &&
4883 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4884 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4885 return megasas_mgmt_info.instance[i];
4886 }
4887 return NULL;
4888 }
4889
4890 /* Check for a second path that is currently UP */
megasas_check_mpio_paths(struct megasas_instance * instance,struct scsi_cmnd * scmd)4891 int megasas_check_mpio_paths(struct megasas_instance *instance,
4892 struct scsi_cmnd *scmd)
4893 {
4894 struct megasas_instance *peer_instance = NULL;
4895 int retval = (DID_REQUEUE << 16);
4896
4897 if (instance->peerIsPresent) {
4898 peer_instance = megasas_get_peer_instance(instance);
4899 if ((peer_instance) &&
4900 (atomic_read(&peer_instance->adprecovery) ==
4901 MEGASAS_HBA_OPERATIONAL))
4902 retval = (DID_NO_CONNECT << 16);
4903 }
4904 return retval;
4905 }
4906
4907 /* Core fusion reset function */
megasas_reset_fusion(struct Scsi_Host * shost,int reason)4908 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4909 {
4910 int retval = SUCCESS, i, j, convert = 0;
4911 struct megasas_instance *instance;
4912 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4913 struct fusion_context *fusion;
4914 u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
4915 u32 io_timeout_in_crash_mode = 0;
4916 struct scsi_cmnd *scmd_local = NULL;
4917 struct scsi_device *sdev;
4918 int ret_target_prop = DCMD_FAILED;
4919 bool is_target_prop = false;
4920 bool do_adp_reset = true;
4921 int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
4922
4923 instance = (struct megasas_instance *)shost->hostdata;
4924 fusion = instance->ctrl_context;
4925
4926 mutex_lock(&instance->reset_mutex);
4927
4928 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4929 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4930 "returning FAILED for scsi%d.\n",
4931 instance->host->host_no);
4932 mutex_unlock(&instance->reset_mutex);
4933 return FAILED;
4934 }
4935 status_reg = instance->instancet->read_fw_status_reg(instance);
4936 abs_state = status_reg & MFI_STATE_MASK;
4937
4938 /* IO timeout detected, forcibly put FW in FAULT state */
4939 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4940 instance->crash_dump_app_support && reason) {
4941 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4942 "forcibly FAULT Firmware\n");
4943 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4944 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4945 writel(status_reg | MFI_STATE_FORCE_OCR,
4946 &instance->reg_set->doorbell);
4947 readl(&instance->reg_set->doorbell);
4948 mutex_unlock(&instance->reset_mutex);
4949 do {
4950 ssleep(3);
4951 io_timeout_in_crash_mode++;
4952 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4953 "seconds for crash dump collection and OCR "
4954 "to be done\n", (io_timeout_in_crash_mode * 3));
4955 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4956 (io_timeout_in_crash_mode < 80));
4957
4958 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4959 dev_info(&instance->pdev->dev, "OCR done for IO "
4960 "timeout case\n");
4961 retval = SUCCESS;
4962 } else {
4963 dev_info(&instance->pdev->dev, "Controller is not "
4964 "operational after 240 seconds wait for IO "
4965 "timeout case in FW crash dump mode\n do "
4966 "OCR/kill adapter\n");
4967 retval = megasas_reset_fusion(shost, 0);
4968 }
4969 return retval;
4970 }
4971
4972 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4973 timer_delete_sync(&instance->sriov_heartbeat_timer);
4974 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4975 set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4976 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4977 instance->instancet->disable_intr(instance);
4978 megasas_sync_irqs((unsigned long)instance);
4979
4980 /* First try waiting for commands to complete */
4981 if (megasas_wait_for_outstanding_fusion(instance, reason,
4982 &convert)) {
4983 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4984 dev_warn(&instance->pdev->dev, "resetting fusion "
4985 "adapter scsi%d.\n", instance->host->host_no);
4986 if (convert)
4987 reason = 0;
4988
4989 if (megasas_dbg_lvl & OCR_DEBUG)
4990 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4991
4992 /* Now return commands back to the OS */
4993 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4994 cmd_fusion = fusion->cmd_list[i];
4995 /*check for extra commands issued by driver*/
4996 if (instance->adapter_type >= VENTURA_SERIES) {
4997 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4998 megasas_return_cmd_fusion(instance, r1_cmd);
4999 }
5000 scmd_local = cmd_fusion->scmd;
5001 if (cmd_fusion->scmd) {
5002 if (megasas_dbg_lvl & OCR_DEBUG) {
5003 sdev_printk(KERN_INFO,
5004 cmd_fusion->scmd->device, "SMID: 0x%x\n",
5005 cmd_fusion->index);
5006 megasas_dump_fusion_io(cmd_fusion->scmd);
5007 }
5008
5009 if (cmd_fusion->io_request->Function ==
5010 MPI2_FUNCTION_SCSI_IO_REQUEST)
5011 fpio_count++;
5012
5013 scmd_local->result =
5014 megasas_check_mpio_paths(instance,
5015 scmd_local);
5016 if (instance->ldio_threshold &&
5017 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
5018 atomic_dec(&instance->ldio_outstanding);
5019 megasas_return_cmd_fusion(instance, cmd_fusion);
5020 scsi_dma_unmap(scmd_local);
5021 scsi_done(scmd_local);
5022 }
5023 }
5024
5025 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
5026 fpio_count);
5027
5028 atomic_set(&instance->fw_outstanding, 0);
5029
5030 status_reg = instance->instancet->read_fw_status_reg(instance);
5031 abs_state = status_reg & MFI_STATE_MASK;
5032 reset_adapter = status_reg & MFI_RESET_ADAPTER;
5033 if (instance->disableOnlineCtrlReset ||
5034 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
5035 /* Reset not supported, kill adapter */
5036 dev_warn(&instance->pdev->dev, "Reset not supported"
5037 ", killing adapter scsi%d.\n",
5038 instance->host->host_no);
5039 goto kill_hba;
5040 }
5041
5042 /* Let SR-IOV VF & PF sync up if there was a HB failure */
5043 if (instance->requestorId && !reason) {
5044 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
5045 do_adp_reset = false;
5046 max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
5047 }
5048
5049 /* Now try to reset the chip */
5050 for (i = 0; i < max_reset_tries; i++) {
5051 /*
5052 * Do adp reset and wait for
5053 * controller to transition to ready
5054 */
5055 if (megasas_adp_reset_wait_for_ready(instance,
5056 do_adp_reset, 1) == FAILED)
5057 continue;
5058
5059 /* Wait for FW to become ready */
5060 if (megasas_transition_to_ready(instance, 1)) {
5061 dev_warn(&instance->pdev->dev,
5062 "Failed to transition controller to ready for "
5063 "scsi%d.\n", instance->host->host_no);
5064 continue;
5065 }
5066 megasas_reset_reply_desc(instance);
5067 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
5068
5069 if (megasas_ioc_init_fusion(instance)) {
5070 continue;
5071 }
5072
5073 if (megasas_get_ctrl_info(instance)) {
5074 dev_info(&instance->pdev->dev,
5075 "Failed from %s %d\n",
5076 __func__, __LINE__);
5077 goto kill_hba;
5078 }
5079
5080 megasas_refire_mgmt_cmd(instance,
5081 (i == (MEGASAS_FUSION_MAX_RESET_TRIES - 1)
5082 ? 1 : 0));
5083
5084 /* Reset load balance info */
5085 if (fusion->load_balance_info)
5086 memset(fusion->load_balance_info, 0,
5087 (sizeof(struct LD_LOAD_BALANCE_INFO) *
5088 MAX_LOGICAL_DRIVES_EXT));
5089
5090 if (!megasas_get_map_info(instance)) {
5091 megasas_sync_map_info(instance);
5092 } else {
5093 /*
5094 * Return pending polled mode cmds before
5095 * retrying OCR
5096 */
5097 megasas_return_polled_cmds(instance);
5098 continue;
5099 }
5100
5101 megasas_setup_jbod_map(instance);
5102
5103 /* reset stream detection array */
5104 if (instance->adapter_type >= VENTURA_SERIES) {
5105 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
5106 memset(fusion->stream_detect_by_ld[j],
5107 0, sizeof(struct LD_STREAM_DETECT));
5108 fusion->stream_detect_by_ld[j]->mru_bit_map
5109 = MR_STREAM_BITMAP;
5110 }
5111 }
5112
5113 clear_bit(MEGASAS_FUSION_IN_RESET,
5114 &instance->reset_flags);
5115 instance->instancet->enable_intr(instance);
5116 megasas_enable_irq_poll(instance);
5117 shost_for_each_device(sdev, shost) {
5118 if ((instance->tgt_prop) &&
5119 (instance->nvme_page_size))
5120 ret_target_prop = megasas_get_target_prop(instance, sdev);
5121
5122 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
5123 megasas_set_dynamic_target_properties(sdev, NULL,
5124 is_target_prop);
5125 }
5126
5127 status_reg = instance->instancet->read_fw_status_reg
5128 (instance);
5129 abs_state = status_reg & MFI_STATE_MASK;
5130 if (abs_state != MFI_STATE_OPERATIONAL) {
5131 dev_info(&instance->pdev->dev,
5132 "Adapter is not OPERATIONAL, state 0x%x for scsi:%d\n",
5133 abs_state, instance->host->host_no);
5134 goto out;
5135 }
5136 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5137
5138 dev_info(&instance->pdev->dev,
5139 "Adapter is OPERATIONAL for scsi:%d\n",
5140 instance->host->host_no);
5141
5142 /* Restart SR-IOV heartbeat */
5143 if (instance->requestorId) {
5144 if (!megasas_sriov_start_heartbeat(instance, 0))
5145 megasas_start_timer(instance);
5146 else
5147 instance->skip_heartbeat_timer_del = 1;
5148 }
5149
5150 if (instance->crash_dump_drv_support &&
5151 instance->crash_dump_app_support)
5152 megasas_set_crash_dump_params(instance,
5153 MR_CRASH_BUF_TURN_ON);
5154 else
5155 megasas_set_crash_dump_params(instance,
5156 MR_CRASH_BUF_TURN_OFF);
5157
5158 if (instance->snapdump_wait_time) {
5159 megasas_get_snapdump_properties(instance);
5160 dev_info(&instance->pdev->dev,
5161 "Snap dump wait time\t: %d\n",
5162 instance->snapdump_wait_time);
5163 }
5164
5165 retval = SUCCESS;
5166
5167 /* Adapter reset completed successfully */
5168 dev_warn(&instance->pdev->dev,
5169 "Reset successful for scsi%d.\n",
5170 instance->host->host_no);
5171
5172 goto out;
5173 }
5174 /* Reset failed, kill the adapter */
5175 dev_warn(&instance->pdev->dev, "Reset failed, killing "
5176 "adapter scsi%d.\n", instance->host->host_no);
5177 goto kill_hba;
5178 } else {
5179 /* For VF: Restart HB timer if we didn't OCR */
5180 if (instance->requestorId) {
5181 megasas_start_timer(instance);
5182 }
5183 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5184 instance->instancet->enable_intr(instance);
5185 megasas_enable_irq_poll(instance);
5186 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5187 goto out;
5188 }
5189 kill_hba:
5190 megaraid_sas_kill_hba(instance);
5191 megasas_enable_irq_poll(instance);
5192 instance->skip_heartbeat_timer_del = 1;
5193 retval = FAILED;
5194 out:
5195 clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
5196 mutex_unlock(&instance->reset_mutex);
5197 return retval;
5198 }
5199
5200 /* Fusion Crash dump collection */
megasas_fusion_crash_dump(struct megasas_instance * instance)5201 static void megasas_fusion_crash_dump(struct megasas_instance *instance)
5202 {
5203 u32 status_reg;
5204 u8 partial_copy = 0;
5205 int wait = 0;
5206
5207
5208 status_reg = instance->instancet->read_fw_status_reg(instance);
5209
5210 /*
5211 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5212 * to host crash buffers
5213 */
5214 if (instance->drv_buf_index == 0) {
5215 /* Buffer is already allocated for old Crash dump.
5216 * Do OCR and do not wait for crash dump collection
5217 */
5218 if (instance->drv_buf_alloc) {
5219 dev_info(&instance->pdev->dev, "earlier crash dump is "
5220 "not yet copied by application, ignoring this "
5221 "crash dump and initiating OCR\n");
5222 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5223 writel(status_reg,
5224 &instance->reg_set->outbound_scratch_pad_0);
5225 readl(&instance->reg_set->outbound_scratch_pad_0);
5226 return;
5227 }
5228 megasas_alloc_host_crash_buffer(instance);
5229 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5230 "allocated: %d\n", instance->drv_buf_alloc);
5231 }
5232
5233 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5234 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5235 if (!(status_reg & MFI_STATE_DMADONE)) {
5236 /*
5237 * Next crash dump buffer is not yet DMA'd by FW
5238 * Check after 10ms. Wait for 1 second for FW to
5239 * post the next buffer. If not bail out.
5240 */
5241 wait++;
5242 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5243 status_reg = instance->instancet->read_fw_status_reg(
5244 instance);
5245 continue;
5246 }
5247
5248 wait = 0;
5249 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5250 dev_info(&instance->pdev->dev,
5251 "Driver is done copying the buffer: %d\n",
5252 instance->drv_buf_alloc);
5253 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5254 partial_copy = 1;
5255 break;
5256 } else {
5257 memcpy(instance->crash_buf[instance->drv_buf_index],
5258 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5259 instance->drv_buf_index++;
5260 status_reg &= ~MFI_STATE_DMADONE;
5261 }
5262
5263 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5264 readl(&instance->reg_set->outbound_scratch_pad_0);
5265
5266 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5267 status_reg = instance->instancet->read_fw_status_reg(instance);
5268 }
5269
5270 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5271 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5272 "of copied buffers: %d\n", instance->drv_buf_index);
5273 instance->fw_crash_buffer_size = instance->drv_buf_index;
5274 instance->fw_crash_state = AVAILABLE;
5275 instance->drv_buf_index = 0;
5276 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5277 readl(&instance->reg_set->outbound_scratch_pad_0);
5278 if (!partial_copy)
5279 megasas_reset_fusion(instance->host, 0);
5280 }
5281 }
5282
5283
5284 /* Fusion OCR work queue */
megasas_fusion_ocr_wq(struct work_struct * work)5285 void megasas_fusion_ocr_wq(struct work_struct *work)
5286 {
5287 struct megasas_instance *instance =
5288 container_of(work, struct megasas_instance, work_init);
5289
5290 megasas_reset_fusion(instance->host, 0);
5291 }
5292
5293 /* Allocate fusion context */
5294 int
megasas_alloc_fusion_context(struct megasas_instance * instance)5295 megasas_alloc_fusion_context(struct megasas_instance *instance)
5296 {
5297 struct fusion_context *fusion;
5298
5299 instance->ctrl_context = kzalloc_obj(struct fusion_context);
5300 if (!instance->ctrl_context) {
5301 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5302 __func__, __LINE__);
5303 return -ENOMEM;
5304 }
5305
5306 fusion = instance->ctrl_context;
5307
5308 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5309 sizeof(LD_SPAN_INFO));
5310 fusion->log_to_span =
5311 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5312 fusion->log_to_span_pages);
5313 if (!fusion->log_to_span) {
5314 fusion->log_to_span =
5315 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5316 sizeof(LD_SPAN_INFO)));
5317 if (!fusion->log_to_span) {
5318 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5319 __func__, __LINE__);
5320 return -ENOMEM;
5321 }
5322 }
5323
5324 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5325 sizeof(struct LD_LOAD_BALANCE_INFO));
5326 fusion->load_balance_info =
5327 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5328 fusion->load_balance_info_pages);
5329 if (!fusion->load_balance_info) {
5330 fusion->load_balance_info =
5331 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5332 sizeof(struct LD_LOAD_BALANCE_INFO)));
5333 if (!fusion->load_balance_info)
5334 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5335 "continuing without Load Balance support\n");
5336 }
5337
5338 return 0;
5339 }
5340
5341 void
megasas_free_fusion_context(struct megasas_instance * instance)5342 megasas_free_fusion_context(struct megasas_instance *instance)
5343 {
5344 struct fusion_context *fusion = instance->ctrl_context;
5345
5346 if (fusion) {
5347 if (fusion->load_balance_info) {
5348 if (is_vmalloc_addr(fusion->load_balance_info))
5349 vfree(fusion->load_balance_info);
5350 else
5351 free_pages((ulong)fusion->load_balance_info,
5352 fusion->load_balance_info_pages);
5353 }
5354
5355 if (fusion->log_to_span) {
5356 if (is_vmalloc_addr(fusion->log_to_span))
5357 vfree(fusion->log_to_span);
5358 else
5359 free_pages((ulong)fusion->log_to_span,
5360 fusion->log_to_span_pages);
5361 }
5362
5363 kfree(fusion);
5364 }
5365 }
5366
5367 struct megasas_instance_template megasas_instance_template_fusion = {
5368 .enable_intr = megasas_enable_intr_fusion,
5369 .disable_intr = megasas_disable_intr_fusion,
5370 .clear_intr = megasas_clear_intr_fusion,
5371 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5372 .adp_reset = megasas_adp_reset_fusion,
5373 .check_reset = megasas_check_reset_fusion,
5374 .service_isr = megasas_isr_fusion,
5375 .tasklet = megasas_complete_cmd_dpc_fusion,
5376 .init_adapter = megasas_init_adapter_fusion,
5377 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5378 .issue_dcmd = megasas_issue_dcmd_fusion,
5379 };
5380