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