2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2012 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_base.c
21 * Version : 06.803.01.00-rc1
23 * Authors: LSI Corporation
27 * Adam Radford <linuxraid@lsi.com>
29 * Send feedback to: <megaraidlinux@lsi.com>
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/list.h>
39 #include <linux/moduleparam.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44 #include <linux/uio.h>
45 #include <linux/slab.h>
46 #include <asm/uaccess.h>
48 #include <linux/compat.h>
49 #include <linux/blkdev.h>
50 #include <linux/mutex.h>
51 #include <linux/poll.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
62 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
65 static unsigned int max_sectors
;
66 module_param_named(max_sectors
, max_sectors
, int, 0);
67 MODULE_PARM_DESC(max_sectors
,
68 "Maximum number of sectors per IO command");
70 static int msix_disable
;
71 module_param(msix_disable
, int, S_IRUGO
);
72 MODULE_PARM_DESC(msix_disable
, "Disable MSI-X interrupt handling. Default: 0");
74 static unsigned int msix_vectors
;
75 module_param(msix_vectors
, int, S_IRUGO
);
76 MODULE_PARM_DESC(msix_vectors
, "MSI-X max vector count. Default: Set by FW");
78 static int allow_vf_ioctls
;
79 module_param(allow_vf_ioctls
, int, S_IRUGO
);
80 MODULE_PARM_DESC(allow_vf_ioctls
, "Allow ioctls in SR-IOV VF mode. Default: 0");
82 static int throttlequeuedepth
= MEGASAS_THROTTLE_QUEUE_DEPTH
;
83 module_param(throttlequeuedepth
, int, S_IRUGO
);
84 MODULE_PARM_DESC(throttlequeuedepth
,
85 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87 int resetwaittime
= MEGASAS_RESET_WAIT_TIME
;
88 module_param(resetwaittime
, int, S_IRUGO
);
89 MODULE_PARM_DESC(resetwaittime
, "Wait time in seconds after I/O timeout "
90 "before resetting adapter. Default: 180");
92 MODULE_LICENSE("GPL");
93 MODULE_VERSION(MEGASAS_VERSION
);
94 MODULE_AUTHOR("megaraidlinux@lsi.com");
95 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
97 int megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
);
98 static int megasas_get_pd_list(struct megasas_instance
*instance
);
99 static int megasas_ld_list_query(struct megasas_instance
*instance
,
101 static int megasas_issue_init_mfi(struct megasas_instance
*instance
);
102 static int megasas_register_aen(struct megasas_instance
*instance
,
103 u32 seq_num
, u32 class_locale_word
);
105 * PCI ID table for all supported controllers
107 static struct pci_device_id megasas_pci_table
[] = {
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1064R
)},
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078R
)},
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078DE
)},
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078GEN2
)},
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0079GEN2
)},
119 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0073SKINNY
)},
121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0071SKINNY
)},
123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_VERDE_ZCR
)},
124 /* xscale IOP, vega */
125 {PCI_DEVICE(PCI_VENDOR_ID_DELL
, PCI_DEVICE_ID_DELL_PERC5
)},
127 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FUSION
)},
129 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_PLASMA
)},
131 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_INVADER
)},
133 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FURY
)},
138 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
140 static int megasas_mgmt_majorno
;
141 struct megasas_mgmt_info megasas_mgmt_info
;
142 static struct fasync_struct
*megasas_async_queue
;
143 static DEFINE_MUTEX(megasas_async_queue_mutex
);
145 static int megasas_poll_wait_aen
;
146 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait
);
147 static u32 support_poll_for_event
;
149 static u32 support_device_change
;
151 /* define lock for aen poll */
152 spinlock_t poll_aen_lock
;
155 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
158 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
);
160 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
161 struct megasas_register_set __iomem
*reg_set
);
162 static irqreturn_t
megasas_isr(int irq
, void *devp
);
164 megasas_init_adapter_mfi(struct megasas_instance
*instance
);
166 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
167 struct scsi_cmnd
*scmd
);
168 static void megasas_complete_cmd_dpc(unsigned long instance_addr
);
170 megasas_release_fusion(struct megasas_instance
*instance
);
172 megasas_ioc_init_fusion(struct megasas_instance
*instance
);
174 megasas_free_cmds_fusion(struct megasas_instance
*instance
);
176 megasas_get_map_info(struct megasas_instance
*instance
);
178 megasas_sync_map_info(struct megasas_instance
*instance
);
180 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
182 void megasas_reset_reply_desc(struct megasas_instance
*instance
);
183 int megasas_reset_fusion(struct Scsi_Host
*shost
, int iotimeout
);
184 void megasas_fusion_ocr_wq(struct work_struct
*work
);
185 static int megasas_get_ld_vf_affiliation(struct megasas_instance
*instance
,
187 int megasas_check_mpio_paths(struct megasas_instance
*instance
,
188 struct scsi_cmnd
*scmd
);
191 megasas_issue_dcmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
193 instance
->instancet
->fire_cmd(instance
,
194 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
198 * megasas_get_cmd - Get a command from the free pool
199 * @instance: Adapter soft state
201 * Returns a free command from the pool
203 struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
207 struct megasas_cmd
*cmd
= NULL
;
209 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
211 if (!list_empty(&instance
->cmd_pool
)) {
212 cmd
= list_entry((&instance
->cmd_pool
)->next
,
213 struct megasas_cmd
, list
);
214 list_del_init(&cmd
->list
);
216 printk(KERN_ERR
"megasas: Command pool empty!\n");
219 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
224 * megasas_return_cmd - Return a cmd to free command pool
225 * @instance: Adapter soft state
226 * @cmd: Command packet to be returned to free command pool
229 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
233 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
236 cmd
->frame_count
= 0;
237 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
238 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_PLASMA
) &&
239 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
240 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
242 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
243 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
245 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
250 * The following functions are defined for xscale
251 * (deviceid : 1064R, PERC5) controllers
255 * megasas_enable_intr_xscale - Enables interrupts
256 * @regs: MFI register set
259 megasas_enable_intr_xscale(struct megasas_instance
*instance
)
261 struct megasas_register_set __iomem
*regs
;
262 regs
= instance
->reg_set
;
263 writel(0, &(regs
)->outbound_intr_mask
);
265 /* Dummy readl to force pci flush */
266 readl(®s
->outbound_intr_mask
);
270 * megasas_disable_intr_xscale -Disables interrupt
271 * @regs: MFI register set
274 megasas_disable_intr_xscale(struct megasas_instance
*instance
)
276 struct megasas_register_set __iomem
*regs
;
278 regs
= instance
->reg_set
;
279 writel(mask
, ®s
->outbound_intr_mask
);
280 /* Dummy readl to force pci flush */
281 readl(®s
->outbound_intr_mask
);
285 * megasas_read_fw_status_reg_xscale - returns the current FW status value
286 * @regs: MFI register set
289 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
291 return readl(&(regs
)->outbound_msg_0
);
294 * megasas_clear_interrupt_xscale - Check & clear interrupt
295 * @regs: MFI register set
298 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
303 * Check if it is our interrupt
305 status
= readl(®s
->outbound_intr_status
);
307 if (status
& MFI_OB_INTR_STATUS_MASK
)
308 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
309 if (status
& MFI_XSCALE_OMR0_CHANGE_INTERRUPT
)
310 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
313 * Clear the interrupt by writing back the same value
316 writel(status
, ®s
->outbound_intr_status
);
318 /* Dummy readl to force pci flush */
319 readl(®s
->outbound_intr_status
);
325 * megasas_fire_cmd_xscale - Sends command to the FW
326 * @frame_phys_addr : Physical address of cmd
327 * @frame_count : Number of frames for the command
328 * @regs : MFI register set
331 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
332 dma_addr_t frame_phys_addr
,
334 struct megasas_register_set __iomem
*regs
)
337 spin_lock_irqsave(&instance
->hba_lock
, flags
);
338 writel((frame_phys_addr
>> 3)|(frame_count
),
339 &(regs
)->inbound_queue_port
);
340 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
344 * megasas_adp_reset_xscale - For controller reset
345 * @regs: MFI register set
348 megasas_adp_reset_xscale(struct megasas_instance
*instance
,
349 struct megasas_register_set __iomem
*regs
)
353 writel(MFI_ADP_RESET
, ®s
->inbound_doorbell
);
355 for (i
= 0; i
< 3; i
++)
356 msleep(1000); /* sleep for 3 secs */
358 pci_read_config_dword(instance
->pdev
, MFI_1068_PCSR_OFFSET
, &pcidata
);
359 printk(KERN_NOTICE
"pcidata = %x\n", pcidata
);
361 printk(KERN_NOTICE
"mfi 1068 offset read=%x\n", pcidata
);
363 pci_write_config_dword(instance
->pdev
,
364 MFI_1068_PCSR_OFFSET
, pcidata
);
366 for (i
= 0; i
< 2; i
++)
367 msleep(1000); /* need to wait 2 secs again */
370 pci_read_config_dword(instance
->pdev
,
371 MFI_1068_FW_HANDSHAKE_OFFSET
, &pcidata
);
372 printk(KERN_NOTICE
"1068 offset handshake read=%x\n", pcidata
);
373 if ((pcidata
& 0xffff0000) == MFI_1068_FW_READY
) {
374 printk(KERN_NOTICE
"1068 offset pcidt=%x\n", pcidata
);
376 pci_write_config_dword(instance
->pdev
,
377 MFI_1068_FW_HANDSHAKE_OFFSET
, pcidata
);
384 * megasas_check_reset_xscale - For controller reset check
385 * @regs: MFI register set
388 megasas_check_reset_xscale(struct megasas_instance
*instance
,
389 struct megasas_register_set __iomem
*regs
)
392 if ((instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) &&
393 (le32_to_cpu(*instance
->consumer
) ==
394 MEGASAS_ADPRESET_INPROG_SIGN
))
399 static struct megasas_instance_template megasas_instance_template_xscale
= {
401 .fire_cmd
= megasas_fire_cmd_xscale
,
402 .enable_intr
= megasas_enable_intr_xscale
,
403 .disable_intr
= megasas_disable_intr_xscale
,
404 .clear_intr
= megasas_clear_intr_xscale
,
405 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
406 .adp_reset
= megasas_adp_reset_xscale
,
407 .check_reset
= megasas_check_reset_xscale
,
408 .service_isr
= megasas_isr
,
409 .tasklet
= megasas_complete_cmd_dpc
,
410 .init_adapter
= megasas_init_adapter_mfi
,
411 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
412 .issue_dcmd
= megasas_issue_dcmd
,
416 * This is the end of set of functions & definitions specific
417 * to xscale (deviceid : 1064R, PERC5) controllers
421 * The following functions are defined for ppc (deviceid : 0x60)
426 * megasas_enable_intr_ppc - Enables interrupts
427 * @regs: MFI register set
430 megasas_enable_intr_ppc(struct megasas_instance
*instance
)
432 struct megasas_register_set __iomem
*regs
;
433 regs
= instance
->reg_set
;
434 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
436 writel(~0x80000000, &(regs
)->outbound_intr_mask
);
438 /* Dummy readl to force pci flush */
439 readl(®s
->outbound_intr_mask
);
443 * megasas_disable_intr_ppc - Disable interrupt
444 * @regs: MFI register set
447 megasas_disable_intr_ppc(struct megasas_instance
*instance
)
449 struct megasas_register_set __iomem
*regs
;
450 u32 mask
= 0xFFFFFFFF;
451 regs
= instance
->reg_set
;
452 writel(mask
, ®s
->outbound_intr_mask
);
453 /* Dummy readl to force pci flush */
454 readl(®s
->outbound_intr_mask
);
458 * megasas_read_fw_status_reg_ppc - returns the current FW status value
459 * @regs: MFI register set
462 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
464 return readl(&(regs
)->outbound_scratch_pad
);
468 * megasas_clear_interrupt_ppc - Check & clear interrupt
469 * @regs: MFI register set
472 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
474 u32 status
, mfiStatus
= 0;
477 * Check if it is our interrupt
479 status
= readl(®s
->outbound_intr_status
);
481 if (status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)
482 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
484 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
)
485 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
488 * Clear the interrupt by writing back the same value
490 writel(status
, ®s
->outbound_doorbell_clear
);
492 /* Dummy readl to force pci flush */
493 readl(®s
->outbound_doorbell_clear
);
499 * megasas_fire_cmd_ppc - Sends command to the FW
500 * @frame_phys_addr : Physical address of cmd
501 * @frame_count : Number of frames for the command
502 * @regs : MFI register set
505 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
506 dma_addr_t frame_phys_addr
,
508 struct megasas_register_set __iomem
*regs
)
511 spin_lock_irqsave(&instance
->hba_lock
, flags
);
512 writel((frame_phys_addr
| (frame_count
<<1))|1,
513 &(regs
)->inbound_queue_port
);
514 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
518 * megasas_check_reset_ppc - For controller reset check
519 * @regs: MFI register set
522 megasas_check_reset_ppc(struct megasas_instance
*instance
,
523 struct megasas_register_set __iomem
*regs
)
525 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
531 static struct megasas_instance_template megasas_instance_template_ppc
= {
533 .fire_cmd
= megasas_fire_cmd_ppc
,
534 .enable_intr
= megasas_enable_intr_ppc
,
535 .disable_intr
= megasas_disable_intr_ppc
,
536 .clear_intr
= megasas_clear_intr_ppc
,
537 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
538 .adp_reset
= megasas_adp_reset_xscale
,
539 .check_reset
= megasas_check_reset_ppc
,
540 .service_isr
= megasas_isr
,
541 .tasklet
= megasas_complete_cmd_dpc
,
542 .init_adapter
= megasas_init_adapter_mfi
,
543 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
544 .issue_dcmd
= megasas_issue_dcmd
,
548 * megasas_enable_intr_skinny - Enables interrupts
549 * @regs: MFI register set
552 megasas_enable_intr_skinny(struct megasas_instance
*instance
)
554 struct megasas_register_set __iomem
*regs
;
555 regs
= instance
->reg_set
;
556 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
558 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
560 /* Dummy readl to force pci flush */
561 readl(®s
->outbound_intr_mask
);
565 * megasas_disable_intr_skinny - Disables interrupt
566 * @regs: MFI register set
569 megasas_disable_intr_skinny(struct megasas_instance
*instance
)
571 struct megasas_register_set __iomem
*regs
;
572 u32 mask
= 0xFFFFFFFF;
573 regs
= instance
->reg_set
;
574 writel(mask
, ®s
->outbound_intr_mask
);
575 /* Dummy readl to force pci flush */
576 readl(®s
->outbound_intr_mask
);
580 * megasas_read_fw_status_reg_skinny - returns the current FW status value
581 * @regs: MFI register set
584 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
586 return readl(&(regs
)->outbound_scratch_pad
);
590 * megasas_clear_interrupt_skinny - Check & clear interrupt
591 * @regs: MFI register set
594 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
600 * Check if it is our interrupt
602 status
= readl(®s
->outbound_intr_status
);
604 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
609 * Check if it is our interrupt
611 if ((megasas_read_fw_status_reg_skinny(regs
) & MFI_STATE_MASK
) ==
613 mfiStatus
= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
615 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
618 * Clear the interrupt by writing back the same value
620 writel(status
, ®s
->outbound_intr_status
);
623 * dummy read to flush PCI
625 readl(®s
->outbound_intr_status
);
631 * megasas_fire_cmd_skinny - Sends command to the FW
632 * @frame_phys_addr : Physical address of cmd
633 * @frame_count : Number of frames for the command
634 * @regs : MFI register set
637 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
638 dma_addr_t frame_phys_addr
,
640 struct megasas_register_set __iomem
*regs
)
643 spin_lock_irqsave(&instance
->hba_lock
, flags
);
644 writel(upper_32_bits(frame_phys_addr
),
645 &(regs
)->inbound_high_queue_port
);
646 writel((lower_32_bits(frame_phys_addr
) | (frame_count
<<1))|1,
647 &(regs
)->inbound_low_queue_port
);
648 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
652 * megasas_check_reset_skinny - For controller reset check
653 * @regs: MFI register set
656 megasas_check_reset_skinny(struct megasas_instance
*instance
,
657 struct megasas_register_set __iomem
*regs
)
659 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
665 static struct megasas_instance_template megasas_instance_template_skinny
= {
667 .fire_cmd
= megasas_fire_cmd_skinny
,
668 .enable_intr
= megasas_enable_intr_skinny
,
669 .disable_intr
= megasas_disable_intr_skinny
,
670 .clear_intr
= megasas_clear_intr_skinny
,
671 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
672 .adp_reset
= megasas_adp_reset_gen2
,
673 .check_reset
= megasas_check_reset_skinny
,
674 .service_isr
= megasas_isr
,
675 .tasklet
= megasas_complete_cmd_dpc
,
676 .init_adapter
= megasas_init_adapter_mfi
,
677 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
678 .issue_dcmd
= megasas_issue_dcmd
,
683 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
688 * megasas_enable_intr_gen2 - Enables interrupts
689 * @regs: MFI register set
692 megasas_enable_intr_gen2(struct megasas_instance
*instance
)
694 struct megasas_register_set __iomem
*regs
;
695 regs
= instance
->reg_set
;
696 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
698 /* write ~0x00000005 (4 & 1) to the intr mask*/
699 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
701 /* Dummy readl to force pci flush */
702 readl(®s
->outbound_intr_mask
);
706 * megasas_disable_intr_gen2 - Disables interrupt
707 * @regs: MFI register set
710 megasas_disable_intr_gen2(struct megasas_instance
*instance
)
712 struct megasas_register_set __iomem
*regs
;
713 u32 mask
= 0xFFFFFFFF;
714 regs
= instance
->reg_set
;
715 writel(mask
, ®s
->outbound_intr_mask
);
716 /* Dummy readl to force pci flush */
717 readl(®s
->outbound_intr_mask
);
721 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
722 * @regs: MFI register set
725 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
727 return readl(&(regs
)->outbound_scratch_pad
);
731 * megasas_clear_interrupt_gen2 - Check & clear interrupt
732 * @regs: MFI register set
735 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
740 * Check if it is our interrupt
742 status
= readl(®s
->outbound_intr_status
);
744 if (status
& MFI_INTR_FLAG_REPLY_MESSAGE
) {
745 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
747 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
) {
748 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
752 * Clear the interrupt by writing back the same value
755 writel(status
, ®s
->outbound_doorbell_clear
);
757 /* Dummy readl to force pci flush */
758 readl(®s
->outbound_intr_status
);
763 * megasas_fire_cmd_gen2 - Sends command to the FW
764 * @frame_phys_addr : Physical address of cmd
765 * @frame_count : Number of frames for the command
766 * @regs : MFI register set
769 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
770 dma_addr_t frame_phys_addr
,
772 struct megasas_register_set __iomem
*regs
)
775 spin_lock_irqsave(&instance
->hba_lock
, flags
);
776 writel((frame_phys_addr
| (frame_count
<<1))|1,
777 &(regs
)->inbound_queue_port
);
778 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
782 * megasas_adp_reset_gen2 - For controller reset
783 * @regs: MFI register set
786 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
787 struct megasas_register_set __iomem
*reg_set
)
791 u32
*seq_offset
= ®_set
->seq_offset
;
792 u32
*hostdiag_offset
= ®_set
->host_diag
;
794 if (instance
->instancet
== &megasas_instance_template_skinny
) {
795 seq_offset
= ®_set
->fusion_seq_offset
;
796 hostdiag_offset
= ®_set
->fusion_host_diag
;
799 writel(0, seq_offset
);
800 writel(4, seq_offset
);
801 writel(0xb, seq_offset
);
802 writel(2, seq_offset
);
803 writel(7, seq_offset
);
804 writel(0xd, seq_offset
);
808 HostDiag
= (u32
)readl(hostdiag_offset
);
810 while ( !( HostDiag
& DIAG_WRITE_ENABLE
) ) {
812 HostDiag
= (u32
)readl(hostdiag_offset
);
813 printk(KERN_NOTICE
"RESETGEN2: retry=%x, hostdiag=%x\n",
821 printk(KERN_NOTICE
"ADP_RESET_GEN2: HostDiag=%x\n", HostDiag
);
823 writel((HostDiag
| DIAG_RESET_ADAPTER
), hostdiag_offset
);
827 HostDiag
= (u32
)readl(hostdiag_offset
);
828 while ( ( HostDiag
& DIAG_RESET_ADAPTER
) ) {
830 HostDiag
= (u32
)readl(hostdiag_offset
);
831 printk(KERN_NOTICE
"RESET_GEN2: retry=%x, hostdiag=%x\n",
842 * megasas_check_reset_gen2 - For controller reset check
843 * @regs: MFI register set
846 megasas_check_reset_gen2(struct megasas_instance
*instance
,
847 struct megasas_register_set __iomem
*regs
)
849 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
856 static struct megasas_instance_template megasas_instance_template_gen2
= {
858 .fire_cmd
= megasas_fire_cmd_gen2
,
859 .enable_intr
= megasas_enable_intr_gen2
,
860 .disable_intr
= megasas_disable_intr_gen2
,
861 .clear_intr
= megasas_clear_intr_gen2
,
862 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
863 .adp_reset
= megasas_adp_reset_gen2
,
864 .check_reset
= megasas_check_reset_gen2
,
865 .service_isr
= megasas_isr
,
866 .tasklet
= megasas_complete_cmd_dpc
,
867 .init_adapter
= megasas_init_adapter_mfi
,
868 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
869 .issue_dcmd
= megasas_issue_dcmd
,
873 * This is the end of set of functions & definitions
874 * specific to gen2 (deviceid : 0x78, 0x79) controllers
878 * Template added for TB (Fusion)
880 extern struct megasas_instance_template megasas_instance_template_fusion
;
883 * megasas_issue_polled - Issues a polling command
884 * @instance: Adapter soft state
885 * @cmd: Command packet to be issued
887 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
890 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
894 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
896 frame_hdr
->cmd_status
= MFI_CMD_STATUS_POLL_MODE
;
897 frame_hdr
->flags
|= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
);
900 * Issue the frame using inbound queue port
902 instance
->instancet
->issue_dcmd(instance
, cmd
);
905 * Wait for cmd_status to change
907 if (instance
->requestorId
)
908 seconds
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
910 seconds
= MFI_POLL_TIMEOUT_SECS
;
911 return wait_and_poll(instance
, cmd
, seconds
);
915 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
916 * @instance: Adapter soft state
917 * @cmd: Command to be issued
918 * @timeout: Timeout in seconds
920 * This function waits on an event for the command to be returned from ISR.
921 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
922 * Used to issue ioctl commands.
925 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
926 struct megasas_cmd
*cmd
, int timeout
)
929 cmd
->cmd_status
= ENODATA
;
931 instance
->instancet
->issue_dcmd(instance
, cmd
);
933 ret
= wait_event_timeout(instance
->int_cmd_wait_q
,
934 cmd
->cmd_status
!= ENODATA
, timeout
* HZ
);
938 wait_event(instance
->int_cmd_wait_q
,
939 cmd
->cmd_status
!= ENODATA
);
945 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
946 * @instance: Adapter soft state
947 * @cmd_to_abort: Previously issued cmd to be aborted
948 * @timeout: Timeout in seconds
950 * MFI firmware can abort previously issued AEN comamnd (automatic event
951 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
952 * cmd and waits for return status.
953 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
956 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
957 struct megasas_cmd
*cmd_to_abort
, int timeout
)
959 struct megasas_cmd
*cmd
;
960 struct megasas_abort_frame
*abort_fr
;
963 cmd
= megasas_get_cmd(instance
);
968 abort_fr
= &cmd
->frame
->abort
;
971 * Prepare and issue the abort frame
973 abort_fr
->cmd
= MFI_CMD_ABORT
;
974 abort_fr
->cmd_status
= 0xFF;
975 abort_fr
->flags
= cpu_to_le16(0);
976 abort_fr
->abort_context
= cpu_to_le32(cmd_to_abort
->index
);
977 abort_fr
->abort_mfi_phys_addr_lo
=
978 cpu_to_le32(lower_32_bits(cmd_to_abort
->frame_phys_addr
));
979 abort_fr
->abort_mfi_phys_addr_hi
=
980 cpu_to_le32(upper_32_bits(cmd_to_abort
->frame_phys_addr
));
983 cmd
->cmd_status
= 0xFF;
985 instance
->instancet
->issue_dcmd(instance
, cmd
);
988 ret
= wait_event_timeout(instance
->abort_cmd_wait_q
,
989 cmd
->cmd_status
!= ENODATA
, timeout
* HZ
);
991 dev_err(&instance
->pdev
->dev
, "Command timedout"
992 "from %s\n", __func__
);
996 wait_event(instance
->abort_cmd_wait_q
,
997 cmd
->cmd_status
!= ENODATA
);
1001 megasas_return_cmd(instance
, cmd
);
1006 * megasas_make_sgl32 - Prepares 32-bit SGL
1007 * @instance: Adapter soft state
1008 * @scp: SCSI command from the mid-layer
1009 * @mfi_sgl: SGL to be filled in
1011 * If successful, this function returns the number of SG elements. Otherwise,
1015 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1016 union megasas_sgl
*mfi_sgl
)
1020 struct scatterlist
*os_sgl
;
1022 sge_count
= scsi_dma_map(scp
);
1023 BUG_ON(sge_count
< 0);
1026 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1027 mfi_sgl
->sge32
[i
].length
= cpu_to_le32(sg_dma_len(os_sgl
));
1028 mfi_sgl
->sge32
[i
].phys_addr
= cpu_to_le32(sg_dma_address(os_sgl
));
1035 * megasas_make_sgl64 - Prepares 64-bit SGL
1036 * @instance: Adapter soft state
1037 * @scp: SCSI command from the mid-layer
1038 * @mfi_sgl: SGL to be filled in
1040 * If successful, this function returns the number of SG elements. Otherwise,
1044 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1045 union megasas_sgl
*mfi_sgl
)
1049 struct scatterlist
*os_sgl
;
1051 sge_count
= scsi_dma_map(scp
);
1052 BUG_ON(sge_count
< 0);
1055 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1056 mfi_sgl
->sge64
[i
].length
= cpu_to_le32(sg_dma_len(os_sgl
));
1057 mfi_sgl
->sge64
[i
].phys_addr
= cpu_to_le64(sg_dma_address(os_sgl
));
1064 * megasas_make_sgl_skinny - Prepares IEEE SGL
1065 * @instance: Adapter soft state
1066 * @scp: SCSI command from the mid-layer
1067 * @mfi_sgl: SGL to be filled in
1069 * If successful, this function returns the number of SG elements. Otherwise,
1073 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
1074 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
1078 struct scatterlist
*os_sgl
;
1080 sge_count
= scsi_dma_map(scp
);
1083 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1084 mfi_sgl
->sge_skinny
[i
].length
=
1085 cpu_to_le32(sg_dma_len(os_sgl
));
1086 mfi_sgl
->sge_skinny
[i
].phys_addr
=
1087 cpu_to_le64(sg_dma_address(os_sgl
));
1088 mfi_sgl
->sge_skinny
[i
].flag
= cpu_to_le32(0);
1095 * megasas_get_frame_count - Computes the number of frames
1096 * @frame_type : type of frame- io or pthru frame
1097 * @sge_count : number of sg elements
1099 * Returns the number of frames required for numnber of sge's (sge_count)
1102 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
1103 u8 sge_count
, u8 frame_type
)
1110 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1111 sizeof(struct megasas_sge32
);
1113 if (instance
->flag_ieee
) {
1114 sge_sz
= sizeof(struct megasas_sge_skinny
);
1118 * Main frame can contain 2 SGEs for 64-bit SGLs and
1119 * 3 SGEs for 32-bit SGLs for ldio &
1120 * 1 SGEs for 64-bit SGLs and
1121 * 2 SGEs for 32-bit SGLs for pthru frame
1123 if (unlikely(frame_type
== PTHRU_FRAME
)) {
1124 if (instance
->flag_ieee
== 1) {
1125 num_cnt
= sge_count
- 1;
1126 } else if (IS_DMA64
)
1127 num_cnt
= sge_count
- 1;
1129 num_cnt
= sge_count
- 2;
1131 if (instance
->flag_ieee
== 1) {
1132 num_cnt
= sge_count
- 1;
1133 } else if (IS_DMA64
)
1134 num_cnt
= sge_count
- 2;
1136 num_cnt
= sge_count
- 3;
1140 sge_bytes
= sge_sz
* num_cnt
;
1142 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
1143 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
1148 if (frame_count
> 7)
1154 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1155 * @instance: Adapter soft state
1156 * @scp: SCSI command
1157 * @cmd: Command to be prepared in
1159 * This function prepares CDB commands. These are typcially pass-through
1160 * commands to the devices.
1163 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1164 struct megasas_cmd
*cmd
)
1169 struct megasas_pthru_frame
*pthru
;
1171 is_logical
= MEGASAS_IS_LOGICAL(scp
);
1172 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1173 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
1175 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1176 flags
= MFI_FRAME_DIR_WRITE
;
1177 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1178 flags
= MFI_FRAME_DIR_READ
;
1179 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
1180 flags
= MFI_FRAME_DIR_NONE
;
1182 if (instance
->flag_ieee
== 1) {
1183 flags
|= MFI_FRAME_IEEE
;
1187 * Prepare the DCDB frame
1189 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
1190 pthru
->cmd_status
= 0x0;
1191 pthru
->scsi_status
= 0x0;
1192 pthru
->target_id
= device_id
;
1193 pthru
->lun
= scp
->device
->lun
;
1194 pthru
->cdb_len
= scp
->cmd_len
;
1197 pthru
->flags
= cpu_to_le16(flags
);
1198 pthru
->data_xfer_len
= cpu_to_le32(scsi_bufflen(scp
));
1200 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
1203 * If the command is for the tape device, set the
1204 * pthru timeout to the os layer timeout value.
1206 if (scp
->device
->type
== TYPE_TAPE
) {
1207 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
1208 pthru
->timeout
= 0xFFFF;
1210 pthru
->timeout
= cpu_to_le16(scp
->request
->timeout
/ HZ
);
1216 if (instance
->flag_ieee
== 1) {
1217 pthru
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1218 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1220 } else if (IS_DMA64
) {
1221 pthru
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1222 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
1225 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
1228 if (pthru
->sge_count
> instance
->max_num_sge
) {
1229 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
1235 * Sense info specific
1237 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1238 pthru
->sense_buf_phys_addr_hi
=
1239 cpu_to_le32(upper_32_bits(cmd
->sense_phys_addr
));
1240 pthru
->sense_buf_phys_addr_lo
=
1241 cpu_to_le32(lower_32_bits(cmd
->sense_phys_addr
));
1244 * Compute the total number of frames this command consumes. FW uses
1245 * this number to pull sufficient number of frames from host memory.
1247 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
1250 return cmd
->frame_count
;
1254 * megasas_build_ldio - Prepares IOs to logical devices
1255 * @instance: Adapter soft state
1256 * @scp: SCSI command
1257 * @cmd: Command to be prepared
1259 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1262 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1263 struct megasas_cmd
*cmd
)
1266 u8 sc
= scp
->cmnd
[0];
1268 struct megasas_io_frame
*ldio
;
1270 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1271 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1273 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1274 flags
= MFI_FRAME_DIR_WRITE
;
1275 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1276 flags
= MFI_FRAME_DIR_READ
;
1278 if (instance
->flag_ieee
== 1) {
1279 flags
|= MFI_FRAME_IEEE
;
1283 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1285 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
1286 ldio
->cmd_status
= 0x0;
1287 ldio
->scsi_status
= 0x0;
1288 ldio
->target_id
= device_id
;
1290 ldio
->reserved_0
= 0;
1292 ldio
->flags
= cpu_to_le16(flags
);
1293 ldio
->start_lba_hi
= 0;
1294 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
1297 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1299 if (scp
->cmd_len
== 6) {
1300 ldio
->lba_count
= cpu_to_le32((u32
) scp
->cmnd
[4]);
1301 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[1] << 16) |
1302 ((u32
) scp
->cmnd
[2] << 8) |
1303 (u32
) scp
->cmnd
[3]);
1305 ldio
->start_lba_lo
&= cpu_to_le32(0x1FFFFF);
1309 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1311 else if (scp
->cmd_len
== 10) {
1312 ldio
->lba_count
= cpu_to_le32((u32
) scp
->cmnd
[8] |
1313 ((u32
) scp
->cmnd
[7] << 8));
1314 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1315 ((u32
) scp
->cmnd
[3] << 16) |
1316 ((u32
) scp
->cmnd
[4] << 8) |
1317 (u32
) scp
->cmnd
[5]);
1321 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1323 else if (scp
->cmd_len
== 12) {
1324 ldio
->lba_count
= cpu_to_le32(((u32
) scp
->cmnd
[6] << 24) |
1325 ((u32
) scp
->cmnd
[7] << 16) |
1326 ((u32
) scp
->cmnd
[8] << 8) |
1327 (u32
) scp
->cmnd
[9]);
1329 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1330 ((u32
) scp
->cmnd
[3] << 16) |
1331 ((u32
) scp
->cmnd
[4] << 8) |
1332 (u32
) scp
->cmnd
[5]);
1336 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1338 else if (scp
->cmd_len
== 16) {
1339 ldio
->lba_count
= cpu_to_le32(((u32
) scp
->cmnd
[10] << 24) |
1340 ((u32
) scp
->cmnd
[11] << 16) |
1341 ((u32
) scp
->cmnd
[12] << 8) |
1342 (u32
) scp
->cmnd
[13]);
1344 ldio
->start_lba_lo
= cpu_to_le32(((u32
) scp
->cmnd
[6] << 24) |
1345 ((u32
) scp
->cmnd
[7] << 16) |
1346 ((u32
) scp
->cmnd
[8] << 8) |
1347 (u32
) scp
->cmnd
[9]);
1349 ldio
->start_lba_hi
= cpu_to_le32(((u32
) scp
->cmnd
[2] << 24) |
1350 ((u32
) scp
->cmnd
[3] << 16) |
1351 ((u32
) scp
->cmnd
[4] << 8) |
1352 (u32
) scp
->cmnd
[5]);
1359 if (instance
->flag_ieee
) {
1360 ldio
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1361 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1363 } else if (IS_DMA64
) {
1364 ldio
->flags
|= cpu_to_le16(MFI_FRAME_SGL64
);
1365 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1367 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1369 if (ldio
->sge_count
> instance
->max_num_sge
) {
1370 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1376 * Sense info specific
1378 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1379 ldio
->sense_buf_phys_addr_hi
= 0;
1380 ldio
->sense_buf_phys_addr_lo
= cpu_to_le32(cmd
->sense_phys_addr
);
1383 * Compute the total number of frames this command consumes. FW uses
1384 * this number to pull sufficient number of frames from host memory.
1386 cmd
->frame_count
= megasas_get_frame_count(instance
,
1387 ldio
->sge_count
, IO_FRAME
);
1389 return cmd
->frame_count
;
1393 * megasas_is_ldio - Checks if the cmd is for logical drive
1394 * @scmd: SCSI command
1396 * Called by megasas_queue_command to find out if the command to be queued
1397 * is a logical drive command
1399 inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1401 if (!MEGASAS_IS_LOGICAL(cmd
))
1403 switch (cmd
->cmnd
[0]) {
1419 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1421 * @instance: Adapter soft state
1424 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1426 struct megasas_cmd
*cmd
;
1428 union megasas_sgl
*mfi_sgl
;
1429 struct megasas_io_frame
*ldio
;
1430 struct megasas_pthru_frame
*pthru
;
1432 u32 max_cmd
= instance
->max_fw_cmds
;
1434 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1435 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1437 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1439 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1441 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1442 for (i
= 0; i
< max_cmd
; i
++) {
1443 cmd
= instance
->cmd_list
[i
];
1446 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1447 if (megasas_is_ldio(cmd
->scmd
)){
1448 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1449 mfi_sgl
= &ldio
->sgl
;
1450 sgcount
= ldio
->sge_count
;
1451 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1452 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1453 instance
->host
->host_no
, cmd
->frame_count
, ldio
->cmd
, ldio
->target_id
,
1454 le32_to_cpu(ldio
->start_lba_lo
), le32_to_cpu(ldio
->start_lba_hi
),
1455 le32_to_cpu(ldio
->sense_buf_phys_addr_lo
), sgcount
);
1458 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1459 mfi_sgl
= &pthru
->sgl
;
1460 sgcount
= pthru
->sge_count
;
1461 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1462 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1463 instance
->host
->host_no
, cmd
->frame_count
, pthru
->cmd
, pthru
->target_id
,
1464 pthru
->lun
, pthru
->cdb_len
, le32_to_cpu(pthru
->data_xfer_len
),
1465 le32_to_cpu(pthru
->sense_buf_phys_addr_lo
), sgcount
);
1467 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1468 for (n
= 0; n
< sgcount
; n
++){
1470 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1471 le32_to_cpu(mfi_sgl
->sge64
[n
].length
),
1472 le64_to_cpu(mfi_sgl
->sge64
[n
].phys_addr
));
1474 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1475 le32_to_cpu(mfi_sgl
->sge32
[n
].length
),
1476 le32_to_cpu(mfi_sgl
->sge32
[n
].phys_addr
));
1479 printk(KERN_ERR
"\n");
1481 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1482 for (i
= 0; i
< max_cmd
; i
++) {
1484 cmd
= instance
->cmd_list
[i
];
1486 if(cmd
->sync_cmd
== 1){
1487 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1490 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1494 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
1495 struct scsi_cmnd
*scmd
)
1497 struct megasas_cmd
*cmd
;
1500 cmd
= megasas_get_cmd(instance
);
1502 return SCSI_MLQUEUE_HOST_BUSY
;
1505 * Logical drive command
1507 if (megasas_is_ldio(scmd
))
1508 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1510 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1513 goto out_return_cmd
;
1516 scmd
->SCp
.ptr
= (char *)cmd
;
1519 * Issue the command to the FW
1521 atomic_inc(&instance
->fw_outstanding
);
1523 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1524 cmd
->frame_count
-1, instance
->reg_set
);
1528 megasas_return_cmd(instance
, cmd
);
1534 * megasas_queue_command - Queue entry point
1535 * @scmd: SCSI command to be queued
1536 * @done: Callback entry point
1539 megasas_queue_command_lck(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
1541 struct megasas_instance
*instance
;
1542 unsigned long flags
;
1544 instance
= (struct megasas_instance
*)
1545 scmd
->device
->host
->hostdata
;
1547 if (instance
->issuepend_done
== 0)
1548 return SCSI_MLQUEUE_HOST_BUSY
;
1550 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1552 /* Check for an mpio path and adjust behavior */
1553 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
1554 if (megasas_check_mpio_paths(instance
, scmd
) ==
1555 (DID_RESET
<< 16)) {
1556 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1557 return SCSI_MLQUEUE_HOST_BUSY
;
1559 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1560 scmd
->result
= DID_NO_CONNECT
<< 16;
1566 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1567 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1568 scmd
->result
= DID_NO_CONNECT
<< 16;
1573 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1574 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1575 return SCSI_MLQUEUE_HOST_BUSY
;
1578 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1580 scmd
->scsi_done
= done
;
1583 if (MEGASAS_IS_LOGICAL(scmd
) &&
1584 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
1585 scmd
->result
= DID_BAD_TARGET
<< 16;
1589 switch (scmd
->cmnd
[0]) {
1590 case SYNCHRONIZE_CACHE
:
1592 * FW takes care of flush cache on its own
1593 * No need to send it down
1595 scmd
->result
= DID_OK
<< 16;
1601 if (instance
->instancet
->build_and_issue_cmd(instance
, scmd
)) {
1602 printk(KERN_ERR
"megasas: Err returned from build_and_issue_cmd\n");
1603 return SCSI_MLQUEUE_HOST_BUSY
;
1613 static DEF_SCSI_QCMD(megasas_queue_command
)
1615 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1619 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1621 if ((megasas_mgmt_info
.instance
[i
]) &&
1622 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1623 return megasas_mgmt_info
.instance
[i
];
1629 static int megasas_slave_configure(struct scsi_device
*sdev
)
1632 struct megasas_instance
*instance
;
1634 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1637 * Don't export physical disk devices to the disk driver.
1639 * FIXME: Currently we don't export them to the midlayer at all.
1640 * That will be fixed once LSI engineers have audited the
1641 * firmware for possible issues.
1643 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1644 sdev
->type
== TYPE_DISK
) {
1645 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1647 if (instance
->pd_list
[pd_index
].driveState
==
1648 MR_PD_STATE_SYSTEM
) {
1649 blk_queue_rq_timeout(sdev
->request_queue
,
1650 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1657 * The RAID firmware may require extended timeouts.
1659 blk_queue_rq_timeout(sdev
->request_queue
,
1660 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1664 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1667 struct megasas_instance
*instance
;
1668 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1669 if ((sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) &&
1670 (sdev
->type
== TYPE_DISK
)) {
1672 * Open the OS scan to the SYSTEM PD
1675 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1677 if ((instance
->pd_list
[pd_index
].driveState
==
1678 MR_PD_STATE_SYSTEM
) &&
1679 (instance
->pd_list
[pd_index
].driveType
==
1688 void megaraid_sas_kill_hba(struct megasas_instance
*instance
)
1690 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1691 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
1692 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1693 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
1694 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1695 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1696 writel(MFI_STOP_ADP
, &instance
->reg_set
->doorbell
);
1698 readl(&instance
->reg_set
->doorbell
);
1699 if (instance
->mpio
&& instance
->requestorId
)
1700 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
1702 writel(MFI_STOP_ADP
, &instance
->reg_set
->inbound_doorbell
);
1707 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1708 * restored to max value
1709 * @instance: Adapter soft state
1713 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
)
1715 unsigned long flags
;
1716 if (instance
->flag
& MEGASAS_FW_BUSY
1717 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1718 && atomic_read(&instance
->fw_outstanding
) <
1719 instance
->throttlequeuedepth
+ 1) {
1721 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1722 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1723 if (instance
->is_imr
) {
1724 instance
->host
->can_queue
=
1725 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1727 instance
->host
->can_queue
=
1728 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1730 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1735 * megasas_complete_cmd_dpc - Returns FW's controller structure
1736 * @instance_addr: Address of adapter soft state
1738 * Tasklet to complete cmds
1740 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1745 struct megasas_cmd
*cmd
;
1746 struct megasas_instance
*instance
=
1747 (struct megasas_instance
*)instance_addr
;
1748 unsigned long flags
;
1750 /* If we have already declared adapter dead, donot complete cmds */
1751 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1754 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1756 producer
= le32_to_cpu(*instance
->producer
);
1757 consumer
= le32_to_cpu(*instance
->consumer
);
1759 while (consumer
!= producer
) {
1760 context
= le32_to_cpu(instance
->reply_queue
[consumer
]);
1761 if (context
>= instance
->max_fw_cmds
) {
1762 printk(KERN_ERR
"Unexpected context value %x\n",
1767 cmd
= instance
->cmd_list
[context
];
1769 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1772 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1777 *instance
->consumer
= cpu_to_le32(producer
);
1779 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1782 * Check if we can restore can_queue
1784 megasas_check_and_restore_queue_depth(instance
);
1788 * megasas_start_timer - Initializes a timer object
1789 * @instance: Adapter soft state
1790 * @timer: timer object to be initialized
1791 * @fn: timer function
1792 * @interval: time interval between timer function call
1795 void megasas_start_timer(struct megasas_instance
*instance
,
1796 struct timer_list
*timer
,
1797 void *fn
, unsigned long interval
)
1800 timer
->expires
= jiffies
+ interval
;
1801 timer
->data
= (unsigned long)instance
;
1802 timer
->function
= fn
;
1807 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
);
1810 process_fw_state_change_wq(struct work_struct
*work
);
1812 void megasas_do_ocr(struct megasas_instance
*instance
)
1814 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
1815 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
1816 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
1817 *instance
->consumer
= cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN
);
1819 instance
->instancet
->disable_intr(instance
);
1820 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
1821 instance
->issuepend_done
= 0;
1823 atomic_set(&instance
->fw_outstanding
, 0);
1824 megasas_internal_reset_defer_cmds(instance
);
1825 process_fw_state_change_wq(&instance
->work_init
);
1828 /* This function will get the current SR-IOV LD/VF affiliation */
1829 static int megasas_get_ld_vf_affiliation(struct megasas_instance
*instance
,
1832 struct megasas_cmd
*cmd
;
1833 struct megasas_dcmd_frame
*dcmd
;
1834 struct MR_LD_VF_AFFILIATION
*new_affiliation
= NULL
;
1835 struct MR_LD_VF_AFFILIATION_111
*new_affiliation_111
= NULL
;
1836 struct MR_LD_VF_MAP
*newmap
= NULL
, *savedmap
= NULL
;
1837 dma_addr_t new_affiliation_h
;
1838 dma_addr_t new_affiliation_111_h
;
1842 cmd
= megasas_get_cmd(instance
);
1845 printk(KERN_DEBUG
"megasas: megasas_get_ld_vf_"
1846 "affiliation: Failed to get cmd for scsi%d.\n",
1847 instance
->host
->host_no
);
1851 dcmd
= &cmd
->frame
->dcmd
;
1853 if (!instance
->vf_affiliation
&& !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 if (instance
->PlasmaFW111
)
1862 memset(instance
->vf_affiliation_111
, 0,
1863 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1865 memset(instance
->vf_affiliation
, 0,
1866 (MAX_LOGICAL_DRIVES
+ 1) *
1867 sizeof(struct MR_LD_VF_AFFILIATION
));
1869 if (instance
->PlasmaFW111
)
1870 new_affiliation_111
=
1871 pci_alloc_consistent(instance
->pdev
,
1872 sizeof(struct MR_LD_VF_AFFILIATION_111
),
1873 &new_affiliation_111_h
);
1876 pci_alloc_consistent(instance
->pdev
,
1877 (MAX_LOGICAL_DRIVES
+ 1) *
1878 sizeof(struct MR_LD_VF_AFFILIATION
),
1879 &new_affiliation_h
);
1880 if (!new_affiliation
&& !new_affiliation_111
) {
1881 printk(KERN_DEBUG
"megasas: SR-IOV: Couldn't allocate "
1882 "memory for new affiliation for scsi%d.\n",
1883 instance
->host
->host_no
);
1884 megasas_return_cmd(instance
, cmd
);
1887 if (instance
->PlasmaFW111
)
1888 memset(new_affiliation_111
, 0,
1889 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1891 memset(new_affiliation
, 0, (MAX_LOGICAL_DRIVES
+ 1) *
1892 sizeof(struct MR_LD_VF_AFFILIATION
));
1895 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1897 dcmd
->cmd
= MFI_CMD_DCMD
;
1898 dcmd
->cmd_status
= 0xFF;
1899 dcmd
->sge_count
= 1;
1900 dcmd
->flags
= MFI_FRAME_DIR_BOTH
;
1903 if (instance
->PlasmaFW111
) {
1904 dcmd
->data_xfer_len
= sizeof(struct MR_LD_VF_AFFILIATION_111
);
1905 dcmd
->opcode
= MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111
;
1907 dcmd
->data_xfer_len
= (MAX_LOGICAL_DRIVES
+ 1) *
1908 sizeof(struct MR_LD_VF_AFFILIATION
);
1909 dcmd
->opcode
= MR_DCMD_LD_VF_MAP_GET_ALL_LDS
;
1913 if (instance
->PlasmaFW111
)
1914 dcmd
->sgl
.sge32
[0].phys_addr
=
1915 instance
->vf_affiliation_111_h
;
1917 dcmd
->sgl
.sge32
[0].phys_addr
=
1918 instance
->vf_affiliation_h
;
1920 if (instance
->PlasmaFW111
)
1921 dcmd
->sgl
.sge32
[0].phys_addr
= new_affiliation_111_h
;
1923 dcmd
->sgl
.sge32
[0].phys_addr
= new_affiliation_h
;
1925 if (instance
->PlasmaFW111
)
1926 dcmd
->sgl
.sge32
[0].length
=
1927 sizeof(struct MR_LD_VF_AFFILIATION_111
);
1929 dcmd
->sgl
.sge32
[0].length
= (MAX_LOGICAL_DRIVES
+ 1) *
1930 sizeof(struct MR_LD_VF_AFFILIATION
);
1932 printk(KERN_WARNING
"megasas: SR-IOV: Getting LD/VF affiliation for "
1933 "scsi%d\n", instance
->host
->host_no
);
1935 megasas_issue_blocked_cmd(instance
, cmd
, 0);
1937 if (dcmd
->cmd_status
) {
1938 printk(KERN_WARNING
"megasas: SR-IOV: LD/VF affiliation DCMD"
1939 " failed with status 0x%x for scsi%d.\n",
1940 dcmd
->cmd_status
, instance
->host
->host_no
);
1941 retval
= 1; /* Do a scan if we couldn't get affiliation */
1946 if (instance
->PlasmaFW111
) {
1947 if (!new_affiliation_111
->vdCount
) {
1948 printk(KERN_WARNING
"megasas: SR-IOV: Got new "
1949 "LD/VF affiliation for passive path "
1951 instance
->host
->host_no
);
1955 thisVf
= new_affiliation_111
->thisVf
;
1956 for (ld
= 0 ; ld
< new_affiliation_111
->vdCount
; ld
++)
1957 if (instance
->vf_affiliation_111
->map
[ld
].policy
[thisVf
] != new_affiliation_111
->map
[ld
].policy
[thisVf
]) {
1958 printk(KERN_WARNING
"megasas: SR-IOV: "
1959 "Got new LD/VF affiliation "
1961 instance
->host
->host_no
);
1962 memcpy(instance
->vf_affiliation_111
,
1963 new_affiliation_111
,
1964 sizeof(struct MR_LD_VF_AFFILIATION_111
));
1969 if (!new_affiliation
->ldCount
) {
1970 printk(KERN_WARNING
"megasas: SR-IOV: Got new "
1971 "LD/VF affiliation for passive "
1972 "path for scsi%d.\n",
1973 instance
->host
->host_no
);
1977 newmap
= new_affiliation
->map
;
1978 savedmap
= instance
->vf_affiliation
->map
;
1979 thisVf
= new_affiliation
->thisVf
;
1980 for (ld
= 0 ; ld
< new_affiliation
->ldCount
; ld
++) {
1981 if (savedmap
->policy
[thisVf
] !=
1982 newmap
->policy
[thisVf
]) {
1983 printk(KERN_WARNING
"megasas: SR-IOV: "
1984 "Got new LD/VF affiliation "
1986 instance
->host
->host_no
);
1987 memcpy(instance
->vf_affiliation
,
1989 new_affiliation
->size
);
1993 savedmap
= (struct MR_LD_VF_MAP
*)
1994 ((unsigned char *)savedmap
+
1996 newmap
= (struct MR_LD_VF_MAP
*)
1997 ((unsigned char *)newmap
+
2003 if (new_affiliation
) {
2004 if (instance
->PlasmaFW111
)
2005 pci_free_consistent(instance
->pdev
,
2006 sizeof(struct MR_LD_VF_AFFILIATION_111
),
2007 new_affiliation_111
,
2008 new_affiliation_111_h
);
2010 pci_free_consistent(instance
->pdev
,
2011 (MAX_LOGICAL_DRIVES
+ 1) *
2012 sizeof(struct MR_LD_VF_AFFILIATION
),
2013 new_affiliation
, new_affiliation_h
);
2015 megasas_return_cmd(instance
, cmd
);
2020 /* This function will tell FW to start the SR-IOV heartbeat */
2021 int megasas_sriov_start_heartbeat(struct megasas_instance
*instance
,
2024 struct megasas_cmd
*cmd
;
2025 struct megasas_dcmd_frame
*dcmd
;
2028 cmd
= megasas_get_cmd(instance
);
2031 printk(KERN_DEBUG
"megasas: megasas_sriov_start_heartbeat: "
2032 "Failed to get cmd for scsi%d.\n",
2033 instance
->host
->host_no
);
2037 dcmd
= &cmd
->frame
->dcmd
;
2040 instance
->hb_host_mem
=
2041 pci_zalloc_consistent(instance
->pdev
,
2042 sizeof(struct MR_CTRL_HB_HOST_MEM
),
2043 &instance
->hb_host_mem_h
);
2044 if (!instance
->hb_host_mem
) {
2045 printk(KERN_DEBUG
"megasas: SR-IOV: Couldn't allocate"
2046 " memory for heartbeat host memory for "
2047 "scsi%d.\n", instance
->host
->host_no
);
2053 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2055 dcmd
->mbox
.s
[0] = sizeof(struct MR_CTRL_HB_HOST_MEM
);
2056 dcmd
->cmd
= MFI_CMD_DCMD
;
2057 dcmd
->cmd_status
= 0xFF;
2058 dcmd
->sge_count
= 1;
2059 dcmd
->flags
= MFI_FRAME_DIR_BOTH
;
2062 dcmd
->data_xfer_len
= sizeof(struct MR_CTRL_HB_HOST_MEM
);
2063 dcmd
->opcode
= MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC
;
2064 dcmd
->sgl
.sge32
[0].phys_addr
= instance
->hb_host_mem_h
;
2065 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_CTRL_HB_HOST_MEM
);
2067 printk(KERN_WARNING
"megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2068 instance
->host
->host_no
);
2070 if (!megasas_issue_polled(instance
, cmd
)) {
2073 printk(KERN_WARNING
"megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2074 "_MEM_ALLOC DCMD timed out for scsi%d\n",
2075 instance
->host
->host_no
);
2081 if (dcmd
->cmd_status
) {
2082 printk(KERN_WARNING
"megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2083 "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2085 instance
->host
->host_no
);
2091 megasas_return_cmd(instance
, cmd
);
2096 /* Handler for SR-IOV heartbeat */
2097 void megasas_sriov_heartbeat_handler(unsigned long instance_addr
)
2099 struct megasas_instance
*instance
=
2100 (struct megasas_instance
*)instance_addr
;
2102 if (instance
->hb_host_mem
->HB
.fwCounter
!=
2103 instance
->hb_host_mem
->HB
.driverCounter
) {
2104 instance
->hb_host_mem
->HB
.driverCounter
=
2105 instance
->hb_host_mem
->HB
.fwCounter
;
2106 mod_timer(&instance
->sriov_heartbeat_timer
,
2107 jiffies
+ MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
2109 printk(KERN_WARNING
"megasas: SR-IOV: Heartbeat never "
2110 "completed for scsi%d\n", instance
->host
->host_no
);
2111 schedule_work(&instance
->work_init
);
2116 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2117 * @instance: Adapter soft state
2119 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2120 * complete all its outstanding commands. Returns error if one or more IOs
2121 * are pending after this time period. It also marks the controller dead.
2123 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
2127 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
2129 unsigned long flags
;
2130 struct list_head clist_local
;
2131 struct megasas_cmd
*reset_cmd
;
2133 u8 kill_adapter_flag
;
2135 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2136 adprecovery
= instance
->adprecovery
;
2137 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2139 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
2141 INIT_LIST_HEAD(&clist_local
);
2142 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2143 list_splice_init(&instance
->internal_reset_pending_q
,
2145 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2147 printk(KERN_NOTICE
"megasas: HBA reset wait ...\n");
2148 for (i
= 0; i
< wait_time
; i
++) {
2150 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2151 adprecovery
= instance
->adprecovery
;
2152 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2153 if (adprecovery
== MEGASAS_HBA_OPERATIONAL
)
2157 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
2158 printk(KERN_NOTICE
"megasas: reset: Stopping HBA.\n");
2159 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2160 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2161 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2166 while (!list_empty(&clist_local
)) {
2167 reset_cmd
= list_entry((&clist_local
)->next
,
2168 struct megasas_cmd
, list
);
2169 list_del_init(&reset_cmd
->list
);
2170 if (reset_cmd
->scmd
) {
2171 reset_cmd
->scmd
->result
= DID_RESET
<< 16;
2172 printk(KERN_NOTICE
"%d:%p reset [%02x]\n",
2173 reset_index
, reset_cmd
,
2174 reset_cmd
->scmd
->cmnd
[0]);
2176 reset_cmd
->scmd
->scsi_done(reset_cmd
->scmd
);
2177 megasas_return_cmd(instance
, reset_cmd
);
2178 } else if (reset_cmd
->sync_cmd
) {
2179 printk(KERN_NOTICE
"megasas:%p synch cmds"
2183 reset_cmd
->cmd_status
= ENODATA
;
2184 instance
->instancet
->fire_cmd(instance
,
2185 reset_cmd
->frame_phys_addr
,
2186 0, instance
->reg_set
);
2188 printk(KERN_NOTICE
"megasas: %p unexpected"
2198 for (i
= 0; i
< resetwaittime
; i
++) {
2200 int outstanding
= atomic_read(&instance
->fw_outstanding
);
2205 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
2206 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
2207 "commands to complete\n",i
,outstanding
);
2209 * Call cmd completion routine. Cmd to be
2210 * be completed directly without depending on isr.
2212 megasas_complete_cmd_dpc((unsigned long)instance
);
2219 kill_adapter_flag
= 0;
2221 fw_state
= instance
->instancet
->read_fw_status_reg(
2222 instance
->reg_set
) & MFI_STATE_MASK
;
2223 if ((fw_state
== MFI_STATE_FAULT
) &&
2224 (instance
->disableOnlineCtrlReset
== 0)) {
2226 kill_adapter_flag
= 2;
2229 megasas_do_ocr(instance
);
2230 kill_adapter_flag
= 1;
2232 /* wait for 1 secs to let FW finish the pending cmds */
2238 if (atomic_read(&instance
->fw_outstanding
) &&
2239 !kill_adapter_flag
) {
2240 if (instance
->disableOnlineCtrlReset
== 0) {
2242 megasas_do_ocr(instance
);
2244 /* wait for 5 secs to let FW finish the pending cmds */
2245 for (i
= 0; i
< wait_time
; i
++) {
2247 atomic_read(&instance
->fw_outstanding
);
2255 if (atomic_read(&instance
->fw_outstanding
) ||
2256 (kill_adapter_flag
== 2)) {
2257 printk(KERN_NOTICE
"megaraid_sas: pending cmds after reset\n");
2259 * Send signal to FW to stop processing any pending cmds.
2260 * The controller will be taken offline by the OS now.
2262 if ((instance
->pdev
->device
==
2263 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2264 (instance
->pdev
->device
==
2265 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
2266 writel(MFI_STOP_ADP
,
2267 &instance
->reg_set
->doorbell
);
2269 writel(MFI_STOP_ADP
,
2270 &instance
->reg_set
->inbound_doorbell
);
2272 megasas_dump_pending_frames(instance
);
2273 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2274 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2275 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2279 printk(KERN_NOTICE
"megaraid_sas: no pending cmds after reset\n");
2285 * megasas_generic_reset - Generic reset routine
2286 * @scmd: Mid-layer SCSI command
2288 * This routine implements a generic reset handler for device, bus and host
2289 * reset requests. Device, bus and host specific reset handlers can use this
2290 * function after they do their specific tasks.
2292 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
2295 struct megasas_instance
*instance
;
2297 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2299 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET cmd=%x retries=%x\n",
2300 scmd
->cmnd
[0], scmd
->retries
);
2302 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2303 printk(KERN_ERR
"megasas: cannot recover from previous reset "
2308 ret_val
= megasas_wait_for_outstanding(instance
);
2309 if (ret_val
== SUCCESS
)
2310 printk(KERN_NOTICE
"megasas: reset successful \n");
2312 printk(KERN_ERR
"megasas: failed to do reset\n");
2318 * megasas_reset_timer - quiesce the adapter if required
2321 * Sets the FW busy flag and reduces the host->can_queue if the
2322 * cmd has not been completed within the timeout period.
2325 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
2327 struct megasas_instance
*instance
;
2328 unsigned long flags
;
2330 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
2331 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
2332 return BLK_EH_NOT_HANDLED
;
2335 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2336 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
2337 /* FW is busy, throttle IO */
2338 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2340 instance
->host
->can_queue
= instance
->throttlequeuedepth
;
2341 instance
->last_time
= jiffies
;
2342 instance
->flag
|= MEGASAS_FW_BUSY
;
2344 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
2346 return BLK_EH_RESET_TIMER
;
2350 * megasas_reset_device - Device reset handler entry point
2352 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
2357 * First wait for all commands to complete
2359 ret
= megasas_generic_reset(scmd
);
2365 * megasas_reset_bus_host - Bus & host reset handler entry point
2367 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
2370 struct megasas_instance
*instance
;
2371 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
2374 * First wait for all commands to complete
2376 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
2377 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
2378 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
2379 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
2380 ret
= megasas_reset_fusion(scmd
->device
->host
, 1);
2382 ret
= megasas_generic_reset(scmd
);
2388 * megasas_bios_param - Returns disk geometry for a disk
2389 * @sdev: device handle
2390 * @bdev: block device
2391 * @capacity: drive capacity
2392 * @geom: geometry parameters
2395 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
2396 sector_t capacity
, int geom
[])
2402 /* Default heads (64) & sectors (32) */
2406 tmp
= heads
* sectors
;
2407 cylinders
= capacity
;
2409 sector_div(cylinders
, tmp
);
2412 * Handle extended translation size for logical drives > 1Gb
2415 if (capacity
>= 0x200000) {
2418 tmp
= heads
*sectors
;
2419 cylinders
= capacity
;
2420 sector_div(cylinders
, tmp
);
2425 geom
[2] = cylinders
;
2430 static void megasas_aen_polling(struct work_struct
*work
);
2433 * megasas_service_aen - Processes an event notification
2434 * @instance: Adapter soft state
2435 * @cmd: AEN command completed by the ISR
2437 * For AEN, driver sends a command down to FW that is held by the FW till an
2438 * event occurs. When an event of interest occurs, FW completes the command
2439 * that it was previously holding.
2441 * This routines sends SIGIO signal to processes that have registered with the
2445 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2447 unsigned long flags
;
2449 * Don't signal app if it is just an aborted previously registered aen
2451 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
2452 spin_lock_irqsave(&poll_aen_lock
, flags
);
2453 megasas_poll_wait_aen
= 1;
2454 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2455 wake_up(&megasas_poll_wait
);
2456 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
2461 instance
->aen_cmd
= NULL
;
2462 megasas_return_cmd(instance
, cmd
);
2464 if ((instance
->unload
== 0) &&
2465 ((instance
->issuepend_done
== 1))) {
2466 struct megasas_aen_event
*ev
;
2467 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
2469 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
2471 ev
->instance
= instance
;
2473 INIT_DELAYED_WORK(&ev
->hotplug_work
,
2474 megasas_aen_polling
);
2475 schedule_delayed_work(&ev
->hotplug_work
, 0);
2480 static int megasas_change_queue_depth(struct scsi_device
*sdev
,
2481 int queue_depth
, int reason
)
2483 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2486 if (queue_depth
> sdev
->host
->can_queue
)
2487 queue_depth
= sdev
->host
->can_queue
;
2488 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
),
2495 * Scsi host template for megaraid_sas driver
2497 static struct scsi_host_template megasas_template
= {
2499 .module
= THIS_MODULE
,
2500 .name
= "LSI SAS based MegaRAID driver",
2501 .proc_name
= "megaraid_sas",
2502 .slave_configure
= megasas_slave_configure
,
2503 .slave_alloc
= megasas_slave_alloc
,
2504 .queuecommand
= megasas_queue_command
,
2505 .eh_device_reset_handler
= megasas_reset_device
,
2506 .eh_bus_reset_handler
= megasas_reset_bus_host
,
2507 .eh_host_reset_handler
= megasas_reset_bus_host
,
2508 .eh_timed_out
= megasas_reset_timer
,
2509 .bios_param
= megasas_bios_param
,
2510 .use_clustering
= ENABLE_CLUSTERING
,
2511 .change_queue_depth
= megasas_change_queue_depth
,
2516 * megasas_complete_int_cmd - Completes an internal command
2517 * @instance: Adapter soft state
2518 * @cmd: Command to be completed
2520 * The megasas_issue_blocked_cmd() function waits for a command to complete
2521 * after it issues a command. This function wakes up that waiting routine by
2522 * calling wake_up() on the wait queue.
2525 megasas_complete_int_cmd(struct megasas_instance
*instance
,
2526 struct megasas_cmd
*cmd
)
2528 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
2530 if (cmd
->cmd_status
== ENODATA
) {
2531 cmd
->cmd_status
= 0;
2533 wake_up(&instance
->int_cmd_wait_q
);
2537 * megasas_complete_abort - Completes aborting a command
2538 * @instance: Adapter soft state
2539 * @cmd: Cmd that was issued to abort another cmd
2541 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2542 * after it issues an abort on a previously issued command. This function
2543 * wakes up all functions waiting on the same wait queue.
2546 megasas_complete_abort(struct megasas_instance
*instance
,
2547 struct megasas_cmd
*cmd
)
2549 if (cmd
->sync_cmd
) {
2551 cmd
->cmd_status
= 0;
2552 wake_up(&instance
->abort_cmd_wait_q
);
2559 * megasas_complete_cmd - Completes a command
2560 * @instance: Adapter soft state
2561 * @cmd: Command to be completed
2562 * @alt_status: If non-zero, use this value as status to
2563 * SCSI mid-layer instead of the value returned
2564 * by the FW. This should be used if caller wants
2565 * an alternate status (as in the case of aborted
2569 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
2573 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
2574 unsigned long flags
;
2575 struct fusion_context
*fusion
= instance
->ctrl_context
;
2578 /* flag for the retry reset */
2579 cmd
->retry_for_fw_reset
= 0;
2582 cmd
->scmd
->SCp
.ptr
= NULL
;
2585 case MFI_CMD_INVALID
:
2586 /* Some older 1068 controller FW may keep a pended
2587 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2588 when booting the kdump kernel. Ignore this command to
2589 prevent a kernel panic on shutdown of the kdump kernel. */
2590 printk(KERN_WARNING
"megaraid_sas: MFI_CMD_INVALID command "
2592 printk(KERN_WARNING
"megaraid_sas: If you have a controller "
2593 "other than PERC5, please upgrade your firmware.\n");
2595 case MFI_CMD_PD_SCSI_IO
:
2596 case MFI_CMD_LD_SCSI_IO
:
2599 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2600 * issued either through an IO path or an IOCTL path. If it
2601 * was via IOCTL, we will send it to internal completion.
2603 if (cmd
->sync_cmd
) {
2605 megasas_complete_int_cmd(instance
, cmd
);
2609 case MFI_CMD_LD_READ
:
2610 case MFI_CMD_LD_WRITE
:
2613 cmd
->scmd
->result
= alt_status
<< 16;
2619 atomic_dec(&instance
->fw_outstanding
);
2621 scsi_dma_unmap(cmd
->scmd
);
2622 cmd
->scmd
->scsi_done(cmd
->scmd
);
2623 megasas_return_cmd(instance
, cmd
);
2628 switch (hdr
->cmd_status
) {
2631 cmd
->scmd
->result
= DID_OK
<< 16;
2634 case MFI_STAT_SCSI_IO_FAILED
:
2635 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2637 (DID_ERROR
<< 16) | hdr
->scsi_status
;
2640 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2642 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
2644 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
2645 memset(cmd
->scmd
->sense_buffer
, 0,
2646 SCSI_SENSE_BUFFERSIZE
);
2647 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
2650 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
2655 case MFI_STAT_LD_OFFLINE
:
2656 case MFI_STAT_DEVICE_NOT_FOUND
:
2657 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
2661 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
2663 cmd
->scmd
->result
= DID_ERROR
<< 16;
2667 atomic_dec(&instance
->fw_outstanding
);
2669 scsi_dma_unmap(cmd
->scmd
);
2670 cmd
->scmd
->scsi_done(cmd
->scmd
);
2671 megasas_return_cmd(instance
, cmd
);
2678 opcode
= le32_to_cpu(cmd
->frame
->dcmd
.opcode
);
2679 /* Check for LD map update */
2680 if ((opcode
== MR_DCMD_LD_MAP_GET_INFO
)
2681 && (cmd
->frame
->dcmd
.mbox
.b
[1] == 1)) {
2682 fusion
->fast_path_io
= 0;
2683 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2684 if (cmd
->frame
->hdr
.cmd_status
!= 0) {
2685 if (cmd
->frame
->hdr
.cmd_status
!=
2687 printk(KERN_WARNING
"megasas: map sync"
2688 "failed, status = 0x%x.\n",
2689 cmd
->frame
->hdr
.cmd_status
);
2691 megasas_return_cmd(instance
, cmd
);
2692 spin_unlock_irqrestore(
2693 instance
->host
->host_lock
,
2699 megasas_return_cmd(instance
, cmd
);
2702 * Set fast path IO to ZERO.
2703 * Validate Map will set proper value.
2704 * Meanwhile all IOs will go as LD IO.
2706 if (MR_ValidateMapInfo(instance
))
2707 fusion
->fast_path_io
= 1;
2709 fusion
->fast_path_io
= 0;
2710 megasas_sync_map_info(instance
);
2711 spin_unlock_irqrestore(instance
->host
->host_lock
,
2715 if (opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
2716 opcode
== MR_DCMD_CTRL_EVENT_GET
) {
2717 spin_lock_irqsave(&poll_aen_lock
, flags
);
2718 megasas_poll_wait_aen
= 0;
2719 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2723 * See if got an event notification
2725 if (opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
2726 megasas_service_aen(instance
, cmd
);
2728 megasas_complete_int_cmd(instance
, cmd
);
2734 * Cmd issued to abort another cmd returned
2736 megasas_complete_abort(instance
, cmd
);
2740 printk("megasas: Unknown command completed! [0x%X]\n",
2747 * megasas_issue_pending_cmds_again - issue all pending cmds
2748 * in FW again because of the fw reset
2749 * @instance: Adapter soft state
2752 megasas_issue_pending_cmds_again(struct megasas_instance
*instance
)
2754 struct megasas_cmd
*cmd
;
2755 struct list_head clist_local
;
2756 union megasas_evt_class_locale class_locale
;
2757 unsigned long flags
;
2760 INIT_LIST_HEAD(&clist_local
);
2761 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2762 list_splice_init(&instance
->internal_reset_pending_q
, &clist_local
);
2763 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2765 while (!list_empty(&clist_local
)) {
2766 cmd
= list_entry((&clist_local
)->next
,
2767 struct megasas_cmd
, list
);
2768 list_del_init(&cmd
->list
);
2770 if (cmd
->sync_cmd
|| cmd
->scmd
) {
2771 printk(KERN_NOTICE
"megaraid_sas: command %p, %p:%d"
2772 "detected to be pending while HBA reset.\n",
2773 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2775 cmd
->retry_for_fw_reset
++;
2777 if (cmd
->retry_for_fw_reset
== 3) {
2778 printk(KERN_NOTICE
"megaraid_sas: cmd %p, %p:%d"
2779 "was tried multiple times during reset."
2780 "Shutting down the HBA\n",
2781 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2782 megaraid_sas_kill_hba(instance
);
2784 instance
->adprecovery
=
2785 MEGASAS_HW_CRITICAL_ERROR
;
2790 if (cmd
->sync_cmd
== 1) {
2792 printk(KERN_NOTICE
"megaraid_sas: unexpected"
2793 "cmd attached to internal command!\n");
2795 printk(KERN_NOTICE
"megasas: %p synchronous cmd"
2796 "on the internal reset queue,"
2797 "issue it again.\n", cmd
);
2798 cmd
->cmd_status
= ENODATA
;
2799 instance
->instancet
->fire_cmd(instance
,
2800 cmd
->frame_phys_addr
,
2801 0, instance
->reg_set
);
2802 } else if (cmd
->scmd
) {
2803 printk(KERN_NOTICE
"megasas: %p scsi cmd [%02x]"
2804 "detected on the internal queue, issue again.\n",
2805 cmd
, cmd
->scmd
->cmnd
[0]);
2807 atomic_inc(&instance
->fw_outstanding
);
2808 instance
->instancet
->fire_cmd(instance
,
2809 cmd
->frame_phys_addr
,
2810 cmd
->frame_count
-1, instance
->reg_set
);
2812 printk(KERN_NOTICE
"megasas: %p unexpected cmd on the"
2813 "internal reset defer list while re-issue!!\n",
2818 if (instance
->aen_cmd
) {
2819 printk(KERN_NOTICE
"megaraid_sas: aen_cmd in def process\n");
2820 megasas_return_cmd(instance
, instance
->aen_cmd
);
2822 instance
->aen_cmd
= NULL
;
2826 * Initiate AEN (Asynchronous Event Notification)
2828 seq_num
= instance
->last_seq_num
;
2829 class_locale
.members
.reserved
= 0;
2830 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2831 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2833 megasas_register_aen(instance
, seq_num
, class_locale
.word
);
2837 * Move the internal reset pending commands to a deferred queue.
2839 * We move the commands pending at internal reset time to a
2840 * pending queue. This queue would be flushed after successful
2841 * completion of the internal reset sequence. if the internal reset
2842 * did not complete in time, the kernel reset handler would flush
2846 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
)
2848 struct megasas_cmd
*cmd
;
2850 u32 max_cmd
= instance
->max_fw_cmds
;
2852 unsigned long flags
;
2855 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
2856 for (i
= 0; i
< max_cmd
; i
++) {
2857 cmd
= instance
->cmd_list
[i
];
2858 if (cmd
->sync_cmd
== 1 || cmd
->scmd
) {
2859 printk(KERN_NOTICE
"megasas: moving cmd[%d]:%p:%d:%p"
2860 "on the defer queue as internal\n",
2861 defer_index
, cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2863 if (!list_empty(&cmd
->list
)) {
2864 printk(KERN_NOTICE
"megaraid_sas: ERROR while"
2865 " moving this cmd:%p, %d %p, it was"
2866 "discovered on some list?\n",
2867 cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2869 list_del_init(&cmd
->list
);
2872 list_add_tail(&cmd
->list
,
2873 &instance
->internal_reset_pending_q
);
2876 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
2881 process_fw_state_change_wq(struct work_struct
*work
)
2883 struct megasas_instance
*instance
=
2884 container_of(work
, struct megasas_instance
, work_init
);
2886 unsigned long flags
;
2888 if (instance
->adprecovery
!= MEGASAS_ADPRESET_SM_INFAULT
) {
2889 printk(KERN_NOTICE
"megaraid_sas: error, recovery st %x \n",
2890 instance
->adprecovery
);
2894 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
2895 printk(KERN_NOTICE
"megaraid_sas: FW detected to be in fault"
2896 "state, restarting it...\n");
2898 instance
->instancet
->disable_intr(instance
);
2899 atomic_set(&instance
->fw_outstanding
, 0);
2901 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
2902 instance
->instancet
->adp_reset(instance
, instance
->reg_set
);
2903 atomic_set(&instance
->fw_reset_no_pci_access
, 0 );
2905 printk(KERN_NOTICE
"megaraid_sas: FW restarted successfully,"
2906 "initiating next stage...\n");
2908 printk(KERN_NOTICE
"megaraid_sas: HBA recovery state machine,"
2909 "state 2 starting...\n");
2911 /*waitting for about 20 second before start the second init*/
2912 for (wait
= 0; wait
< 30; wait
++) {
2916 if (megasas_transition_to_ready(instance
, 1)) {
2917 printk(KERN_NOTICE
"megaraid_sas:adapter not ready\n");
2919 megaraid_sas_kill_hba(instance
);
2920 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2924 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
2925 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
2926 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)
2928 *instance
->consumer
= *instance
->producer
;
2930 *instance
->consumer
= 0;
2931 *instance
->producer
= 0;
2934 megasas_issue_init_mfi(instance
);
2936 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2937 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2938 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2939 instance
->instancet
->enable_intr(instance
);
2941 megasas_issue_pending_cmds_again(instance
);
2942 instance
->issuepend_done
= 1;
2948 * megasas_deplete_reply_queue - Processes all completed commands
2949 * @instance: Adapter soft state
2950 * @alt_status: Alternate status to be returned to
2951 * SCSI mid-layer instead of the status
2952 * returned by the FW
2953 * Note: this must be called with hba lock held
2956 megasas_deplete_reply_queue(struct megasas_instance
*instance
,
2962 if ((mfiStatus
= instance
->instancet
->check_reset(instance
,
2963 instance
->reg_set
)) == 1) {
2967 if ((mfiStatus
= instance
->instancet
->clear_intr(
2970 /* Hardware may not set outbound_intr_status in MSI-X mode */
2971 if (!instance
->msix_vectors
)
2975 instance
->mfiStatus
= mfiStatus
;
2977 if ((mfiStatus
& MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
)) {
2978 fw_state
= instance
->instancet
->read_fw_status_reg(
2979 instance
->reg_set
) & MFI_STATE_MASK
;
2981 if (fw_state
!= MFI_STATE_FAULT
) {
2982 printk(KERN_NOTICE
"megaraid_sas: fw state:%x\n",
2986 if ((fw_state
== MFI_STATE_FAULT
) &&
2987 (instance
->disableOnlineCtrlReset
== 0)) {
2988 printk(KERN_NOTICE
"megaraid_sas: wait adp restart\n");
2990 if ((instance
->pdev
->device
==
2991 PCI_DEVICE_ID_LSI_SAS1064R
) ||
2992 (instance
->pdev
->device
==
2993 PCI_DEVICE_ID_DELL_PERC5
) ||
2994 (instance
->pdev
->device
==
2995 PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
2997 *instance
->consumer
=
2998 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN
);
3002 instance
->instancet
->disable_intr(instance
);
3003 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
3004 instance
->issuepend_done
= 0;
3006 atomic_set(&instance
->fw_outstanding
, 0);
3007 megasas_internal_reset_defer_cmds(instance
);
3009 printk(KERN_NOTICE
"megasas: fwState=%x, stage:%d\n",
3010 fw_state
, instance
->adprecovery
);
3012 schedule_work(&instance
->work_init
);
3016 printk(KERN_NOTICE
"megasas: fwstate:%x, dis_OCR=%x\n",
3017 fw_state
, instance
->disableOnlineCtrlReset
);
3021 tasklet_schedule(&instance
->isr_tasklet
);
3025 * megasas_isr - isr entry point
3027 static irqreturn_t
megasas_isr(int irq
, void *devp
)
3029 struct megasas_irq_context
*irq_context
= devp
;
3030 struct megasas_instance
*instance
= irq_context
->instance
;
3031 unsigned long flags
;
3034 if (atomic_read(&instance
->fw_reset_no_pci_access
))
3037 spin_lock_irqsave(&instance
->hba_lock
, flags
);
3038 rc
= megasas_deplete_reply_queue(instance
, DID_OK
);
3039 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
3045 * megasas_transition_to_ready - Move the FW to READY state
3046 * @instance: Adapter soft state
3048 * During the initialization, FW passes can potentially be in any one of
3049 * several possible states. If the FW in operational, waiting-for-handshake
3050 * states, driver must take steps to bring it to ready state. Otherwise, it
3051 * has to wait for the ready state.
3054 megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
)
3060 u32 abs_state
, curr_abs_state
;
3062 abs_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
3063 fw_state
= abs_state
& MFI_STATE_MASK
;
3065 if (fw_state
!= MFI_STATE_READY
)
3066 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
3069 while (fw_state
!= MFI_STATE_READY
) {
3073 case MFI_STATE_FAULT
:
3074 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
3076 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3077 cur_state
= MFI_STATE_FAULT
;
3082 case MFI_STATE_WAIT_HANDSHAKE
:
3084 * Set the CLR bit in inbound doorbell
3086 if ((instance
->pdev
->device
==
3087 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3088 (instance
->pdev
->device
==
3089 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3090 (instance
->pdev
->device
==
3091 PCI_DEVICE_ID_LSI_FUSION
) ||
3092 (instance
->pdev
->device
==
3093 PCI_DEVICE_ID_LSI_PLASMA
) ||
3094 (instance
->pdev
->device
==
3095 PCI_DEVICE_ID_LSI_INVADER
) ||
3096 (instance
->pdev
->device
==
3097 PCI_DEVICE_ID_LSI_FURY
)) {
3099 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
3100 &instance
->reg_set
->doorbell
);
3103 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
3104 &instance
->reg_set
->inbound_doorbell
);
3107 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3108 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
3111 case MFI_STATE_BOOT_MESSAGE_PENDING
:
3112 if ((instance
->pdev
->device
==
3113 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3114 (instance
->pdev
->device
==
3115 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3116 (instance
->pdev
->device
==
3117 PCI_DEVICE_ID_LSI_FUSION
) ||
3118 (instance
->pdev
->device
==
3119 PCI_DEVICE_ID_LSI_PLASMA
) ||
3120 (instance
->pdev
->device
==
3121 PCI_DEVICE_ID_LSI_INVADER
) ||
3122 (instance
->pdev
->device
==
3123 PCI_DEVICE_ID_LSI_FURY
)) {
3124 writel(MFI_INIT_HOTPLUG
,
3125 &instance
->reg_set
->doorbell
);
3127 writel(MFI_INIT_HOTPLUG
,
3128 &instance
->reg_set
->inbound_doorbell
);
3130 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3131 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
3134 case MFI_STATE_OPERATIONAL
:
3136 * Bring it to READY state; assuming max wait 10 secs
3138 instance
->instancet
->disable_intr(instance
);
3139 if ((instance
->pdev
->device
==
3140 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3141 (instance
->pdev
->device
==
3142 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
3143 (instance
->pdev
->device
3144 == PCI_DEVICE_ID_LSI_FUSION
) ||
3145 (instance
->pdev
->device
3146 == PCI_DEVICE_ID_LSI_PLASMA
) ||
3147 (instance
->pdev
->device
3148 == PCI_DEVICE_ID_LSI_INVADER
) ||
3149 (instance
->pdev
->device
3150 == PCI_DEVICE_ID_LSI_FURY
)) {
3151 writel(MFI_RESET_FLAGS
,
3152 &instance
->reg_set
->doorbell
);
3153 if ((instance
->pdev
->device
==
3154 PCI_DEVICE_ID_LSI_FUSION
) ||
3155 (instance
->pdev
->device
==
3156 PCI_DEVICE_ID_LSI_PLASMA
) ||
3157 (instance
->pdev
->device
==
3158 PCI_DEVICE_ID_LSI_INVADER
) ||
3159 (instance
->pdev
->device
==
3160 PCI_DEVICE_ID_LSI_FURY
)) {
3161 for (i
= 0; i
< (10 * 1000); i
+= 20) {
3172 writel(MFI_RESET_FLAGS
,
3173 &instance
->reg_set
->inbound_doorbell
);
3175 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3176 cur_state
= MFI_STATE_OPERATIONAL
;
3179 case MFI_STATE_UNDEFINED
:
3181 * This state should not last for more than 2 seconds
3183 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3184 cur_state
= MFI_STATE_UNDEFINED
;
3187 case MFI_STATE_BB_INIT
:
3188 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3189 cur_state
= MFI_STATE_BB_INIT
;
3192 case MFI_STATE_FW_INIT
:
3193 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3194 cur_state
= MFI_STATE_FW_INIT
;
3197 case MFI_STATE_FW_INIT_2
:
3198 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3199 cur_state
= MFI_STATE_FW_INIT_2
;
3202 case MFI_STATE_DEVICE_SCAN
:
3203 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3204 cur_state
= MFI_STATE_DEVICE_SCAN
;
3207 case MFI_STATE_FLUSH_CACHE
:
3208 max_wait
= MEGASAS_RESET_WAIT_TIME
;
3209 cur_state
= MFI_STATE_FLUSH_CACHE
;
3213 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
3219 * The cur_state should not last for more than max_wait secs
3221 for (i
= 0; i
< (max_wait
* 1000); i
++) {
3222 curr_abs_state
= instance
->instancet
->
3223 read_fw_status_reg(instance
->reg_set
);
3225 if (abs_state
== curr_abs_state
) {
3232 * Return error if fw_state hasn't changed after max_wait
3234 if (curr_abs_state
== abs_state
) {
3235 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
3236 "in %d secs\n", fw_state
, max_wait
);
3240 abs_state
= curr_abs_state
;
3241 fw_state
= curr_abs_state
& MFI_STATE_MASK
;
3243 printk(KERN_INFO
"megasas: FW now in Ready state\n");
3249 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3250 * @instance: Adapter soft state
3252 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
3255 u32 max_cmd
= instance
->max_mfi_cmds
;
3256 struct megasas_cmd
*cmd
;
3258 if (!instance
->frame_dma_pool
)
3262 * Return all frames to pool
3264 for (i
= 0; i
< max_cmd
; i
++) {
3266 cmd
= instance
->cmd_list
[i
];
3269 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
3270 cmd
->frame_phys_addr
);
3273 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
3274 cmd
->sense_phys_addr
);
3278 * Now destroy the pool itself
3280 pci_pool_destroy(instance
->frame_dma_pool
);
3281 pci_pool_destroy(instance
->sense_dma_pool
);
3283 instance
->frame_dma_pool
= NULL
;
3284 instance
->sense_dma_pool
= NULL
;
3288 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3289 * @instance: Adapter soft state
3291 * Each command packet has an embedded DMA memory buffer that is used for
3292 * filling MFI frame and the SG list that immediately follows the frame. This
3293 * function creates those DMA memory buffers for each command packet by using
3294 * PCI pool facility.
3296 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
3304 struct megasas_cmd
*cmd
;
3306 max_cmd
= instance
->max_mfi_cmds
;
3309 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3310 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3312 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
3313 sizeof(struct megasas_sge32
);
3315 if (instance
->flag_ieee
) {
3316 sge_sz
= sizeof(struct megasas_sge_skinny
);
3320 * Calculated the number of 64byte frames required for SGL
3322 sgl_sz
= sge_sz
* instance
->max_num_sge
;
3323 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
3327 * We need one extra frame for the MFI command
3331 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
3333 * Use DMA pool facility provided by PCI layer
3335 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
3336 instance
->pdev
, total_sz
, 64,
3339 if (!instance
->frame_dma_pool
) {
3340 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
3344 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
3345 instance
->pdev
, 128, 4, 0);
3347 if (!instance
->sense_dma_pool
) {
3348 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
3350 pci_pool_destroy(instance
->frame_dma_pool
);
3351 instance
->frame_dma_pool
= NULL
;
3357 * Allocate and attach a frame to each of the commands in cmd_list.
3358 * By making cmd->index as the context instead of the &cmd, we can
3359 * always use 32bit context regardless of the architecture
3361 for (i
= 0; i
< max_cmd
; i
++) {
3363 cmd
= instance
->cmd_list
[i
];
3365 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
3366 GFP_KERNEL
, &cmd
->frame_phys_addr
);
3368 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
3369 GFP_KERNEL
, &cmd
->sense_phys_addr
);
3372 * megasas_teardown_frame_pool() takes care of freeing
3373 * whatever has been allocated
3375 if (!cmd
->frame
|| !cmd
->sense
) {
3376 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
3377 megasas_teardown_frame_pool(instance
);
3381 memset(cmd
->frame
, 0, total_sz
);
3382 cmd
->frame
->io
.context
= cpu_to_le32(cmd
->index
);
3383 cmd
->frame
->io
.pad_0
= 0;
3384 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
3385 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_PLASMA
) &&
3386 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
3387 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
3389 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
3396 * megasas_free_cmds - Free all the cmds in the free cmd pool
3397 * @instance: Adapter soft state
3399 void megasas_free_cmds(struct megasas_instance
*instance
)
3402 /* First free the MFI frame pool */
3403 megasas_teardown_frame_pool(instance
);
3405 /* Free all the commands in the cmd_list */
3406 for (i
= 0; i
< instance
->max_mfi_cmds
; i
++)
3408 kfree(instance
->cmd_list
[i
]);
3410 /* Free the cmd_list buffer itself */
3411 kfree(instance
->cmd_list
);
3412 instance
->cmd_list
= NULL
;
3414 INIT_LIST_HEAD(&instance
->cmd_pool
);
3418 * megasas_alloc_cmds - Allocates the command packets
3419 * @instance: Adapter soft state
3421 * Each command that is issued to the FW, whether IO commands from the OS or
3422 * internal commands like IOCTLs, are wrapped in local data structure called
3423 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3426 * Each frame has a 32-bit field called context (tag). This context is used
3427 * to get back the megasas_cmd from the frame when a frame gets completed in
3428 * the ISR. Typically the address of the megasas_cmd itself would be used as
3429 * the context. But we wanted to keep the differences between 32 and 64 bit
3430 * systems to the mininum. We always use 32 bit integers for the context. In
3431 * this driver, the 32 bit values are the indices into an array cmd_list.
3432 * This array is used only to look up the megasas_cmd given the context. The
3433 * free commands themselves are maintained in a linked list called cmd_pool.
3435 int megasas_alloc_cmds(struct megasas_instance
*instance
)
3440 struct megasas_cmd
*cmd
;
3442 max_cmd
= instance
->max_mfi_cmds
;
3445 * instance->cmd_list is an array of struct megasas_cmd pointers.
3446 * Allocate the dynamic array first and then allocate individual
3449 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
3451 if (!instance
->cmd_list
) {
3452 printk(KERN_DEBUG
"megasas: out of memory\n");
3456 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) *max_cmd
);
3458 for (i
= 0; i
< max_cmd
; i
++) {
3459 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
3462 if (!instance
->cmd_list
[i
]) {
3464 for (j
= 0; j
< i
; j
++)
3465 kfree(instance
->cmd_list
[j
]);
3467 kfree(instance
->cmd_list
);
3468 instance
->cmd_list
= NULL
;
3475 * Add all the commands to command pool (instance->cmd_pool)
3477 for (i
= 0; i
< max_cmd
; i
++) {
3478 cmd
= instance
->cmd_list
[i
];
3479 memset(cmd
, 0, sizeof(struct megasas_cmd
));
3482 cmd
->instance
= instance
;
3484 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
3488 * Create a frame pool and assign one frame to each cmd
3490 if (megasas_create_frame_pool(instance
)) {
3491 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
3492 megasas_free_cmds(instance
);
3499 * megasas_get_pd_list_info - Returns FW's pd_list structure
3500 * @instance: Adapter soft state
3501 * @pd_list: pd_list structure
3503 * Issues an internal command (DCMD) to get the FW's controller PD
3504 * list structure. This information is mainly used to find out SYSTEM
3505 * supported by the FW.
3508 megasas_get_pd_list(struct megasas_instance
*instance
)
3510 int ret
= 0, pd_index
= 0;
3511 struct megasas_cmd
*cmd
;
3512 struct megasas_dcmd_frame
*dcmd
;
3513 struct MR_PD_LIST
*ci
;
3514 struct MR_PD_ADDRESS
*pd_addr
;
3515 dma_addr_t ci_h
= 0;
3517 cmd
= megasas_get_cmd(instance
);
3520 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
3524 dcmd
= &cmd
->frame
->dcmd
;
3526 ci
= pci_alloc_consistent(instance
->pdev
,
3527 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
3530 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
3531 megasas_return_cmd(instance
, cmd
);
3535 memset(ci
, 0, sizeof(*ci
));
3536 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3538 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
3539 dcmd
->mbox
.b
[1] = 0;
3540 dcmd
->cmd
= MFI_CMD_DCMD
;
3541 dcmd
->cmd_status
= 0xFF;
3542 dcmd
->sge_count
= 1;
3543 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3546 dcmd
->data_xfer_len
= cpu_to_le32(MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
));
3547 dcmd
->opcode
= cpu_to_le32(MR_DCMD_PD_LIST_QUERY
);
3548 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3549 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
));
3551 if (!megasas_issue_polled(instance
, cmd
)) {
3558 * the following function will get the instance PD LIST.
3564 (le32_to_cpu(ci
->count
) <
3565 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
3567 memset(instance
->local_pd_list
, 0,
3568 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
3570 for (pd_index
= 0; pd_index
< le32_to_cpu(ci
->count
); pd_index
++) {
3572 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].tid
=
3573 le16_to_cpu(pd_addr
->deviceId
);
3574 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].driveType
=
3575 pd_addr
->scsiDevType
;
3576 instance
->local_pd_list
[le16_to_cpu(pd_addr
->deviceId
)].driveState
=
3580 memcpy(instance
->pd_list
, instance
->local_pd_list
,
3581 sizeof(instance
->pd_list
));
3584 pci_free_consistent(instance
->pdev
,
3585 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
3587 megasas_return_cmd(instance
, cmd
);
3593 * megasas_get_ld_list_info - Returns FW's ld_list structure
3594 * @instance: Adapter soft state
3595 * @ld_list: ld_list structure
3597 * Issues an internal command (DCMD) to get the FW's controller PD
3598 * list structure. This information is mainly used to find out SYSTEM
3599 * supported by the FW.
3602 megasas_get_ld_list(struct megasas_instance
*instance
)
3604 int ret
= 0, ld_index
= 0, ids
= 0;
3605 struct megasas_cmd
*cmd
;
3606 struct megasas_dcmd_frame
*dcmd
;
3607 struct MR_LD_LIST
*ci
;
3608 dma_addr_t ci_h
= 0;
3611 cmd
= megasas_get_cmd(instance
);
3614 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
3618 dcmd
= &cmd
->frame
->dcmd
;
3620 ci
= pci_alloc_consistent(instance
->pdev
,
3621 sizeof(struct MR_LD_LIST
),
3625 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
3626 megasas_return_cmd(instance
, cmd
);
3630 memset(ci
, 0, sizeof(*ci
));
3631 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3633 dcmd
->cmd
= MFI_CMD_DCMD
;
3634 dcmd
->cmd_status
= 0xFF;
3635 dcmd
->sge_count
= 1;
3636 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3638 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct MR_LD_LIST
));
3639 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_GET_LIST
);
3640 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3641 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct MR_LD_LIST
));
3644 if (!megasas_issue_polled(instance
, cmd
)) {
3650 ld_count
= le32_to_cpu(ci
->ldCount
);
3652 /* the following function will get the instance PD LIST */
3654 if ((ret
== 0) && (ld_count
<= MAX_LOGICAL_DRIVES
)) {
3655 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3657 for (ld_index
= 0; ld_index
< ld_count
; ld_index
++) {
3658 if (ci
->ldList
[ld_index
].state
!= 0) {
3659 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
3660 instance
->ld_ids
[ids
] =
3661 ci
->ldList
[ld_index
].ref
.targetId
;
3666 pci_free_consistent(instance
->pdev
,
3667 sizeof(struct MR_LD_LIST
),
3671 megasas_return_cmd(instance
, cmd
);
3676 * megasas_ld_list_query - Returns FW's ld_list structure
3677 * @instance: Adapter soft state
3678 * @ld_list: ld_list structure
3680 * Issues an internal command (DCMD) to get the FW's controller PD
3681 * list structure. This information is mainly used to find out SYSTEM
3682 * supported by the FW.
3685 megasas_ld_list_query(struct megasas_instance
*instance
, u8 query_type
)
3687 int ret
= 0, ld_index
= 0, ids
= 0;
3688 struct megasas_cmd
*cmd
;
3689 struct megasas_dcmd_frame
*dcmd
;
3690 struct MR_LD_TARGETID_LIST
*ci
;
3691 dma_addr_t ci_h
= 0;
3694 cmd
= megasas_get_cmd(instance
);
3698 "megasas:(megasas_ld_list_query): Failed to get cmd\n");
3702 dcmd
= &cmd
->frame
->dcmd
;
3704 ci
= pci_alloc_consistent(instance
->pdev
,
3705 sizeof(struct MR_LD_TARGETID_LIST
), &ci_h
);
3709 "megasas: Failed to alloc mem for ld_list_query\n");
3710 megasas_return_cmd(instance
, cmd
);
3714 memset(ci
, 0, sizeof(*ci
));
3715 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3717 dcmd
->mbox
.b
[0] = query_type
;
3719 dcmd
->cmd
= MFI_CMD_DCMD
;
3720 dcmd
->cmd_status
= 0xFF;
3721 dcmd
->sge_count
= 1;
3722 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3724 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST
));
3725 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_LIST_QUERY
);
3726 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3727 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST
));
3730 if (!megasas_issue_polled(instance
, cmd
) && !dcmd
->cmd_status
) {
3733 /* On failure, call older LD list DCMD */
3737 tgtid_count
= le32_to_cpu(ci
->count
);
3739 if ((ret
== 0) && (tgtid_count
<= (MAX_LOGICAL_DRIVES
))) {
3740 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3741 for (ld_index
= 0; ld_index
< tgtid_count
; ld_index
++) {
3742 ids
= ci
->targetId
[ld_index
];
3743 instance
->ld_ids
[ids
] = ci
->targetId
[ld_index
];
3748 pci_free_consistent(instance
->pdev
, sizeof(struct MR_LD_TARGETID_LIST
),
3751 megasas_return_cmd(instance
, cmd
);
3757 * megasas_get_controller_info - Returns FW's controller structure
3758 * @instance: Adapter soft state
3759 * @ctrl_info: Controller information structure
3761 * Issues an internal command (DCMD) to get the FW's controller structure.
3762 * This information is mainly used to find out the maximum IO transfer per
3763 * command supported by the FW.
3766 megasas_get_ctrl_info(struct megasas_instance
*instance
,
3767 struct megasas_ctrl_info
*ctrl_info
)
3770 struct megasas_cmd
*cmd
;
3771 struct megasas_dcmd_frame
*dcmd
;
3772 struct megasas_ctrl_info
*ci
;
3773 dma_addr_t ci_h
= 0;
3775 cmd
= megasas_get_cmd(instance
);
3778 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
3782 dcmd
= &cmd
->frame
->dcmd
;
3784 ci
= pci_alloc_consistent(instance
->pdev
,
3785 sizeof(struct megasas_ctrl_info
), &ci_h
);
3788 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
3789 megasas_return_cmd(instance
, cmd
);
3793 memset(ci
, 0, sizeof(*ci
));
3794 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3796 dcmd
->cmd
= MFI_CMD_DCMD
;
3797 dcmd
->cmd_status
= 0xFF;
3798 dcmd
->sge_count
= 1;
3799 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
3802 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_ctrl_info
));
3803 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_GET_INFO
);
3804 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
3805 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_ctrl_info
));
3807 if (!megasas_issue_polled(instance
, cmd
)) {
3809 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
3814 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
3817 megasas_return_cmd(instance
, cmd
);
3822 * megasas_issue_init_mfi - Initializes the FW
3823 * @instance: Adapter soft state
3825 * Issues the INIT MFI cmd
3828 megasas_issue_init_mfi(struct megasas_instance
*instance
)
3832 struct megasas_cmd
*cmd
;
3834 struct megasas_init_frame
*init_frame
;
3835 struct megasas_init_queue_info
*initq_info
;
3836 dma_addr_t init_frame_h
;
3837 dma_addr_t initq_info_h
;
3840 * Prepare a init frame. Note the init frame points to queue info
3841 * structure. Each frame has SGL allocated after first 64 bytes. For
3842 * this frame - since we don't need any SGL - we use SGL's space as
3843 * queue info structure
3845 * We will not get a NULL command below. We just created the pool.
3847 cmd
= megasas_get_cmd(instance
);
3849 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
3850 initq_info
= (struct megasas_init_queue_info
*)
3851 ((unsigned long)init_frame
+ 64);
3853 init_frame_h
= cmd
->frame_phys_addr
;
3854 initq_info_h
= init_frame_h
+ 64;
3856 context
= init_frame
->context
;
3857 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
3858 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
3859 init_frame
->context
= context
;
3861 initq_info
->reply_queue_entries
= cpu_to_le32(instance
->max_fw_cmds
+ 1);
3862 initq_info
->reply_queue_start_phys_addr_lo
= cpu_to_le32(instance
->reply_queue_h
);
3864 initq_info
->producer_index_phys_addr_lo
= cpu_to_le32(instance
->producer_h
);
3865 initq_info
->consumer_index_phys_addr_lo
= cpu_to_le32(instance
->consumer_h
);
3867 init_frame
->cmd
= MFI_CMD_INIT
;
3868 init_frame
->cmd_status
= 0xFF;
3869 init_frame
->queue_info_new_phys_addr_lo
=
3870 cpu_to_le32(lower_32_bits(initq_info_h
));
3871 init_frame
->queue_info_new_phys_addr_hi
=
3872 cpu_to_le32(upper_32_bits(initq_info_h
));
3874 init_frame
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_init_queue_info
));
3877 * disable the intr before firing the init frame to FW
3879 instance
->instancet
->disable_intr(instance
);
3882 * Issue the init frame in polled mode
3885 if (megasas_issue_polled(instance
, cmd
)) {
3886 printk(KERN_ERR
"megasas: Failed to init firmware\n");
3887 megasas_return_cmd(instance
, cmd
);
3891 megasas_return_cmd(instance
, cmd
);
3900 megasas_init_adapter_mfi(struct megasas_instance
*instance
)
3902 struct megasas_register_set __iomem
*reg_set
;
3906 reg_set
= instance
->reg_set
;
3909 * Get various operational parameters from status register
3911 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
3913 * Reduce the max supported cmds by 1. This is to ensure that the
3914 * reply_q_sz (1 more than the max cmd that driver may send)
3915 * does not exceed max cmds that the FW can support
3917 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
3918 instance
->max_mfi_cmds
= instance
->max_fw_cmds
;
3919 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
3922 * Create a pool of commands
3924 if (megasas_alloc_cmds(instance
))
3925 goto fail_alloc_cmds
;
3928 * Allocate memory for reply queue. Length of reply queue should
3929 * be _one_ more than the maximum commands handled by the firmware.
3931 * Note: When FW completes commands, it places corresponding contex
3932 * values in this circular reply queue. This circular queue is a fairly
3933 * typical producer-consumer queue. FW is the producer (of completed
3934 * commands) and the driver is the consumer.
3936 context_sz
= sizeof(u32
);
3937 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
3939 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
3941 &instance
->reply_queue_h
);
3943 if (!instance
->reply_queue
) {
3944 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
3945 goto fail_reply_queue
;
3948 if (megasas_issue_init_mfi(instance
))
3951 instance
->fw_support_ieee
= 0;
3952 instance
->fw_support_ieee
=
3953 (instance
->instancet
->read_fw_status_reg(reg_set
) &
3956 printk(KERN_NOTICE
"megasas_init_mfi: fw_support_ieee=%d",
3957 instance
->fw_support_ieee
);
3959 if (instance
->fw_support_ieee
)
3960 instance
->flag_ieee
= 1;
3966 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3967 instance
->reply_queue
, instance
->reply_queue_h
);
3969 megasas_free_cmds(instance
);
3976 * megasas_init_fw - Initializes the FW
3977 * @instance: Adapter soft state
3979 * This is the main function for initializing firmware
3982 static int megasas_init_fw(struct megasas_instance
*instance
)
3986 u32 tmp_sectors
, msix_enable
, scratch_pad_2
;
3987 resource_size_t base_addr
;
3988 struct megasas_register_set __iomem
*reg_set
;
3989 struct megasas_ctrl_info
*ctrl_info
;
3990 unsigned long bar_list
;
3991 int i
, loop
, fw_msix_count
= 0;
3992 struct IOV_111
*iovPtr
;
3994 /* Find first memory bar */
3995 bar_list
= pci_select_bars(instance
->pdev
, IORESOURCE_MEM
);
3996 instance
->bar
= find_first_bit(&bar_list
, sizeof(unsigned long));
3997 if (pci_request_selected_regions(instance
->pdev
, instance
->bar
,
3999 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
4003 base_addr
= pci_resource_start(instance
->pdev
, instance
->bar
);
4004 instance
->reg_set
= ioremap_nocache(base_addr
, 8192);
4006 if (!instance
->reg_set
) {
4007 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
4011 reg_set
= instance
->reg_set
;
4013 switch (instance
->pdev
->device
) {
4014 case PCI_DEVICE_ID_LSI_FUSION
:
4015 case PCI_DEVICE_ID_LSI_PLASMA
:
4016 case PCI_DEVICE_ID_LSI_INVADER
:
4017 case PCI_DEVICE_ID_LSI_FURY
:
4018 instance
->instancet
= &megasas_instance_template_fusion
;
4020 case PCI_DEVICE_ID_LSI_SAS1078R
:
4021 case PCI_DEVICE_ID_LSI_SAS1078DE
:
4022 instance
->instancet
= &megasas_instance_template_ppc
;
4024 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
4025 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
4026 instance
->instancet
= &megasas_instance_template_gen2
;
4028 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
4029 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
4030 instance
->instancet
= &megasas_instance_template_skinny
;
4032 case PCI_DEVICE_ID_LSI_SAS1064R
:
4033 case PCI_DEVICE_ID_DELL_PERC5
:
4035 instance
->instancet
= &megasas_instance_template_xscale
;
4039 if (megasas_transition_to_ready(instance
, 0)) {
4040 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
4041 instance
->instancet
->adp_reset
4042 (instance
, instance
->reg_set
);
4043 atomic_set(&instance
->fw_reset_no_pci_access
, 0);
4044 dev_info(&instance
->pdev
->dev
,
4045 "megasas: FW restarted successfully from %s!\n",
4048 /*waitting for about 30 second before retry*/
4051 if (megasas_transition_to_ready(instance
, 0))
4052 goto fail_ready_state
;
4056 * MSI-X host index 0 is common for all adapter.
4057 * It is used for all MPT based Adapters.
4059 instance
->reply_post_host_index_addr
[0] =
4060 (u32
*)((u8
*)instance
->reg_set
+
4061 MPI2_REPLY_POST_HOST_INDEX_OFFSET
);
4063 /* Check if MSI-X is supported while in ready state */
4064 msix_enable
= (instance
->instancet
->read_fw_status_reg(reg_set
) &
4066 if (msix_enable
&& !msix_disable
) {
4067 scratch_pad_2
= readl
4068 (&instance
->reg_set
->outbound_scratch_pad_2
);
4069 /* Check max MSI-X vectors */
4070 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4071 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
)) {
4072 instance
->msix_vectors
= (scratch_pad_2
4073 & MR_MAX_REPLY_QUEUES_OFFSET
) + 1;
4074 fw_msix_count
= instance
->msix_vectors
;
4076 instance
->msix_vectors
=
4078 instance
->msix_vectors
);
4079 } else if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)
4080 || (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
4081 /* Invader/Fury supports more than 8 MSI-X */
4082 instance
->msix_vectors
= ((scratch_pad_2
4083 & MR_MAX_REPLY_QUEUES_EXT_OFFSET
)
4084 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT
) + 1;
4085 fw_msix_count
= instance
->msix_vectors
;
4086 /* Save 1-15 reply post index address to local memory
4087 * Index 0 is already saved from reg offset
4088 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4090 for (loop
= 1; loop
< MR_MAX_MSIX_REG_ARRAY
; loop
++) {
4091 instance
->reply_post_host_index_addr
[loop
] =
4092 (u32
*)((u8
*)instance
->reg_set
+
4093 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4097 instance
->msix_vectors
= min(msix_vectors
,
4098 instance
->msix_vectors
);
4100 instance
->msix_vectors
= 1;
4101 /* Don't bother allocating more MSI-X vectors than cpus */
4102 instance
->msix_vectors
= min(instance
->msix_vectors
,
4103 (unsigned int)num_online_cpus());
4104 for (i
= 0; i
< instance
->msix_vectors
; i
++)
4105 instance
->msixentry
[i
].entry
= i
;
4106 i
= pci_enable_msix(instance
->pdev
, instance
->msixentry
,
4107 instance
->msix_vectors
);
4110 if (!pci_enable_msix(instance
->pdev
,
4111 instance
->msixentry
, i
))
4112 instance
->msix_vectors
= i
;
4114 instance
->msix_vectors
= 0;
4117 instance
->msix_vectors
= 0;
4119 dev_info(&instance
->pdev
->dev
, "[scsi%d]: FW supports"
4120 "<%d> MSIX vector,Online CPUs: <%d>,"
4121 "Current MSIX <%d>\n", instance
->host
->host_no
,
4122 fw_msix_count
, (unsigned int)num_online_cpus(),
4123 instance
->msix_vectors
);
4126 /* Get operational params, sge flags, send init cmd to controller */
4127 if (instance
->instancet
->init_adapter(instance
))
4128 goto fail_init_adapter
;
4130 printk(KERN_ERR
"megasas: INIT adapter done\n");
4133 * the following function will get the PD LIST.
4136 memset(instance
->pd_list
, 0 ,
4137 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
4138 if (megasas_get_pd_list(instance
) < 0) {
4139 printk(KERN_ERR
"megasas: failed to get PD list\n");
4140 goto fail_init_adapter
;
4143 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
4144 if (megasas_ld_list_query(instance
,
4145 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
4146 megasas_get_ld_list(instance
);
4148 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
4151 * Compute the max allowed sectors per IO: The controller info has two
4152 * limits on max sectors. Driver should use the minimum of these two.
4154 * 1 << stripe_sz_ops.min = max sectors per strip
4156 * Note that older firmwares ( < FW ver 30) didn't report information
4157 * to calculate max_sectors_1. So the number ended up as zero always.
4160 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
4162 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
4163 le16_to_cpu(ctrl_info
->max_strips_per_io
);
4164 max_sectors_2
= le32_to_cpu(ctrl_info
->max_request_size
);
4166 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
4168 /*Check whether controller is iMR or MR */
4169 if (ctrl_info
->memory_size
) {
4170 instance
->is_imr
= 0;
4171 dev_info(&instance
->pdev
->dev
, "Controller type: MR,"
4172 "Memory size is: %dMB\n",
4173 le16_to_cpu(ctrl_info
->memory_size
));
4175 instance
->is_imr
= 1;
4176 dev_info(&instance
->pdev
->dev
,
4177 "Controller type: iMR\n");
4179 /* OnOffProperties are converted into CPU arch*/
4180 le32_to_cpus((u32
*)&ctrl_info
->properties
.OnOffProperties
);
4181 instance
->disableOnlineCtrlReset
=
4182 ctrl_info
->properties
.OnOffProperties
.disableOnlineCtrlReset
;
4183 /* adapterOperations2 are converted into CPU arch*/
4184 le32_to_cpus((u32
*)&ctrl_info
->adapterOperations2
);
4185 instance
->mpio
= ctrl_info
->adapterOperations2
.mpio
;
4186 instance
->UnevenSpanSupport
=
4187 ctrl_info
->adapterOperations2
.supportUnevenSpans
;
4188 if (instance
->UnevenSpanSupport
) {
4189 struct fusion_context
*fusion
= instance
->ctrl_context
;
4190 dev_info(&instance
->pdev
->dev
, "FW supports: "
4191 "UnevenSpanSupport=%x\n", instance
->UnevenSpanSupport
);
4192 if (MR_ValidateMapInfo(instance
))
4193 fusion
->fast_path_io
= 1;
4195 fusion
->fast_path_io
= 0;
4198 if (ctrl_info
->host_interface
.SRIOV
) {
4199 if (!ctrl_info
->adapterOperations2
.activePassive
)
4200 instance
->PlasmaFW111
= 1;
4202 if (!instance
->PlasmaFW111
)
4203 instance
->requestorId
=
4204 ctrl_info
->iov
.requestorId
;
4206 iovPtr
= (struct IOV_111
*)((unsigned char *)ctrl_info
+ IOV_111_OFFSET
);
4207 instance
->requestorId
= iovPtr
->requestorId
;
4209 printk(KERN_WARNING
"megaraid_sas: I am VF "
4210 "requestorId %d\n", instance
->requestorId
);
4213 instance
->max_sectors_per_req
= instance
->max_num_sge
*
4215 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
4216 instance
->max_sectors_per_req
= tmp_sectors
;
4220 /* Check for valid throttlequeuedepth module parameter */
4221 if (instance
->is_imr
) {
4222 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
4223 MEGASAS_SKINNY_INT_CMDS
))
4224 instance
->throttlequeuedepth
=
4225 MEGASAS_THROTTLE_QUEUE_DEPTH
;
4227 instance
->throttlequeuedepth
= throttlequeuedepth
;
4229 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
4231 instance
->throttlequeuedepth
=
4232 MEGASAS_THROTTLE_QUEUE_DEPTH
;
4234 instance
->throttlequeuedepth
= throttlequeuedepth
;
4238 * Setup tasklet for cmd completion
4241 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
4242 (unsigned long)instance
);
4244 /* Launch SR-IOV heartbeat timer */
4245 if (instance
->requestorId
) {
4246 if (!megasas_sriov_start_heartbeat(instance
, 1))
4247 megasas_start_timer(instance
,
4248 &instance
->sriov_heartbeat_timer
,
4249 megasas_sriov_heartbeat_handler
,
4250 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
4252 instance
->skip_heartbeat_timer_del
= 1;
4259 iounmap(instance
->reg_set
);
4262 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
4268 * megasas_release_mfi - Reverses the FW initialization
4269 * @intance: Adapter soft state
4271 static void megasas_release_mfi(struct megasas_instance
*instance
)
4273 u32 reply_q_sz
= sizeof(u32
) *(instance
->max_mfi_cmds
+ 1);
4275 if (instance
->reply_queue
)
4276 pci_free_consistent(instance
->pdev
, reply_q_sz
,
4277 instance
->reply_queue
, instance
->reply_queue_h
);
4279 megasas_free_cmds(instance
);
4281 iounmap(instance
->reg_set
);
4283 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
4287 * megasas_get_seq_num - Gets latest event sequence numbers
4288 * @instance: Adapter soft state
4289 * @eli: FW event log sequence numbers information
4291 * FW maintains a log of all events in a non-volatile area. Upper layers would
4292 * usually find out the latest sequence number of the events, the seq number at
4293 * the boot etc. They would "read" all the events below the latest seq number
4294 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4295 * number), they would subsribe to AEN (asynchronous event notification) and
4296 * wait for the events to happen.
4299 megasas_get_seq_num(struct megasas_instance
*instance
,
4300 struct megasas_evt_log_info
*eli
)
4302 struct megasas_cmd
*cmd
;
4303 struct megasas_dcmd_frame
*dcmd
;
4304 struct megasas_evt_log_info
*el_info
;
4305 dma_addr_t el_info_h
= 0;
4307 cmd
= megasas_get_cmd(instance
);
4313 dcmd
= &cmd
->frame
->dcmd
;
4314 el_info
= pci_alloc_consistent(instance
->pdev
,
4315 sizeof(struct megasas_evt_log_info
),
4319 megasas_return_cmd(instance
, cmd
);
4323 memset(el_info
, 0, sizeof(*el_info
));
4324 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4326 dcmd
->cmd
= MFI_CMD_DCMD
;
4327 dcmd
->cmd_status
= 0x0;
4328 dcmd
->sge_count
= 1;
4329 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
4332 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_evt_log_info
));
4333 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO
);
4334 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(el_info_h
);
4335 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_evt_log_info
));
4337 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
4338 dev_err(&instance
->pdev
->dev
, "Command timedout"
4339 "from %s\n", __func__
);
4342 * Copy the data back into callers buffer
4344 eli
->newest_seq_num
= le32_to_cpu(el_info
->newest_seq_num
);
4345 eli
->oldest_seq_num
= le32_to_cpu(el_info
->oldest_seq_num
);
4346 eli
->clear_seq_num
= le32_to_cpu(el_info
->clear_seq_num
);
4347 eli
->shutdown_seq_num
= le32_to_cpu(el_info
->shutdown_seq_num
);
4348 eli
->boot_seq_num
= le32_to_cpu(el_info
->boot_seq_num
);
4351 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
4352 el_info
, el_info_h
);
4354 megasas_return_cmd(instance
, cmd
);
4360 * megasas_register_aen - Registers for asynchronous event notification
4361 * @instance: Adapter soft state
4362 * @seq_num: The starting sequence number
4363 * @class_locale: Class of the event
4365 * This function subscribes for AEN for events beyond the @seq_num. It requests
4366 * to be notified if and only if the event is of type @class_locale
4369 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
4370 u32 class_locale_word
)
4373 struct megasas_cmd
*cmd
;
4374 struct megasas_dcmd_frame
*dcmd
;
4375 union megasas_evt_class_locale curr_aen
;
4376 union megasas_evt_class_locale prev_aen
;
4379 * If there an AEN pending already (aen_cmd), check if the
4380 * class_locale of that pending AEN is inclusive of the new
4381 * AEN request we currently have. If it is, then we don't have
4382 * to do anything. In other words, whichever events the current
4383 * AEN request is subscribing to, have already been subscribed
4386 * If the old_cmd is _not_ inclusive, then we have to abort
4387 * that command, form a class_locale that is superset of both
4388 * old and current and re-issue to the FW
4391 curr_aen
.word
= class_locale_word
;
4393 if (instance
->aen_cmd
) {
4395 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
4396 prev_aen
.members
.locale
= le16_to_cpu(prev_aen
.members
.locale
);
4399 * A class whose enum value is smaller is inclusive of all
4400 * higher values. If a PROGRESS (= -1) was previously
4401 * registered, then a new registration requests for higher
4402 * classes need not be sent to FW. They are automatically
4405 * Locale numbers don't have such hierarchy. They are bitmap
4408 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
4409 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
4410 curr_aen
.members
.locale
)) {
4412 * Previously issued event registration includes
4413 * current request. Nothing to do.
4417 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
4419 if (prev_aen
.members
.class < curr_aen
.members
.class)
4420 curr_aen
.members
.class = prev_aen
.members
.class;
4422 instance
->aen_cmd
->abort_aen
= 1;
4423 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
4428 printk(KERN_DEBUG
"megasas: Failed to abort "
4429 "previous AEN command\n");
4435 cmd
= megasas_get_cmd(instance
);
4440 dcmd
= &cmd
->frame
->dcmd
;
4442 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
4445 * Prepare DCMD for aen registration
4447 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4449 dcmd
->cmd
= MFI_CMD_DCMD
;
4450 dcmd
->cmd_status
= 0x0;
4451 dcmd
->sge_count
= 1;
4452 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
4455 dcmd
->data_xfer_len
= cpu_to_le32(sizeof(struct megasas_evt_detail
));
4456 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT
);
4457 dcmd
->mbox
.w
[0] = cpu_to_le32(seq_num
);
4458 instance
->last_seq_num
= seq_num
;
4459 dcmd
->mbox
.w
[1] = cpu_to_le32(curr_aen
.word
);
4460 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(instance
->evt_detail_h
);
4461 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(sizeof(struct megasas_evt_detail
));
4463 if (instance
->aen_cmd
!= NULL
) {
4464 megasas_return_cmd(instance
, cmd
);
4469 * Store reference to the cmd used to register for AEN. When an
4470 * application wants us to register for AEN, we have to abort this
4471 * cmd and re-register with a new EVENT LOCALE supplied by that app
4473 instance
->aen_cmd
= cmd
;
4476 * Issue the aen registration frame
4478 instance
->instancet
->issue_dcmd(instance
, cmd
);
4484 * megasas_start_aen - Subscribes to AEN during driver load time
4485 * @instance: Adapter soft state
4487 static int megasas_start_aen(struct megasas_instance
*instance
)
4489 struct megasas_evt_log_info eli
;
4490 union megasas_evt_class_locale class_locale
;
4493 * Get the latest sequence number from FW
4495 memset(&eli
, 0, sizeof(eli
));
4497 if (megasas_get_seq_num(instance
, &eli
))
4501 * Register AEN with FW for latest sequence number plus 1
4503 class_locale
.members
.reserved
= 0;
4504 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
4505 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
4507 return megasas_register_aen(instance
,
4508 eli
.newest_seq_num
+ 1,
4513 * megasas_io_attach - Attaches this driver to SCSI mid-layer
4514 * @instance: Adapter soft state
4516 static int megasas_io_attach(struct megasas_instance
*instance
)
4518 struct Scsi_Host
*host
= instance
->host
;
4521 * Export parameters required by SCSI mid-layer
4523 host
->irq
= instance
->pdev
->irq
;
4524 host
->unique_id
= instance
->unique_id
;
4525 if (instance
->is_imr
) {
4527 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
4530 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
4531 host
->this_id
= instance
->init_id
;
4532 host
->sg_tablesize
= instance
->max_num_sge
;
4534 if (instance
->fw_support_ieee
)
4535 instance
->max_sectors_per_req
= MEGASAS_MAX_SECTORS_IEEE
;
4538 * Check if the module parameter value for max_sectors can be used
4540 if (max_sectors
&& max_sectors
< instance
->max_sectors_per_req
)
4541 instance
->max_sectors_per_req
= max_sectors
;
4544 if (((instance
->pdev
->device
==
4545 PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
4546 (instance
->pdev
->device
==
4547 PCI_DEVICE_ID_LSI_SAS0079GEN2
)) &&
4548 (max_sectors
<= MEGASAS_MAX_SECTORS
)) {
4549 instance
->max_sectors_per_req
= max_sectors
;
4551 printk(KERN_INFO
"megasas: max_sectors should be > 0"
4552 "and <= %d (or < 1MB for GEN2 controller)\n",
4553 instance
->max_sectors_per_req
);
4558 host
->max_sectors
= instance
->max_sectors_per_req
;
4559 host
->cmd_per_lun
= MEGASAS_DEFAULT_CMD_PER_LUN
;
4560 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
4561 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
4562 host
->max_lun
= MEGASAS_MAX_LUN
;
4563 host
->max_cmd_len
= 16;
4565 /* Fusion only supports host reset */
4566 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4567 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
4568 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4569 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
4570 host
->hostt
->eh_device_reset_handler
= NULL
;
4571 host
->hostt
->eh_bus_reset_handler
= NULL
;
4575 * Notify the mid-layer about the new controller
4577 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
4578 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
4583 * Trigger SCSI to scan our drives
4585 scsi_scan_host(host
);
4590 megasas_set_dma_mask(struct pci_dev
*pdev
)
4593 * All our contollers are capable of performing 64-bit DMA
4596 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
4598 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
4599 goto fail_set_dma_mask
;
4602 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
4603 goto fail_set_dma_mask
;
4606 * Ensure that all data structures are allocated in 32-bit
4609 if (pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0) {
4610 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
4611 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))
4612 && !pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32)))
4613 dev_info(&pdev
->dev
, "set 32bit DMA mask"
4614 "and 32 bit consistent mask\n");
4616 goto fail_set_dma_mask
;
4626 * megasas_probe_one - PCI hotplug entry point
4627 * @pdev: PCI device structure
4628 * @id: PCI ids of supported hotplugged adapter
4630 static int megasas_probe_one(struct pci_dev
*pdev
,
4631 const struct pci_device_id
*id
)
4633 int rval
, pos
, i
, j
, cpu
;
4634 struct Scsi_Host
*host
;
4635 struct megasas_instance
*instance
;
4638 /* Reset MSI-X in the kdump kernel */
4639 if (reset_devices
) {
4640 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
4642 pci_read_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
,
4644 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
4645 dev_info(&pdev
->dev
, "resetting MSI-X\n");
4646 pci_write_config_word(pdev
,
4647 pos
+ PCI_MSIX_FLAGS
,
4649 ~PCI_MSIX_FLAGS_ENABLE
);
4655 * Announce PCI information
4657 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4658 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
4659 pdev
->subsystem_device
);
4661 printk("bus %d:slot %d:func %d\n",
4662 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
4665 * PCI prepping: enable device set bus mastering and dma mask
4667 rval
= pci_enable_device_mem(pdev
);
4673 pci_set_master(pdev
);
4675 if (megasas_set_dma_mask(pdev
))
4676 goto fail_set_dma_mask
;
4678 host
= scsi_host_alloc(&megasas_template
,
4679 sizeof(struct megasas_instance
));
4682 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
4683 goto fail_alloc_instance
;
4686 instance
= (struct megasas_instance
*)host
->hostdata
;
4687 memset(instance
, 0, sizeof(*instance
));
4688 atomic_set( &instance
->fw_reset_no_pci_access
, 0 );
4689 instance
->pdev
= pdev
;
4691 switch (instance
->pdev
->device
) {
4692 case PCI_DEVICE_ID_LSI_FUSION
:
4693 case PCI_DEVICE_ID_LSI_PLASMA
:
4694 case PCI_DEVICE_ID_LSI_INVADER
:
4695 case PCI_DEVICE_ID_LSI_FURY
:
4697 struct fusion_context
*fusion
;
4699 instance
->ctrl_context
=
4700 kzalloc(sizeof(struct fusion_context
), GFP_KERNEL
);
4701 if (!instance
->ctrl_context
) {
4702 printk(KERN_DEBUG
"megasas: Failed to allocate "
4703 "memory for Fusion context info\n");
4704 goto fail_alloc_dma_buf
;
4706 fusion
= instance
->ctrl_context
;
4707 INIT_LIST_HEAD(&fusion
->cmd_pool
);
4708 spin_lock_init(&fusion
->cmd_pool_lock
);
4711 default: /* For all other supported controllers */
4713 instance
->producer
=
4714 pci_alloc_consistent(pdev
, sizeof(u32
),
4715 &instance
->producer_h
);
4716 instance
->consumer
=
4717 pci_alloc_consistent(pdev
, sizeof(u32
),
4718 &instance
->consumer_h
);
4720 if (!instance
->producer
|| !instance
->consumer
) {
4721 printk(KERN_DEBUG
"megasas: Failed to allocate"
4722 "memory for producer, consumer\n");
4723 goto fail_alloc_dma_buf
;
4726 *instance
->producer
= 0;
4727 *instance
->consumer
= 0;
4731 megasas_poll_wait_aen
= 0;
4732 instance
->flag_ieee
= 0;
4733 instance
->ev
= NULL
;
4734 instance
->issuepend_done
= 1;
4735 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
4736 instance
->is_imr
= 0;
4737 megasas_poll_wait_aen
= 0;
4739 instance
->evt_detail
= pci_alloc_consistent(pdev
,
4741 megasas_evt_detail
),
4742 &instance
->evt_detail_h
);
4744 if (!instance
->evt_detail
) {
4745 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
4746 "event detail structure\n");
4747 goto fail_alloc_dma_buf
;
4751 * Initialize locks and queues
4753 INIT_LIST_HEAD(&instance
->cmd_pool
);
4754 INIT_LIST_HEAD(&instance
->internal_reset_pending_q
);
4756 atomic_set(&instance
->fw_outstanding
,0);
4758 init_waitqueue_head(&instance
->int_cmd_wait_q
);
4759 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
4761 spin_lock_init(&instance
->cmd_pool_lock
);
4762 spin_lock_init(&instance
->hba_lock
);
4763 spin_lock_init(&instance
->completion_lock
);
4765 mutex_init(&instance
->aen_mutex
);
4766 mutex_init(&instance
->reset_mutex
);
4769 * Initialize PCI related and misc parameters
4771 instance
->host
= host
;
4772 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
4773 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
4775 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
4776 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
4777 instance
->flag_ieee
= 1;
4778 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
4780 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
4782 megasas_dbg_lvl
= 0;
4784 instance
->unload
= 1;
4785 instance
->last_time
= 0;
4786 instance
->disableOnlineCtrlReset
= 1;
4787 instance
->UnevenSpanSupport
= 0;
4789 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4790 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
4791 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4792 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4793 INIT_WORK(&instance
->work_init
, megasas_fusion_ocr_wq
);
4795 INIT_WORK(&instance
->work_init
, process_fw_state_change_wq
);
4798 * Initialize MFI Firmware
4800 if (megasas_init_fw(instance
))
4803 if (instance
->requestorId
) {
4804 if (instance
->PlasmaFW111
) {
4805 instance
->vf_affiliation_111
=
4806 pci_alloc_consistent(pdev
, sizeof(struct MR_LD_VF_AFFILIATION_111
),
4807 &instance
->vf_affiliation_111_h
);
4808 if (!instance
->vf_affiliation_111
)
4809 printk(KERN_WARNING
"megasas: Can't allocate "
4810 "memory for VF affiliation buffer\n");
4812 instance
->vf_affiliation
=
4813 pci_alloc_consistent(pdev
,
4814 (MAX_LOGICAL_DRIVES
+ 1) *
4815 sizeof(struct MR_LD_VF_AFFILIATION
),
4816 &instance
->vf_affiliation_h
);
4817 if (!instance
->vf_affiliation
)
4818 printk(KERN_WARNING
"megasas: Can't allocate "
4819 "memory for VF affiliation buffer\n");
4827 if (instance
->msix_vectors
) {
4828 cpu
= cpumask_first(cpu_online_mask
);
4829 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
4830 instance
->irq_context
[i
].instance
= instance
;
4831 instance
->irq_context
[i
].MSIxIndex
= i
;
4832 if (request_irq(instance
->msixentry
[i
].vector
,
4833 instance
->instancet
->service_isr
, 0,
4835 &instance
->irq_context
[i
])) {
4836 printk(KERN_DEBUG
"megasas: Failed to "
4837 "register IRQ for vector %d.\n", i
);
4838 for (j
= 0; j
< i
; j
++) {
4839 irq_set_affinity_hint(
4840 instance
->msixentry
[j
].vector
, NULL
);
4842 instance
->msixentry
[j
].vector
,
4843 &instance
->irq_context
[j
]);
4845 /* Retry irq register for IO_APIC */
4846 instance
->msix_vectors
= 0;
4847 goto retry_irq_register
;
4849 if (irq_set_affinity_hint(instance
->msixentry
[i
].vector
,
4851 dev_err(&instance
->pdev
->dev
, "Error setting"
4852 "affinity hint for cpu %d\n", cpu
);
4853 cpu
= cpumask_next(cpu
, cpu_online_mask
);
4856 instance
->irq_context
[0].instance
= instance
;
4857 instance
->irq_context
[0].MSIxIndex
= 0;
4858 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4859 IRQF_SHARED
, "megasas",
4860 &instance
->irq_context
[0])) {
4861 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4866 instance
->instancet
->enable_intr(instance
);
4869 * Store instance in PCI softstate
4871 pci_set_drvdata(pdev
, instance
);
4874 * Add this controller to megasas_mgmt_info structure so that it
4875 * can be exported to management applications
4877 megasas_mgmt_info
.count
++;
4878 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
4879 megasas_mgmt_info
.max_index
++;
4882 * Register with SCSI mid-layer
4884 if (megasas_io_attach(instance
))
4885 goto fail_io_attach
;
4887 instance
->unload
= 0;
4890 * Initiate AEN (Asynchronous Event Notification)
4892 if (megasas_start_aen(instance
)) {
4893 printk(KERN_DEBUG
"megasas: start aen failed\n");
4894 goto fail_start_aen
;
4901 megasas_mgmt_info
.count
--;
4902 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
4903 megasas_mgmt_info
.max_index
--;
4905 instance
->instancet
->disable_intr(instance
);
4906 if (instance
->msix_vectors
)
4907 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
4908 irq_set_affinity_hint(
4909 instance
->msixentry
[i
].vector
, NULL
);
4910 free_irq(instance
->msixentry
[i
].vector
,
4911 &instance
->irq_context
[i
]);
4914 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4916 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4917 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_PLASMA
) ||
4918 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4919 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4920 megasas_release_fusion(instance
);
4922 megasas_release_mfi(instance
);
4924 if (instance
->msix_vectors
)
4925 pci_disable_msix(instance
->pdev
);
4927 if (instance
->evt_detail
)
4928 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4929 instance
->evt_detail
,
4930 instance
->evt_detail_h
);
4932 if (instance
->producer
)
4933 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4934 instance
->producer_h
);
4935 if (instance
->consumer
)
4936 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4937 instance
->consumer_h
);
4938 scsi_host_put(host
);
4940 fail_alloc_instance
:
4942 pci_disable_device(pdev
);
4948 * megasas_flush_cache - Requests FW to flush all its caches
4949 * @instance: Adapter soft state
4951 static void megasas_flush_cache(struct megasas_instance
*instance
)
4953 struct megasas_cmd
*cmd
;
4954 struct megasas_dcmd_frame
*dcmd
;
4956 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4959 cmd
= megasas_get_cmd(instance
);
4964 dcmd
= &cmd
->frame
->dcmd
;
4966 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4968 dcmd
->cmd
= MFI_CMD_DCMD
;
4969 dcmd
->cmd_status
= 0x0;
4970 dcmd
->sge_count
= 0;
4971 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_NONE
);
4974 dcmd
->data_xfer_len
= 0;
4975 dcmd
->opcode
= cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH
);
4976 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
4978 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
4979 dev_err(&instance
->pdev
->dev
, "Command timedout"
4980 " from %s\n", __func__
);
4982 megasas_return_cmd(instance
, cmd
);
4988 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4989 * @instance: Adapter soft state
4990 * @opcode: Shutdown/Hibernate
4992 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
4995 struct megasas_cmd
*cmd
;
4996 struct megasas_dcmd_frame
*dcmd
;
4998 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
5001 cmd
= megasas_get_cmd(instance
);
5006 if (instance
->aen_cmd
)
5007 megasas_issue_blocked_abort_cmd(instance
,
5008 instance
->aen_cmd
, 30);
5009 if (instance
->map_update_cmd
)
5010 megasas_issue_blocked_abort_cmd(instance
,
5011 instance
->map_update_cmd
, 30);
5012 dcmd
= &cmd
->frame
->dcmd
;
5014 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
5016 dcmd
->cmd
= MFI_CMD_DCMD
;
5017 dcmd
->cmd_status
= 0x0;
5018 dcmd
->sge_count
= 0;
5019 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_NONE
);
5022 dcmd
->data_xfer_len
= 0;
5023 dcmd
->opcode
= cpu_to_le32(opcode
);
5025 if (megasas_issue_blocked_cmd(instance
, cmd
, 30))
5026 dev_err(&instance
->pdev
->dev
, "Command timedout"
5027 "from %s\n", __func__
);
5029 megasas_return_cmd(instance
, cmd
);
5036 * megasas_suspend - driver suspend entry point
5037 * @pdev: PCI device structure
5038 * @state: PCI power state to suspend routine
5041 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
5043 struct Scsi_Host
*host
;
5044 struct megasas_instance
*instance
;
5047 instance
= pci_get_drvdata(pdev
);
5048 host
= instance
->host
;
5049 instance
->unload
= 1;
5051 /* Shutdown SR-IOV heartbeat timer */
5052 if (instance
->requestorId
&& !instance
->skip_heartbeat_timer_del
)
5053 del_timer_sync(&instance
->sriov_heartbeat_timer
);
5055 megasas_flush_cache(instance
);
5056 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
5058 /* cancel the delayed work if this work still in queue */
5059 if (instance
->ev
!= NULL
) {
5060 struct megasas_aen_event
*ev
= instance
->ev
;
5061 cancel_delayed_work_sync(&ev
->hotplug_work
);
5062 instance
->ev
= NULL
;
5065 tasklet_kill(&instance
->isr_tasklet
);
5067 pci_set_drvdata(instance
->pdev
, instance
);
5068 instance
->instancet
->disable_intr(instance
);
5070 if (instance
->msix_vectors
)
5071 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5072 irq_set_affinity_hint(
5073 instance
->msixentry
[i
].vector
, NULL
);
5074 free_irq(instance
->msixentry
[i
].vector
,
5075 &instance
->irq_context
[i
]);
5078 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5079 if (instance
->msix_vectors
)
5080 pci_disable_msix(instance
->pdev
);
5082 pci_save_state(pdev
);
5083 pci_disable_device(pdev
);
5085 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
5091 * megasas_resume- driver resume entry point
5092 * @pdev: PCI device structure
5095 megasas_resume(struct pci_dev
*pdev
)
5097 int rval
, i
, j
, cpu
;
5098 struct Scsi_Host
*host
;
5099 struct megasas_instance
*instance
;
5101 instance
= pci_get_drvdata(pdev
);
5102 host
= instance
->host
;
5103 pci_set_power_state(pdev
, PCI_D0
);
5104 pci_enable_wake(pdev
, PCI_D0
, 0);
5105 pci_restore_state(pdev
);
5108 * PCI prepping: enable device set bus mastering and dma mask
5110 rval
= pci_enable_device_mem(pdev
);
5113 printk(KERN_ERR
"megasas: Enable device failed\n");
5117 pci_set_master(pdev
);
5119 if (megasas_set_dma_mask(pdev
))
5120 goto fail_set_dma_mask
;
5123 * Initialize MFI Firmware
5126 atomic_set(&instance
->fw_outstanding
, 0);
5129 * We expect the FW state to be READY
5131 if (megasas_transition_to_ready(instance
, 0))
5132 goto fail_ready_state
;
5134 /* Now re-enable MSI-X */
5135 if (instance
->msix_vectors
&&
5136 pci_enable_msix(instance
->pdev
, instance
->msixentry
,
5137 instance
->msix_vectors
))
5138 goto fail_reenable_msix
;
5140 switch (instance
->pdev
->device
) {
5141 case PCI_DEVICE_ID_LSI_FUSION
:
5142 case PCI_DEVICE_ID_LSI_PLASMA
:
5143 case PCI_DEVICE_ID_LSI_INVADER
:
5144 case PCI_DEVICE_ID_LSI_FURY
:
5146 megasas_reset_reply_desc(instance
);
5147 if (megasas_ioc_init_fusion(instance
)) {
5148 megasas_free_cmds(instance
);
5149 megasas_free_cmds_fusion(instance
);
5152 if (!megasas_get_map_info(instance
))
5153 megasas_sync_map_info(instance
);
5157 *instance
->producer
= 0;
5158 *instance
->consumer
= 0;
5159 if (megasas_issue_init_mfi(instance
))
5164 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
5165 (unsigned long)instance
);
5170 if (instance
->msix_vectors
) {
5171 cpu
= cpumask_first(cpu_online_mask
);
5172 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
5173 instance
->irq_context
[i
].instance
= instance
;
5174 instance
->irq_context
[i
].MSIxIndex
= i
;
5175 if (request_irq(instance
->msixentry
[i
].vector
,
5176 instance
->instancet
->service_isr
, 0,
5178 &instance
->irq_context
[i
])) {
5179 printk(KERN_DEBUG
"megasas: Failed to "
5180 "register IRQ for vector %d.\n", i
);
5181 for (j
= 0; j
< i
; j
++) {
5182 irq_set_affinity_hint(
5183 instance
->msixentry
[j
].vector
, NULL
);
5185 instance
->msixentry
[j
].vector
,
5186 &instance
->irq_context
[j
]);
5191 if (irq_set_affinity_hint(instance
->msixentry
[i
].vector
,
5193 dev_err(&instance
->pdev
->dev
, "Error setting"
5194 "affinity hint for cpu %d\n", cpu
);
5195 cpu
= cpumask_next(cpu
, cpu_online_mask
);
5198 instance
->irq_context
[0].instance
= instance
;
5199 instance
->irq_context
[0].MSIxIndex
= 0;
5200 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
5201 IRQF_SHARED
, "megasas",
5202 &instance
->irq_context
[0])) {
5203 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
5208 /* Re-launch SR-IOV heartbeat timer */
5209 if (instance
->requestorId
) {
5210 if (!megasas_sriov_start_heartbeat(instance
, 0))
5211 megasas_start_timer(instance
,
5212 &instance
->sriov_heartbeat_timer
,
5213 megasas_sriov_heartbeat_handler
,
5214 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
);
5216 instance
->skip_heartbeat_timer_del
= 1;
5219 instance
->instancet
->enable_intr(instance
);
5220 instance
->unload
= 0;
5223 * Initiate AEN (Asynchronous Event Notification)
5225 if (megasas_start_aen(instance
))
5226 printk(KERN_ERR
"megasas: Start AEN failed\n");
5232 if (instance
->evt_detail
)
5233 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
5234 instance
->evt_detail
,
5235 instance
->evt_detail_h
);
5237 if (instance
->producer
)
5238 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
5239 instance
->producer_h
);
5240 if (instance
->consumer
)
5241 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
5242 instance
->consumer_h
);
5243 scsi_host_put(host
);
5249 pci_disable_device(pdev
);
5254 #define megasas_suspend NULL
5255 #define megasas_resume NULL
5259 * megasas_detach_one - PCI hot"un"plug entry point
5260 * @pdev: PCI device structure
5262 static void megasas_detach_one(struct pci_dev
*pdev
)
5265 struct Scsi_Host
*host
;
5266 struct megasas_instance
*instance
;
5267 struct fusion_context
*fusion
;
5269 instance
= pci_get_drvdata(pdev
);
5270 instance
->unload
= 1;
5271 host
= instance
->host
;
5272 fusion
= instance
->ctrl_context
;
5274 /* Shutdown SR-IOV heartbeat timer */
5275 if (instance
->requestorId
&& !instance
->skip_heartbeat_timer_del
)
5276 del_timer_sync(&instance
->sriov_heartbeat_timer
);
5278 scsi_remove_host(instance
->host
);
5279 megasas_flush_cache(instance
);
5280 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
5282 /* cancel the delayed work if this work still in queue*/
5283 if (instance
->ev
!= NULL
) {
5284 struct megasas_aen_event
*ev
= instance
->ev
;
5285 cancel_delayed_work_sync(&ev
->hotplug_work
);
5286 instance
->ev
= NULL
;
5289 /* cancel all wait events */
5290 wake_up_all(&instance
->int_cmd_wait_q
);
5292 tasklet_kill(&instance
->isr_tasklet
);
5295 * Take the instance off the instance array. Note that we will not
5296 * decrement the max_index. We let this array be sparse array
5298 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
5299 if (megasas_mgmt_info
.instance
[i
] == instance
) {
5300 megasas_mgmt_info
.count
--;
5301 megasas_mgmt_info
.instance
[i
] = NULL
;
5307 instance
->instancet
->disable_intr(instance
);
5309 if (instance
->msix_vectors
)
5310 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5311 irq_set_affinity_hint(
5312 instance
->msixentry
[i
].vector
, NULL
);
5313 free_irq(instance
->msixentry
[i
].vector
,
5314 &instance
->irq_context
[i
]);
5317 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5318 if (instance
->msix_vectors
)
5319 pci_disable_msix(instance
->pdev
);
5321 switch (instance
->pdev
->device
) {
5322 case PCI_DEVICE_ID_LSI_FUSION
:
5323 case PCI_DEVICE_ID_LSI_PLASMA
:
5324 case PCI_DEVICE_ID_LSI_INVADER
:
5325 case PCI_DEVICE_ID_LSI_FURY
:
5326 megasas_release_fusion(instance
);
5327 for (i
= 0; i
< 2 ; i
++)
5328 if (fusion
->ld_map
[i
])
5329 dma_free_coherent(&instance
->pdev
->dev
,
5334 kfree(instance
->ctrl_context
);
5337 megasas_release_mfi(instance
);
5338 pci_free_consistent(pdev
, sizeof(u32
),
5340 instance
->producer_h
);
5341 pci_free_consistent(pdev
, sizeof(u32
),
5343 instance
->consumer_h
);
5347 if (instance
->evt_detail
)
5348 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
5349 instance
->evt_detail
, instance
->evt_detail_h
);
5351 if (instance
->vf_affiliation
)
5352 pci_free_consistent(pdev
, (MAX_LOGICAL_DRIVES
+ 1) *
5353 sizeof(struct MR_LD_VF_AFFILIATION
),
5354 instance
->vf_affiliation
,
5355 instance
->vf_affiliation_h
);
5357 if (instance
->vf_affiliation_111
)
5358 pci_free_consistent(pdev
,
5359 sizeof(struct MR_LD_VF_AFFILIATION_111
),
5360 instance
->vf_affiliation_111
,
5361 instance
->vf_affiliation_111_h
);
5363 if (instance
->hb_host_mem
)
5364 pci_free_consistent(pdev
, sizeof(struct MR_CTRL_HB_HOST_MEM
),
5365 instance
->hb_host_mem
,
5366 instance
->hb_host_mem_h
);
5368 scsi_host_put(host
);
5370 pci_disable_device(pdev
);
5376 * megasas_shutdown - Shutdown entry point
5377 * @device: Generic device structure
5379 static void megasas_shutdown(struct pci_dev
*pdev
)
5382 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
5384 instance
->unload
= 1;
5385 megasas_flush_cache(instance
);
5386 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
5387 instance
->instancet
->disable_intr(instance
);
5388 if (instance
->msix_vectors
)
5389 for (i
= 0; i
< instance
->msix_vectors
; i
++) {
5390 irq_set_affinity_hint(
5391 instance
->msixentry
[i
].vector
, NULL
);
5392 free_irq(instance
->msixentry
[i
].vector
,
5393 &instance
->irq_context
[i
]);
5396 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
5397 if (instance
->msix_vectors
)
5398 pci_disable_msix(instance
->pdev
);
5402 * megasas_mgmt_open - char node "open" entry point
5404 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
5407 * Allow only those users with admin rights
5409 if (!capable(CAP_SYS_ADMIN
))
5416 * megasas_mgmt_fasync - Async notifier registration from applications
5418 * This function adds the calling process to a driver global queue. When an
5419 * event occurs, SIGIO will be sent to all processes in this queue.
5421 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
5425 mutex_lock(&megasas_async_queue_mutex
);
5427 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
5429 mutex_unlock(&megasas_async_queue_mutex
);
5432 /* For sanity check when we get ioctl */
5433 filep
->private_data
= filep
;
5437 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
5443 * megasas_mgmt_poll - char node "poll" entry point
5445 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
5448 unsigned long flags
;
5449 poll_wait(file
, &megasas_poll_wait
, wait
);
5450 spin_lock_irqsave(&poll_aen_lock
, flags
);
5451 if (megasas_poll_wait_aen
)
5452 mask
= (POLLIN
| POLLRDNORM
);
5455 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
5460 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
5461 * @instance: Adapter soft state
5462 * @argp: User's ioctl packet
5465 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
5466 struct megasas_iocpacket __user
* user_ioc
,
5467 struct megasas_iocpacket
*ioc
)
5469 struct megasas_sge32
*kern_sge32
;
5470 struct megasas_cmd
*cmd
;
5471 void *kbuff_arr
[MAX_IOCTL_SGE
];
5472 dma_addr_t buf_handle
= 0;
5475 dma_addr_t sense_handle
;
5476 unsigned long *sense_ptr
;
5478 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
5480 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
5481 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
5482 ioc
->sge_count
, MAX_IOCTL_SGE
);
5486 cmd
= megasas_get_cmd(instance
);
5488 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
5493 * User's IOCTL packet has 2 frames (maximum). Copy those two
5494 * frames into our cmd's frames. cmd->frame's context will get
5495 * overwritten when we copy from user's frames. So set that value
5498 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
5499 cmd
->frame
->hdr
.context
= cpu_to_le32(cmd
->index
);
5500 cmd
->frame
->hdr
.pad_0
= 0;
5501 cmd
->frame
->hdr
.flags
&= cpu_to_le16(~(MFI_FRAME_IEEE
|
5503 MFI_FRAME_SENSE64
));
5506 * The management interface between applications and the fw uses
5507 * MFI frames. E.g, RAID configuration changes, LD property changes
5508 * etc are accomplishes through different kinds of MFI frames. The
5509 * driver needs to care only about substituting user buffers with
5510 * kernel buffers in SGLs. The location of SGL is embedded in the
5511 * struct iocpacket itself.
5513 kern_sge32
= (struct megasas_sge32
*)
5514 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
5517 * For each user buffer, create a mirror buffer and copy in
5519 for (i
= 0; i
< ioc
->sge_count
; i
++) {
5520 if (!ioc
->sgl
[i
].iov_len
)
5523 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
5524 ioc
->sgl
[i
].iov_len
,
5525 &buf_handle
, GFP_KERNEL
);
5526 if (!kbuff_arr
[i
]) {
5527 printk(KERN_DEBUG
"megasas: Failed to alloc "
5528 "kernel SGL buffer for IOCTL \n");
5534 * We don't change the dma_coherent_mask, so
5535 * pci_alloc_consistent only returns 32bit addresses
5537 kern_sge32
[i
].phys_addr
= cpu_to_le32(buf_handle
);
5538 kern_sge32
[i
].length
= cpu_to_le32(ioc
->sgl
[i
].iov_len
);
5541 * We created a kernel buffer corresponding to the
5542 * user buffer. Now copy in from the user buffer
5544 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
5545 (u32
) (ioc
->sgl
[i
].iov_len
))) {
5551 if (ioc
->sense_len
) {
5552 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
5553 &sense_handle
, GFP_KERNEL
);
5560 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
5561 *sense_ptr
= cpu_to_le32(sense_handle
);
5565 * Set the sync_cmd flag so that the ISR knows not to complete this
5566 * cmd to the SCSI mid-layer
5569 megasas_issue_blocked_cmd(instance
, cmd
, 0);
5573 * copy out the kernel buffers to user buffers
5575 for (i
= 0; i
< ioc
->sge_count
; i
++) {
5576 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
5577 ioc
->sgl
[i
].iov_len
)) {
5584 * copy out the sense
5586 if (ioc
->sense_len
) {
5588 * sense_ptr points to the location that has the user
5589 * sense buffer address
5591 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
5594 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
5595 sense
, ioc
->sense_len
)) {
5596 printk(KERN_ERR
"megasas: Failed to copy out to user "
5604 * copy the status codes returned by the fw
5606 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
5607 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
5608 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
5614 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
5615 sense
, sense_handle
);
5618 for (i
= 0; i
< ioc
->sge_count
; i
++) {
5620 dma_free_coherent(&instance
->pdev
->dev
,
5621 le32_to_cpu(kern_sge32
[i
].length
),
5623 le32_to_cpu(kern_sge32
[i
].phys_addr
));
5626 megasas_return_cmd(instance
, cmd
);
5630 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
5632 struct megasas_iocpacket __user
*user_ioc
=
5633 (struct megasas_iocpacket __user
*)arg
;
5634 struct megasas_iocpacket
*ioc
;
5635 struct megasas_instance
*instance
;
5638 unsigned long flags
;
5639 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
5641 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
5645 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
5650 instance
= megasas_lookup_instance(ioc
->host_no
);
5656 /* Adjust ioctl wait time for VF mode */
5657 if (instance
->requestorId
)
5658 wait_time
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
5660 /* Block ioctls in VF mode */
5661 if (instance
->requestorId
&& !allow_vf_ioctls
) {
5666 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
5667 printk(KERN_ERR
"Controller in crit error\n");
5672 if (instance
->unload
== 1) {
5678 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
5680 if (down_interruptible(&instance
->ioctl_sem
)) {
5681 error
= -ERESTARTSYS
;
5685 for (i
= 0; i
< wait_time
; i
++) {
5687 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5688 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
5689 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5692 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5694 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5695 printk(KERN_NOTICE
"megasas: waiting"
5696 "for controller reset to finish\n");
5702 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5703 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5704 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5706 printk(KERN_ERR
"megaraid_sas: timed out while"
5707 "waiting for HBA to recover\n");
5711 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5713 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
5715 up(&instance
->ioctl_sem
);
5722 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
5724 struct megasas_instance
*instance
;
5725 struct megasas_aen aen
;
5728 unsigned long flags
;
5729 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
5731 if (file
->private_data
!= file
) {
5732 printk(KERN_DEBUG
"megasas: fasync_helper was not "
5737 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
5740 instance
= megasas_lookup_instance(aen
.host_no
);
5745 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
5749 if (instance
->unload
== 1) {
5753 for (i
= 0; i
< wait_time
; i
++) {
5755 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5756 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
5757 spin_unlock_irqrestore(&instance
->hba_lock
,
5762 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5764 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5765 printk(KERN_NOTICE
"megasas: waiting for"
5766 "controller reset to finish\n");
5772 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5773 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5774 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5775 printk(KERN_ERR
"megaraid_sas: timed out while waiting"
5776 "for HBA to recover.\n");
5779 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5781 mutex_lock(&instance
->aen_mutex
);
5782 error
= megasas_register_aen(instance
, aen
.seq_num
,
5783 aen
.class_locale_word
);
5784 mutex_unlock(&instance
->aen_mutex
);
5789 * megasas_mgmt_ioctl - char node ioctl entry point
5792 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5795 case MEGASAS_IOC_FIRMWARE
:
5796 return megasas_mgmt_ioctl_fw(file
, arg
);
5798 case MEGASAS_IOC_GET_AEN
:
5799 return megasas_mgmt_ioctl_aen(file
, arg
);
5805 #ifdef CONFIG_COMPAT
5806 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
5808 struct compat_megasas_iocpacket __user
*cioc
=
5809 (struct compat_megasas_iocpacket __user
*)arg
;
5810 struct megasas_iocpacket __user
*ioc
=
5811 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
5816 if (clear_user(ioc
, sizeof(*ioc
)))
5819 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
5820 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
5821 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
5822 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
5823 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
5824 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
5828 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5829 * sense_len is not null, so prepare the 64bit value under
5830 * the same condition.
5832 if (ioc
->sense_len
) {
5833 void __user
**sense_ioc_ptr
=
5834 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
5835 compat_uptr_t
*sense_cioc_ptr
=
5836 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
5837 if (get_user(ptr
, sense_cioc_ptr
) ||
5838 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
5842 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
5843 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
5844 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
5845 copy_in_user(&ioc
->sgl
[i
].iov_len
,
5846 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
5850 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
5852 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
5853 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
5854 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
5861 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
5865 case MEGASAS_IOC_FIRMWARE32
:
5866 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
5867 case MEGASAS_IOC_GET_AEN
:
5868 return megasas_mgmt_ioctl_aen(file
, arg
);
5876 * File operations structure for management interface
5878 static const struct file_operations megasas_mgmt_fops
= {
5879 .owner
= THIS_MODULE
,
5880 .open
= megasas_mgmt_open
,
5881 .fasync
= megasas_mgmt_fasync
,
5882 .unlocked_ioctl
= megasas_mgmt_ioctl
,
5883 .poll
= megasas_mgmt_poll
,
5884 #ifdef CONFIG_COMPAT
5885 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
5887 .llseek
= noop_llseek
,
5891 * PCI hotplug support registration structure
5893 static struct pci_driver megasas_pci_driver
= {
5895 .name
= "megaraid_sas",
5896 .id_table
= megasas_pci_table
,
5897 .probe
= megasas_probe_one
,
5898 .remove
= megasas_detach_one
,
5899 .suspend
= megasas_suspend
,
5900 .resume
= megasas_resume
,
5901 .shutdown
= megasas_shutdown
,
5905 * Sysfs driver attributes
5907 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
5909 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
5913 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
5916 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
5918 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
5922 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
5926 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
5928 return sprintf(buf
, "%u\n", support_poll_for_event
);
5931 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
5932 megasas_sysfs_show_support_poll_for_event
, NULL
);
5935 megasas_sysfs_show_support_device_change(struct device_driver
*dd
, char *buf
)
5937 return sprintf(buf
, "%u\n", support_device_change
);
5940 static DRIVER_ATTR(support_device_change
, S_IRUGO
,
5941 megasas_sysfs_show_support_device_change
, NULL
);
5944 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
5946 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
5950 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
5953 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
5954 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
5960 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
5961 megasas_sysfs_set_dbg_lvl
);
5964 megasas_aen_polling(struct work_struct
*work
)
5966 struct megasas_aen_event
*ev
=
5967 container_of(work
, struct megasas_aen_event
, hotplug_work
.work
);
5968 struct megasas_instance
*instance
= ev
->instance
;
5969 union megasas_evt_class_locale class_locale
;
5970 struct Scsi_Host
*host
;
5971 struct scsi_device
*sdev1
;
5974 int i
, j
, doscan
= 0;
5975 u32 seq_num
, wait_time
= MEGASAS_RESET_WAIT_TIME
;
5979 printk(KERN_ERR
"invalid instance!\n");
5984 /* Adjust event workqueue thread wait time for VF mode */
5985 if (instance
->requestorId
)
5986 wait_time
= MEGASAS_ROUTINE_WAIT_TIME_VF
;
5988 /* Don't run the event workqueue thread if OCR is running */
5989 for (i
= 0; i
< wait_time
; i
++) {
5990 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
)
5992 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5993 printk(KERN_NOTICE
"megasas: %s waiting for "
5994 "controller reset to finish for scsi%d\n",
5995 __func__
, instance
->host
->host_no
);
6000 instance
->ev
= NULL
;
6001 host
= instance
->host
;
6002 if (instance
->evt_detail
) {
6004 switch (le32_to_cpu(instance
->evt_detail
->code
)) {
6005 case MR_EVT_PD_INSERTED
:
6006 if (megasas_get_pd_list(instance
) == 0) {
6007 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6009 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6013 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6016 scsi_device_lookup(host
, i
, j
, 0);
6018 if (instance
->pd_list
[pd_index
].driveState
6019 == MR_PD_STATE_SYSTEM
) {
6021 scsi_add_device(host
, i
, j
, 0);
6025 scsi_device_put(sdev1
);
6033 case MR_EVT_PD_REMOVED
:
6034 if (megasas_get_pd_list(instance
) == 0) {
6035 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6037 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6041 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6044 scsi_device_lookup(host
, i
, j
, 0);
6046 if (instance
->pd_list
[pd_index
].driveState
6047 == MR_PD_STATE_SYSTEM
) {
6049 scsi_device_put(sdev1
);
6053 scsi_remove_device(sdev1
);
6054 scsi_device_put(sdev1
);
6063 case MR_EVT_LD_OFFLINE
:
6064 case MR_EVT_CFG_CLEARED
:
6065 case MR_EVT_LD_DELETED
:
6066 if (!instance
->requestorId
||
6067 (instance
->requestorId
&&
6068 megasas_get_ld_vf_affiliation(instance
, 0))) {
6069 if (megasas_ld_list_query(instance
,
6070 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6071 megasas_get_ld_list(instance
);
6072 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6074 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6078 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6080 sdev1
= scsi_device_lookup(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6082 if (instance
->ld_ids
[ld_index
]
6085 scsi_device_put(sdev1
);
6088 scsi_remove_device(sdev1
);
6089 scsi_device_put(sdev1
);
6097 case MR_EVT_LD_CREATED
:
6098 if (!instance
->requestorId
||
6099 (instance
->requestorId
&&
6100 megasas_get_ld_vf_affiliation(instance
, 0))) {
6101 if (megasas_ld_list_query(instance
,
6102 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6103 megasas_get_ld_list(instance
);
6104 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6106 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6109 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6111 sdev1
= scsi_device_lookup(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6113 if (instance
->ld_ids
[ld_index
]
6116 scsi_add_device(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6119 scsi_device_put(sdev1
);
6125 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
6126 case MR_EVT_FOREIGN_CFG_IMPORTED
:
6127 case MR_EVT_LD_STATE_CHANGE
:
6135 printk(KERN_ERR
"invalid evt_detail!\n");
6141 printk(KERN_INFO
"megaraid_sas: scanning for scsi%d...\n",
6142 instance
->host
->host_no
);
6143 if (megasas_get_pd_list(instance
) == 0) {
6144 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
6145 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
6146 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
6147 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
6148 if (instance
->pd_list
[pd_index
].driveState
==
6149 MR_PD_STATE_SYSTEM
) {
6151 scsi_add_device(host
, i
, j
, 0);
6154 scsi_device_put(sdev1
);
6157 scsi_remove_device(sdev1
);
6158 scsi_device_put(sdev1
);
6165 if (!instance
->requestorId
||
6166 (instance
->requestorId
&&
6167 megasas_get_ld_vf_affiliation(instance
, 0))) {
6168 if (megasas_ld_list_query(instance
,
6169 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST
))
6170 megasas_get_ld_list(instance
);
6171 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
6172 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
6175 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
6177 sdev1
= scsi_device_lookup(host
,
6178 MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6179 if (instance
->ld_ids
[ld_index
]
6182 scsi_add_device(host
, MEGASAS_MAX_PD_CHANNELS
+ i
, j
, 0);
6184 scsi_device_put(sdev1
);
6187 scsi_remove_device(sdev1
);
6188 scsi_device_put(sdev1
);
6196 if ( instance
->aen_cmd
!= NULL
) {
6201 seq_num
= le32_to_cpu(instance
->evt_detail
->seq_num
) + 1;
6203 /* Register AEN with FW for latest sequence number plus 1 */
6204 class_locale
.members
.reserved
= 0;
6205 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
6206 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
6207 mutex_lock(&instance
->aen_mutex
);
6208 error
= megasas_register_aen(instance
, seq_num
,
6210 mutex_unlock(&instance
->aen_mutex
);
6213 printk(KERN_ERR
"register aen failed error %x\n", error
);
6219 * megasas_init - Driver load entry point
6221 static int __init
megasas_init(void)
6226 * Announce driver version and other information
6228 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
6229 MEGASAS_EXT_VERSION
);
6231 spin_lock_init(&poll_aen_lock
);
6233 support_poll_for_event
= 2;
6234 support_device_change
= 1;
6236 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
6239 * Register character device node
6241 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
6244 printk(KERN_DEBUG
"megasas: failed to open device node\n");
6248 megasas_mgmt_majorno
= rval
;
6251 * Register ourselves as PCI hotplug module
6253 rval
= pci_register_driver(&megasas_pci_driver
);
6256 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
6260 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6261 &driver_attr_version
);
6263 goto err_dcf_attr_ver
;
6264 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6265 &driver_attr_release_date
);
6267 goto err_dcf_rel_date
;
6269 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6270 &driver_attr_support_poll_for_event
);
6272 goto err_dcf_support_poll_for_event
;
6274 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6275 &driver_attr_dbg_lvl
);
6277 goto err_dcf_dbg_lvl
;
6278 rval
= driver_create_file(&megasas_pci_driver
.driver
,
6279 &driver_attr_support_device_change
);
6281 goto err_dcf_support_device_change
;
6285 err_dcf_support_device_change
:
6286 driver_remove_file(&megasas_pci_driver
.driver
,
6287 &driver_attr_dbg_lvl
);
6289 driver_remove_file(&megasas_pci_driver
.driver
,
6290 &driver_attr_support_poll_for_event
);
6292 err_dcf_support_poll_for_event
:
6293 driver_remove_file(&megasas_pci_driver
.driver
,
6294 &driver_attr_release_date
);
6297 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
6299 pci_unregister_driver(&megasas_pci_driver
);
6301 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
6306 * megasas_exit - Driver unload entry point
6308 static void __exit
megasas_exit(void)
6310 driver_remove_file(&megasas_pci_driver
.driver
,
6311 &driver_attr_dbg_lvl
);
6312 driver_remove_file(&megasas_pci_driver
.driver
,
6313 &driver_attr_support_poll_for_event
);
6314 driver_remove_file(&megasas_pci_driver
.driver
,
6315 &driver_attr_support_device_change
);
6316 driver_remove_file(&megasas_pci_driver
.driver
,
6317 &driver_attr_release_date
);
6318 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
6320 pci_unregister_driver(&megasas_pci_driver
);
6321 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
6324 module_init(megasas_init
);
6325 module_exit(megasas_exit
);