2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
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.
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.
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/>.
20 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
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/slab.h>
45 #include <asm/uaccess.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
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_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors
;
65 module_param_named(max_sectors
, max_sectors
, int, 0);
66 MODULE_PARM_DESC(max_sectors
,
67 "Maximum number of sectors per IO command");
69 static int msix_disable
;
70 module_param(msix_disable
, int, S_IRUGO
);
71 MODULE_PARM_DESC(msix_disable
, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors
;
74 module_param(msix_vectors
, int, S_IRUGO
);
75 MODULE_PARM_DESC(msix_vectors
, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls
;
78 module_param(allow_vf_ioctls
, int, S_IRUGO
);
79 MODULE_PARM_DESC(allow_vf_ioctls
, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static int throttlequeuedepth
= MEGASAS_THROTTLE_QUEUE_DEPTH
;
82 module_param(throttlequeuedepth
, int, S_IRUGO
);
83 MODULE_PARM_DESC(throttlequeuedepth
,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 int resetwaittime
= MEGASAS_RESET_WAIT_TIME
;
87 module_param(resetwaittime
, int, S_IRUGO
);
88 MODULE_PARM_DESC(resetwaittime
, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable
= 1;
92 module_param(smp_affinity_enable
, int, S_IRUGO
);
93 MODULE_PARM_DESC(smp_affinity_enable
, "SMP affinity feature enable/disbale Default: enable(1)");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION
);
97 MODULE_AUTHOR("megaraidlinux@lsi.com");
98 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
100 int megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
);
101 static int megasas_get_pd_list(struct megasas_instance
*instance
);
102 static int megasas_ld_list_query(struct megasas_instance
*instance
,
104 static int megasas_issue_init_mfi(struct megasas_instance
*instance
);
105 static int megasas_register_aen(struct megasas_instance
*instance
,
106 u32 seq_num
, u32 class_locale_word
);
108 * PCI ID table for all supported controllers
110 static struct pci_device_id megasas_pci_table
[] = {
112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1064R
)},
114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078R
)},
116 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078DE
)},
118 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078GEN2
)},
120 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0079GEN2
)},
122 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0073SKINNY
)},
124 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0071SKINNY
)},
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_VERDE_ZCR
)},
127 /* xscale IOP, vega */
128 {PCI_DEVICE(PCI_VENDOR_ID_DELL
, PCI_DEVICE_ID_DELL_PERC5
)},
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FUSION
)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_PLASMA
)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_INVADER
)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FURY
)},
141 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
143 static int megasas_mgmt_majorno
;
144 struct megasas_mgmt_info megasas_mgmt_info
;
145 static struct fasync_struct
*megasas_async_queue
;
146 static DEFINE_MUTEX(megasas_async_queue_mutex
);
148 static int megasas_poll_wait_aen
;
149 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait
);
150 static u32 support_poll_for_event
;
152 static u32 support_device_change
;
154 /* define lock for aen poll */
155 spinlock_t poll_aen_lock
;
158 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
161 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
);
163 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
164 struct megasas_register_set __iomem
*reg_set
);
165 static irqreturn_t
megasas_isr(int irq
, void *devp
);
167 megasas_init_adapter_mfi(struct megasas_instance
*instance
);
169 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
170 struct scsi_cmnd
*scmd
);
171 static void megasas_complete_cmd_dpc(unsigned long instance_addr
);
173 megasas_release_fusion(struct megasas_instance
*instance
);
175 megasas_ioc_init_fusion(struct megasas_instance
*instance
);
177 megasas_free_cmds_fusion(struct megasas_instance
*instance
);
179 megasas_get_map_info(struct megasas_instance
*instance
);
181 megasas_sync_map_info(struct megasas_instance
*instance
);
183 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
185 void megasas_reset_reply_desc(struct megasas_instance
*instance
);
186 int megasas_reset_fusion(struct Scsi_Host
*shost
, int iotimeout
);
187 void megasas_fusion_ocr_wq(struct work_struct
*work
);
188 static int megasas_get_ld_vf_affiliation(struct megasas_instance
*instance
,
190 int megasas_check_mpio_paths(struct megasas_instance
*instance
,
191 struct scsi_cmnd
*scmd
);
194 megasas_issue_dcmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
196 instance
->instancet
->fire_cmd(instance
,
197 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
201 * megasas_get_cmd - Get a command from the free pool
202 * @instance: Adapter soft state
204 * Returns a free command from the pool
206 struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
210 struct megasas_cmd
*cmd
= NULL
;
212 spin_lock_irqsave(&instance
->mfi_pool_lock
, flags
);
214 if (!list_empty(&instance
->cmd_pool
)) {
215 cmd
= list_entry((&instance
->cmd_pool
)->next
,
216 struct megasas_cmd
, list
);
217 list_del_init(&cmd
->list
);
218 atomic_set(&cmd
->mfi_mpt_pthr
, MFI_MPT_DETACHED
);
220 printk(KERN_ERR
"megasas: Command pool empty!\n");
223 spin_unlock_irqrestore(&instance
->mfi_pool_lock
, flags
);
228 * __megasas_return_cmd - Return a cmd to free command pool
229 * @instance: Adapter soft state
230 * @cmd: Command packet to be returned to free command pool
233 __megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
236 * Don't go ahead and free the MFI frame, if corresponding
237 * MPT frame is not freed(valid for only fusion adapters).
238 * In case of MFI adapters, anyways for any allocated MFI
239 * frame will have cmd->mfi_mpt_mpthr set to MFI_MPT_DETACHED
241 if (atomic_read(&cmd
->mfi_mpt_pthr
) != MFI_MPT_DETACHED
)
245 cmd
->frame_count
= 0;
246 cmd
->is_wait_event
= 0;
247 cmd
->mpt_pthr_cmd_blocked
= NULL
;
249 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
250 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
251 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
253 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
255 atomic_set(&cmd
->mfi_mpt_pthr
, MFI_LIST_ADDED
);
256 list_add(&cmd
->list
, (&instance
->cmd_pool
)->next
);
260 * megasas_return_cmd - Return a cmd to free command pool
261 * @instance: Adapter soft state
262 * @cmd: Command packet to be returned to free command pool
265 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
269 spin_lock_irqsave(&instance
->mfi_pool_lock
, flags
);
270 __megasas_return_cmd(instance
, cmd
);
271 spin_unlock_irqrestore(&instance
->mfi_pool_lock
, flags
);
276 * The following functions are defined for xscale
277 * (deviceid : 1064R, PERC5) controllers
281 * megasas_enable_intr_xscale - Enables interrupts
282 * @regs: MFI register set
285 megasas_enable_intr_xscale(struct megasas_instance
*instance
)
287 struct megasas_register_set __iomem
*regs
;
288 regs
= instance
->reg_set
;
289 writel(0, &(regs
)->outbound_intr_mask
);
291 /* Dummy readl to force pci flush */
292 readl(®s
->outbound_intr_mask
);
296 * megasas_disable_intr_xscale -Disables interrupt
297 * @regs: MFI register set
300 megasas_disable_intr_xscale(struct megasas_instance
*instance
)
302 struct megasas_register_set __iomem
*regs
;
304 regs
= instance
->reg_set
;
305 writel(mask
, ®s
->outbound_intr_mask
);
306 /* Dummy readl to force pci flush */
307 readl(®s
->outbound_intr_mask
);
311 * megasas_read_fw_status_reg_xscale - returns the current FW status value
312 * @regs: MFI register set
315 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
317 return readl(&(regs
)->outbound_msg_0
);
320 * megasas_clear_interrupt_xscale - Check & clear interrupt
321 * @regs: MFI register set
324 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
329 * Check if it is our interrupt
331 status
= readl(®s
->outbound_intr_status
);
333 if (status
& MFI_OB_INTR_STATUS_MASK
)
334 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
335 if (status
& MFI_XSCALE_OMR0_CHANGE_INTERRUPT
)
336 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
339 * Clear the interrupt by writing back the same value
342 writel(status
, ®s
->outbound_intr_status
);
344 /* Dummy readl to force pci flush */
345 readl(®s
->outbound_intr_status
);
351 * megasas_fire_cmd_xscale - Sends command to the FW
352 * @frame_phys_addr : Physical address of cmd
353 * @frame_count : Number of frames for the command
354 * @regs : MFI register set
357 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
358 dma_addr_t frame_phys_addr
,
360 struct megasas_register_set __iomem
*regs
)
363 spin_lock_irqsave(&instance
->hba_lock
, flags
);
364 writel((frame_phys_addr
>> 3)|(frame_count
),
365 &(regs
)->inbound_queue_port
);
366 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
370 * megasas_adp_reset_xscale - For controller reset
371 * @regs: MFI register set
374 megasas_adp_reset_xscale(struct megasas_instance
*instance
,
375 struct megasas_register_set __iomem
*regs
)
379 writel(MFI_ADP_RESET
, ®s
->inbound_doorbell
);
381 for (i
= 0; i
< 3; i
++)
382 msleep(1000); /* sleep for 3 secs */
384 pci_read_config_dword(instance
->pdev
, MFI_1068_PCSR_OFFSET
, &pcidata
);
385 printk(KERN_NOTICE
"pcidata = %x\n", pcidata
);
387 printk(KERN_NOTICE
"mfi 1068 offset read=%x\n", pcidata
);
389 pci_write_config_dword(instance
->pdev
,
390 MFI_1068_PCSR_OFFSET
, pcidata
);
392 for (i
= 0; i
< 2; i
++)
393 msleep(1000); /* need to wait 2 secs again */
396 pci_read_config_dword(instance
->pdev
,
397 MFI_1068_FW_HANDSHAKE_OFFSET
, &pcidata
);
398 printk(KERN_NOTICE
"1068 offset handshake read=%x\n", pcidata
);
399 if ((pcidata
& 0xffff0000) == MFI_1068_FW_READY
) {
400 printk(KERN_NOTICE
"1068 offset pcidt=%x\n", pcidata
);
402 pci_write_config_dword(instance
->pdev
,
403 MFI_1068_FW_HANDSHAKE_OFFSET
, pcidata
);
410 * megasas_check_reset_xscale - For controller reset check
411 * @regs: MFI register set
414 megasas_check_reset_xscale(struct megasas_instance
*instance
,
415 struct megasas_register_set __iomem
*regs
)
418 if ((instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) &&
419 (le32_to_cpu(*instance
->consumer
) ==
420 MEGASAS_ADPRESET_INPROG_SIGN
))
425 static struct megasas_instance_template megasas_instance_template_xscale
= {
427 .fire_cmd
= megasas_fire_cmd_xscale
,
428 .enable_intr
= megasas_enable_intr_xscale
,
429 .disable_intr
= megasas_disable_intr_xscale
,
430 .clear_intr
= megasas_clear_intr_xscale
,
431 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
432 .adp_reset
= megasas_adp_reset_xscale
,
433 .check_reset
= megasas_check_reset_xscale
,
434 .service_isr
= megasas_isr
,
435 .tasklet
= megasas_complete_cmd_dpc
,
436 .init_adapter
= megasas_init_adapter_mfi
,
437 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
438 .issue_dcmd
= megasas_issue_dcmd
,
442 * This is the end of set of functions & definitions specific
443 * to xscale (deviceid : 1064R, PERC5) controllers
447 * The following functions are defined for ppc (deviceid : 0x60)
452 * megasas_enable_intr_ppc - Enables interrupts
453 * @regs: MFI register set
456 megasas_enable_intr_ppc(struct megasas_instance
*instance
)
458 struct megasas_register_set __iomem
*regs
;
459 regs
= instance
->reg_set
;
460 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
462 writel(~0x80000000, &(regs
)->outbound_intr_mask
);
464 /* Dummy readl to force pci flush */
465 readl(®s
->outbound_intr_mask
);
469 * megasas_disable_intr_ppc - Disable interrupt
470 * @regs: MFI register set
473 megasas_disable_intr_ppc(struct megasas_instance
*instance
)
475 struct megasas_register_set __iomem
*regs
;
476 u32 mask
= 0xFFFFFFFF;
477 regs
= instance
->reg_set
;
478 writel(mask
, ®s
->outbound_intr_mask
);
479 /* Dummy readl to force pci flush */
480 readl(®s
->outbound_intr_mask
);
484 * megasas_read_fw_status_reg_ppc - returns the current FW status value
485 * @regs: MFI register set
488 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
490 return readl(&(regs
)->outbound_scratch_pad
);
494 * megasas_clear_interrupt_ppc - Check & clear interrupt
495 * @regs: MFI register set
498 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
500 u32 status
, mfiStatus
= 0;
503 * Check if it is our interrupt
505 status
= readl(®s
->outbound_intr_status
);
507 if (status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)
508 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
510 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
)
511 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
514 * Clear the interrupt by writing back the same value
516 writel(status
, ®s
->outbound_doorbell_clear
);
518 /* Dummy readl to force pci flush */
519 readl(®s
->outbound_doorbell_clear
);
525 * megasas_fire_cmd_ppc - Sends command to the FW
526 * @frame_phys_addr : Physical address of cmd
527 * @frame_count : Number of frames for the command
528 * @regs : MFI register set
531 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
532 dma_addr_t frame_phys_addr
,
534 struct megasas_register_set __iomem
*regs
)
537 spin_lock_irqsave(&instance
->hba_lock
, flags
);
538 writel((frame_phys_addr
| (frame_count
<<1))|1,
539 &(regs
)->inbound_queue_port
);
540 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
544 * megasas_check_reset_ppc - For controller reset check
545 * @regs: MFI register set
548 megasas_check_reset_ppc(struct megasas_instance
*instance
,
549 struct megasas_register_set __iomem
*regs
)
551 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
557 static struct megasas_instance_template megasas_instance_template_ppc
= {
559 .fire_cmd
= megasas_fire_cmd_ppc
,
560 .enable_intr
= megasas_enable_intr_ppc
,
561 .disable_intr
= megasas_disable_intr_ppc
,
562 .clear_intr
= megasas_clear_intr_ppc
,
563 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
564 .adp_reset
= megasas_adp_reset_xscale
,
565 .check_reset
= megasas_check_reset_ppc
,
566 .service_isr
= megasas_isr
,
567 .tasklet
= megasas_complete_cmd_dpc
,
568 .init_adapter
= megasas_init_adapter_mfi
,
569 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
570 .issue_dcmd
= megasas_issue_dcmd
,
574 * megasas_enable_intr_skinny - Enables interrupts
575 * @regs: MFI register set
578 megasas_enable_intr_skinny(struct megasas_instance
*instance
)
580 struct megasas_register_set __iomem
*regs
;
581 regs
= instance
->reg_set
;
582 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
584 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
586 /* Dummy readl to force pci flush */
587 readl(®s
->outbound_intr_mask
);
591 * megasas_disable_intr_skinny - Disables interrupt
592 * @regs: MFI register set
595 megasas_disable_intr_skinny(struct megasas_instance
*instance
)
597 struct megasas_register_set __iomem
*regs
;
598 u32 mask
= 0xFFFFFFFF;
599 regs
= instance
->reg_set
;
600 writel(mask
, ®s
->outbound_intr_mask
);
601 /* Dummy readl to force pci flush */
602 readl(®s
->outbound_intr_mask
);
606 * megasas_read_fw_status_reg_skinny - returns the current FW status value
607 * @regs: MFI register set
610 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
612 return readl(&(regs
)->outbound_scratch_pad
);
616 * megasas_clear_interrupt_skinny - Check & clear interrupt
617 * @regs: MFI register set
620 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
626 * Check if it is our interrupt
628 status
= readl(®s
->outbound_intr_status
);
630 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
635 * Check if it is our interrupt
637 if ((megasas_read_fw_status_reg_skinny(regs
) & MFI_STATE_MASK
) ==
639 mfiStatus
= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
641 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
644 * Clear the interrupt by writing back the same value
646 writel(status
, ®s
->outbound_intr_status
);
649 * dummy read to flush PCI
651 readl(®s
->outbound_intr_status
);
657 * megasas_fire_cmd_skinny - Sends command to the FW
658 * @frame_phys_addr : Physical address of cmd
659 * @frame_count : Number of frames for the command
660 * @regs : MFI register set
663 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
664 dma_addr_t frame_phys_addr
,
666 struct megasas_register_set __iomem
*regs
)
669 spin_lock_irqsave(&instance
->hba_lock
, flags
);
670 writel(upper_32_bits(frame_phys_addr
),
671 &(regs
)->inbound_high_queue_port
);
672 writel((lower_32_bits(frame_phys_addr
) | (frame_count
<<1))|1,
673 &(regs
)->inbound_low_queue_port
);
674 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
678 * megasas_check_reset_skinny - For controller reset check
679 * @regs: MFI register set
682 megasas_check_reset_skinny(struct megasas_instance
*instance
,
683 struct megasas_register_set __iomem
*regs
)
685 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
691 static struct megasas_instance_template megasas_instance_template_skinny
= {
693 .fire_cmd
= megasas_fire_cmd_skinny
,
694 .enable_intr
= megasas_enable_intr_skinny
,
695 .disable_intr
= megasas_disable_intr_skinny
,
696 .clear_intr
= megasas_clear_intr_skinny
,
697 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
698 .adp_reset
= megasas_adp_reset_gen2
,
699 .check_reset
= megasas_check_reset_skinny
,
700 .service_isr
= megasas_isr
,
701 .tasklet
= megasas_complete_cmd_dpc
,
702 .init_adapter
= megasas_init_adapter_mfi
,
703 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
704 .issue_dcmd
= megasas_issue_dcmd
,
709 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
714 * megasas_enable_intr_gen2 - Enables interrupts
715 * @regs: MFI register set
718 megasas_enable_intr_gen2(struct megasas_instance
*instance
)
720 struct megasas_register_set __iomem
*regs
;
721 regs
= instance
->reg_set
;
722 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
724 /* write ~0x00000005 (4 & 1) to the intr mask*/
725 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
727 /* Dummy readl to force pci flush */
728 readl(®s
->outbound_intr_mask
);
732 * megasas_disable_intr_gen2 - Disables interrupt
733 * @regs: MFI register set
736 megasas_disable_intr_gen2(struct megasas_instance
*instance
)
738 struct megasas_register_set __iomem
*regs
;
739 u32 mask
= 0xFFFFFFFF;
740 regs
= instance
->reg_set
;
741 writel(mask
, ®s
->outbound_intr_mask
);
742 /* Dummy readl to force pci flush */
743 readl(®s
->outbound_intr_mask
);
747 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
748 * @regs: MFI register set
751 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
753 return readl(&(regs
)->outbound_scratch_pad
);
757 * megasas_clear_interrupt_gen2 - Check & clear interrupt
758 * @regs: MFI register set
761 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
766 * Check if it is our interrupt
768 status
= readl(®s
->outbound_intr_status
);
770 if (status
& MFI_INTR_FLAG_REPLY_MESSAGE
) {
771 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
773 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
) {
774 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
778 * Clear the interrupt by writing back the same value
781 writel(status
, ®s
->outbound_doorbell_clear
);
783 /* Dummy readl to force pci flush */
784 readl(®s
->outbound_intr_status
);
789 * megasas_fire_cmd_gen2 - Sends command to the FW
790 * @frame_phys_addr : Physical address of cmd
791 * @frame_count : Number of frames for the command
792 * @regs : MFI register set
795 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
796 dma_addr_t frame_phys_addr
,
798 struct megasas_register_set __iomem
*regs
)
801 spin_lock_irqsave(&instance
->hba_lock
, flags
);
802 writel((frame_phys_addr
| (frame_count
<<1))|1,
803 &(regs
)->inbound_queue_port
);
804 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
808 * megasas_adp_reset_gen2 - For controller reset
809 * @regs: MFI register set
812 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
813 struct megasas_register_set __iomem
*reg_set
)
817 u32
*seq_offset
= ®_set
->seq_offset
;
818 u32
*hostdiag_offset
= ®_set
->host_diag
;
820 if (instance
->instancet
== &megasas_instance_template_skinny
) {
821 seq_offset
= ®_set
->fusion_seq_offset
;
822 hostdiag_offset
= ®_set
->fusion_host_diag
;
825 writel(0, seq_offset
);
826 writel(4, seq_offset
);
827 writel(0xb, seq_offset
);
828 writel(2, seq_offset
);
829 writel(7, seq_offset
);
830 writel(0xd, seq_offset
);
834 HostDiag
= (u32
)readl(hostdiag_offset
);
836 while ( !( HostDiag
& DIAG_WRITE_ENABLE
) ) {
838 HostDiag
= (u32
)readl(hostdiag_offset
);
839 printk(KERN_NOTICE
"RESETGEN2: retry=%x, hostdiag=%x\n",
847 printk(KERN_NOTICE
"ADP_RESET_GEN2: HostDiag=%x\n", HostDiag
);
849 writel((HostDiag
| DIAG_RESET_ADAPTER
), hostdiag_offset
);
853 HostDiag
= (u32
)readl(hostdiag_offset
);
854 while ( ( HostDiag
& DIAG_RESET_ADAPTER
) ) {
856 HostDiag
= (u32
)readl(hostdiag_offset
);
857 printk(KERN_NOTICE
"RESET_GEN2: retry=%x, hostdiag=%x\n",
868 * megasas_check_reset_gen2 - For controller reset check
869 * @regs: MFI register set
872 megasas_check_reset_gen2(struct megasas_instance
*instance
,
873 struct megasas_register_set __iomem
*regs
)
875 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
882 static struct megasas_instance_template megasas_instance_template_gen2
= {
884 .fire_cmd
= megasas_fire_cmd_gen2
,
885 .enable_intr
= megasas_enable_intr_gen2
,
886 .disable_intr
= megasas_disable_intr_gen2
,
887 .clear_intr
= megasas_clear_intr_gen2
,
888 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
889 .adp_reset
= megasas_adp_reset_gen2
,
890 .check_reset
= megasas_check_reset_gen2
,
891 .service_isr
= megasas_isr
,
892 .tasklet
= megasas_complete_cmd_dpc
,
893 .init_adapter
= megasas_init_adapter_mfi
,
894 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
895 .issue_dcmd
= megasas_issue_dcmd
,
899 * This is the end of set of functions & definitions
900 * specific to gen2 (deviceid : 0x78, 0x79) controllers
904 * Template added for TB (Fusion)
906 extern struct megasas_instance_template megasas_instance_template_fusion
;
909 * megasas_issue_polled - Issues a polling command
910 * @instance: Adapter soft state
911 * @cmd: Command packet to be issued
913 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
916 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
920 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
922 frame_hdr
->cmd_status
= MFI_CMD_STATUS_POLL_MODE
;
923 frame_hdr
->flags
|= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
);
926 * Issue the frame using inbound queue port
928 instance
->instancet
->issue_dcmd(instance
, cmd
);
931 * Wait for cmd_status to change
933 if (instance
->requestorId
)
934 seconds
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
936 seconds
= MFI_POLL_TIMEOUT_SECS
;
937 return wait_and_poll(instance
, cmd
, seconds
);
941 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
942 * @instance: Adapter soft state
943 * @cmd: Command to be issued
944 * @timeout: Timeout in seconds
946 * This function waits on an event for the command to be returned from ISR.
947 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
948 * Used to issue ioctl commands.
951 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
952 struct megasas_cmd
*cmd
, int timeout
)
955 cmd
->cmd_status
= ENODATA
;
957 cmd
->is_wait_event
= 1;
958 instance
->instancet
->issue_dcmd(instance
, cmd
);
960 ret
= wait_event_timeout(instance
->int_cmd_wait_q
,
961 cmd
->cmd_status
!= ENODATA
, timeout
* HZ
);
965 wait_event(instance
->int_cmd_wait_q
,
966 cmd
->cmd_status
!= ENODATA
);
972 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
973 * @instance: Adapter soft state
974 * @cmd_to_abort: Previously issued cmd to be aborted
975 * @timeout: Timeout in seconds
977 * MFI firmware can abort previously issued AEN comamnd (automatic event
978 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
979 * cmd and waits for return status.
980 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
983 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
984 struct megasas_cmd
*cmd_to_abort
, int timeout
)
986 struct megasas_cmd
*cmd
;
987 struct megasas_abort_frame
*abort_fr
;
990 cmd
= megasas_get_cmd(instance
);
995 abort_fr
= &cmd
->frame
->abort
;
998 * Prepare and issue the abort frame
1000 abort_fr
->cmd
= MFI_CMD_ABORT
;
1001 abort_fr
->cmd_status
= 0xFF;
1002 abort_fr
->flags
= cpu_to_le16(0);
1003 abort_fr
->abort_context
= cpu_to_le32(cmd_to_abort
->index
);
1004 abort_fr
->abort_mfi_phys_addr_lo
=
1005 cpu_to_le32(lower_32_bits(cmd_to_abort
->frame_phys_addr
));
1006 abort_fr
->abort_mfi_phys_addr_hi
=
1007 cpu_to_le32(upper_32_bits(cmd_to_abort
->frame_phys_addr
));
1010 cmd
->cmd_status
= ENODATA
;
1012 instance
->instancet
->issue_dcmd(instance
, cmd
);
1015 ret
= wait_event_timeout(instance
->abort_cmd_wait_q
,
1016 cmd
->cmd_status
!= ENODATA
, timeout
* HZ
);
1018 dev_err(&instance
->pdev
->dev
, "Command timedout"
1019 "from %s\n", __func__
);
1023 wait_event(instance
->abort_cmd_wait_q
,
1024 cmd
->cmd_status
!= ENODATA
);
1028 megasas_return_cmd(instance
, cmd
);
1033 * megasas_make_sgl32 - Prepares 32-bit SGL
1034 * @instance: Adapter soft state
1035 * @scp: SCSI command from the mid-layer
1036 * @mfi_sgl: SGL to be filled in
1038 * If successful, this function returns the number of SG elements. Otherwise,
1042 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1043 union megasas_sgl
*mfi_sgl
)
1047 struct scatterlist
*os_sgl
;
1049 sge_count
= scsi_dma_map(scp
);
1050 BUG_ON(sge_count
< 0);
1053 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1054 mfi_sgl
->sge32
[i
].length
= cpu_to_le32(sg_dma_len(os_sgl
));
1055 mfi_sgl
->sge32
[i
].phys_addr
= cpu_to_le32(sg_dma_address(os_sgl
));
1062 * megasas_make_sgl64 - Prepares 64-bit SGL
1063 * @instance: Adapter soft state
1064 * @scp: SCSI command from the mid-layer
1065 * @mfi_sgl: SGL to be filled in
1067 * If successful, this function returns the number of SG elements. Otherwise,
1071 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1072 union megasas_sgl
*mfi_sgl
)
1076 struct scatterlist
*os_sgl
;
1078 sge_count
= scsi_dma_map(scp
);
1079 BUG_ON(sge_count
< 0);
1082 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1083 mfi_sgl
->sge64
[i
].length
= cpu_to_le32(sg_dma_len(os_sgl
));
1084 mfi_sgl
->sge64
[i
].phys_addr
= cpu_to_le64(sg_dma_address(os_sgl
));
1091 * megasas_make_sgl_skinny - Prepares IEEE SGL
1092 * @instance: Adapter soft state
1093 * @scp: SCSI command from the mid-layer
1094 * @mfi_sgl: SGL to be filled in
1096 * If successful, this function returns the number of SG elements. Otherwise,
1100 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
1101 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
1105 struct scatterlist
*os_sgl
;
1107 sge_count
= scsi_dma_map(scp
);
1110 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1111 mfi_sgl
->sge_skinny
[i
].length
=
1112 cpu_to_le32(sg_dma_len(os_sgl
));
1113 mfi_sgl
->sge_skinny
[i
].phys_addr
=
1114 cpu_to_le64(sg_dma_address(os_sgl
));
1115 mfi_sgl
->sge_skinny
[i
].flag
= cpu_to_le32(0);
1122 * megasas_get_frame_count - Computes the number of frames
1123 * @frame_type : type of frame- io or pthru frame
1124 * @sge_count : number of sg elements
1126 * Returns the number of frames required for numnber of sge's (sge_count)
1129 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
1130 u8 sge_count
, u8 frame_type
)
1137 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1138 sizeof(struct megasas_sge32
);
1140 if (instance
->flag_ieee
) {
1141 sge_sz
= sizeof(struct megasas_sge_skinny
);
1145 * Main frame can contain 2 SGEs for 64-bit SGLs and
1146 * 3 SGEs for 32-bit SGLs for ldio &
1147 * 1 SGEs for 64-bit SGLs and
1148 * 2 SGEs for 32-bit SGLs for pthru frame
1150 if (unlikely(frame_type
== PTHRU_FRAME
)) {
1151 if (instance
->flag_ieee
== 1) {
1152 num_cnt
= sge_count
- 1;
1153 } else if (IS_DMA64
)
1154 num_cnt
= sge_count
- 1;
1156 num_cnt
= sge_count
- 2;
1158 if (instance
->flag_ieee
== 1) {
1159 num_cnt
= sge_count
- 1;
1160 } else if (IS_DMA64
)
1161 num_cnt
= sge_count
- 2;
1163 num_cnt
= sge_count
- 3;
1167 sge_bytes
= sge_sz
* num_cnt
;
1169 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
1170 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
1175 if (frame_count
> 7)
1181 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1182 * @instance: Adapter soft state
1183 * @scp: SCSI command
1184 * @cmd: Command to be prepared in
1186 * This function prepares CDB commands. These are typcially pass-through
1187 * commands to the devices.
1190 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1191 struct megasas_cmd
*cmd
)
1196 struct megasas_pthru_frame
*pthru
;
1198 is_logical
= MEGASAS_IS_LOGICAL(scp
);
1199 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1200 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
1202 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1203 flags
= MFI_FRAME_DIR_WRITE
;
1204 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1205 flags
= MFI_FRAME_DIR_READ
;
1206 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
1207 flags
= MFI_FRAME_DIR_NONE
;
1209 if (instance
->flag_ieee
== 1) {
1210 flags
|= MFI_FRAME_IEEE
;
1214 * Prepare the DCDB frame
1216 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
1217 pthru
->cmd_status
= 0x0;
1218 pthru
->scsi_status
= 0x0;
1219 pthru
->target_id
= device_id
;
1220 pthru
->lun
= scp
->device
->lun
;
1221 pthru
->cdb_len
= scp
->cmd_len
;
1224 pthru
->flags
= cpu_to_le16(flags
);
1225 pthru
->data_xfer_len
= cpu_to_le32(scsi_bufflen(scp
));
1227 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
1230 * If the command is for the tape device, set the
1231 * pthru timeout to the os layer timeout value.
1233 if (scp
->device
->type
== TYPE_TAPE
) {
1234 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
1235 pthru
->timeout
= 0xFFFF;
1237 pthru
->timeout
= cpu_to_le16(scp
->request
->timeout
/ HZ
);
1243 if (instance
->flag_ieee
== 1) {
1244 pthru
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1245 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1247 } else if (IS_DMA64
) {
1248 pthru
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1249 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
1252 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
1255 if (pthru
->sge_count
> instance
->max_num_sge
) {
1256 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
1262 * Sense info specific
1264 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1265 pthru
->sense_buf_phys_addr_hi
=
1266 cpu_to_le32(upper_32_bits(cmd
->sense_phys_addr
));
1267 pthru
->sense_buf_phys_addr_lo
=
1268 cpu_to_le32(lower_32_bits(cmd
->sense_phys_addr
));
1271 * Compute the total number of frames this command consumes. FW uses
1272 * this number to pull sufficient number of frames from host memory.
1274 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
1277 return cmd
->frame_count
;
1281 * megasas_build_ldio - Prepares IOs to logical devices
1282 * @instance: Adapter soft state
1283 * @scp: SCSI command
1284 * @cmd: Command to be prepared
1286 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1289 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1290 struct megasas_cmd
*cmd
)
1293 u8 sc
= scp
->cmnd
[0];
1295 struct megasas_io_frame
*ldio
;
1297 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1298 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1300 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1301 flags
= MFI_FRAME_DIR_WRITE
;
1302 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1303 flags
= MFI_FRAME_DIR_READ
;
1305 if (instance
->flag_ieee
== 1) {
1306 flags
|= MFI_FRAME_IEEE
;
1310 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1312 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
1313 ldio
->cmd_status
= 0x0;
1314 ldio
->scsi_status
= 0x0;
1315 ldio
->target_id
= device_id
;
1317 ldio
->reserved_0
= 0;
1319 ldio
->flags
= cpu_to_le16(flags
);
1320 ldio
->start_lba_hi
= 0;
1321 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
1324 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1326 if (scp
->cmd_len
== 6) {
1327 ldio
->lba_count
= cpu_to_le32((u32
) scp
->cmnd
[4]);
1328 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[1] << 16) |
1329 ((u32
) scp
->cmnd
[2] << 8) |
1330 (u32
) scp
->cmnd
[3]);
1332 ldio
->start_lba_lo
&= cpu_to_le32(0x1FFFFF);
1336 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1338 else if (scp
->cmd_len
== 10) {
1339 ldio
->lba_count
= cpu_to_le32((u32
) scp
->cmnd
[8] |
1340 ((u32
) scp
->cmnd
[7] << 8));
1341 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1342 ((u32
) scp
->cmnd
[3] << 16) |
1343 ((u32
) scp
->cmnd
[4] << 8) |
1344 (u32
) scp
->cmnd
[5]);
1348 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1350 else if (scp
->cmd_len
== 12) {
1351 ldio
->lba_count
= cpu_to_le32(((u32
) scp
->cmnd
[6] << 24) |
1352 ((u32
) scp
->cmnd
[7] << 16) |
1353 ((u32
) scp
->cmnd
[8] << 8) |
1354 (u32
) scp
->cmnd
[9]);
1356 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1357 ((u32
) scp
->cmnd
[3] << 16) |
1358 ((u32
) scp
->cmnd
[4] << 8) |
1359 (u32
) scp
->cmnd
[5]);
1363 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1365 else if (scp
->cmd_len
== 16) {
1366 ldio
->lba_count
= cpu_to_le32(((u32
) scp
->cmnd
[10] << 24) |
1367 ((u32
) scp
->cmnd
[11] << 16) |
1368 ((u32
) scp
->cmnd
[12] << 8) |
1369 (u32
) scp
->cmnd
[13]);
1371 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[6] << 24) |
1372 ((u32
) scp
->cmnd
[7] << 16) |
1373 ((u32
) scp
->cmnd
[8] << 8) |
1374 (u32
) scp
->cmnd
[9]);
1376 ldio
->start_lba_hi
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1377 ((u32
) scp
->cmnd
[3] << 16) |
1378 ((u32
) scp
->cmnd
[4] << 8) |
1379 (u32
) scp
->cmnd
[5]);
1386 if (instance
->flag_ieee
) {
1387 ldio
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1388 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1390 } else if (IS_DMA64
) {
1391 ldio
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1392 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1394 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1396 if (ldio
->sge_count
> instance
->max_num_sge
) {
1397 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1403 * Sense info specific
1405 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1406 ldio
->sense_buf_phys_addr_hi
= 0;
1407 ldio
->sense_buf_phys_addr_lo
= cpu_to_le32(cmd
->sense_phys_addr
);
1410 * Compute the total number of frames this command consumes. FW uses
1411 * this number to pull sufficient number of frames from host memory.
1413 cmd
->frame_count
= megasas_get_frame_count(instance
,
1414 ldio
->sge_count
, IO_FRAME
);
1416 return cmd
->frame_count
;
1420 * megasas_is_ldio - Checks if the cmd is for logical drive
1421 * @scmd: SCSI command
1423 * Called by megasas_queue_command to find out if the command to be queued
1424 * is a logical drive command
1426 inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1428 if (!MEGASAS_IS_LOGICAL(cmd
))
1430 switch (cmd
->cmnd
[0]) {
1446 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1448 * @instance: Adapter soft state
1451 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1453 struct megasas_cmd
*cmd
;
1455 union megasas_sgl
*mfi_sgl
;
1456 struct megasas_io_frame
*ldio
;
1457 struct megasas_pthru_frame
*pthru
;
1459 u32 max_cmd
= instance
->max_fw_cmds
;
1461 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1462 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1464 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1466 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1468 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1469 for (i
= 0; i
< max_cmd
; i
++) {
1470 cmd
= instance
->cmd_list
[i
];
1473 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1474 if (megasas_is_ldio(cmd
->scmd
)){
1475 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1476 mfi_sgl
= &ldio
->sgl
;
1477 sgcount
= ldio
->sge_count
;
1478 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1479 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1480 instance
->host
->host_no
, cmd
->frame_count
, ldio
->cmd
, ldio
->target_id
,
1481 le32_to_cpu(ldio
->start_lba_lo
), le32_to_cpu(ldio
->start_lba_hi
),
1482 le32_to_cpu(ldio
->sense_buf_phys_addr_lo
), sgcount
);
1485 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1486 mfi_sgl
= &pthru
->sgl
;
1487 sgcount
= pthru
->sge_count
;
1488 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1489 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1490 instance
->host
->host_no
, cmd
->frame_count
, pthru
->cmd
, pthru
->target_id
,
1491 pthru
->lun
, pthru
->cdb_len
, le32_to_cpu(pthru
->data_xfer_len
),
1492 le32_to_cpu(pthru
->sense_buf_phys_addr_lo
), sgcount
);
1494 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1495 for (n
= 0; n
< sgcount
; n
++){
1497 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1498 le32_to_cpu(mfi_sgl
->sge64
[n
].length
),
1499 le64_to_cpu(mfi_sgl
->sge64
[n
].phys_addr
));
1501 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1502 le32_to_cpu(mfi_sgl
->sge32
[n
].length
),
1503 le32_to_cpu(mfi_sgl
->sge32
[n
].phys_addr
));
1506 printk(KERN_ERR
"\n");
1508 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1509 for (i
= 0; i
< max_cmd
; i
++) {
1511 cmd
= instance
->cmd_list
[i
];
1513 if(cmd
->sync_cmd
== 1){
1514 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1517 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1521 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
1522 struct scsi_cmnd
*scmd
)
1524 struct megasas_cmd
*cmd
;
1527 cmd
= megasas_get_cmd(instance
);
1529 return SCSI_MLQUEUE_HOST_BUSY
;
1532 * Logical drive command
1534 if (megasas_is_ldio(scmd
))
1535 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1537 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1540 goto out_return_cmd
;
1543 scmd
->SCp
.ptr
= (char *)cmd
;
1546 * Issue the command to the FW
1548 atomic_inc(&instance
->fw_outstanding
);
1550 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1551 cmd
->frame_count
-1, instance
->reg_set
);
1555 megasas_return_cmd(instance
, cmd
);
1561 * megasas_queue_command - Queue entry point
1562 * @scmd: SCSI command to be queued
1563 * @done: Callback entry point
1566 megasas_queue_command(struct Scsi_Host
*shost
, struct scsi_cmnd
*scmd
)
1568 struct megasas_instance
*instance
;
1569 unsigned long flags
;
1571 instance
= (struct megasas_instance
*)
1572 scmd
->device
->host
->hostdata
;
1574 if (instance
->unload
== 1) {
1575 scmd
->result
= DID_NO_CONNECT
<< 16;
1576 scmd
->scsi_done(scmd
);
1580 if (instance
->issuepend_done
== 0)
1581 return SCSI_MLQUEUE_HOST_BUSY
;
1583 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1585 /* Check for an mpio path and adjust behavior */
1586 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
1587 if (megasas_check_mpio_paths(instance
, scmd
) ==
1588 (DID_RESET
<< 16)) {
1589 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1590 return SCSI_MLQUEUE_HOST_BUSY
;
1592 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1593 scmd
->result
= DID_NO_CONNECT
<< 16;
1594 scmd
->scsi_done(scmd
);
1599 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1600 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1601 scmd
->result
= DID_NO_CONNECT
<< 16;
1602 scmd
->scsi_done(scmd
);
1606 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1607 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1608 return SCSI_MLQUEUE_HOST_BUSY
;
1611 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1615 if (MEGASAS_IS_LOGICAL(scmd
) &&
1616 (scmd
->device
->id
>= instance
->fw_supported_vd_count
||
1617 scmd
->device
->lun
)) {
1618 scmd
->result
= DID_BAD_TARGET
<< 16;
1622 switch (scmd
->cmnd
[0]) {
1623 case SYNCHRONIZE_CACHE
:
1625 * FW takes care of flush cache on its own
1626 * No need to send it down
1628 scmd
->result
= DID_OK
<< 16;
1634 if (instance
->instancet
->build_and_issue_cmd(instance
, scmd
)) {
1635 printk(KERN_ERR
"megasas: Err returned from build_and_issue_cmd\n");
1636 return SCSI_MLQUEUE_HOST_BUSY
;
1642 scmd
->scsi_done(scmd
);
1646 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1650 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1652 if ((megasas_mgmt_info
.instance
[i
]) &&
1653 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1654 return megasas_mgmt_info
.instance
[i
];
1660 static int megasas_slave_configure(struct scsi_device
*sdev
)
1663 * The RAID firmware may require extended timeouts.
1665 blk_queue_rq_timeout(sdev
->request_queue
,
1666 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1671 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1674 struct megasas_instance
*instance
;
1675 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1676 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) {
1678 * Open the OS scan to the SYSTEM PD
1681 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1683 if (instance
->pd_list
[pd_index
].driveState
==
1684 MR_PD_STATE_SYSTEM
) {
1692 void megaraid_sas_kill_hba(struct megasas_instance
*instance
)
1694 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1695 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
1696 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1697 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
1698 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1699 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1700 writel(MFI_STOP_ADP
, &instance
->reg_set
->doorbell
);
1702 readl(&instance
->reg_set
->doorbell
);
1703 if (instance
->mpio
&& instance
->requestorId
)
1704 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
1706 writel(MFI_STOP_ADP
, &instance
->reg_set
->inbound_doorbell
);
1711 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1712 * restored to max value
1713 * @instance: Adapter soft state
1717 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
)
1719 unsigned long flags
;
1720 if (instance
->flag
& MEGASAS_FW_BUSY
1721 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1722 && atomic_read(&instance
->fw_outstanding
) <
1723 instance
->throttlequeuedepth
+ 1) {
1725 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1726 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1727 if (instance
->is_imr
) {
1728 instance
->host
->can_queue
=
1729 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1731 instance
->host
->can_queue
=
1732 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1734 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1739 * megasas_complete_cmd_dpc - Returns FW's controller structure
1740 * @instance_addr: Address of adapter soft state
1742 * Tasklet to complete cmds
1744 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1749 struct megasas_cmd
*cmd
;
1750 struct megasas_instance
*instance
=
1751 (struct megasas_instance
*)instance_addr
;
1752 unsigned long flags
;
1754 /* If we have already declared adapter dead, donot complete cmds */
1755 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1758 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1760 producer
= le32_to_cpu(*instance
->producer
);
1761 consumer
= le32_to_cpu(*instance
->consumer
);
1763 while (consumer
!= producer
) {
1764 context
= le32_to_cpu(instance
->reply_queue
[consumer
]);
1765 if (context
>= instance
->max_fw_cmds
) {
1766 printk(KERN_ERR
"Unexpected context value %x\n",
1771 cmd
= instance
->cmd_list
[context
];
1773 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1776 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1781 *instance
->consumer
= cpu_to_le32(producer
);
1783 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1786 * Check if we can restore can_queue
1788 megasas_check_and_restore_queue_depth(instance
);
1792 * megasas_start_timer - Initializes a timer object
1793 * @instance: Adapter soft state
1794 * @timer: timer object to be initialized
1795 * @fn: timer function
1796 * @interval: time interval between timer function call
1799 void megasas_start_timer(struct megasas_instance
*instance
,
1800 struct timer_list
*timer
,
1801 void *fn
, unsigned long interval
)
1804 timer
->expires
= jiffies
+ interval
;
1805 timer
->data
= (unsigned long)instance
;
1806 timer
->function
= fn
;
1811 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
);
1814 process_fw_state_change_wq(struct work_struct
*work
);
1816 void megasas_do_ocr(struct megasas_instance
*instance
)
1818 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
1819 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
1820 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
1821 *instance
->consumer
= cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN
);
1823 instance
->instancet
->disable_intr(instance
);
1824 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
1825 instance
->issuepend_done
= 0;
1827 atomic_set(&instance
->fw_outstanding
, 0);
1828 megasas_internal_reset_defer_cmds(instance
);
1829 process_fw_state_change_wq(&instance
->work_init
);
1832 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance
*instance
,
1835 struct megasas_cmd
*cmd
;
1836 struct megasas_dcmd_frame
*dcmd
;
1837 struct MR_LD_VF_AFFILIATION_111
*new_affiliation_111
= NULL
;
1838 dma_addr_t new_affiliation_111_h
;
1842 cmd
= megasas_get_cmd(instance
);
1845 printk(KERN_DEBUG
"megasas: megasas_get_ld_vf_affiliation_111:"
1846 "Failed to get cmd for scsi%d.\n",
1847 instance
->host
->host_no
);
1851 dcmd
= &cmd
->frame
->dcmd
;
1853 if (!instance
->vf_affiliation_111
) {
1854 printk(KERN_WARNING
"megasas: SR-IOV: Couldn't get LD/VF "
1855 "affiliation for scsi%d.\n", instance
->host
->host_no
);
1856 megasas_return_cmd(instance
, cmd
);
1861 memset(instance
->vf_affiliation_111
, 0,
1862 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1864 new_affiliation_111
=
1865 pci_alloc_consistent(instance
->pdev
,
1866 sizeof(struct MR_LD_VF_AFFILIATION_111
),
1867 &new_affiliation_111_h
);
1868 if (!new_affiliation_111
) {
1869 printk(KERN_DEBUG
"megasas: SR-IOV: Couldn't allocate "
1870 "memory for new affiliation for scsi%d.\n",
1871 instance
->host
->host_no
);
1872 megasas_return_cmd(instance
, cmd
);
1875 memset(new_affiliation_111
, 0,
1876 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1879 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1881 dcmd
->cmd
= MFI_CMD_DCMD
;
1882 dcmd
->cmd_status
= 0xFF;
1883 dcmd
->sge_count
= 1;
1884 dcmd
->flags
= MFI_FRAME_DIR_BOTH
;
1887 dcmd
->data_xfer_len
= sizeof(struct MR_LD_VF_AFFILIATION_111
);
1888 dcmd
->opcode
= MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111
;
1891 dcmd
->sgl
.sge32
[0].phys_addr
=
1892 instance
->vf_affiliation_111_h
;
1894 dcmd
->sgl
.sge32
[0].phys_addr
= new_affiliation_111_h
;
1896 dcmd
->sgl
.sge32
[0].length
=
1897 sizeof(struct MR_LD_VF_AFFILIATION_111
);
1899 printk(KERN_WARNING
"megasas: SR-IOV: Getting LD/VF affiliation for "
1900 "scsi%d\n", instance
->host
->host_no
);
1902 megasas_issue_blocked_cmd(instance
, cmd
, 0);
1904 if (dcmd
->cmd_status
) {
1905 printk(KERN_WARNING
"megasas: SR-IOV: LD/VF affiliation DCMD"
1906 " failed with status 0x%x for scsi%d.\n",
1907 dcmd
->cmd_status
, instance
->host
->host_no
);
1908 retval
= 1; /* Do a scan if we couldn't get affiliation */
1913 thisVf
= new_affiliation_111
->thisVf
;
1914 for (ld
= 0 ; ld
< new_affiliation_111
->vdCount
; ld
++)
1915 if (instance
->vf_affiliation_111
->map
[ld
].policy
[thisVf
] !=
1916 new_affiliation_111
->map
[ld
].policy
[thisVf
]) {
1917 printk(KERN_WARNING
"megasas: SR-IOV: "
1918 "Got new LD/VF affiliation "
1920 instance
->host
->host_no
);
1921 memcpy(instance
->vf_affiliation_111
,
1922 new_affiliation_111
,
1923 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1929 if (new_affiliation_111
) {
1930 pci_free_consistent(instance
->pdev
,
1931 sizeof(struct MR_LD_VF_AFFILIATION_111
),
1932 new_affiliation_111
,
1933 new_affiliation_111_h
);
1936 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
1937 megasas_return_mfi_mpt_pthr(instance
, cmd
,
1938 cmd
->mpt_pthr_cmd_blocked
);
1940 megasas_return_cmd(instance
, cmd
);
1945 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance
*instance
,
1948 struct megasas_cmd
*cmd
;
1949 struct megasas_dcmd_frame
*dcmd
;
1950 struct MR_LD_VF_AFFILIATION
*new_affiliation
= NULL
;
1951 struct MR_LD_VF_MAP
*newmap
= NULL
, *savedmap
= NULL
;
1952 dma_addr_t new_affiliation_h
;
1953 int i
, j
, retval
= 0, found
= 0, doscan
= 0;
1956 cmd
= megasas_get_cmd(instance
);
1959 printk(KERN_DEBUG
"megasas: megasas_get_ld_vf_affiliation12: "
1960 "Failed to get cmd for scsi%d.\n",
1961 instance
->host
->host_no
);
1965 dcmd
= &cmd
->frame
->dcmd
;
1967 if (!instance
->vf_affiliation
) {
1968 printk(KERN_WARNING
"megasas: SR-IOV: Couldn't get LD/VF "
1969 "affiliation for scsi%d.\n", instance
->host
->host_no
);
1970 megasas_return_cmd(instance
, cmd
);
1975 memset(instance
->vf_affiliation
, 0, (MAX_LOGICAL_DRIVES
+ 1) *
1976 sizeof(struct MR_LD_VF_AFFILIATION
));
1979 pci_alloc_consistent(instance
->pdev
,
1980 (MAX_LOGICAL_DRIVES
+ 1) *
1981 sizeof(struct MR_LD_VF_AFFILIATION
),
1982 &new_affiliation_h
);
1983 if (!new_affiliation
) {
1984 printk(KERN_DEBUG
"megasas: SR-IOV: Couldn't allocate "
1985 "memory for new affiliation for scsi%d.\n",
1986 instance
->host
->host_no
);
1987 megasas_return_cmd(instance
, cmd
);
1990 memset(new_affiliation
, 0, (MAX_LOGICAL_DRIVES
+ 1) *
1991 sizeof(struct MR_LD_VF_AFFILIATION
));
1994 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1996 dcmd
->cmd
= MFI_CMD_DCMD
;
1997 dcmd
->cmd_status
= 0xFF;
1998 dcmd
->sge_count
= 1;
1999 dcmd
->flags
= MFI_FRAME_DIR_BOTH
;
2002 dcmd
->data_xfer_len
= (MAX_LOGICAL_DRIVES
+ 1) *
2003 sizeof(struct MR_LD_VF_AFFILIATION
);
2004 dcmd
->opcode
= MR_DCMD_LD_VF_MAP_GET_ALL_LDS
;
2007 dcmd
->sgl
.sge32
[0].phys_addr
= instance
->vf_affiliation_h
;
2009 dcmd
->sgl
.sge32
[0].phys_addr
= new_affiliation_h
;
2011 dcmd
->sgl
.sge32
[0].length
= (MAX_LOGICAL_DRIVES
+ 1) *
2012 sizeof(struct MR_LD_VF_AFFILIATION
);
2014 printk(KERN_WARNING
"megasas: SR-IOV: Getting LD/VF affiliation for "
2015 "scsi%d\n", instance
->host
->host_no
);
2017 megasas_issue_blocked_cmd(instance
, cmd
, 0);
2019 if (dcmd
->cmd_status
) {
2020 printk(KERN_WARNING
"megasas: SR-IOV: LD/VF affiliation DCMD"
2021 " failed with status 0x%x for scsi%d.\n",
2022 dcmd
->cmd_status
, instance
->host
->host_no
);
2023 retval
= 1; /* Do a scan if we couldn't get affiliation */
2028 if (!new_affiliation
->ldCount
) {
2029 printk(KERN_WARNING
"megasas: SR-IOV: Got new LD/VF "
2030 "affiliation for passive path for scsi%d.\n",
2031 instance
->host
->host_no
);
2035 newmap
= new_affiliation
->map
;
2036 savedmap
= instance
->vf_affiliation
->map
;
2037 thisVf
= new_affiliation
->thisVf
;
2038 for (i
= 0 ; i
< new_affiliation
->ldCount
; i
++) {
2040 for (j
= 0; j
< instance
->vf_affiliation
->ldCount
;
2042 if (newmap
->ref
.targetId
==
2043 savedmap
->ref
.targetId
) {
2045 if (newmap
->policy
[thisVf
] !=
2046 savedmap
->policy
[thisVf
]) {
2051 savedmap
= (struct MR_LD_VF_MAP
*)
2052 ((unsigned char *)savedmap
+
2055 if (!found
&& newmap
->policy
[thisVf
] !=
2056 MR_LD_ACCESS_HIDDEN
) {
2060 newmap
= (struct MR_LD_VF_MAP
*)
2061 ((unsigned char *)newmap
+ newmap
->size
);
2064 newmap
= new_affiliation
->map
;
2065 savedmap
= instance
->vf_affiliation
->map
;
2067 for (i
= 0 ; i
< instance
->vf_affiliation
->ldCount
; i
++) {
2069 for (j
= 0 ; j
< new_affiliation
->ldCount
; j
++) {
2070 if (savedmap
->ref
.targetId
==
2071 newmap
->ref
.targetId
) {
2073 if (savedmap
->policy
[thisVf
] !=
2074 newmap
->policy
[thisVf
]) {
2079 newmap
= (struct MR_LD_VF_MAP
*)
2080 ((unsigned char *)newmap
+
2083 if (!found
&& savedmap
->policy
[thisVf
] !=
2084 MR_LD_ACCESS_HIDDEN
) {
2088 savedmap
= (struct MR_LD_VF_MAP
*)
2089 ((unsigned char *)savedmap
+
2095 printk(KERN_WARNING
"megasas: SR-IOV: Got new LD/VF "
2096 "affiliation for scsi%d.\n", instance
->host
->host_no
);
2097 memcpy(instance
->vf_affiliation
, new_affiliation
,
2098 new_affiliation
->size
);
2102 if (new_affiliation
)
2103 pci_free_consistent(instance
->pdev
,
2104 (MAX_LOGICAL_DRIVES
+ 1) *
2105 sizeof(struct MR_LD_VF_AFFILIATION
),
2106 new_affiliation
, new_affiliation_h
);
2107 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
2108 megasas_return_mfi_mpt_pthr(instance
, cmd
,
2109 cmd
->mpt_pthr_cmd_blocked
);
2111 megasas_return_cmd(instance
, cmd
);
2116 /* This function will get the current SR-IOV LD/VF affiliation */
2117 static int megasas_get_ld_vf_affiliation(struct megasas_instance
*instance
,
2122 if (instance
->PlasmaFW111
)
2123 retval
= megasas_get_ld_vf_affiliation_111(instance
, initial
);
2125 retval
= megasas_get_ld_vf_affiliation_12(instance
, initial
);
2129 /* This function will tell FW to start the SR-IOV heartbeat */
2130 int megasas_sriov_start_heartbeat(struct megasas_instance
*instance
,
2133 struct megasas_cmd
*cmd
;
2134 struct megasas_dcmd_frame
*dcmd
;
2137 cmd
= megasas_get_cmd(instance
);
2140 printk(KERN_DEBUG
"megasas: megasas_sriov_start_heartbeat: "
2141 "Failed to get cmd for scsi%d.\n",
2142 instance
->host
->host_no
);
2146 dcmd
= &cmd
->frame
->dcmd
;
2149 instance
->hb_host_mem
=
2150 pci_zalloc_consistent(instance
->pdev
,
2151 sizeof(struct MR_CTRL_HB_HOST_MEM
),
2152 &instance
->hb_host_mem_h
);
2153 if (!instance
->hb_host_mem
) {
2154 printk(KERN_DEBUG
"megasas: SR-IOV: Couldn't allocate"
2155 " memory for heartbeat host memory for "
2156 "scsi%d.\n", instance
->host
->host_no
);
2162 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2164 dcmd
->mbox
.s
[0] = sizeof(struct MR_CTRL_HB_HOST_MEM
);
2165 dcmd
->cmd
= MFI_CMD_DCMD
;
2166 dcmd
->cmd_status
= 0xFF;
2167 dcmd
->sge_count
= 1;
2168 dcmd
->flags
= MFI_FRAME_DIR_BOTH
;
2171 dcmd
->data_xfer_len
= sizeof(struct MR_CTRL_HB_HOST_MEM
);
2172 dcmd
->opcode
= MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC
;
2173 dcmd
->sgl
.sge32
[0].phys_addr
= instance
->hb_host_mem_h
;
2174 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_CTRL_HB_HOST_MEM
);
2176 printk(KERN_WARNING
"megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2177 instance
->host
->host_no
);
2179 if (!megasas_issue_polled(instance
, cmd
)) {
2182 printk(KERN_WARNING
"megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2183 "_MEM_ALLOC DCMD timed out for scsi%d\n",
2184 instance
->host
->host_no
);
2190 if (dcmd
->cmd_status
) {
2191 printk(KERN_WARNING
"megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2192 "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2194 instance
->host
->host_no
);
2200 megasas_return_cmd(instance
, cmd
);
2205 /* Handler for SR-IOV heartbeat */
2206 void megasas_sriov_heartbeat_handler(unsigned long instance_addr
)
2208 struct megasas_instance
*instance
=
2209 (struct megasas_instance
*)instance_addr
;
2211 if (instance
->hb_host_mem
->HB
.fwCounter
!=
2212 instance
->hb_host_mem
->HB
.driverCounter
) {
2213 instance
->hb_host_mem
->HB
.driverCounter
=
2214 instance
->hb_host_mem
->HB
.fwCounter
;
2215 mod_timer(&instance
->sriov_heartbeat_timer
,
2216 jiffies
+ MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
2218 printk(KERN_WARNING
"megasas: SR-IOV: Heartbeat never "
2219 "completed for scsi%d\n", instance
->host
->host_no
);
2220 schedule_work(&instance
->work_init
);
2225 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2226 * @instance: Adapter soft state
2228 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2229 * complete all its outstanding commands. Returns error if one or more IOs
2230 * are pending after this time period. It also marks the controller dead.
2232 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
2236 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
2238 unsigned long flags
;
2239 struct list_head clist_local
;
2240 struct megasas_cmd
*reset_cmd
;
2242 u8 kill_adapter_flag
;
2244 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2245 adprecovery
= instance
->adprecovery
;
2246 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2248 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
2250 INIT_LIST_HEAD(&clist_local
);
2251 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2252 list_splice_init(&instance
->internal_reset_pending_q
,
2254 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2256 printk(KERN_NOTICE
"megasas: HBA reset wait ...\n");
2257 for (i
= 0; i
< wait_time
; i
++) {
2259 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2260 adprecovery
= instance
->adprecovery
;
2261 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2262 if (adprecovery
== MEGASAS_HBA_OPERATIONAL
)
2266 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
2267 printk(KERN_NOTICE
"megasas: reset: Stopping HBA.\n");
2268 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2269 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2270 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2275 while (!list_empty(&clist_local
)) {
2276 reset_cmd
= list_entry((&clist_local
)->next
,
2277 struct megasas_cmd
, list
);
2278 list_del_init(&reset_cmd
->list
);
2279 if (reset_cmd
->scmd
) {
2280 reset_cmd
->scmd
->result
= DID_RESET
<< 16;
2281 printk(KERN_NOTICE
"%d:%p reset [%02x]\n",
2282 reset_index
, reset_cmd
,
2283 reset_cmd
->scmd
->cmnd
[0]);
2285 reset_cmd
->scmd
->scsi_done(reset_cmd
->scmd
);
2286 megasas_return_cmd(instance
, reset_cmd
);
2287 } else if (reset_cmd
->sync_cmd
) {
2288 printk(KERN_NOTICE
"megasas:%p synch cmds"
2292 reset_cmd
->cmd_status
= ENODATA
;
2293 instance
->instancet
->fire_cmd(instance
,
2294 reset_cmd
->frame_phys_addr
,
2295 0, instance
->reg_set
);
2297 printk(KERN_NOTICE
"megasas: %p unexpected"
2307 for (i
= 0; i
< resetwaittime
; i
++) {
2309 int outstanding
= atomic_read(&instance
->fw_outstanding
);
2314 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
2315 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
2316 "commands to complete\n",i
,outstanding
);
2318 * Call cmd completion routine. Cmd to be
2319 * be completed directly without depending on isr.
2321 megasas_complete_cmd_dpc((unsigned long)instance
);
2328 kill_adapter_flag
= 0;
2330 fw_state
= instance
->instancet
->read_fw_status_reg(
2331 instance
->reg_set
) & MFI_STATE_MASK
;
2332 if ((fw_state
== MFI_STATE_FAULT
) &&
2333 (instance
->disableOnlineCtrlReset
== 0)) {
2335 kill_adapter_flag
= 2;
2338 megasas_do_ocr(instance
);
2339 kill_adapter_flag
= 1;
2341 /* wait for 1 secs to let FW finish the pending cmds */
2347 if (atomic_read(&instance
->fw_outstanding
) &&
2348 !kill_adapter_flag
) {
2349 if (instance
->disableOnlineCtrlReset
== 0) {
2351 megasas_do_ocr(instance
);
2353 /* wait for 5 secs to let FW finish the pending cmds */
2354 for (i
= 0; i
< wait_time
; i
++) {
2356 atomic_read(&instance
->fw_outstanding
);
2364 if (atomic_read(&instance
->fw_outstanding
) ||
2365 (kill_adapter_flag
== 2)) {
2366 printk(KERN_NOTICE
"megaraid_sas: pending cmds after reset\n");
2368 * Send signal to FW to stop processing any pending cmds.
2369 * The controller will be taken offline by the OS now.
2371 if ((instance
->pdev
->device
==
2372 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2373 (instance
->pdev
->device
==
2374 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
2375 writel(MFI_STOP_ADP
,
2376 &instance
->reg_set
->doorbell
);
2378 writel(MFI_STOP_ADP
,
2379 &instance
->reg_set
->inbound_doorbell
);
2381 megasas_dump_pending_frames(instance
);
2382 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2383 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2384 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2388 printk(KERN_NOTICE
"megaraid_sas: no pending cmds after reset\n");
2394 * megasas_generic_reset - Generic reset routine
2395 * @scmd: Mid-layer SCSI command
2397 * This routine implements a generic reset handler for device, bus and host
2398 * reset requests. Device, bus and host specific reset handlers can use this
2399 * function after they do their specific tasks.
2401 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
2404 struct megasas_instance
*instance
;
2406 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2408 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET cmd=%x retries=%x\n",
2409 scmd
->cmnd
[0], scmd
->retries
);
2411 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2412 printk(KERN_ERR
"megasas: cannot recover from previous reset "
2417 ret_val
= megasas_wait_for_outstanding(instance
);
2418 if (ret_val
== SUCCESS
)
2419 printk(KERN_NOTICE
"megasas: reset successful \n");
2421 printk(KERN_ERR
"megasas: failed to do reset\n");
2427 * megasas_reset_timer - quiesce the adapter if required
2430 * Sets the FW busy flag and reduces the host->can_queue if the
2431 * cmd has not been completed within the timeout period.
2434 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
2436 struct megasas_instance
*instance
;
2437 unsigned long flags
;
2439 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
2440 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
2441 return BLK_EH_NOT_HANDLED
;
2444 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2445 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
2446 /* FW is busy, throttle IO */
2447 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2449 instance
->host
->can_queue
= instance
->throttlequeuedepth
;
2450 instance
->last_time
= jiffies
;
2451 instance
->flag
|= MEGASAS_FW_BUSY
;
2453 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
2455 return BLK_EH_RESET_TIMER
;
2459 * megasas_reset_device - Device reset handler entry point
2461 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
2466 * First wait for all commands to complete
2468 ret
= megasas_generic_reset(scmd
);
2474 * megasas_reset_bus_host - Bus & host reset handler entry point
2476 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
2479 struct megasas_instance
*instance
;
2480 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2483 * First wait for all commands to complete
2485 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
2486 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
2487 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
2488 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
2489 ret
= megasas_reset_fusion(scmd
->device
->host
, 1);
2491 ret
= megasas_generic_reset(scmd
);
2497 * megasas_bios_param - Returns disk geometry for a disk
2498 * @sdev: device handle
2499 * @bdev: block device
2500 * @capacity: drive capacity
2501 * @geom: geometry parameters
2504 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
2505 sector_t capacity
, int geom
[])
2511 /* Default heads (64) & sectors (32) */
2515 tmp
= heads
* sectors
;
2516 cylinders
= capacity
;
2518 sector_div(cylinders
, tmp
);
2521 * Handle extended translation size for logical drives > 1Gb
2524 if (capacity
>= 0x200000) {
2527 tmp
= heads
*sectors
;
2528 cylinders
= capacity
;
2529 sector_div(cylinders
, tmp
);
2534 geom
[2] = cylinders
;
2539 static void megasas_aen_polling(struct work_struct
*work
);
2542 * megasas_service_aen - Processes an event notification
2543 * @instance: Adapter soft state
2544 * @cmd: AEN command completed by the ISR
2546 * For AEN, driver sends a command down to FW that is held by the FW till an
2547 * event occurs. When an event of interest occurs, FW completes the command
2548 * that it was previously holding.
2550 * This routines sends SIGIO signal to processes that have registered with the
2554 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2556 unsigned long flags
;
2558 * Don't signal app if it is just an aborted previously registered aen
2560 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
2561 spin_lock_irqsave(&poll_aen_lock
, flags
);
2562 megasas_poll_wait_aen
= 1;
2563 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2564 wake_up(&megasas_poll_wait
);
2565 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
2570 instance
->aen_cmd
= NULL
;
2572 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
2573 megasas_return_mfi_mpt_pthr(instance
, cmd
,
2574 cmd
->mpt_pthr_cmd_blocked
);
2576 megasas_return_cmd(instance
, cmd
);
2578 if ((instance
->unload
== 0) &&
2579 ((instance
->issuepend_done
== 1))) {
2580 struct megasas_aen_event
*ev
;
2581 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
2583 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
2585 ev
->instance
= instance
;
2587 INIT_DELAYED_WORK(&ev
->hotplug_work
,
2588 megasas_aen_polling
);
2589 schedule_delayed_work(&ev
->hotplug_work
, 0);
2595 megasas_fw_crash_buffer_store(struct device
*cdev
,
2596 struct device_attribute
*attr
, const char *buf
, size_t count
)
2598 struct Scsi_Host
*shost
= class_to_shost(cdev
);
2599 struct megasas_instance
*instance
=
2600 (struct megasas_instance
*) shost
->hostdata
;
2602 unsigned long flags
;
2604 if (kstrtoint(buf
, 0, &val
) != 0)
2607 spin_lock_irqsave(&instance
->crashdump_lock
, flags
);
2608 instance
->fw_crash_buffer_offset
= val
;
2609 spin_unlock_irqrestore(&instance
->crashdump_lock
, flags
);
2614 megasas_fw_crash_buffer_show(struct device
*cdev
,
2615 struct device_attribute
*attr
, char *buf
)
2617 struct Scsi_Host
*shost
= class_to_shost(cdev
);
2618 struct megasas_instance
*instance
=
2619 (struct megasas_instance
*) shost
->hostdata
;
2621 unsigned long buff_addr
;
2622 unsigned long dmachunk
= CRASH_DMA_BUF_SIZE
;
2623 unsigned long src_addr
;
2624 unsigned long flags
;
2627 spin_lock_irqsave(&instance
->crashdump_lock
, flags
);
2628 buff_offset
= instance
->fw_crash_buffer_offset
;
2629 if (!instance
->crash_dump_buf
&&
2630 !((instance
->fw_crash_state
== AVAILABLE
) ||
2631 (instance
->fw_crash_state
== COPYING
))) {
2632 dev_err(&instance
->pdev
->dev
,
2633 "Firmware crash dump is not available\n");
2634 spin_unlock_irqrestore(&instance
->crashdump_lock
, flags
);
2638 buff_addr
= (unsigned long) buf
;
2641 (instance
->fw_crash_buffer_size
* dmachunk
)) {
2642 dev_err(&instance
->pdev
->dev
,
2643 "Firmware crash dump offset is out of range\n");
2644 spin_unlock_irqrestore(&instance
->crashdump_lock
, flags
);
2648 size
= (instance
->fw_crash_buffer_size
* dmachunk
) - buff_offset
;
2649 size
= (size
>= PAGE_SIZE
) ? (PAGE_SIZE
- 1) : size
;
2651 src_addr
= (unsigned long)instance
->crash_buf
[buff_offset
/ dmachunk
] +
2652 (buff_offset
% dmachunk
);
2653 memcpy(buf
, (void *)src_addr
, size
);
2654 spin_unlock_irqrestore(&instance
->crashdump_lock
, flags
);
2660 megasas_fw_crash_buffer_size_show(struct device
*cdev
,
2661 struct device_attribute
*attr
, char *buf
)
2663 struct Scsi_Host
*shost
= class_to_shost(cdev
);
2664 struct megasas_instance
*instance
=
2665 (struct megasas_instance
*) shost
->hostdata
;
2667 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (unsigned long)
2668 ((instance
->fw_crash_buffer_size
) * 1024 * 1024)/PAGE_SIZE
);
2672 megasas_fw_crash_state_store(struct device
*cdev
,
2673 struct device_attribute
*attr
, const char *buf
, size_t count
)
2675 struct Scsi_Host
*shost
= class_to_shost(cdev
);
2676 struct megasas_instance
*instance
=
2677 (struct megasas_instance
*) shost
->hostdata
;
2679 unsigned long flags
;
2681 if (kstrtoint(buf
, 0, &val
) != 0)
2684 if ((val
<= AVAILABLE
|| val
> COPY_ERROR
)) {
2685 dev_err(&instance
->pdev
->dev
, "application updates invalid "
2686 "firmware crash state\n");
2690 instance
->fw_crash_state
= val
;
2692 if ((val
== COPIED
) || (val
== COPY_ERROR
)) {
2693 spin_lock_irqsave(&instance
->crashdump_lock
, flags
);
2694 megasas_free_host_crash_buffer(instance
);
2695 spin_unlock_irqrestore(&instance
->crashdump_lock
, flags
);
2696 if (val
== COPY_ERROR
)
2697 dev_info(&instance
->pdev
->dev
, "application failed to "
2698 "copy Firmware crash dump\n");
2700 dev_info(&instance
->pdev
->dev
, "Firmware crash dump "
2701 "copied successfully\n");
2707 megasas_fw_crash_state_show(struct device
*cdev
,
2708 struct device_attribute
*attr
, char *buf
)
2710 struct Scsi_Host
*shost
= class_to_shost(cdev
);
2711 struct megasas_instance
*instance
=
2712 (struct megasas_instance
*) shost
->hostdata
;
2713 return snprintf(buf
, PAGE_SIZE
, "%d\n", instance
->fw_crash_state
);
2717 megasas_page_size_show(struct device
*cdev
,
2718 struct device_attribute
*attr
, char *buf
)
2720 return snprintf(buf
, PAGE_SIZE
, "%ld\n", (unsigned long)PAGE_SIZE
- 1);
2723 static DEVICE_ATTR(fw_crash_buffer
, S_IRUGO
| S_IWUSR
,
2724 megasas_fw_crash_buffer_show
, megasas_fw_crash_buffer_store
);
2725 static DEVICE_ATTR(fw_crash_buffer_size
, S_IRUGO
,
2726 megasas_fw_crash_buffer_size_show
, NULL
);
2727 static DEVICE_ATTR(fw_crash_state
, S_IRUGO
| S_IWUSR
,
2728 megasas_fw_crash_state_show
, megasas_fw_crash_state_store
);
2729 static DEVICE_ATTR(page_size
, S_IRUGO
,
2730 megasas_page_size_show
, NULL
);
2732 struct device_attribute
*megaraid_host_attrs
[] = {
2733 &dev_attr_fw_crash_buffer_size
,
2734 &dev_attr_fw_crash_buffer
,
2735 &dev_attr_fw_crash_state
,
2736 &dev_attr_page_size
,
2741 * Scsi host template for megaraid_sas driver
2743 static struct scsi_host_template megasas_template
= {
2745 .module
= THIS_MODULE
,
2746 .name
= "LSI SAS based MegaRAID driver",
2747 .proc_name
= "megaraid_sas",
2748 .slave_configure
= megasas_slave_configure
,
2749 .slave_alloc
= megasas_slave_alloc
,
2750 .queuecommand
= megasas_queue_command
,
2751 .eh_device_reset_handler
= megasas_reset_device
,
2752 .eh_bus_reset_handler
= megasas_reset_bus_host
,
2753 .eh_host_reset_handler
= megasas_reset_bus_host
,
2754 .eh_timed_out
= megasas_reset_timer
,
2755 .shost_attrs
= megaraid_host_attrs
,
2756 .bios_param
= megasas_bios_param
,
2757 .use_clustering
= ENABLE_CLUSTERING
,
2758 .change_queue_depth
= scsi_change_queue_depth
,
2763 * megasas_complete_int_cmd - Completes an internal command
2764 * @instance: Adapter soft state
2765 * @cmd: Command to be completed
2767 * The megasas_issue_blocked_cmd() function waits for a command to complete
2768 * after it issues a command. This function wakes up that waiting routine by
2769 * calling wake_up() on the wait queue.
2772 megasas_complete_int_cmd(struct megasas_instance
*instance
,
2773 struct megasas_cmd
*cmd
)
2775 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
2777 if (cmd
->cmd_status
== ENODATA
) {
2778 cmd
->cmd_status
= 0;
2780 wake_up(&instance
->int_cmd_wait_q
);
2784 * megasas_complete_abort - Completes aborting a command
2785 * @instance: Adapter soft state
2786 * @cmd: Cmd that was issued to abort another cmd
2788 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2789 * after it issues an abort on a previously issued command. This function
2790 * wakes up all functions waiting on the same wait queue.
2793 megasas_complete_abort(struct megasas_instance
*instance
,
2794 struct megasas_cmd
*cmd
)
2796 if (cmd
->sync_cmd
) {
2798 cmd
->cmd_status
= 0;
2799 wake_up(&instance
->abort_cmd_wait_q
);
2806 * megasas_complete_cmd - Completes a command
2807 * @instance: Adapter soft state
2808 * @cmd: Command to be completed
2809 * @alt_status: If non-zero, use this value as status to
2810 * SCSI mid-layer instead of the value returned
2811 * by the FW. This should be used if caller wants
2812 * an alternate status (as in the case of aborted
2816 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
2820 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
2821 unsigned long flags
;
2822 struct fusion_context
*fusion
= instance
->ctrl_context
;
2825 /* flag for the retry reset */
2826 cmd
->retry_for_fw_reset
= 0;
2829 cmd
->scmd
->SCp
.ptr
= NULL
;
2832 case MFI_CMD_INVALID
:
2833 /* Some older 1068 controller FW may keep a pended
2834 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2835 when booting the kdump kernel. Ignore this command to
2836 prevent a kernel panic on shutdown of the kdump kernel. */
2837 printk(KERN_WARNING
"megaraid_sas: MFI_CMD_INVALID command "
2839 printk(KERN_WARNING
"megaraid_sas: If you have a controller "
2840 "other than PERC5, please upgrade your firmware.\n");
2842 case MFI_CMD_PD_SCSI_IO
:
2843 case MFI_CMD_LD_SCSI_IO
:
2846 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2847 * issued either through an IO path or an IOCTL path. If it
2848 * was via IOCTL, we will send it to internal completion.
2850 if (cmd
->sync_cmd
) {
2852 megasas_complete_int_cmd(instance
, cmd
);
2856 case MFI_CMD_LD_READ
:
2857 case MFI_CMD_LD_WRITE
:
2860 cmd
->scmd
->result
= alt_status
<< 16;
2866 atomic_dec(&instance
->fw_outstanding
);
2868 scsi_dma_unmap(cmd
->scmd
);
2869 cmd
->scmd
->scsi_done(cmd
->scmd
);
2870 megasas_return_cmd(instance
, cmd
);
2875 switch (hdr
->cmd_status
) {
2878 cmd
->scmd
->result
= DID_OK
<< 16;
2881 case MFI_STAT_SCSI_IO_FAILED
:
2882 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2884 (DID_ERROR
<< 16) | hdr
->scsi_status
;
2887 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2889 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
2891 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
2892 memset(cmd
->scmd
->sense_buffer
, 0,
2893 SCSI_SENSE_BUFFERSIZE
);
2894 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
2897 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
2902 case MFI_STAT_LD_OFFLINE
:
2903 case MFI_STAT_DEVICE_NOT_FOUND
:
2904 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
2908 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
2910 cmd
->scmd
->result
= DID_ERROR
<< 16;
2914 atomic_dec(&instance
->fw_outstanding
);
2916 scsi_dma_unmap(cmd
->scmd
);
2917 cmd
->scmd
->scsi_done(cmd
->scmd
);
2918 megasas_return_cmd(instance
, cmd
);
2925 opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
2926 /* Check for LD map update */
2927 if ((opcode
== MR_DCMD_LD_MAP_GET_INFO
)
2928 && (cmd
->frame
->dcmd
.mbox
.b
[1] == 1)) {
2929 fusion
->fast_path_io
= 0;
2930 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2931 if (cmd
->frame
->hdr
.cmd_status
!= 0) {
2932 if (cmd
->frame
->hdr
.cmd_status
!=
2934 printk(KERN_WARNING
"megasas: map sync"
2935 "failed, status = 0x%x.\n",
2936 cmd
->frame
->hdr
.cmd_status
);
2938 megasas_return_mfi_mpt_pthr(instance
,
2939 cmd
, cmd
->mpt_pthr_cmd_blocked
);
2940 spin_unlock_irqrestore(
2941 instance
->host
->host_lock
,
2947 megasas_return_mfi_mpt_pthr(instance
, cmd
,
2948 cmd
->mpt_pthr_cmd_blocked
);
2951 * Set fast path IO to ZERO.
2952 * Validate Map will set proper value.
2953 * Meanwhile all IOs will go as LD IO.
2955 if (MR_ValidateMapInfo(instance
))
2956 fusion
->fast_path_io
= 1;
2958 fusion
->fast_path_io
= 0;
2959 megasas_sync_map_info(instance
);
2960 spin_unlock_irqrestore(instance
->host
->host_lock
,
2964 if (opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
2965 opcode
== MR_DCMD_CTRL_EVENT_GET
) {
2966 spin_lock_irqsave(&poll_aen_lock
, flags
);
2967 megasas_poll_wait_aen
= 0;
2968 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2972 * See if got an event notification
2974 if (opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
2975 megasas_service_aen(instance
, cmd
);
2977 megasas_complete_int_cmd(instance
, cmd
);
2983 * Cmd issued to abort another cmd returned
2985 megasas_complete_abort(instance
, cmd
);
2989 printk("megasas: Unknown command completed! [0x%X]\n",
2996 * megasas_issue_pending_cmds_again - issue all pending cmds
2997 * in FW again because of the fw reset
2998 * @instance: Adapter soft state
3001 megasas_issue_pending_cmds_again(struct megasas_instance
*instance
)
3003 struct megasas_cmd
*cmd
;
3004 struct list_head clist_local
;
3005 union megasas_evt_class_locale class_locale
;
3006 unsigned long flags
;
3009 INIT_LIST_HEAD(&clist_local
);
3010 spin_lock_irqsave(&instance
->hba_lock
, flags
);
3011 list_splice_init(&instance
->internal_reset_pending_q
, &clist_local
);
3012 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
3014 while (!list_empty(&clist_local
)) {
3015 cmd
= list_entry((&clist_local
)->next
,
3016 struct megasas_cmd
, list
);
3017 list_del_init(&cmd
->list
);
3019 if (cmd
->sync_cmd
|| cmd
->scmd
) {
3020 printk(KERN_NOTICE
"megaraid_sas: command %p, %p:%d"
3021 "detected to be pending while HBA reset.\n",
3022 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
3024 cmd
->retry_for_fw_reset
++;
3026 if (cmd
->retry_for_fw_reset
== 3) {
3027 printk(KERN_NOTICE
"megaraid_sas: cmd %p, %p:%d"
3028 "was tried multiple times during reset."
3029 "Shutting down the HBA\n",
3030 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
3031 megaraid_sas_kill_hba(instance
);
3033 instance
->adprecovery
=
3034 MEGASAS_HW_CRITICAL_ERROR
;
3039 if (cmd
->sync_cmd
== 1) {
3041 printk(KERN_NOTICE
"megaraid_sas: unexpected"
3042 "cmd attached to internal command!\n");
3044 printk(KERN_NOTICE
"megasas: %p synchronous cmd"
3045 "on the internal reset queue,"
3046 "issue it again.\n", cmd
);
3047 cmd
->cmd_status
= ENODATA
;
3048 instance
->instancet
->fire_cmd(instance
,
3049 cmd
->frame_phys_addr
,
3050 0, instance
->reg_set
);
3051 } else if (cmd
->scmd
) {
3052 printk(KERN_NOTICE
"megasas: %p scsi cmd [%02x]"
3053 "detected on the internal queue, issue again.\n",
3054 cmd
, cmd
->scmd
->cmnd
[0]);
3056 atomic_inc(&instance
->fw_outstanding
);
3057 instance
->instancet
->fire_cmd(instance
,
3058 cmd
->frame_phys_addr
,
3059 cmd
->frame_count
-1, instance
->reg_set
);
3061 printk(KERN_NOTICE
"megasas: %p unexpected cmd on the"
3062 "internal reset defer list while re-issue!!\n",
3067 if (instance
->aen_cmd
) {
3068 printk(KERN_NOTICE
"megaraid_sas: aen_cmd in def process\n");
3069 megasas_return_cmd(instance
, instance
->aen_cmd
);
3071 instance
->aen_cmd
= NULL
;
3075 * Initiate AEN (Asynchronous Event Notification)
3077 seq_num
= instance
->last_seq_num
;
3078 class_locale
.members
.reserved
= 0;
3079 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
3080 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
3082 megasas_register_aen(instance
, seq_num
, class_locale
.word
);
3086 * Move the internal reset pending commands to a deferred queue.
3088 * We move the commands pending at internal reset time to a
3089 * pending queue. This queue would be flushed after successful
3090 * completion of the internal reset sequence. if the internal reset
3091 * did not complete in time, the kernel reset handler would flush
3095 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
)
3097 struct megasas_cmd
*cmd
;
3099 u32 max_cmd
= instance
->max_fw_cmds
;
3101 unsigned long flags
;
3104 spin_lock_irqsave(&instance
->mfi_pool_lock
, flags
);
3105 for (i
= 0; i
< max_cmd
; i
++) {
3106 cmd
= instance
->cmd_list
[i
];
3107 if (cmd
->sync_cmd
== 1 || cmd
->scmd
) {
3108 printk(KERN_NOTICE
"megasas: moving cmd[%d]:%p:%d:%p"
3109 "on the defer queue as internal\n",
3110 defer_index
, cmd
, cmd
->sync_cmd
, cmd
->scmd
);
3112 if (!list_empty(&cmd
->list
)) {
3113 printk(KERN_NOTICE
"megaraid_sas: ERROR while"
3114 " moving this cmd:%p, %d %p, it was"
3115 "discovered on some list?\n",
3116 cmd
, cmd
->sync_cmd
, cmd
->scmd
);
3118 list_del_init(&cmd
->list
);
3121 list_add_tail(&cmd
->list
,
3122 &instance
->internal_reset_pending_q
);
3125 spin_unlock_irqrestore(&instance
->mfi_pool_lock
, flags
);
3130 process_fw_state_change_wq(struct work_struct
*work
)
3132 struct megasas_instance
*instance
=
3133 container_of(work
, struct megasas_instance
, work_init
);
3135 unsigned long flags
;
3137 if (instance
->adprecovery
!= MEGASAS_ADPRESET_SM_INFAULT
) {
3138 printk(KERN_NOTICE
"megaraid_sas: error, recovery st %x \n",
3139 instance
->adprecovery
);
3143 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
3144 printk(KERN_NOTICE
"megaraid_sas: FW detected to be in fault"
3145 "state, restarting it...\n");
3147 instance
->instancet
->disable_intr(instance
);
3148 atomic_set(&instance
->fw_outstanding
, 0);
3150 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
3151 instance
->instancet
->adp_reset(instance
, instance
->reg_set
);
3152 atomic_set(&instance
->fw_reset_no_pci_access
, 0 );
3154 printk(KERN_NOTICE
"megaraid_sas: FW restarted successfully,"
3155 "initiating next stage...\n");
3157 printk(KERN_NOTICE
"megaraid_sas: HBA recovery state machine,"
3158 "state 2 starting...\n");
3160 /*waitting for about 20 second before start the second init*/
3161 for (wait
= 0; wait
< 30; wait
++) {
3165 if (megasas_transition_to_ready(instance
, 1)) {
3166 printk(KERN_NOTICE
"megaraid_sas:adapter not ready\n");
3168 megaraid_sas_kill_hba(instance
);
3169 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
3173 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
3174 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
3175 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)
3177 *instance
->consumer
= *instance
->producer
;
3179 *instance
->consumer
= 0;
3180 *instance
->producer
= 0;
3183 megasas_issue_init_mfi(instance
);
3185 spin_lock_irqsave(&instance
->hba_lock
, flags
);
3186 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
3187 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
3188 instance
->instancet
->enable_intr(instance
);
3190 megasas_issue_pending_cmds_again(instance
);
3191 instance
->issuepend_done
= 1;
3197 * megasas_deplete_reply_queue - Processes all completed commands
3198 * @instance: Adapter soft state
3199 * @alt_status: Alternate status to be returned to
3200 * SCSI mid-layer instead of the status
3201 * returned by the FW
3202 * Note: this must be called with hba lock held
3205 megasas_deplete_reply_queue(struct megasas_instance
*instance
,
3211 if ((mfiStatus
= instance
->instancet
->check_reset(instance
,
3212 instance
->reg_set
)) == 1) {
3216 if ((mfiStatus
= instance
->instancet
->clear_intr(
3219 /* Hardware may not set outbound_intr_status in MSI-X mode */
3220 if (!instance
->msix_vectors
)
3224 instance
->mfiStatus
= mfiStatus
;
3226 if ((mfiStatus
& MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
)) {
3227 fw_state
= instance
->instancet
->read_fw_status_reg(
3228 instance
->reg_set
) & MFI_STATE_MASK
;
3230 if (fw_state
!= MFI_STATE_FAULT
) {
3231 printk(KERN_NOTICE
"megaraid_sas: fw state:%x\n",
3235 if ((fw_state
== MFI_STATE_FAULT
) &&
3236 (instance
->disableOnlineCtrlReset
== 0)) {
3237 printk(KERN_NOTICE
"megaraid_sas: wait adp restart\n");
3239 if ((instance
->pdev
->device
==
3240 PCI_DEVICE_ID_LSI_SAS1064R
) ||
3241 (instance
->pdev
->device
==
3242 PCI_DEVICE_ID_DELL_PERC5
) ||
3243 (instance
->pdev
->device
==
3244 PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
3246 *instance
->consumer
=
3247 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN
);
3251 instance
->instancet
->disable_intr(instance
);
3252 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
3253 instance
->issuepend_done
= 0;
3255 atomic_set(&instance
->fw_outstanding
, 0);
3256 megasas_internal_reset_defer_cmds(instance
);
3258 printk(KERN_NOTICE
"megasas: fwState=%x, stage:%d\n",
3259 fw_state
, instance
->adprecovery
);
3261 schedule_work(&instance
->work_init
);
3265 printk(KERN_NOTICE
"megasas: fwstate:%x, dis_OCR=%x\n",
3266 fw_state
, instance
->disableOnlineCtrlReset
);
3270 tasklet_schedule(&instance
->isr_tasklet
);
3274 * megasas_isr - isr entry point
3276 static irqreturn_t
megasas_isr(int irq
, void *devp
)
3278 struct megasas_irq_context
*irq_context
= devp
;
3279 struct megasas_instance
*instance
= irq_context
->instance
;
3280 unsigned long flags
;
3283 if (atomic_read(&instance
->fw_reset_no_pci_access
))
3286 spin_lock_irqsave(&instance
->hba_lock
, flags
);
3287 rc
= megasas_deplete_reply_queue(instance
, DID_OK
);
3288 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
3294 * megasas_transition_to_ready - Move the FW to READY state
3295 * @instance: Adapter soft state
3297 * During the initialization, FW passes can potentially be in any one of
3298 * several possible states. If the FW in operational, waiting-for-handshake
3299 * states, driver must take steps to bring it to ready state. Otherwise, it
3300 * has to wait for the ready state.
3303 megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
)
3309 u32 abs_state
, curr_abs_state
;
3311 abs_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
3312 fw_state
= abs_state
& MFI_STATE_MASK
;
3314 if (fw_state
!= MFI_STATE_READY
)
3315 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
3318 while (fw_state
!= MFI_STATE_READY
) {
3322 case MFI_STATE_FAULT
:
3323 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
3325 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3326 cur_state
= MFI_STATE_FAULT
;
3331 case MFI_STATE_WAIT_HANDSHAKE
:
3333 * Set the CLR bit in inbound doorbell
3335 if ((instance
->pdev
->device
==
3336 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3337 (instance
->pdev
->device
==
3338 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3339 (instance
->pdev
->device
==
3340 PCI_DEVICE_ID_LSI_FUSION
) ||
3341 (instance
->pdev
->device
==
3342 PCI_DEVICE_ID_LSI_PLASMA
) ||
3343 (instance
->pdev
->device
==
3344 PCI_DEVICE_ID_LSI_INVADER
) ||
3345 (instance
->pdev
->device
==
3346 PCI_DEVICE_ID_LSI_FURY
)) {
3348 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
3349 &instance
->reg_set
->doorbell
);
3352 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
3353 &instance
->reg_set
->inbound_doorbell
);
3356 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3357 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
3360 case MFI_STATE_BOOT_MESSAGE_PENDING
:
3361 if ((instance
->pdev
->device
==
3362 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3363 (instance
->pdev
->device
==
3364 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3365 (instance
->pdev
->device
==
3366 PCI_DEVICE_ID_LSI_FUSION
) ||
3367 (instance
->pdev
->device
==
3368 PCI_DEVICE_ID_LSI_PLASMA
) ||
3369 (instance
->pdev
->device
==
3370 PCI_DEVICE_ID_LSI_INVADER
) ||
3371 (instance
->pdev
->device
==
3372 PCI_DEVICE_ID_LSI_FURY
)) {
3373 writel(MFI_INIT_HOTPLUG
,
3374 &instance
->reg_set
->doorbell
);
3376 writel(MFI_INIT_HOTPLUG
,
3377 &instance
->reg_set
->inbound_doorbell
);
3379 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3380 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
3383 case MFI_STATE_OPERATIONAL
:
3385 * Bring it to READY state; assuming max wait 10 secs
3387 instance
->instancet
->disable_intr(instance
);
3388 if ((instance
->pdev
->device
==
3389 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3390 (instance
->pdev
->device
==
3391 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3392 (instance
->pdev
->device
3393 == PCI_DEVICE_ID_LSI_FUSION
) ||
3394 (instance
->pdev
->device
3395 == PCI_DEVICE_ID_LSI_PLASMA
) ||
3396 (instance
->pdev
->device
3397 == PCI_DEVICE_ID_LSI_INVADER
) ||
3398 (instance
->pdev
->device
3399 == PCI_DEVICE_ID_LSI_FURY
)) {
3400 writel(MFI_RESET_FLAGS
,
3401 &instance
->reg_set
->doorbell
);
3402 if ((instance
->pdev
->device
==
3403 PCI_DEVICE_ID_LSI_FUSION
) ||
3404 (instance
->pdev
->device
==
3405 PCI_DEVICE_ID_LSI_PLASMA
) ||
3406 (instance
->pdev
->device
==
3407 PCI_DEVICE_ID_LSI_INVADER
) ||
3408 (instance
->pdev
->device
==
3409 PCI_DEVICE_ID_LSI_FURY
)) {
3410 for (i
= 0; i
< (10 * 1000); i
+= 20) {
3421 writel(MFI_RESET_FLAGS
,
3422 &instance
->reg_set
->inbound_doorbell
);
3424 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3425 cur_state
= MFI_STATE_OPERATIONAL
;
3428 case MFI_STATE_UNDEFINED
:
3430 * This state should not last for more than 2 seconds
3432 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3433 cur_state
= MFI_STATE_UNDEFINED
;
3436 case MFI_STATE_BB_INIT
:
3437 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3438 cur_state
= MFI_STATE_BB_INIT
;
3441 case MFI_STATE_FW_INIT
:
3442 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3443 cur_state
= MFI_STATE_FW_INIT
;
3446 case MFI_STATE_FW_INIT_2
:
3447 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3448 cur_state
= MFI_STATE_FW_INIT_2
;
3451 case MFI_STATE_DEVICE_SCAN
:
3452 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3453 cur_state
= MFI_STATE_DEVICE_SCAN
;
3456 case MFI_STATE_FLUSH_CACHE
:
3457 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3458 cur_state
= MFI_STATE_FLUSH_CACHE
;
3462 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
3468 * The cur_state should not last for more than max_wait secs
3470 for (i
= 0; i
< (max_wait
* 1000); i
++) {
3471 curr_abs_state
= instance
->instancet
->
3472 read_fw_status_reg(instance
->reg_set
);
3474 if (abs_state
== curr_abs_state
) {
3481 * Return error if fw_state hasn't changed after max_wait
3483 if (curr_abs_state
== abs_state
) {
3484 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
3485 "in %d secs\n", fw_state
, max_wait
);
3489 abs_state
= curr_abs_state
;
3490 fw_state
= curr_abs_state
& MFI_STATE_MASK
;
3492 printk(KERN_INFO
"megasas: FW now in Ready state\n");
3498 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3499 * @instance: Adapter soft state
3501 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
3504 u32 max_cmd
= instance
->max_mfi_cmds
;
3505 struct megasas_cmd
*cmd
;
3507 if (!instance
->frame_dma_pool
)
3511 * Return all frames to pool
3513 for (i
= 0; i
< max_cmd
; i
++) {
3515 cmd
= instance
->cmd_list
[i
];
3518 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
3519 cmd
->frame_phys_addr
);
3522 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
3523 cmd
->sense_phys_addr
);
3527 * Now destroy the pool itself
3529 pci_pool_destroy(instance
->frame_dma_pool
);
3530 pci_pool_destroy(instance
->sense_dma_pool
);
3532 instance
->frame_dma_pool
= NULL
;
3533 instance
->sense_dma_pool
= NULL
;
3537 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3538 * @instance: Adapter soft state
3540 * Each command packet has an embedded DMA memory buffer that is used for
3541 * filling MFI frame and the SG list that immediately follows the frame. This
3542 * function creates those DMA memory buffers for each command packet by using
3543 * PCI pool facility.
3545 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
3552 struct megasas_cmd
*cmd
;
3554 max_cmd
= instance
->max_mfi_cmds
;
3557 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3558 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3560 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
3561 sizeof(struct megasas_sge32
);
3563 if (instance
->flag_ieee
) {
3564 sge_sz
= sizeof(struct megasas_sge_skinny
);
3568 * For MFI controllers.
3570 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3571 * Total 960 byte (15 MFI frame of 64 byte)
3573 * Fusion adapter require only 3 extra frame.
3574 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3575 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3576 * Total 192 byte (3 MFI frame of 64 byte)
3578 frame_count
= instance
->ctrl_context
? (3 + 1) : (15 + 1);
3579 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
3581 * Use DMA pool facility provided by PCI layer
3583 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
3584 instance
->pdev
, total_sz
, 256, 0);
3586 if (!instance
->frame_dma_pool
) {
3587 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
3591 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
3592 instance
->pdev
, 128, 4, 0);
3594 if (!instance
->sense_dma_pool
) {
3595 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
3597 pci_pool_destroy(instance
->frame_dma_pool
);
3598 instance
->frame_dma_pool
= NULL
;
3604 * Allocate and attach a frame to each of the commands in cmd_list.
3605 * By making cmd->index as the context instead of the &cmd, we can
3606 * always use 32bit context regardless of the architecture
3608 for (i
= 0; i
< max_cmd
; i
++) {
3610 cmd
= instance
->cmd_list
[i
];
3612 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
3613 GFP_KERNEL
, &cmd
->frame_phys_addr
);
3615 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
3616 GFP_KERNEL
, &cmd
->sense_phys_addr
);
3619 * megasas_teardown_frame_pool() takes care of freeing
3620 * whatever has been allocated
3622 if (!cmd
->frame
|| !cmd
->sense
) {
3623 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
3624 megasas_teardown_frame_pool(instance
);
3628 memset(cmd
->frame
, 0, total_sz
);
3629 cmd
->frame
->io
.context
= cpu_to_le32(cmd
->index
);
3630 cmd
->frame
->io
.pad_0
= 0;
3631 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
3632 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_PLASMA
) &&
3633 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
3634 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
3636 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
3643 * megasas_free_cmds - Free all the cmds in the free cmd pool
3644 * @instance: Adapter soft state
3646 void megasas_free_cmds(struct megasas_instance
*instance
)
3649 /* First free the MFI frame pool */
3650 megasas_teardown_frame_pool(instance
);
3652 /* Free all the commands in the cmd_list */
3653 for (i
= 0; i
< instance
->max_mfi_cmds
; i
++)
3655 kfree(instance
->cmd_list
[i
]);
3657 /* Free the cmd_list buffer itself */
3658 kfree(instance
->cmd_list
);
3659 instance
->cmd_list
= NULL
;
3661 INIT_LIST_HEAD(&instance
->cmd_pool
);
3665 * megasas_alloc_cmds - Allocates the command packets
3666 * @instance: Adapter soft state
3668 * Each command that is issued to the FW, whether IO commands from the OS or
3669 * internal commands like IOCTLs, are wrapped in local data structure called
3670 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3673 * Each frame has a 32-bit field called context (tag). This context is used
3674 * to get back the megasas_cmd from the frame when a frame gets completed in
3675 * the ISR. Typically the address of the megasas_cmd itself would be used as
3676 * the context. But we wanted to keep the differences between 32 and 64 bit
3677 * systems to the mininum. We always use 32 bit integers for the context. In
3678 * this driver, the 32 bit values are the indices into an array cmd_list.
3679 * This array is used only to look up the megasas_cmd given the context. The
3680 * free commands themselves are maintained in a linked list called cmd_pool.
3682 int megasas_alloc_cmds(struct megasas_instance
*instance
)
3687 struct megasas_cmd
*cmd
;
3688 struct fusion_context
*fusion
;
3690 fusion
= instance
->ctrl_context
;
3691 max_cmd
= instance
->max_mfi_cmds
;
3694 * instance->cmd_list is an array of struct megasas_cmd pointers.
3695 * Allocate the dynamic array first and then allocate individual
3698 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
3700 if (!instance
->cmd_list
) {
3701 printk(KERN_DEBUG
"megasas: out of memory\n");
3705 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) *max_cmd
);
3707 for (i
= 0; i
< max_cmd
; i
++) {
3708 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
3711 if (!instance
->cmd_list
[i
]) {
3713 for (j
= 0; j
< i
; j
++)
3714 kfree(instance
->cmd_list
[j
]);
3716 kfree(instance
->cmd_list
);
3717 instance
->cmd_list
= NULL
;
3723 for (i
= 0; i
< max_cmd
; i
++) {
3724 cmd
= instance
->cmd_list
[i
];
3725 memset(cmd
, 0, sizeof(struct megasas_cmd
));
3727 atomic_set(&cmd
->mfi_mpt_pthr
, MFI_LIST_ADDED
);
3729 cmd
->instance
= instance
;
3731 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
3735 * Create a frame pool and assign one frame to each cmd
3737 if (megasas_create_frame_pool(instance
)) {
3738 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
3739 megasas_free_cmds(instance
);
3746 * megasas_get_pd_list_info - Returns FW's pd_list structure
3747 * @instance: Adapter soft state
3748 * @pd_list: pd_list structure
3750 * Issues an internal command (DCMD) to get the FW's controller PD
3751 * list structure. This information is mainly used to find out SYSTEM
3752 * supported by the FW.
3755 megasas_get_pd_list(struct megasas_instance
*instance
)
3757 int ret
= 0, pd_index
= 0;
3758 struct megasas_cmd
*cmd
;
3759 struct megasas_dcmd_frame
*dcmd
;
3760 struct MR_PD_LIST
*ci
;
3761 struct MR_PD_ADDRESS
*pd_addr
;
3762 dma_addr_t ci_h
= 0;
3764 cmd
= megasas_get_cmd(instance
);
3767 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
3771 dcmd
= &cmd
->frame
->dcmd
;
3773 ci
= pci_alloc_consistent(instance
->pdev
,
3774 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
3777 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
3778 megasas_return_cmd(instance
, cmd
);
3782 memset(ci
, 0, sizeof(*ci
));
3783 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3785 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
3786 dcmd
->mbox
.b
[1] = 0;
3787 dcmd
->cmd
= MFI_CMD_DCMD
;
3788 dcmd
->cmd_status
= 0xFF;
3789 dcmd
->sge_count
= 1;
3790 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3793 dcmd
->data_xfer_len
= cpu_to_le32(MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
));
3794 dcmd
->opcode
= cpu_to_le32(MR_DCMD_PD_LIST_QUERY
);
3795 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3796 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
));
3798 if (instance
->ctrl_context
&& !instance
->mask_interrupts
)
3799 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
3800 MEGASAS_BLOCKED_CMD_TIMEOUT
);
3802 ret
= megasas_issue_polled(instance
, cmd
);
3805 * the following function will get the instance PD LIST.
3811 (le32_to_cpu(ci
->count
) <
3812 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
3814 memset(instance
->local_pd_list
, 0,
3815 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
3817 for (pd_index
= 0; pd_index
< le32_to_cpu(ci
->count
); pd_index
++) {
3819 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].tid
=
3820 le16_to_cpu(pd_addr
->deviceId
);
3821 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].driveType
=
3822 pd_addr
->scsiDevType
;
3823 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].driveState
=
3827 memcpy(instance
->pd_list
, instance
->local_pd_list
,
3828 sizeof(instance
->pd_list
));
3831 pci_free_consistent(instance
->pdev
,
3832 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
3835 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
3836 megasas_return_mfi_mpt_pthr(instance
, cmd
,
3837 cmd
->mpt_pthr_cmd_blocked
);
3839 megasas_return_cmd(instance
, cmd
);
3845 * megasas_get_ld_list_info - Returns FW's ld_list structure
3846 * @instance: Adapter soft state
3847 * @ld_list: ld_list structure
3849 * Issues an internal command (DCMD) to get the FW's controller PD
3850 * list structure. This information is mainly used to find out SYSTEM
3851 * supported by the FW.
3854 megasas_get_ld_list(struct megasas_instance
*instance
)
3856 int ret
= 0, ld_index
= 0, ids
= 0;
3857 struct megasas_cmd
*cmd
;
3858 struct megasas_dcmd_frame
*dcmd
;
3859 struct MR_LD_LIST
*ci
;
3860 dma_addr_t ci_h
= 0;
3863 cmd
= megasas_get_cmd(instance
);
3866 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
3870 dcmd
= &cmd
->frame
->dcmd
;
3872 ci
= pci_alloc_consistent(instance
->pdev
,
3873 sizeof(struct MR_LD_LIST
),
3877 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
3878 megasas_return_cmd(instance
, cmd
);
3882 memset(ci
, 0, sizeof(*ci
));
3883 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3885 if (instance
->supportmax256vd
)
3886 dcmd
->mbox
.b
[0] = 1;
3887 dcmd
->cmd
= MFI_CMD_DCMD
;
3888 dcmd
->cmd_status
= 0xFF;
3889 dcmd
->sge_count
= 1;
3890 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3892 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct MR_LD_LIST
));
3893 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_GET_LIST
);
3894 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3895 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct MR_LD_LIST
));
3898 if (instance
->ctrl_context
&& !instance
->mask_interrupts
)
3899 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
3900 MEGASAS_BLOCKED_CMD_TIMEOUT
);
3902 ret
= megasas_issue_polled(instance
, cmd
);
3905 ld_count
= le32_to_cpu(ci
->ldCount
);
3907 /* the following function will get the instance PD LIST */
3909 if ((ret
== 0) && (ld_count
<= instance
->fw_supported_vd_count
)) {
3910 memset(instance
->ld_ids
, 0xff, MAX_LOGICAL_DRIVES_EXT
);
3912 for (ld_index
= 0; ld_index
< ld_count
; ld_index
++) {
3913 if (ci
->ldList
[ld_index
].state
!= 0) {
3914 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
3915 instance
->ld_ids
[ids
] =
3916 ci
->ldList
[ld_index
].ref
.targetId
;
3921 pci_free_consistent(instance
->pdev
,
3922 sizeof(struct MR_LD_LIST
),
3926 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
3927 megasas_return_mfi_mpt_pthr(instance
, cmd
,
3928 cmd
->mpt_pthr_cmd_blocked
);
3930 megasas_return_cmd(instance
, cmd
);
3935 * megasas_ld_list_query - Returns FW's ld_list structure
3936 * @instance: Adapter soft state
3937 * @ld_list: ld_list structure
3939 * Issues an internal command (DCMD) to get the FW's controller PD
3940 * list structure. This information is mainly used to find out SYSTEM
3941 * supported by the FW.
3944 megasas_ld_list_query(struct megasas_instance
*instance
, u8 query_type
)
3946 int ret
= 0, ld_index
= 0, ids
= 0;
3947 struct megasas_cmd
*cmd
;
3948 struct megasas_dcmd_frame
*dcmd
;
3949 struct MR_LD_TARGETID_LIST
*ci
;
3950 dma_addr_t ci_h
= 0;
3953 cmd
= megasas_get_cmd(instance
);
3957 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3961 dcmd
= &cmd
->frame
->dcmd
;
3963 ci
= pci_alloc_consistent(instance
->pdev
,
3964 sizeof(struct MR_LD_TARGETID_LIST
), &ci_h
);
3968 "megasas: Failed to alloc mem for ld_list_query\n");
3969 megasas_return_cmd(instance
, cmd
);
3973 memset(ci
, 0, sizeof(*ci
));
3974 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3976 dcmd
->mbox
.b
[0] = query_type
;
3977 if (instance
->supportmax256vd
)
3978 dcmd
->mbox
.b
[2] = 1;
3980 dcmd
->cmd
= MFI_CMD_DCMD
;
3981 dcmd
->cmd_status
= 0xFF;
3982 dcmd
->sge_count
= 1;
3983 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3985 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST
));
3986 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_LIST_QUERY
);
3987 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3988 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST
));
3991 if (instance
->ctrl_context
&& !instance
->mask_interrupts
)
3992 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
3993 MEGASAS_BLOCKED_CMD_TIMEOUT
);
3995 ret
= megasas_issue_polled(instance
, cmd
);
3997 tgtid_count
= le32_to_cpu(ci
->count
);
3999 if ((ret
== 0) && (tgtid_count
<= (instance
->fw_supported_vd_count
))) {
4000 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
4001 for (ld_index
= 0; ld_index
< tgtid_count
; ld_index
++) {
4002 ids
= ci
->targetId
[ld_index
];
4003 instance
->ld_ids
[ids
] = ci
->targetId
[ld_index
];
4008 pci_free_consistent(instance
->pdev
, sizeof(struct MR_LD_TARGETID_LIST
),
4011 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
4012 megasas_return_mfi_mpt_pthr(instance
, cmd
,
4013 cmd
->mpt_pthr_cmd_blocked
);
4015 megasas_return_cmd(instance
, cmd
);
4021 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4022 * instance : Controller's instance
4024 static void megasas_update_ext_vd_details(struct megasas_instance
*instance
)
4026 struct fusion_context
*fusion
;
4030 fusion
= instance
->ctrl_context
;
4031 /* For MFI based controllers return dummy success */
4035 instance
->supportmax256vd
=
4036 instance
->ctrl_info
->adapterOperations3
.supportMaxExtLDs
;
4037 /* Below is additional check to address future FW enhancement */
4038 if (instance
->ctrl_info
->max_lds
> 64)
4039 instance
->supportmax256vd
= 1;
4041 instance
->drv_supported_vd_count
= MEGASAS_MAX_LD_CHANNELS
4042 * MEGASAS_MAX_DEV_PER_CHANNEL
;
4043 instance
->drv_supported_pd_count
= MEGASAS_MAX_PD_CHANNELS
4044 * MEGASAS_MAX_DEV_PER_CHANNEL
;
4045 if (instance
->supportmax256vd
) {
4046 instance
->fw_supported_vd_count
= MAX_LOGICAL_DRIVES_EXT
;
4047 instance
->fw_supported_pd_count
= MAX_PHYSICAL_DEVICES
;
4049 instance
->fw_supported_vd_count
= MAX_LOGICAL_DRIVES
;
4050 instance
->fw_supported_pd_count
= MAX_PHYSICAL_DEVICES
;
4052 dev_info(&instance
->pdev
->dev
, "Firmware supports %d VD %d PD\n",
4053 instance
->fw_supported_vd_count
,
4054 instance
->fw_supported_pd_count
);
4055 dev_info(&instance
->pdev
->dev
, "Driver supports %d VD %d PD\n",
4056 instance
->drv_supported_vd_count
,
4057 instance
->drv_supported_pd_count
);
4059 old_map_sz
= sizeof(struct MR_FW_RAID_MAP
) +
4060 (sizeof(struct MR_LD_SPAN_MAP
) *
4061 (instance
->fw_supported_vd_count
- 1));
4062 new_map_sz
= sizeof(struct MR_FW_RAID_MAP_EXT
);
4063 fusion
->drv_map_sz
= sizeof(struct MR_DRV_RAID_MAP
) +
4064 (sizeof(struct MR_LD_SPAN_MAP
) *
4065 (instance
->drv_supported_vd_count
- 1));
4067 fusion
->max_map_sz
= max(old_map_sz
, new_map_sz
);
4070 if (instance
->supportmax256vd
)
4071 fusion
->current_map_sz
= new_map_sz
;
4073 fusion
->current_map_sz
= old_map_sz
;
4078 * megasas_get_controller_info - Returns FW's controller structure
4079 * @instance: Adapter soft state
4081 * Issues an internal command (DCMD) to get the FW's controller structure.
4082 * This information is mainly used to find out the maximum IO transfer per
4083 * command supported by the FW.
4086 megasas_get_ctrl_info(struct megasas_instance
*instance
)
4089 struct megasas_cmd
*cmd
;
4090 struct megasas_dcmd_frame
*dcmd
;
4091 struct megasas_ctrl_info
*ci
;
4092 struct megasas_ctrl_info
*ctrl_info
;
4093 dma_addr_t ci_h
= 0;
4095 ctrl_info
= instance
->ctrl_info
;
4097 cmd
= megasas_get_cmd(instance
);
4100 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
4104 dcmd
= &cmd
->frame
->dcmd
;
4106 ci
= pci_alloc_consistent(instance
->pdev
,
4107 sizeof(struct megasas_ctrl_info
), &ci_h
);
4110 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
4111 megasas_return_cmd(instance
, cmd
);
4115 memset(ci
, 0, sizeof(*ci
));
4116 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4118 dcmd
->cmd
= MFI_CMD_DCMD
;
4119 dcmd
->cmd_status
= 0xFF;
4120 dcmd
->sge_count
= 1;
4121 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
4124 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_ctrl_info
));
4125 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_GET_INFO
);
4126 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
4127 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_ctrl_info
));
4128 dcmd
->mbox
.b
[0] = 1;
4130 if (instance
->ctrl_context
&& !instance
->mask_interrupts
)
4131 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
4132 MEGASAS_BLOCKED_CMD_TIMEOUT
);
4134 ret
= megasas_issue_polled(instance
, cmd
);
4137 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
4138 le32_to_cpus((u32
*)&ctrl_info
->properties
.OnOffProperties
);
4139 le32_to_cpus((u32
*)&ctrl_info
->adapterOperations2
);
4140 le32_to_cpus((u32
*)&ctrl_info
->adapterOperations3
);
4141 megasas_update_ext_vd_details(instance
);
4144 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
4147 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
4148 megasas_return_mfi_mpt_pthr(instance
, cmd
,
4149 cmd
->mpt_pthr_cmd_blocked
);
4151 megasas_return_cmd(instance
, cmd
);
4156 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4159 * @instance: Adapter soft state
4160 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4161 MR_CRASH_BUF_TURN_OFF = 0
4162 MR_CRASH_BUF_TURN_ON = 1
4163 * @return 0 on success non-zero on failure.
4164 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4165 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4166 * that driver supports crash dump feature. This DCMD will be sent only if
4167 * crash dump feature is supported by the FW.
4170 int megasas_set_crash_dump_params(struct megasas_instance
*instance
,
4174 struct megasas_cmd
*cmd
;
4175 struct megasas_dcmd_frame
*dcmd
;
4177 cmd
= megasas_get_cmd(instance
);
4180 dev_err(&instance
->pdev
->dev
, "Failed to get a free cmd\n");
4185 dcmd
= &cmd
->frame
->dcmd
;
4187 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4188 dcmd
->mbox
.b
[0] = crash_buf_state
;
4189 dcmd
->cmd
= MFI_CMD_DCMD
;
4190 dcmd
->cmd_status
= 0xFF;
4191 dcmd
->sge_count
= 1;
4192 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_NONE
);
4195 dcmd
->data_xfer_len
= cpu_to_le32(CRASH_DMA_BUF_SIZE
);
4196 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS
);
4197 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(instance
->crash_dump_h
);
4198 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(CRASH_DMA_BUF_SIZE
);
4200 if (instance
->ctrl_context
&& !instance
->mask_interrupts
)
4201 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
4202 MEGASAS_BLOCKED_CMD_TIMEOUT
);
4204 ret
= megasas_issue_polled(instance
, cmd
);
4206 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
4207 megasas_return_mfi_mpt_pthr(instance
, cmd
,
4208 cmd
->mpt_pthr_cmd_blocked
);
4210 megasas_return_cmd(instance
, cmd
);
4215 * megasas_issue_init_mfi - Initializes the FW
4216 * @instance: Adapter soft state
4218 * Issues the INIT MFI cmd
4221 megasas_issue_init_mfi(struct megasas_instance
*instance
)
4225 struct megasas_cmd
*cmd
;
4227 struct megasas_init_frame
*init_frame
;
4228 struct megasas_init_queue_info
*initq_info
;
4229 dma_addr_t init_frame_h
;
4230 dma_addr_t initq_info_h
;
4233 * Prepare a init frame. Note the init frame points to queue info
4234 * structure. Each frame has SGL allocated after first 64 bytes. For
4235 * this frame - since we don't need any SGL - we use SGL's space as
4236 * queue info structure
4238 * We will not get a NULL command below. We just created the pool.
4240 cmd
= megasas_get_cmd(instance
);
4242 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
4243 initq_info
= (struct megasas_init_queue_info
*)
4244 ((unsigned long)init_frame
+ 64);
4246 init_frame_h
= cmd
->frame_phys_addr
;
4247 initq_info_h
= init_frame_h
+ 64;
4249 context
= init_frame
->context
;
4250 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
4251 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
4252 init_frame
->context
= context
;
4254 initq_info
->reply_queue_entries
= cpu_to_le32(instance
->max_fw_cmds
+ 1);
4255 initq_info
->reply_queue_start_phys_addr_lo
= cpu_to_le32(instance
->reply_queue_h
);
4257 initq_info
->producer_index_phys_addr_lo
= cpu_to_le32(instance
->producer_h
);
4258 initq_info
->consumer_index_phys_addr_lo
= cpu_to_le32(instance
->consumer_h
);
4260 init_frame
->cmd
= MFI_CMD_INIT
;
4261 init_frame
->cmd_status
= 0xFF;
4262 init_frame
->queue_info_new_phys_addr_lo
=
4263 cpu_to_le32(lower_32_bits(initq_info_h
));
4264 init_frame
->queue_info_new_phys_addr_hi
=
4265 cpu_to_le32(upper_32_bits(initq_info_h
));
4267 init_frame
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_init_queue_info
));
4270 * disable the intr before firing the init frame to FW
4272 instance
->instancet
->disable_intr(instance
);
4275 * Issue the init frame in polled mode
4278 if (megasas_issue_polled(instance
, cmd
)) {
4279 printk(KERN_ERR
"megasas: Failed to init firmware\n");
4280 megasas_return_cmd(instance
, cmd
);
4284 megasas_return_cmd(instance
, cmd
);
4293 megasas_init_adapter_mfi(struct megasas_instance
*instance
)
4295 struct megasas_register_set __iomem
*reg_set
;
4299 reg_set
= instance
->reg_set
;
4302 * Get various operational parameters from status register
4304 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
4306 * Reduce the max supported cmds by 1. This is to ensure that the
4307 * reply_q_sz (1 more than the max cmd that driver may send)
4308 * does not exceed max cmds that the FW can support
4310 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
4311 instance
->max_mfi_cmds
= instance
->max_fw_cmds
;
4312 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
4315 * Create a pool of commands
4317 if (megasas_alloc_cmds(instance
))
4318 goto fail_alloc_cmds
;
4321 * Allocate memory for reply queue. Length of reply queue should
4322 * be _one_ more than the maximum commands handled by the firmware.
4324 * Note: When FW completes commands, it places corresponding contex
4325 * values in this circular reply queue. This circular queue is a fairly
4326 * typical producer-consumer queue. FW is the producer (of completed
4327 * commands) and the driver is the consumer.
4329 context_sz
= sizeof(u32
);
4330 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
4332 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
4334 &instance
->reply_queue_h
);
4336 if (!instance
->reply_queue
) {
4337 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
4338 goto fail_reply_queue
;
4341 if (megasas_issue_init_mfi(instance
))
4344 if (megasas_get_ctrl_info(instance
)) {
4345 dev_err(&instance
->pdev
->dev
, "(%d): Could get controller info "
4346 "Fail from %s %d\n", instance
->unique_id
,
4347 __func__
, __LINE__
);
4351 instance
->fw_support_ieee
= 0;
4352 instance
->fw_support_ieee
=
4353 (instance
->instancet
->read_fw_status_reg(reg_set
) &
4356 printk(KERN_NOTICE
"megasas_init_mfi: fw_support_ieee=%d",
4357 instance
->fw_support_ieee
);
4359 if (instance
->fw_support_ieee
)
4360 instance
->flag_ieee
= 1;
4366 pci_free_consistent(instance
->pdev
, reply_q_sz
,
4367 instance
->reply_queue
, instance
->reply_queue_h
);
4369 megasas_free_cmds(instance
);
4376 * megasas_init_fw - Initializes the FW
4377 * @instance: Adapter soft state
4379 * This is the main function for initializing firmware
4382 static int megasas_init_fw(struct megasas_instance
*instance
)
4386 u32 tmp_sectors
, msix_enable
, scratch_pad_2
;
4387 resource_size_t base_addr
;
4388 struct megasas_register_set __iomem
*reg_set
;
4389 struct megasas_ctrl_info
*ctrl_info
= NULL
;
4390 unsigned long bar_list
;
4391 int i
, loop
, fw_msix_count
= 0;
4392 struct IOV_111
*iovPtr
;
4394 /* Find first memory bar */
4395 bar_list
= pci_select_bars(instance
->pdev
, IORESOURCE_MEM
);
4396 instance
->bar
= find_first_bit(&bar_list
, sizeof(unsigned long));
4397 if (pci_request_selected_regions(instance
->pdev
, instance
->bar
,
4399 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
4403 base_addr
= pci_resource_start(instance
->pdev
, instance
->bar
);
4404 instance
->reg_set
= ioremap_nocache(base_addr
, 8192);
4406 if (!instance
->reg_set
) {
4407 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
4411 reg_set
= instance
->reg_set
;
4413 switch (instance
->pdev
->device
) {
4414 case PCI_DEVICE_ID_LSI_FUSION
:
4415 case PCI_DEVICE_ID_LSI_PLASMA
:
4416 case PCI_DEVICE_ID_LSI_INVADER
:
4417 case PCI_DEVICE_ID_LSI_FURY
:
4418 instance
->instancet
= &megasas_instance_template_fusion
;
4420 case PCI_DEVICE_ID_LSI_SAS1078R
:
4421 case PCI_DEVICE_ID_LSI_SAS1078DE
:
4422 instance
->instancet
= &megasas_instance_template_ppc
;
4424 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
4425 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
4426 instance
->instancet
= &megasas_instance_template_gen2
;
4428 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
4429 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
4430 instance
->instancet
= &megasas_instance_template_skinny
;
4432 case PCI_DEVICE_ID_LSI_SAS1064R
:
4433 case PCI_DEVICE_ID_DELL_PERC5
:
4435 instance
->instancet
= &megasas_instance_template_xscale
;
4439 if (megasas_transition_to_ready(instance
, 0)) {
4440 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
4441 instance
->instancet
->adp_reset
4442 (instance
, instance
->reg_set
);
4443 atomic_set(&instance
->fw_reset_no_pci_access
, 0);
4444 dev_info(&instance
->pdev
->dev
,
4445 "megasas: FW restarted successfully from %s!\n",
4448 /*waitting for about 30 second before retry*/
4451 if (megasas_transition_to_ready(instance
, 0))
4452 goto fail_ready_state
;
4456 * MSI-X host index 0 is common for all adapter.
4457 * It is used for all MPT based Adapters.
4459 instance
->reply_post_host_index_addr
[0] =
4460 (u32
*)((u8
*)instance
->reg_set
+
4461 MPI2_REPLY_POST_HOST_INDEX_OFFSET
);
4463 /* Check if MSI-X is supported while in ready state */
4464 msix_enable
= (instance
->instancet
->read_fw_status_reg(reg_set
) &
4466 if (msix_enable
&& !msix_disable
) {
4467 scratch_pad_2
= readl
4468 (&instance
->reg_set
->outbound_scratch_pad_2
);
4469 /* Check max MSI-X vectors */
4470 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4471 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
)) {
4472 instance
->msix_vectors
= (scratch_pad_2
4473 & MR_MAX_REPLY_QUEUES_OFFSET
) + 1;
4474 fw_msix_count
= instance
->msix_vectors
;
4476 instance
->msix_vectors
=
4478 instance
->msix_vectors
);
4479 } else if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)
4480 || (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
4481 /* Invader/Fury supports more than 8 MSI-X */
4482 instance
->msix_vectors
= ((scratch_pad_2
4483 & MR_MAX_REPLY_QUEUES_EXT_OFFSET
)
4484 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT
) + 1;
4485 fw_msix_count
= instance
->msix_vectors
;
4486 /* Save 1-15 reply post index address to local memory
4487 * Index 0 is already saved from reg offset
4488 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4490 for (loop
= 1; loop
< MR_MAX_MSIX_REG_ARRAY
; loop
++) {
4491 instance
->reply_post_host_index_addr
[loop
] =
4492 (u32
*)((u8
*)instance
->reg_set
+
4493 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4497 instance
->msix_vectors
= min(msix_vectors
,
4498 instance
->msix_vectors
);
4500 instance
->msix_vectors
= 1;
4501 /* Don't bother allocating more MSI-X vectors than cpus */
4502 instance
->msix_vectors
= min(instance
->msix_vectors
,
4503 (unsigned int)num_online_cpus());
4504 for (i
= 0; i
< instance
->msix_vectors
; i
++)
4505 instance
->msixentry
[i
].entry
= i
;
4506 i
= pci_enable_msix_range(instance
->pdev
, instance
->msixentry
,
4507 1, instance
->msix_vectors
);
4509 instance
->msix_vectors
= i
;
4511 instance
->msix_vectors
= 0;
4513 dev_info(&instance
->pdev
->dev
, "[scsi%d]: FW supports"
4514 "<%d> MSIX vector,Online CPUs: <%d>,"
4515 "Current MSIX <%d>\n", instance
->host
->host_no
,
4516 fw_msix_count
, (unsigned int)num_online_cpus(),
4517 instance
->msix_vectors
);
4520 instance
->ctrl_info
= kzalloc(sizeof(struct megasas_ctrl_info
),
4522 if (instance
->ctrl_info
== NULL
)
4523 goto fail_init_adapter
;
4526 * Below are default value for legacy Firmware.
4527 * non-fusion based controllers
4529 instance
->fw_supported_vd_count
= MAX_LOGICAL_DRIVES
;
4530 instance
->fw_supported_pd_count
= MAX_PHYSICAL_DEVICES
;
4531 /* Get operational params, sge flags, send init cmd to controller */
4532 if (instance
->instancet
->init_adapter(instance
))
4533 goto fail_init_adapter
;
4535 printk(KERN_ERR
"megasas: INIT adapter done\n");
4538 * the following function will get the PD LIST.
4541 memset(instance
->pd_list
, 0 ,
4542 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
4543 if (megasas_get_pd_list(instance
) < 0) {
4544 printk(KERN_ERR
"megasas: failed to get PD list\n");
4545 goto fail_init_adapter
;
4548 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
4549 if (megasas_ld_list_query(instance
,
4550 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
4551 megasas_get_ld_list(instance
);
4554 * Compute the max allowed sectors per IO: The controller info has two
4555 * limits on max sectors. Driver should use the minimum of these two.
4557 * 1 << stripe_sz_ops.min = max sectors per strip
4559 * Note that older firmwares ( < FW ver 30) didn't report information
4560 * to calculate max_sectors_1. So the number ended up as zero always.
4563 ctrl_info
= instance
->ctrl_info
;
4565 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
4566 le16_to_cpu(ctrl_info
->max_strips_per_io
);
4567 max_sectors_2
= le32_to_cpu(ctrl_info
->max_request_size
);
4569 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
4571 /*Check whether controller is iMR or MR */
4572 if (ctrl_info
->memory_size
) {
4573 instance
->is_imr
= 0;
4574 dev_info(&instance
->pdev
->dev
, "Controller type: MR,"
4575 "Memory size is: %dMB\n",
4576 le16_to_cpu(ctrl_info
->memory_size
));
4578 instance
->is_imr
= 1;
4579 dev_info(&instance
->pdev
->dev
,
4580 "Controller type: iMR\n");
4582 instance
->disableOnlineCtrlReset
=
4583 ctrl_info
->properties
.OnOffProperties
.disableOnlineCtrlReset
;
4584 instance
->mpio
= ctrl_info
->adapterOperations2
.mpio
;
4585 instance
->UnevenSpanSupport
=
4586 ctrl_info
->adapterOperations2
.supportUnevenSpans
;
4587 if (instance
->UnevenSpanSupport
) {
4588 struct fusion_context
*fusion
= instance
->ctrl_context
;
4590 dev_info(&instance
->pdev
->dev
, "FW supports: "
4591 "UnevenSpanSupport=%x\n", instance
->UnevenSpanSupport
);
4592 if (MR_ValidateMapInfo(instance
))
4593 fusion
->fast_path_io
= 1;
4595 fusion
->fast_path_io
= 0;
4598 if (ctrl_info
->host_interface
.SRIOV
) {
4599 if (!ctrl_info
->adapterOperations2
.activePassive
)
4600 instance
->PlasmaFW111
= 1;
4602 if (!instance
->PlasmaFW111
)
4603 instance
->requestorId
=
4604 ctrl_info
->iov
.requestorId
;
4606 iovPtr
= (struct IOV_111
*)((unsigned char *)ctrl_info
+ IOV_111_OFFSET
);
4607 instance
->requestorId
= iovPtr
->requestorId
;
4609 dev_warn(&instance
->pdev
->dev
, "I am VF "
4610 "requestorId %d\n", instance
->requestorId
);
4613 instance
->crash_dump_fw_support
=
4614 ctrl_info
->adapterOperations3
.supportCrashDump
;
4615 instance
->crash_dump_drv_support
=
4616 (instance
->crash_dump_fw_support
&&
4617 instance
->crash_dump_buf
);
4618 if (instance
->crash_dump_drv_support
) {
4619 dev_info(&instance
->pdev
->dev
, "Firmware Crash dump "
4620 "feature is supported\n");
4621 megasas_set_crash_dump_params(instance
,
4622 MR_CRASH_BUF_TURN_OFF
);
4625 if (instance
->crash_dump_buf
)
4626 pci_free_consistent(instance
->pdev
,
4628 instance
->crash_dump_buf
,
4629 instance
->crash_dump_h
);
4630 instance
->crash_dump_buf
= NULL
;
4632 instance
->max_sectors_per_req
= instance
->max_num_sge
*
4634 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
4635 instance
->max_sectors_per_req
= tmp_sectors
;
4637 /* Check for valid throttlequeuedepth module parameter */
4638 if (instance
->is_imr
) {
4639 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
4640 MEGASAS_SKINNY_INT_CMDS
))
4641 instance
->throttlequeuedepth
=
4642 MEGASAS_THROTTLE_QUEUE_DEPTH
;
4644 instance
->throttlequeuedepth
= throttlequeuedepth
;
4646 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
4648 instance
->throttlequeuedepth
=
4649 MEGASAS_THROTTLE_QUEUE_DEPTH
;
4651 instance
->throttlequeuedepth
= throttlequeuedepth
;
4655 * Setup tasklet for cmd completion
4658 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
4659 (unsigned long)instance
);
4661 /* Launch SR-IOV heartbeat timer */
4662 if (instance
->requestorId
) {
4663 if (!megasas_sriov_start_heartbeat(instance
, 1))
4664 megasas_start_timer(instance
,
4665 &instance
->sriov_heartbeat_timer
,
4666 megasas_sriov_heartbeat_handler
,
4667 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
4669 instance
->skip_heartbeat_timer_del
= 1;
4676 kfree(instance
->ctrl_info
);
4677 instance
->ctrl_info
= NULL
;
4678 iounmap(instance
->reg_set
);
4681 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
4687 * megasas_release_mfi - Reverses the FW initialization
4688 * @intance: Adapter soft state
4690 static void megasas_release_mfi(struct megasas_instance
*instance
)
4692 u32 reply_q_sz
= sizeof(u32
) *(instance
->max_mfi_cmds
+ 1);
4694 if (instance
->reply_queue
)
4695 pci_free_consistent(instance
->pdev
, reply_q_sz
,
4696 instance
->reply_queue
, instance
->reply_queue_h
);
4698 megasas_free_cmds(instance
);
4700 iounmap(instance
->reg_set
);
4702 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
4706 * megasas_get_seq_num - Gets latest event sequence numbers
4707 * @instance: Adapter soft state
4708 * @eli: FW event log sequence numbers information
4710 * FW maintains a log of all events in a non-volatile area. Upper layers would
4711 * usually find out the latest sequence number of the events, the seq number at
4712 * the boot etc. They would "read" all the events below the latest seq number
4713 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4714 * number), they would subsribe to AEN (asynchronous event notification) and
4715 * wait for the events to happen.
4718 megasas_get_seq_num(struct megasas_instance
*instance
,
4719 struct megasas_evt_log_info
*eli
)
4721 struct megasas_cmd
*cmd
;
4722 struct megasas_dcmd_frame
*dcmd
;
4723 struct megasas_evt_log_info
*el_info
;
4724 dma_addr_t el_info_h
= 0;
4726 cmd
= megasas_get_cmd(instance
);
4732 dcmd
= &cmd
->frame
->dcmd
;
4733 el_info
= pci_alloc_consistent(instance
->pdev
,
4734 sizeof(struct megasas_evt_log_info
),
4738 megasas_return_cmd(instance
, cmd
);
4742 memset(el_info
, 0, sizeof(*el_info
));
4743 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4745 dcmd
->cmd
= MFI_CMD_DCMD
;
4746 dcmd
->cmd_status
= 0x0;
4747 dcmd
->sge_count
= 1;
4748 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
4751 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_evt_log_info
));
4752 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO
);
4753 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(el_info_h
);
4754 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_evt_log_info
));
4756 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
4757 dev_err(&instance
->pdev
->dev
, "Command timedout"
4758 "from %s\n", __func__
);
4761 * Copy the data back into callers buffer
4763 eli
->newest_seq_num
= le32_to_cpu(el_info
->newest_seq_num
);
4764 eli
->oldest_seq_num
= le32_to_cpu(el_info
->oldest_seq_num
);
4765 eli
->clear_seq_num
= le32_to_cpu(el_info
->clear_seq_num
);
4766 eli
->shutdown_seq_num
= le32_to_cpu(el_info
->shutdown_seq_num
);
4767 eli
->boot_seq_num
= le32_to_cpu(el_info
->boot_seq_num
);
4770 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
4771 el_info
, el_info_h
);
4773 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
4774 megasas_return_mfi_mpt_pthr(instance
, cmd
,
4775 cmd
->mpt_pthr_cmd_blocked
);
4777 megasas_return_cmd(instance
, cmd
);
4783 * megasas_register_aen - Registers for asynchronous event notification
4784 * @instance: Adapter soft state
4785 * @seq_num: The starting sequence number
4786 * @class_locale: Class of the event
4788 * This function subscribes for AEN for events beyond the @seq_num. It requests
4789 * to be notified if and only if the event is of type @class_locale
4792 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
4793 u32 class_locale_word
)
4796 struct megasas_cmd
*cmd
;
4797 struct megasas_dcmd_frame
*dcmd
;
4798 union megasas_evt_class_locale curr_aen
;
4799 union megasas_evt_class_locale prev_aen
;
4802 * If there an AEN pending already (aen_cmd), check if the
4803 * class_locale of that pending AEN is inclusive of the new
4804 * AEN request we currently have. If it is, then we don't have
4805 * to do anything. In other words, whichever events the current
4806 * AEN request is subscribing to, have already been subscribed
4809 * If the old_cmd is _not_ inclusive, then we have to abort
4810 * that command, form a class_locale that is superset of both
4811 * old and current and re-issue to the FW
4814 curr_aen
.word
= class_locale_word
;
4816 if (instance
->aen_cmd
) {
4818 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
4819 prev_aen
.members
.locale
= le16_to_cpu(prev_aen
.members
.locale
);
4822 * A class whose enum value is smaller is inclusive of all
4823 * higher values. If a PROGRESS (= -1) was previously
4824 * registered, then a new registration requests for higher
4825 * classes need not be sent to FW. They are automatically
4828 * Locale numbers don't have such hierarchy. They are bitmap
4831 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
4832 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
4833 curr_aen
.members
.locale
)) {
4835 * Previously issued event registration includes
4836 * current request. Nothing to do.
4840 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
4842 if (prev_aen
.members
.class < curr_aen
.members
.class)
4843 curr_aen
.members
.class = prev_aen
.members
.class;
4845 instance
->aen_cmd
->abort_aen
= 1;
4846 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
4851 printk(KERN_DEBUG
"megasas: Failed to abort "
4852 "previous AEN command\n");
4858 cmd
= megasas_get_cmd(instance
);
4863 dcmd
= &cmd
->frame
->dcmd
;
4865 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
4868 * Prepare DCMD for aen registration
4870 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4872 dcmd
->cmd
= MFI_CMD_DCMD
;
4873 dcmd
->cmd_status
= 0x0;
4874 dcmd
->sge_count
= 1;
4875 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
4878 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_evt_detail
));
4879 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT
);
4880 dcmd
->mbox
.w
[0] = cpu_to_le32(seq_num
);
4881 instance
->last_seq_num
= seq_num
;
4882 dcmd
->mbox
.w
[1] = cpu_to_le32(curr_aen
.word
);
4883 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(instance
->evt_detail_h
);
4884 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_evt_detail
));
4886 if (instance
->aen_cmd
!= NULL
) {
4887 megasas_return_cmd(instance
, cmd
);
4892 * Store reference to the cmd used to register for AEN. When an
4893 * application wants us to register for AEN, we have to abort this
4894 * cmd and re-register with a new EVENT LOCALE supplied by that app
4896 instance
->aen_cmd
= cmd
;
4899 * Issue the aen registration frame
4901 instance
->instancet
->issue_dcmd(instance
, cmd
);
4907 * megasas_start_aen - Subscribes to AEN during driver load time
4908 * @instance: Adapter soft state
4910 static int megasas_start_aen(struct megasas_instance
*instance
)
4912 struct megasas_evt_log_info eli
;
4913 union megasas_evt_class_locale class_locale
;
4916 * Get the latest sequence number from FW
4918 memset(&eli
, 0, sizeof(eli
));
4920 if (megasas_get_seq_num(instance
, &eli
))
4924 * Register AEN with FW for latest sequence number plus 1
4926 class_locale
.members
.reserved
= 0;
4927 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
4928 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
4930 return megasas_register_aen(instance
,
4931 eli
.newest_seq_num
+ 1,
4936 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4937 * @instance: Adapter soft state
4939 static int megasas_io_attach(struct megasas_instance
*instance
)
4941 struct Scsi_Host
*host
= instance
->host
;
4944 * Export parameters required by SCSI mid-layer
4946 host
->irq
= instance
->pdev
->irq
;
4947 host
->unique_id
= instance
->unique_id
;
4948 if (instance
->is_imr
) {
4950 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
4953 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
4954 host
->this_id
= instance
->init_id
;
4955 host
->sg_tablesize
= instance
->max_num_sge
;
4957 if (instance
->fw_support_ieee
)
4958 instance
->max_sectors_per_req
= MEGASAS_MAX_SECTORS_IEEE
;
4961 * Check if the module parameter value for max_sectors can be used
4963 if (max_sectors
&& max_sectors
< instance
->max_sectors_per_req
)
4964 instance
->max_sectors_per_req
= max_sectors
;
4967 if (((instance
->pdev
->device
==
4968 PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
4969 (instance
->pdev
->device
==
4970 PCI_DEVICE_ID_LSI_SAS0079GEN2
)) &&
4971 (max_sectors
<= MEGASAS_MAX_SECTORS
)) {
4972 instance
->max_sectors_per_req
= max_sectors
;
4974 printk(KERN_INFO
"megasas: max_sectors should be > 0"
4975 "and <= %d (or < 1MB for GEN2 controller)\n",
4976 instance
->max_sectors_per_req
);
4981 host
->max_sectors
= instance
->max_sectors_per_req
;
4982 host
->cmd_per_lun
= MEGASAS_DEFAULT_CMD_PER_LUN
;
4983 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
4984 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
4985 host
->max_lun
= MEGASAS_MAX_LUN
;
4986 host
->max_cmd_len
= 16;
4988 /* Fusion only supports host reset */
4989 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4990 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
4991 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4992 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
4993 host
->hostt
->eh_device_reset_handler
= NULL
;
4994 host
->hostt
->eh_bus_reset_handler
= NULL
;
4998 * Notify the mid-layer about the new controller
5000 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
5001 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
5009 megasas_set_dma_mask(struct pci_dev
*pdev
)
5012 * All our contollers are capable of performing 64-bit DMA
5015 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
5017 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
5018 goto fail_set_dma_mask
;
5021 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
5022 goto fail_set_dma_mask
;
5025 * Ensure that all data structures are allocated in 32-bit
5028 if (pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0) {
5029 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5030 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))
5031 && !pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)))
5032 dev_info(&pdev
->dev
, "set 32bit DMA mask"
5033 "and 32 bit consistent mask\n");
5035 goto fail_set_dma_mask
;
5045 * megasas_probe_one - PCI hotplug entry point
5046 * @pdev: PCI device structure
5047 * @id: PCI ids of supported hotplugged adapter
5049 static int megasas_probe_one(struct pci_dev
*pdev
,
5050 const struct pci_device_id
*id
)
5052 int rval
, pos
, i
, j
, cpu
;
5053 struct Scsi_Host
*host
;
5054 struct megasas_instance
*instance
;
5056 struct fusion_context
*fusion
= NULL
;
5058 /* Reset MSI-X in the kdump kernel */
5059 if (reset_devices
) {
5060 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
5062 pci_read_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
,
5064 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
5065 dev_info(&pdev
->dev
, "resetting MSI-X\n");
5066 pci_write_config_word(pdev
,
5067 pos
+ PCI_MSIX_FLAGS
,
5069 ~PCI_MSIX_FLAGS_ENABLE
);
5075 * Announce PCI information
5077 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
5078 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
5079 pdev
->subsystem_device
);
5081 printk("bus %d:slot %d:func %d\n",
5082 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
5085 * PCI prepping: enable device set bus mastering and dma mask
5087 rval
= pci_enable_device_mem(pdev
);
5093 pci_set_master(pdev
);
5095 if (megasas_set_dma_mask(pdev
))
5096 goto fail_set_dma_mask
;
5098 host
= scsi_host_alloc(&megasas_template
,
5099 sizeof(struct megasas_instance
));
5102 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
5103 goto fail_alloc_instance
;
5106 instance
= (struct megasas_instance
*)host
->hostdata
;
5107 memset(instance
, 0, sizeof(*instance
));
5108 atomic_set( &instance
->fw_reset_no_pci_access
, 0 );
5109 instance
->pdev
= pdev
;
5111 switch (instance
->pdev
->device
) {
5112 case PCI_DEVICE_ID_LSI_FUSION
:
5113 case PCI_DEVICE_ID_LSI_PLASMA
:
5114 case PCI_DEVICE_ID_LSI_INVADER
:
5115 case PCI_DEVICE_ID_LSI_FURY
:
5117 instance
->ctrl_context_pages
=
5118 get_order(sizeof(struct fusion_context
));
5119 instance
->ctrl_context
= (void *)__get_free_pages(GFP_KERNEL
,
5120 instance
->ctrl_context_pages
);
5121 if (!instance
->ctrl_context
) {
5122 printk(KERN_DEBUG
"megasas: Failed to allocate "
5123 "memory for Fusion context info\n");
5124 goto fail_alloc_dma_buf
;
5126 fusion
= instance
->ctrl_context
;
5128 ((1 << PAGE_SHIFT
) << instance
->ctrl_context_pages
));
5129 INIT_LIST_HEAD(&fusion
->cmd_pool
);
5130 spin_lock_init(&fusion
->mpt_pool_lock
);
5131 memset(fusion
->load_balance_info
, 0,
5132 sizeof(struct LD_LOAD_BALANCE_INFO
) * MAX_LOGICAL_DRIVES_EXT
);
5135 default: /* For all other supported controllers */
5137 instance
->producer
=
5138 pci_alloc_consistent(pdev
, sizeof(u32
),
5139 &instance
->producer_h
);
5140 instance
->consumer
=
5141 pci_alloc_consistent(pdev
, sizeof(u32
),
5142 &instance
->consumer_h
);
5144 if (!instance
->producer
|| !instance
->consumer
) {
5145 printk(KERN_DEBUG
"megasas: Failed to allocate"
5146 "memory for producer, consumer\n");
5147 goto fail_alloc_dma_buf
;
5150 *instance
->producer
= 0;
5151 *instance
->consumer
= 0;
5155 /* Crash dump feature related initialisation*/
5156 instance
->drv_buf_index
= 0;
5157 instance
->drv_buf_alloc
= 0;
5158 instance
->crash_dump_fw_support
= 0;
5159 instance
->crash_dump_app_support
= 0;
5160 instance
->fw_crash_state
= UNAVAILABLE
;
5161 spin_lock_init(&instance
->crashdump_lock
);
5162 instance
->crash_dump_buf
= NULL
;
5165 instance
->crash_dump_buf
= pci_alloc_consistent(pdev
,
5167 &instance
->crash_dump_h
);
5168 if (!instance
->crash_dump_buf
)
5169 dev_err(&instance
->pdev
->dev
, "Can't allocate Firmware "
5170 "crash dump DMA buffer\n");
5172 megasas_poll_wait_aen
= 0;
5173 instance
->flag_ieee
= 0;
5174 instance
->ev
= NULL
;
5175 instance
->issuepend_done
= 1;
5176 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
5177 instance
->is_imr
= 0;
5179 instance
->evt_detail
= pci_alloc_consistent(pdev
,
5181 megasas_evt_detail
),
5182 &instance
->evt_detail_h
);
5184 if (!instance
->evt_detail
) {
5185 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
5186 "event detail structure\n");
5187 goto fail_alloc_dma_buf
;
5191 * Initialize locks and queues
5193 INIT_LIST_HEAD(&instance
->cmd_pool
);
5194 INIT_LIST_HEAD(&instance
->internal_reset_pending_q
);
5196 atomic_set(&instance
->fw_outstanding
,0);
5198 init_waitqueue_head(&instance
->int_cmd_wait_q
);
5199 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
5201 spin_lock_init(&instance
->mfi_pool_lock
);
5202 spin_lock_init(&instance
->hba_lock
);
5203 spin_lock_init(&instance
->completion_lock
);
5205 mutex_init(&instance
->aen_mutex
);
5206 mutex_init(&instance
->reset_mutex
);
5209 * Initialize PCI related and misc parameters
5211 instance
->host
= host
;
5212 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
5213 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
5214 instance
->ctrl_info
= NULL
;
5216 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
5217 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
5218 instance
->flag_ieee
= 1;
5219 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
5221 sema_init(&instance
->ioctl_sem
, (MEGASAS_INT_CMDS
- 5));
5223 megasas_dbg_lvl
= 0;
5225 instance
->unload
= 1;
5226 instance
->last_time
= 0;
5227 instance
->disableOnlineCtrlReset
= 1;
5228 instance
->UnevenSpanSupport
= 0;
5230 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
5231 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
5232 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
5233 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
5234 INIT_WORK(&instance
->work_init
, megasas_fusion_ocr_wq
);
5235 INIT_WORK(&instance
->crash_init
, megasas_fusion_crash_dump_wq
);
5237 INIT_WORK(&instance
->work_init
, process_fw_state_change_wq
);
5240 * Initialize MFI Firmware
5242 if (megasas_init_fw(instance
))
5245 if (instance
->requestorId
) {
5246 if (instance
->PlasmaFW111
) {
5247 instance
->vf_affiliation_111
=
5248 pci_alloc_consistent(pdev
, sizeof(struct MR_LD_VF_AFFILIATION_111
),
5249 &instance
->vf_affiliation_111_h
);
5250 if (!instance
->vf_affiliation_111
)
5251 printk(KERN_WARNING
"megasas: Can't allocate "
5252 "memory for VF affiliation buffer\n");
5254 instance
->vf_affiliation
=
5255 pci_alloc_consistent(pdev
,
5256 (MAX_LOGICAL_DRIVES
+ 1) *
5257 sizeof(struct MR_LD_VF_AFFILIATION
),
5258 &instance
->vf_affiliation_h
);
5259 if (!instance
->vf_affiliation
)
5260 printk(KERN_WARNING
"megasas: Can't allocate "
5261 "memory for VF affiliation buffer\n");
5269 if (instance
->msix_vectors
) {
5270 cpu
= cpumask_first(cpu_online_mask
);
5271 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5272 instance
->irq_context
[i
].instance
= instance
;
5273 instance
->irq_context
[i
].MSIxIndex
= i
;
5274 if (request_irq(instance
->msixentry
[i
].vector
,
5275 instance
->instancet
->service_isr
, 0,
5277 &instance
->irq_context
[i
])) {
5278 printk(KERN_DEBUG
"megasas: Failed to "
5279 "register IRQ for vector %d.\n", i
);
5280 for (j
= 0; j
< i
; j
++) {
5281 if (smp_affinity_enable
)
5282 irq_set_affinity_hint(
5283 instance
->msixentry
[j
].vector
, NULL
);
5285 instance
->msixentry
[j
].vector
,
5286 &instance
->irq_context
[j
]);
5288 /* Retry irq register for IO_APIC */
5289 instance
->msix_vectors
= 0;
5290 goto retry_irq_register
;
5292 if (smp_affinity_enable
) {
5293 if (irq_set_affinity_hint(instance
->msixentry
[i
].vector
,
5295 dev_err(&instance
->pdev
->dev
,
5296 "Error setting affinity hint "
5297 "for cpu %d\n", cpu
);
5298 cpu
= cpumask_next(cpu
, cpu_online_mask
);
5302 instance
->irq_context
[0].instance
= instance
;
5303 instance
->irq_context
[0].MSIxIndex
= 0;
5304 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
5305 IRQF_SHARED
, "megasas",
5306 &instance
->irq_context
[0])) {
5307 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
5312 instance
->instancet
->enable_intr(instance
);
5315 * Store instance in PCI softstate
5317 pci_set_drvdata(pdev
, instance
);
5320 * Add this controller to megasas_mgmt_info structure so that it
5321 * can be exported to management applications
5323 megasas_mgmt_info
.count
++;
5324 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
5325 megasas_mgmt_info
.max_index
++;
5328 * Register with SCSI mid-layer
5330 if (megasas_io_attach(instance
))
5331 goto fail_io_attach
;
5333 instance
->unload
= 0;
5335 * Trigger SCSI to scan our drives
5337 scsi_scan_host(host
);
5340 * Initiate AEN (Asynchronous Event Notification)
5342 if (megasas_start_aen(instance
)) {
5343 printk(KERN_DEBUG
"megasas: start aen failed\n");
5344 goto fail_start_aen
;
5347 /* Get current SR-IOV LD/VF affiliation */
5348 if (instance
->requestorId
)
5349 megasas_get_ld_vf_affiliation(instance
, 1);
5355 megasas_mgmt_info
.count
--;
5356 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
5357 megasas_mgmt_info
.max_index
--;
5359 instance
->instancet
->disable_intr(instance
);
5360 if (instance
->msix_vectors
)
5361 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5362 if (smp_affinity_enable
)
5363 irq_set_affinity_hint(
5364 instance
->msixentry
[i
].vector
, NULL
);
5365 free_irq(instance
->msixentry
[i
].vector
,
5366 &instance
->irq_context
[i
]);
5369 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5371 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
5372 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
5373 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
5374 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
5375 megasas_release_fusion(instance
);
5377 megasas_release_mfi(instance
);
5379 if (instance
->msix_vectors
)
5380 pci_disable_msix(instance
->pdev
);
5382 if (instance
->evt_detail
)
5383 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
5384 instance
->evt_detail
,
5385 instance
->evt_detail_h
);
5387 if (instance
->producer
)
5388 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
5389 instance
->producer_h
);
5390 if (instance
->consumer
)
5391 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
5392 instance
->consumer_h
);
5393 scsi_host_put(host
);
5395 fail_alloc_instance
:
5397 pci_disable_device(pdev
);
5403 * megasas_flush_cache - Requests FW to flush all its caches
5404 * @instance: Adapter soft state
5406 static void megasas_flush_cache(struct megasas_instance
*instance
)
5408 struct megasas_cmd
*cmd
;
5409 struct megasas_dcmd_frame
*dcmd
;
5411 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
5414 cmd
= megasas_get_cmd(instance
);
5419 dcmd
= &cmd
->frame
->dcmd
;
5421 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
5423 dcmd
->cmd
= MFI_CMD_DCMD
;
5424 dcmd
->cmd_status
= 0x0;
5425 dcmd
->sge_count
= 0;
5426 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_NONE
);
5429 dcmd
->data_xfer_len
= 0;
5430 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH
);
5431 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
5433 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
5434 dev_err(&instance
->pdev
->dev
, "Command timedout"
5435 " from %s\n", __func__
);
5437 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
5438 megasas_return_mfi_mpt_pthr(instance
, cmd
,
5439 cmd
->mpt_pthr_cmd_blocked
);
5441 megasas_return_cmd(instance
, cmd
);
5447 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5448 * @instance: Adapter soft state
5449 * @opcode: Shutdown/Hibernate
5451 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
5454 struct megasas_cmd
*cmd
;
5455 struct megasas_dcmd_frame
*dcmd
;
5457 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
5460 cmd
= megasas_get_cmd(instance
);
5465 if (instance
->aen_cmd
)
5466 megasas_issue_blocked_abort_cmd(instance
,
5467 instance
->aen_cmd
, 30);
5468 if (instance
->map_update_cmd
)
5469 megasas_issue_blocked_abort_cmd(instance
,
5470 instance
->map_update_cmd
, 30);
5471 dcmd
= &cmd
->frame
->dcmd
;
5473 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
5475 dcmd
->cmd
= MFI_CMD_DCMD
;
5476 dcmd
->cmd_status
= 0x0;
5477 dcmd
->sge_count
= 0;
5478 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_NONE
);
5481 dcmd
->data_xfer_len
= 0;
5482 dcmd
->opcode
= cpu_to_le32(opcode
);
5484 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
5485 dev_err(&instance
->pdev
->dev
, "Command timedout"
5486 "from %s\n", __func__
);
5488 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
5489 megasas_return_mfi_mpt_pthr(instance
, cmd
,
5490 cmd
->mpt_pthr_cmd_blocked
);
5492 megasas_return_cmd(instance
, cmd
);
5499 * megasas_suspend - driver suspend entry point
5500 * @pdev: PCI device structure
5501 * @state: PCI power state to suspend routine
5504 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5506 struct Scsi_Host
*host
;
5507 struct megasas_instance
*instance
;
5510 instance
= pci_get_drvdata(pdev
);
5511 host
= instance
->host
;
5512 instance
->unload
= 1;
5514 /* Shutdown SR-IOV heartbeat timer */
5515 if (instance
->requestorId
&& !instance
->skip_heartbeat_timer_del
)
5516 del_timer_sync(&instance
->sriov_heartbeat_timer
);
5518 megasas_flush_cache(instance
);
5519 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
5521 /* cancel the delayed work if this work still in queue */
5522 if (instance
->ev
!= NULL
) {
5523 struct megasas_aen_event
*ev
= instance
->ev
;
5524 cancel_delayed_work_sync(&ev
->hotplug_work
);
5525 instance
->ev
= NULL
;
5528 tasklet_kill(&instance
->isr_tasklet
);
5530 pci_set_drvdata(instance
->pdev
, instance
);
5531 instance
->instancet
->disable_intr(instance
);
5533 if (instance
->msix_vectors
)
5534 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5535 if (smp_affinity_enable
)
5536 irq_set_affinity_hint(
5537 instance
->msixentry
[i
].vector
, NULL
);
5538 free_irq(instance
->msixentry
[i
].vector
,
5539 &instance
->irq_context
[i
]);
5542 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5543 if (instance
->msix_vectors
)
5544 pci_disable_msix(instance
->pdev
);
5546 pci_save_state(pdev
);
5547 pci_disable_device(pdev
);
5549 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
5555 * megasas_resume- driver resume entry point
5556 * @pdev: PCI device structure
5559 megasas_resume(struct pci_dev
*pdev
)
5561 int rval
, i
, j
, cpu
;
5562 struct Scsi_Host
*host
;
5563 struct megasas_instance
*instance
;
5565 instance
= pci_get_drvdata(pdev
);
5566 host
= instance
->host
;
5567 pci_set_power_state(pdev
, PCI_D0
);
5568 pci_enable_wake(pdev
, PCI_D0
, 0);
5569 pci_restore_state(pdev
);
5572 * PCI prepping: enable device set bus mastering and dma mask
5574 rval
= pci_enable_device_mem(pdev
);
5577 printk(KERN_ERR
"megasas: Enable device failed\n");
5581 pci_set_master(pdev
);
5583 if (megasas_set_dma_mask(pdev
))
5584 goto fail_set_dma_mask
;
5587 * Initialize MFI Firmware
5590 atomic_set(&instance
->fw_outstanding
, 0);
5593 * We expect the FW state to be READY
5595 if (megasas_transition_to_ready(instance
, 0))
5596 goto fail_ready_state
;
5598 /* Now re-enable MSI-X */
5599 if (instance
->msix_vectors
&&
5600 pci_enable_msix_exact(instance
->pdev
, instance
->msixentry
,
5601 instance
->msix_vectors
))
5602 goto fail_reenable_msix
;
5604 switch (instance
->pdev
->device
) {
5605 case PCI_DEVICE_ID_LSI_FUSION
:
5606 case PCI_DEVICE_ID_LSI_PLASMA
:
5607 case PCI_DEVICE_ID_LSI_INVADER
:
5608 case PCI_DEVICE_ID_LSI_FURY
:
5610 megasas_reset_reply_desc(instance
);
5611 if (megasas_ioc_init_fusion(instance
)) {
5612 megasas_free_cmds(instance
);
5613 megasas_free_cmds_fusion(instance
);
5616 if (!megasas_get_map_info(instance
))
5617 megasas_sync_map_info(instance
);
5621 *instance
->producer
= 0;
5622 *instance
->consumer
= 0;
5623 if (megasas_issue_init_mfi(instance
))
5628 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
5629 (unsigned long)instance
);
5634 if (instance
->msix_vectors
) {
5635 cpu
= cpumask_first(cpu_online_mask
);
5636 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
5637 instance
->irq_context
[i
].instance
= instance
;
5638 instance
->irq_context
[i
].MSIxIndex
= i
;
5639 if (request_irq(instance
->msixentry
[i
].vector
,
5640 instance
->instancet
->service_isr
, 0,
5642 &instance
->irq_context
[i
])) {
5643 printk(KERN_DEBUG
"megasas: Failed to "
5644 "register IRQ for vector %d.\n", i
);
5645 for (j
= 0; j
< i
; j
++) {
5646 if (smp_affinity_enable
)
5647 irq_set_affinity_hint(
5648 instance
->msixentry
[j
].vector
, NULL
);
5650 instance
->msixentry
[j
].vector
,
5651 &instance
->irq_context
[j
]);
5656 if (smp_affinity_enable
) {
5657 if (irq_set_affinity_hint(instance
->msixentry
[i
].vector
,
5659 dev_err(&instance
->pdev
->dev
, "Error "
5660 "setting affinity hint for cpu "
5662 cpu
= cpumask_next(cpu
, cpu_online_mask
);
5666 instance
->irq_context
[0].instance
= instance
;
5667 instance
->irq_context
[0].MSIxIndex
= 0;
5668 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
5669 IRQF_SHARED
, "megasas",
5670 &instance
->irq_context
[0])) {
5671 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
5676 /* Re-launch SR-IOV heartbeat timer */
5677 if (instance
->requestorId
) {
5678 if (!megasas_sriov_start_heartbeat(instance
, 0))
5679 megasas_start_timer(instance
,
5680 &instance
->sriov_heartbeat_timer
,
5681 megasas_sriov_heartbeat_handler
,
5682 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
5684 instance
->skip_heartbeat_timer_del
= 1;
5687 instance
->instancet
->enable_intr(instance
);
5688 instance
->unload
= 0;
5691 * Initiate AEN (Asynchronous Event Notification)
5693 if (megasas_start_aen(instance
))
5694 printk(KERN_ERR
"megasas: Start AEN failed\n");
5700 if (instance
->evt_detail
)
5701 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
5702 instance
->evt_detail
,
5703 instance
->evt_detail_h
);
5705 if (instance
->producer
)
5706 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
5707 instance
->producer_h
);
5708 if (instance
->consumer
)
5709 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
5710 instance
->consumer_h
);
5711 scsi_host_put(host
);
5717 pci_disable_device(pdev
);
5722 #define megasas_suspend NULL
5723 #define megasas_resume NULL
5727 * megasas_detach_one - PCI hot"un"plug entry point
5728 * @pdev: PCI device structure
5730 static void megasas_detach_one(struct pci_dev
*pdev
)
5733 struct Scsi_Host
*host
;
5734 struct megasas_instance
*instance
;
5735 struct fusion_context
*fusion
;
5737 instance
= pci_get_drvdata(pdev
);
5738 instance
->unload
= 1;
5739 host
= instance
->host
;
5740 fusion
= instance
->ctrl_context
;
5742 /* Shutdown SR-IOV heartbeat timer */
5743 if (instance
->requestorId
&& !instance
->skip_heartbeat_timer_del
)
5744 del_timer_sync(&instance
->sriov_heartbeat_timer
);
5746 if (instance
->fw_crash_state
!= UNAVAILABLE
)
5747 megasas_free_host_crash_buffer(instance
);
5748 scsi_remove_host(instance
->host
);
5749 megasas_flush_cache(instance
);
5750 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
5752 /* cancel the delayed work if this work still in queue*/
5753 if (instance
->ev
!= NULL
) {
5754 struct megasas_aen_event
*ev
= instance
->ev
;
5755 cancel_delayed_work_sync(&ev
->hotplug_work
);
5756 instance
->ev
= NULL
;
5759 /* cancel all wait events */
5760 wake_up_all(&instance
->int_cmd_wait_q
);
5762 tasklet_kill(&instance
->isr_tasklet
);
5765 * Take the instance off the instance array. Note that we will not
5766 * decrement the max_index. We let this array be sparse array
5768 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
5769 if (megasas_mgmt_info
.instance
[i
] == instance
) {
5770 megasas_mgmt_info
.count
--;
5771 megasas_mgmt_info
.instance
[i
] = NULL
;
5777 instance
->instancet
->disable_intr(instance
);
5779 if (instance
->msix_vectors
)
5780 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5781 if (smp_affinity_enable
)
5782 irq_set_affinity_hint(
5783 instance
->msixentry
[i
].vector
, NULL
);
5784 free_irq(instance
->msixentry
[i
].vector
,
5785 &instance
->irq_context
[i
]);
5788 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5789 if (instance
->msix_vectors
)
5790 pci_disable_msix(instance
->pdev
);
5792 switch (instance
->pdev
->device
) {
5793 case PCI_DEVICE_ID_LSI_FUSION
:
5794 case PCI_DEVICE_ID_LSI_PLASMA
:
5795 case PCI_DEVICE_ID_LSI_INVADER
:
5796 case PCI_DEVICE_ID_LSI_FURY
:
5797 megasas_release_fusion(instance
);
5798 for (i
= 0; i
< 2 ; i
++) {
5799 if (fusion
->ld_map
[i
])
5800 dma_free_coherent(&instance
->pdev
->dev
,
5803 fusion
->ld_map_phys
[i
]);
5804 if (fusion
->ld_drv_map
[i
])
5805 free_pages((ulong
)fusion
->ld_drv_map
[i
],
5806 fusion
->drv_map_pages
);
5808 free_pages((ulong
)instance
->ctrl_context
,
5809 instance
->ctrl_context_pages
);
5812 megasas_release_mfi(instance
);
5813 pci_free_consistent(pdev
, sizeof(u32
),
5815 instance
->producer_h
);
5816 pci_free_consistent(pdev
, sizeof(u32
),
5818 instance
->consumer_h
);
5822 kfree(instance
->ctrl_info
);
5824 if (instance
->evt_detail
)
5825 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
5826 instance
->evt_detail
, instance
->evt_detail_h
);
5828 if (instance
->vf_affiliation
)
5829 pci_free_consistent(pdev
, (MAX_LOGICAL_DRIVES
+ 1) *
5830 sizeof(struct MR_LD_VF_AFFILIATION
),
5831 instance
->vf_affiliation
,
5832 instance
->vf_affiliation_h
);
5834 if (instance
->vf_affiliation_111
)
5835 pci_free_consistent(pdev
,
5836 sizeof(struct MR_LD_VF_AFFILIATION_111
),
5837 instance
->vf_affiliation_111
,
5838 instance
->vf_affiliation_111_h
);
5840 if (instance
->hb_host_mem
)
5841 pci_free_consistent(pdev
, sizeof(struct MR_CTRL_HB_HOST_MEM
),
5842 instance
->hb_host_mem
,
5843 instance
->hb_host_mem_h
);
5845 if (instance
->crash_dump_buf
)
5846 pci_free_consistent(pdev
, CRASH_DMA_BUF_SIZE
,
5847 instance
->crash_dump_buf
, instance
->crash_dump_h
);
5849 scsi_host_put(host
);
5851 pci_disable_device(pdev
);
5857 * megasas_shutdown - Shutdown entry point
5858 * @device: Generic device structure
5860 static void megasas_shutdown(struct pci_dev
*pdev
)
5863 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
5865 instance
->unload
= 1;
5866 megasas_flush_cache(instance
);
5867 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
5868 instance
->instancet
->disable_intr(instance
);
5869 if (instance
->msix_vectors
)
5870 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5871 if (smp_affinity_enable
)
5872 irq_set_affinity_hint(
5873 instance
->msixentry
[i
].vector
, NULL
);
5874 free_irq(instance
->msixentry
[i
].vector
,
5875 &instance
->irq_context
[i
]);
5878 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5879 if (instance
->msix_vectors
)
5880 pci_disable_msix(instance
->pdev
);
5884 * megasas_mgmt_open - char node "open" entry point
5886 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
5889 * Allow only those users with admin rights
5891 if (!capable(CAP_SYS_ADMIN
))
5898 * megasas_mgmt_fasync - Async notifier registration from applications
5900 * This function adds the calling process to a driver global queue. When an
5901 * event occurs, SIGIO will be sent to all processes in this queue.
5903 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
5907 mutex_lock(&megasas_async_queue_mutex
);
5909 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
5911 mutex_unlock(&megasas_async_queue_mutex
);
5914 /* For sanity check when we get ioctl */
5915 filep
->private_data
= filep
;
5919 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
5925 * megasas_mgmt_poll - char node "poll" entry point
5927 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
5930 unsigned long flags
;
5931 poll_wait(file
, &megasas_poll_wait
, wait
);
5932 spin_lock_irqsave(&poll_aen_lock
, flags
);
5933 if (megasas_poll_wait_aen
)
5934 mask
= (POLLIN
| POLLRDNORM
);
5938 megasas_poll_wait_aen
= 0;
5939 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
5944 * megasas_set_crash_dump_params_ioctl:
5945 * Send CRASH_DUMP_MODE DCMD to all controllers
5946 * @cmd: MFI command frame
5949 static int megasas_set_crash_dump_params_ioctl(
5950 struct megasas_cmd
*cmd
)
5952 struct megasas_instance
*local_instance
;
5956 crash_support
= cmd
->frame
->dcmd
.mbox
.w
[0];
5958 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
5959 local_instance
= megasas_mgmt_info
.instance
[i
];
5960 if (local_instance
&& local_instance
->crash_dump_drv_support
) {
5961 if ((local_instance
->adprecovery
==
5962 MEGASAS_HBA_OPERATIONAL
) &&
5963 !megasas_set_crash_dump_params(local_instance
,
5965 local_instance
->crash_dump_app_support
=
5967 dev_info(&local_instance
->pdev
->dev
,
5968 "Application firmware crash "
5969 "dump mode set success\n");
5972 dev_info(&local_instance
->pdev
->dev
,
5973 "Application firmware crash "
5974 "dump mode set failed\n");
5983 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5984 * @instance: Adapter soft state
5985 * @argp: User's ioctl packet
5988 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
5989 struct megasas_iocpacket __user
* user_ioc
,
5990 struct megasas_iocpacket
*ioc
)
5992 struct megasas_sge32
*kern_sge32
;
5993 struct megasas_cmd
*cmd
;
5994 void *kbuff_arr
[MAX_IOCTL_SGE
];
5995 dma_addr_t buf_handle
= 0;
5998 dma_addr_t sense_handle
;
5999 unsigned long *sense_ptr
;
6001 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
6003 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
6004 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
6005 ioc
->sge_count
, MAX_IOCTL_SGE
);
6009 cmd
= megasas_get_cmd(instance
);
6011 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
6016 * User's IOCTL packet has 2 frames (maximum). Copy those two
6017 * frames into our cmd's frames. cmd->frame's context will get
6018 * overwritten when we copy from user's frames. So set that value
6021 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
6022 cmd
->frame
->hdr
.context
= cpu_to_le32(cmd
->index
);
6023 cmd
->frame
->hdr
.pad_0
= 0;
6024 cmd
->frame
->hdr
.flags
&= cpu_to_le16(~(MFI_FRAME_IEEE
|
6026 MFI_FRAME_SENSE64
));
6028 if (cmd
->frame
->dcmd
.opcode
== MR_DRIVER_SET_APP_CRASHDUMP_MODE
) {
6029 error
= megasas_set_crash_dump_params_ioctl(cmd
);
6030 megasas_return_cmd(instance
, cmd
);
6035 * The management interface between applications and the fw uses
6036 * MFI frames. E.g, RAID configuration changes, LD property changes
6037 * etc are accomplishes through different kinds of MFI frames. The
6038 * driver needs to care only about substituting user buffers with
6039 * kernel buffers in SGLs. The location of SGL is embedded in the
6040 * struct iocpacket itself.
6042 kern_sge32
= (struct megasas_sge32
*)
6043 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
6046 * For each user buffer, create a mirror buffer and copy in
6048 for (i
= 0; i
< ioc
->sge_count
; i
++) {
6049 if (!ioc
->sgl
[i
].iov_len
)
6052 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
6053 ioc
->sgl
[i
].iov_len
,
6054 &buf_handle
, GFP_KERNEL
);
6055 if (!kbuff_arr
[i
]) {
6056 printk(KERN_DEBUG
"megasas: Failed to alloc "
6057 "kernel SGL buffer for IOCTL \n");
6063 * We don't change the dma_coherent_mask, so
6064 * pci_alloc_consistent only returns 32bit addresses
6066 kern_sge32
[i
].phys_addr
= cpu_to_le32(buf_handle
);
6067 kern_sge32
[i
].length
= cpu_to_le32(ioc
->sgl
[i
].iov_len
);
6070 * We created a kernel buffer corresponding to the
6071 * user buffer. Now copy in from the user buffer
6073 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
6074 (u32
) (ioc
->sgl
[i
].iov_len
))) {
6080 if (ioc
->sense_len
) {
6081 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
6082 &sense_handle
, GFP_KERNEL
);
6089 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
6090 *sense_ptr
= cpu_to_le32(sense_handle
);
6094 * Set the sync_cmd flag so that the ISR knows not to complete this
6095 * cmd to the SCSI mid-layer
6098 megasas_issue_blocked_cmd(instance
, cmd
, 0);
6101 if (instance
->unload
== 1) {
6102 dev_info(&instance
->pdev
->dev
, "Driver unload is in progress "
6103 "don't submit data to application\n");
6107 * copy out the kernel buffers to user buffers
6109 for (i
= 0; i
< ioc
->sge_count
; i
++) {
6110 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
6111 ioc
->sgl
[i
].iov_len
)) {
6118 * copy out the sense
6120 if (ioc
->sense_len
) {
6122 * sense_ptr points to the location that has the user
6123 * sense buffer address
6125 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
6128 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
6129 sense
, ioc
->sense_len
)) {
6130 printk(KERN_ERR
"megasas: Failed to copy out to user "
6138 * copy the status codes returned by the fw
6140 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
6141 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
6142 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
6148 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
6149 sense
, sense_handle
);
6152 for (i
= 0; i
< ioc
->sge_count
; i
++) {
6154 dma_free_coherent(&instance
->pdev
->dev
,
6155 le32_to_cpu(kern_sge32
[i
].length
),
6157 le32_to_cpu(kern_sge32
[i
].phys_addr
));
6158 kbuff_arr
[i
] = NULL
;
6161 if (instance
->ctrl_context
&& cmd
->mpt_pthr_cmd_blocked
)
6162 megasas_return_mfi_mpt_pthr(instance
, cmd
,
6163 cmd
->mpt_pthr_cmd_blocked
);
6165 megasas_return_cmd(instance
, cmd
);
6169 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
6171 struct megasas_iocpacket __user
*user_ioc
=
6172 (struct megasas_iocpacket __user
*)arg
;
6173 struct megasas_iocpacket
*ioc
;
6174 struct megasas_instance
*instance
;
6177 unsigned long flags
;
6178 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
6180 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
6184 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
6189 instance
= megasas_lookup_instance(ioc
->host_no
);
6195 /* Adjust ioctl wait time for VF mode */
6196 if (instance
->requestorId
)
6197 wait_time
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
6199 /* Block ioctls in VF mode */
6200 if (instance
->requestorId
&& !allow_vf_ioctls
) {
6205 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
6206 printk(KERN_ERR
"Controller in crit error\n");
6211 if (instance
->unload
== 1) {
6217 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
6219 if (down_interruptible(&instance
->ioctl_sem
)) {
6220 error
= -ERESTARTSYS
;
6224 for (i
= 0; i
< wait_time
; i
++) {
6226 spin_lock_irqsave(&instance
->hba_lock
, flags
);
6227 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
6228 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6231 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6233 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
6234 printk(KERN_NOTICE
"megasas: waiting"
6235 "for controller reset to finish\n");
6241 spin_lock_irqsave(&instance
->hba_lock
, flags
);
6242 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
6243 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6245 printk(KERN_ERR
"megaraid_sas: timed out while"
6246 "waiting for HBA to recover\n");
6250 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6252 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
6254 up(&instance
->ioctl_sem
);
6261 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
6263 struct megasas_instance
*instance
;
6264 struct megasas_aen aen
;
6267 unsigned long flags
;
6268 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
6270 if (file
->private_data
!= file
) {
6271 printk(KERN_DEBUG
"megasas: fasync_helper was not "
6276 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
6279 instance
= megasas_lookup_instance(aen
.host_no
);
6284 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
6288 if (instance
->unload
== 1) {
6292 for (i
= 0; i
< wait_time
; i
++) {
6294 spin_lock_irqsave(&instance
->hba_lock
, flags
);
6295 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
6296 spin_unlock_irqrestore(&instance
->hba_lock
,
6301 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6303 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
6304 printk(KERN_NOTICE
"megasas: waiting for"
6305 "controller reset to finish\n");
6311 spin_lock_irqsave(&instance
->hba_lock
, flags
);
6312 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
6313 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6314 printk(KERN_ERR
"megaraid_sas: timed out while waiting"
6315 "for HBA to recover.\n");
6318 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
6320 mutex_lock(&instance
->aen_mutex
);
6321 error
= megasas_register_aen(instance
, aen
.seq_num
,
6322 aen
.class_locale_word
);
6323 mutex_unlock(&instance
->aen_mutex
);
6328 * megasas_mgmt_ioctl - char node ioctl entry point
6331 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
6334 case MEGASAS_IOC_FIRMWARE
:
6335 return megasas_mgmt_ioctl_fw(file
, arg
);
6337 case MEGASAS_IOC_GET_AEN
:
6338 return megasas_mgmt_ioctl_aen(file
, arg
);
6344 #ifdef CONFIG_COMPAT
6345 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
6347 struct compat_megasas_iocpacket __user
*cioc
=
6348 (struct compat_megasas_iocpacket __user
*)arg
;
6349 struct megasas_iocpacket __user
*ioc
=
6350 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
6355 if (clear_user(ioc
, sizeof(*ioc
)))
6358 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
6359 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
6360 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
6361 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
6362 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
6363 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
6367 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6368 * sense_len is not null, so prepare the 64bit value under
6369 * the same condition.
6371 if (ioc
->sense_len
) {
6372 void __user
**sense_ioc_ptr
=
6373 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
6374 compat_uptr_t
*sense_cioc_ptr
=
6375 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
6376 if (get_user(ptr
, sense_cioc_ptr
) ||
6377 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
6381 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
6382 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
6383 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
6384 copy_in_user(&ioc
->sgl
[i
].iov_len
,
6385 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
6389 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
6391 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
6392 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
6393 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
6400 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
6404 case MEGASAS_IOC_FIRMWARE32
:
6405 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
6406 case MEGASAS_IOC_GET_AEN
:
6407 return megasas_mgmt_ioctl_aen(file
, arg
);
6415 * File operations structure for management interface
6417 static const struct file_operations megasas_mgmt_fops
= {
6418 .owner
= THIS_MODULE
,
6419 .open
= megasas_mgmt_open
,
6420 .fasync
= megasas_mgmt_fasync
,
6421 .unlocked_ioctl
= megasas_mgmt_ioctl
,
6422 .poll
= megasas_mgmt_poll
,
6423 #ifdef CONFIG_COMPAT
6424 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
6426 .llseek
= noop_llseek
,
6430 * PCI hotplug support registration structure
6432 static struct pci_driver megasas_pci_driver
= {
6434 .name
= "megaraid_sas",
6435 .id_table
= megasas_pci_table
,
6436 .probe
= megasas_probe_one
,
6437 .remove
= megasas_detach_one
,
6438 .suspend
= megasas_suspend
,
6439 .resume
= megasas_resume
,
6440 .shutdown
= megasas_shutdown
,
6444 * Sysfs driver attributes
6446 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
6448 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
6452 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
6455 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
6457 return sprintf(buf
, "%u\n", support_poll_for_event
);
6460 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
6461 megasas_sysfs_show_support_poll_for_event
, NULL
);
6464 megasas_sysfs_show_support_device_change(struct device_driver
*dd
, char *buf
)
6466 return sprintf(buf
, "%u\n", support_device_change
);
6469 static DRIVER_ATTR(support_device_change
, S_IRUGO
,
6470 megasas_sysfs_show_support_device_change
, NULL
);
6473 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
6475 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
6479 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
6482 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
6483 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
6489 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
6490 megasas_sysfs_set_dbg_lvl
);
6493 megasas_aen_polling(struct work_struct
*work
)
6495 struct megasas_aen_event
*ev
=
6496 container_of(work
, struct megasas_aen_event
, hotplug_work
.work
);
6497 struct megasas_instance
*instance
= ev
->instance
;
6498 union megasas_evt_class_locale class_locale
;
6499 struct Scsi_Host
*host
;
6500 struct scsi_device
*sdev1
;
6503 int i
, j
, doscan
= 0;
6504 u32 seq_num
, wait_time
= MEGASAS_RESET_WAIT_TIME
;
6508 printk(KERN_ERR
"invalid instance!\n");
6513 /* Adjust event workqueue thread wait time for VF mode */
6514 if (instance
->requestorId
)
6515 wait_time
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
6517 /* Don't run the event workqueue thread if OCR is running */
6518 for (i
= 0; i
< wait_time
; i
++) {
6519 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
)
6521 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
6522 printk(KERN_NOTICE
"megasas: %s waiting for "
6523 "controller reset to finish for scsi%d\n",
6524 __func__
, instance
->host
->host_no
);
6529 instance
->ev
= NULL
;
6530 host
= instance
->host
;
6531 if (instance
->evt_detail
) {
6533 switch (le32_to_cpu(instance
->evt_detail
->code
)) {
6534 case MR_EVT_PD_INSERTED
:
6535 if (megasas_get_pd_list(instance
) == 0) {
6536 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6538 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6542 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6545 scsi_device_lookup(host
, i
, j
, 0);
6547 if (instance
->pd_list
[pd_index
].driveState
6548 == MR_PD_STATE_SYSTEM
) {
6550 scsi_add_device(host
, i
, j
, 0);
6554 scsi_device_put(sdev1
);
6562 case MR_EVT_PD_REMOVED
:
6563 if (megasas_get_pd_list(instance
) == 0) {
6564 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6566 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6570 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6573 scsi_device_lookup(host
, i
, j
, 0);
6575 if (instance
->pd_list
[pd_index
].driveState
6576 == MR_PD_STATE_SYSTEM
) {
6578 scsi_device_put(sdev1
);
6582 scsi_remove_device(sdev1
);
6583 scsi_device_put(sdev1
);
6592 case MR_EVT_LD_OFFLINE
:
6593 case MR_EVT_CFG_CLEARED
:
6594 case MR_EVT_LD_DELETED
:
6595 if (!instance
->requestorId
||
6596 (instance
->requestorId
&&
6597 megasas_get_ld_vf_affiliation(instance
, 0))) {
6598 if (megasas_ld_list_query(instance
,
6599 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6600 megasas_get_ld_list(instance
);
6601 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6603 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6607 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6609 sdev1
= scsi_device_lookup(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6611 if (instance
->ld_ids
[ld_index
]
6614 scsi_device_put(sdev1
);
6617 scsi_remove_device(sdev1
);
6618 scsi_device_put(sdev1
);
6626 case MR_EVT_LD_CREATED
:
6627 if (!instance
->requestorId
||
6628 (instance
->requestorId
&&
6629 megasas_get_ld_vf_affiliation(instance
, 0))) {
6630 if (megasas_ld_list_query(instance
,
6631 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6632 megasas_get_ld_list(instance
);
6633 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6635 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6638 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6640 sdev1
= scsi_device_lookup(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6642 if (instance
->ld_ids
[ld_index
]
6645 scsi_add_device(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6648 scsi_device_put(sdev1
);
6654 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
6655 case MR_EVT_FOREIGN_CFG_IMPORTED
:
6656 case MR_EVT_LD_STATE_CHANGE
:
6664 printk(KERN_ERR
"invalid evt_detail!\n");
6670 printk(KERN_INFO
"megaraid_sas: scanning for scsi%d...\n",
6671 instance
->host
->host_no
);
6672 if (megasas_get_pd_list(instance
) == 0) {
6673 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6674 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
6675 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
6676 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
6677 if (instance
->pd_list
[pd_index
].driveState
==
6678 MR_PD_STATE_SYSTEM
) {
6680 scsi_add_device(host
, i
, j
, 0);
6683 scsi_device_put(sdev1
);
6686 scsi_remove_device(sdev1
);
6687 scsi_device_put(sdev1
);
6694 if (!instance
->requestorId
||
6695 (instance
->requestorId
&&
6696 megasas_get_ld_vf_affiliation(instance
, 0))) {
6697 if (megasas_ld_list_query(instance
,
6698 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6699 megasas_get_ld_list(instance
);
6700 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6701 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6704 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6706 sdev1
= scsi_device_lookup(host
,
6707 MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6708 if (instance
->ld_ids
[ld_index
]
6711 scsi_add_device(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6713 scsi_device_put(sdev1
);
6716 scsi_remove_device(sdev1
);
6717 scsi_device_put(sdev1
);
6725 if ( instance
->aen_cmd
!= NULL
) {
6730 seq_num
= le32_to_cpu(instance
->evt_detail
->seq_num
) + 1;
6732 /* Register AEN with FW for latest sequence number plus 1 */
6733 class_locale
.members
.reserved
= 0;
6734 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
6735 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
6736 mutex_lock(&instance
->aen_mutex
);
6737 error
= megasas_register_aen(instance
, seq_num
,
6739 mutex_unlock(&instance
->aen_mutex
);
6742 printk(KERN_ERR
"register aen failed error %x\n", error
);
6748 * megasas_init - Driver load entry point
6750 static int __init
megasas_init(void)
6755 * Announce driver version and other information
6757 pr_info("megasas: %s\n", MEGASAS_VERSION
);
6759 spin_lock_init(&poll_aen_lock
);
6761 support_poll_for_event
= 2;
6762 support_device_change
= 1;
6764 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
6767 * Register character device node
6769 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
6772 printk(KERN_DEBUG
"megasas: failed to open device node\n");
6776 megasas_mgmt_majorno
= rval
;
6779 * Register ourselves as PCI hotplug module
6781 rval
= pci_register_driver(&megasas_pci_driver
);
6784 printk(KERN_DEBUG
"megasas: PCI hotplug registration failed \n");
6788 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6789 &driver_attr_version
);
6791 goto err_dcf_attr_ver
;
6793 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6794 &driver_attr_support_poll_for_event
);
6796 goto err_dcf_support_poll_for_event
;
6798 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6799 &driver_attr_dbg_lvl
);
6801 goto err_dcf_dbg_lvl
;
6802 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6803 &driver_attr_support_device_change
);
6805 goto err_dcf_support_device_change
;
6809 err_dcf_support_device_change
:
6810 driver_remove_file(&megasas_pci_driver
.driver
,
6811 &driver_attr_dbg_lvl
);
6813 driver_remove_file(&megasas_pci_driver
.driver
,
6814 &driver_attr_support_poll_for_event
);
6815 err_dcf_support_poll_for_event
:
6816 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
6818 pci_unregister_driver(&megasas_pci_driver
);
6820 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
6825 * megasas_exit - Driver unload entry point
6827 static void __exit
megasas_exit(void)
6829 driver_remove_file(&megasas_pci_driver
.driver
,
6830 &driver_attr_dbg_lvl
);
6831 driver_remove_file(&megasas_pci_driver
.driver
,
6832 &driver_attr_support_poll_for_event
);
6833 driver_remove_file(&megasas_pci_driver
.driver
,
6834 &driver_attr_support_device_change
);
6835 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
6837 pci_unregister_driver(&megasas_pci_driver
);
6838 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
6841 module_init(megasas_init
);
6842 module_exit(megasas_exit
);