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