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 : v06.504.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 throttlequeuedepth
= MEGASAS_THROTTLE_QUEUE_DEPTH
;
79 module_param(throttlequeuedepth
, int, S_IRUGO
);
80 MODULE_PARM_DESC(throttlequeuedepth
,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
83 int resetwaittime
= MEGASAS_RESET_WAIT_TIME
;
84 module_param(resetwaittime
, int, S_IRUGO
);
85 MODULE_PARM_DESC(resetwaittime
, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(MEGASAS_VERSION
);
90 MODULE_AUTHOR("megaraidlinux@lsi.com");
91 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
93 int megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
);
94 static int megasas_get_pd_list(struct megasas_instance
*instance
);
95 static int megasas_issue_init_mfi(struct megasas_instance
*instance
);
96 static int megasas_register_aen(struct megasas_instance
*instance
,
97 u32 seq_num
, u32 class_locale_word
);
99 * PCI ID table for all supported controllers
101 static struct pci_device_id megasas_pci_table
[] = {
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1064R
)},
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078R
)},
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078DE
)},
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078GEN2
)},
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0079GEN2
)},
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0073SKINNY
)},
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0071SKINNY
)},
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_VERDE_ZCR
)},
118 /* xscale IOP, vega */
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL
, PCI_DEVICE_ID_DELL_PERC5
)},
121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FUSION
)},
123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_INVADER
)},
128 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
130 static int megasas_mgmt_majorno
;
131 static struct megasas_mgmt_info megasas_mgmt_info
;
132 static struct fasync_struct
*megasas_async_queue
;
133 static DEFINE_MUTEX(megasas_async_queue_mutex
);
135 static int megasas_poll_wait_aen
;
136 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait
);
137 static u32 support_poll_for_event
;
139 static u32 support_device_change
;
141 /* define lock for aen poll */
142 spinlock_t poll_aen_lock
;
145 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
148 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
);
150 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
151 struct megasas_register_set __iomem
*reg_set
);
152 static irqreturn_t
megasas_isr(int irq
, void *devp
);
154 megasas_init_adapter_mfi(struct megasas_instance
*instance
);
156 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
157 struct scsi_cmnd
*scmd
);
158 static void megasas_complete_cmd_dpc(unsigned long instance_addr
);
160 megasas_release_fusion(struct megasas_instance
*instance
);
162 megasas_ioc_init_fusion(struct megasas_instance
*instance
);
164 megasas_free_cmds_fusion(struct megasas_instance
*instance
);
166 megasas_get_map_info(struct megasas_instance
*instance
);
168 megasas_sync_map_info(struct megasas_instance
*instance
);
170 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
);
171 void megasas_reset_reply_desc(struct megasas_instance
*instance
);
172 u8
MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL
*map
,
173 struct LD_LOAD_BALANCE_INFO
*lbInfo
);
174 int megasas_reset_fusion(struct Scsi_Host
*shost
);
175 void megasas_fusion_ocr_wq(struct work_struct
*work
);
178 megasas_issue_dcmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
180 instance
->instancet
->fire_cmd(instance
,
181 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
185 * megasas_get_cmd - Get a command from the free pool
186 * @instance: Adapter soft state
188 * Returns a free command from the pool
190 struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
194 struct megasas_cmd
*cmd
= NULL
;
196 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
198 if (!list_empty(&instance
->cmd_pool
)) {
199 cmd
= list_entry((&instance
->cmd_pool
)->next
,
200 struct megasas_cmd
, list
);
201 list_del_init(&cmd
->list
);
203 printk(KERN_ERR
"megasas: Command pool empty!\n");
206 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
211 * megasas_return_cmd - Return a cmd to free command pool
212 * @instance: Adapter soft state
213 * @cmd: Command packet to be returned to free command pool
216 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
220 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
223 cmd
->frame_count
= 0;
224 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
225 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
227 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
228 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
230 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
235 * The following functions are defined for xscale
236 * (deviceid : 1064R, PERC5) controllers
240 * megasas_enable_intr_xscale - Enables interrupts
241 * @regs: MFI register set
244 megasas_enable_intr_xscale(struct megasas_register_set __iomem
* regs
)
246 writel(0, &(regs
)->outbound_intr_mask
);
248 /* Dummy readl to force pci flush */
249 readl(®s
->outbound_intr_mask
);
253 * megasas_disable_intr_xscale -Disables interrupt
254 * @regs: MFI register set
257 megasas_disable_intr_xscale(struct megasas_register_set __iomem
* regs
)
260 writel(mask
, ®s
->outbound_intr_mask
);
261 /* Dummy readl to force pci flush */
262 readl(®s
->outbound_intr_mask
);
266 * megasas_read_fw_status_reg_xscale - returns the current FW status value
267 * @regs: MFI register set
270 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
272 return readl(&(regs
)->outbound_msg_0
);
275 * megasas_clear_interrupt_xscale - Check & clear interrupt
276 * @regs: MFI register set
279 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
284 * Check if it is our interrupt
286 status
= readl(®s
->outbound_intr_status
);
288 if (status
& MFI_OB_INTR_STATUS_MASK
)
289 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
290 if (status
& MFI_XSCALE_OMR0_CHANGE_INTERRUPT
)
291 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
294 * Clear the interrupt by writing back the same value
297 writel(status
, ®s
->outbound_intr_status
);
299 /* Dummy readl to force pci flush */
300 readl(®s
->outbound_intr_status
);
306 * megasas_fire_cmd_xscale - Sends command to the FW
307 * @frame_phys_addr : Physical address of cmd
308 * @frame_count : Number of frames for the command
309 * @regs : MFI register set
312 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
313 dma_addr_t frame_phys_addr
,
315 struct megasas_register_set __iomem
*regs
)
318 spin_lock_irqsave(&instance
->hba_lock
, flags
);
319 writel((frame_phys_addr
>> 3)|(frame_count
),
320 &(regs
)->inbound_queue_port
);
321 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
325 * megasas_adp_reset_xscale - For controller reset
326 * @regs: MFI register set
329 megasas_adp_reset_xscale(struct megasas_instance
*instance
,
330 struct megasas_register_set __iomem
*regs
)
334 writel(MFI_ADP_RESET
, ®s
->inbound_doorbell
);
336 for (i
= 0; i
< 3; i
++)
337 msleep(1000); /* sleep for 3 secs */
339 pci_read_config_dword(instance
->pdev
, MFI_1068_PCSR_OFFSET
, &pcidata
);
340 printk(KERN_NOTICE
"pcidata = %x\n", pcidata
);
342 printk(KERN_NOTICE
"mfi 1068 offset read=%x\n", pcidata
);
344 pci_write_config_dword(instance
->pdev
,
345 MFI_1068_PCSR_OFFSET
, pcidata
);
347 for (i
= 0; i
< 2; i
++)
348 msleep(1000); /* need to wait 2 secs again */
351 pci_read_config_dword(instance
->pdev
,
352 MFI_1068_FW_HANDSHAKE_OFFSET
, &pcidata
);
353 printk(KERN_NOTICE
"1068 offset handshake read=%x\n", pcidata
);
354 if ((pcidata
& 0xffff0000) == MFI_1068_FW_READY
) {
355 printk(KERN_NOTICE
"1068 offset pcidt=%x\n", pcidata
);
357 pci_write_config_dword(instance
->pdev
,
358 MFI_1068_FW_HANDSHAKE_OFFSET
, pcidata
);
365 * megasas_check_reset_xscale - For controller reset check
366 * @regs: MFI register set
369 megasas_check_reset_xscale(struct megasas_instance
*instance
,
370 struct megasas_register_set __iomem
*regs
)
373 consumer
= *instance
->consumer
;
375 if ((instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) &&
376 (*instance
->consumer
== MEGASAS_ADPRESET_INPROG_SIGN
)) {
382 static struct megasas_instance_template megasas_instance_template_xscale
= {
384 .fire_cmd
= megasas_fire_cmd_xscale
,
385 .enable_intr
= megasas_enable_intr_xscale
,
386 .disable_intr
= megasas_disable_intr_xscale
,
387 .clear_intr
= megasas_clear_intr_xscale
,
388 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
389 .adp_reset
= megasas_adp_reset_xscale
,
390 .check_reset
= megasas_check_reset_xscale
,
391 .service_isr
= megasas_isr
,
392 .tasklet
= megasas_complete_cmd_dpc
,
393 .init_adapter
= megasas_init_adapter_mfi
,
394 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
395 .issue_dcmd
= megasas_issue_dcmd
,
399 * This is the end of set of functions & definitions specific
400 * to xscale (deviceid : 1064R, PERC5) controllers
404 * The following functions are defined for ppc (deviceid : 0x60)
409 * megasas_enable_intr_ppc - Enables interrupts
410 * @regs: MFI register set
413 megasas_enable_intr_ppc(struct megasas_register_set __iomem
* regs
)
415 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
417 writel(~0x80000000, &(regs
)->outbound_intr_mask
);
419 /* Dummy readl to force pci flush */
420 readl(®s
->outbound_intr_mask
);
424 * megasas_disable_intr_ppc - Disable interrupt
425 * @regs: MFI register set
428 megasas_disable_intr_ppc(struct megasas_register_set __iomem
* regs
)
430 u32 mask
= 0xFFFFFFFF;
431 writel(mask
, ®s
->outbound_intr_mask
);
432 /* Dummy readl to force pci flush */
433 readl(®s
->outbound_intr_mask
);
437 * megasas_read_fw_status_reg_ppc - returns the current FW status value
438 * @regs: MFI register set
441 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
443 return readl(&(regs
)->outbound_scratch_pad
);
447 * megasas_clear_interrupt_ppc - Check & clear interrupt
448 * @regs: MFI register set
451 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
453 u32 status
, mfiStatus
= 0;
456 * Check if it is our interrupt
458 status
= readl(®s
->outbound_intr_status
);
460 if (status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)
461 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
463 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
)
464 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
467 * Clear the interrupt by writing back the same value
469 writel(status
, ®s
->outbound_doorbell_clear
);
471 /* Dummy readl to force pci flush */
472 readl(®s
->outbound_doorbell_clear
);
478 * megasas_fire_cmd_ppc - Sends command to the FW
479 * @frame_phys_addr : Physical address of cmd
480 * @frame_count : Number of frames for the command
481 * @regs : MFI register set
484 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
485 dma_addr_t frame_phys_addr
,
487 struct megasas_register_set __iomem
*regs
)
490 spin_lock_irqsave(&instance
->hba_lock
, flags
);
491 writel((frame_phys_addr
| (frame_count
<<1))|1,
492 &(regs
)->inbound_queue_port
);
493 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
497 * megasas_check_reset_ppc - For controller reset check
498 * @regs: MFI register set
501 megasas_check_reset_ppc(struct megasas_instance
*instance
,
502 struct megasas_register_set __iomem
*regs
)
504 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
510 static struct megasas_instance_template megasas_instance_template_ppc
= {
512 .fire_cmd
= megasas_fire_cmd_ppc
,
513 .enable_intr
= megasas_enable_intr_ppc
,
514 .disable_intr
= megasas_disable_intr_ppc
,
515 .clear_intr
= megasas_clear_intr_ppc
,
516 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
517 .adp_reset
= megasas_adp_reset_xscale
,
518 .check_reset
= megasas_check_reset_ppc
,
519 .service_isr
= megasas_isr
,
520 .tasklet
= megasas_complete_cmd_dpc
,
521 .init_adapter
= megasas_init_adapter_mfi
,
522 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
523 .issue_dcmd
= megasas_issue_dcmd
,
527 * megasas_enable_intr_skinny - Enables interrupts
528 * @regs: MFI register set
531 megasas_enable_intr_skinny(struct megasas_register_set __iomem
*regs
)
533 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
535 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
537 /* Dummy readl to force pci flush */
538 readl(®s
->outbound_intr_mask
);
542 * megasas_disable_intr_skinny - Disables interrupt
543 * @regs: MFI register set
546 megasas_disable_intr_skinny(struct megasas_register_set __iomem
*regs
)
548 u32 mask
= 0xFFFFFFFF;
549 writel(mask
, ®s
->outbound_intr_mask
);
550 /* Dummy readl to force pci flush */
551 readl(®s
->outbound_intr_mask
);
555 * megasas_read_fw_status_reg_skinny - returns the current FW status value
556 * @regs: MFI register set
559 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
561 return readl(&(regs
)->outbound_scratch_pad
);
565 * megasas_clear_interrupt_skinny - Check & clear interrupt
566 * @regs: MFI register set
569 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
575 * Check if it is our interrupt
577 status
= readl(®s
->outbound_intr_status
);
579 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
584 * Check if it is our interrupt
586 if ((megasas_read_fw_status_reg_gen2(regs
) & MFI_STATE_MASK
) ==
588 mfiStatus
= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
590 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
593 * Clear the interrupt by writing back the same value
595 writel(status
, ®s
->outbound_intr_status
);
598 * dummy read to flush PCI
600 readl(®s
->outbound_intr_status
);
606 * megasas_fire_cmd_skinny - Sends command to the FW
607 * @frame_phys_addr : Physical address of cmd
608 * @frame_count : Number of frames for the command
609 * @regs : MFI register set
612 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
613 dma_addr_t frame_phys_addr
,
615 struct megasas_register_set __iomem
*regs
)
618 spin_lock_irqsave(&instance
->hba_lock
, flags
);
619 writel(0, &(regs
)->inbound_high_queue_port
);
620 writel((frame_phys_addr
| (frame_count
<<1))|1,
621 &(regs
)->inbound_low_queue_port
);
622 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
626 * megasas_check_reset_skinny - For controller reset check
627 * @regs: MFI register set
630 megasas_check_reset_skinny(struct megasas_instance
*instance
,
631 struct megasas_register_set __iomem
*regs
)
633 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
639 static struct megasas_instance_template megasas_instance_template_skinny
= {
641 .fire_cmd
= megasas_fire_cmd_skinny
,
642 .enable_intr
= megasas_enable_intr_skinny
,
643 .disable_intr
= megasas_disable_intr_skinny
,
644 .clear_intr
= megasas_clear_intr_skinny
,
645 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
646 .adp_reset
= megasas_adp_reset_gen2
,
647 .check_reset
= megasas_check_reset_skinny
,
648 .service_isr
= megasas_isr
,
649 .tasklet
= megasas_complete_cmd_dpc
,
650 .init_adapter
= megasas_init_adapter_mfi
,
651 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
652 .issue_dcmd
= megasas_issue_dcmd
,
657 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
662 * megasas_enable_intr_gen2 - Enables interrupts
663 * @regs: MFI register set
666 megasas_enable_intr_gen2(struct megasas_register_set __iomem
*regs
)
668 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
670 /* write ~0x00000005 (4 & 1) to the intr mask*/
671 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
673 /* Dummy readl to force pci flush */
674 readl(®s
->outbound_intr_mask
);
678 * megasas_disable_intr_gen2 - Disables interrupt
679 * @regs: MFI register set
682 megasas_disable_intr_gen2(struct megasas_register_set __iomem
*regs
)
684 u32 mask
= 0xFFFFFFFF;
685 writel(mask
, ®s
->outbound_intr_mask
);
686 /* Dummy readl to force pci flush */
687 readl(®s
->outbound_intr_mask
);
691 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
692 * @regs: MFI register set
695 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
697 return readl(&(regs
)->outbound_scratch_pad
);
701 * megasas_clear_interrupt_gen2 - Check & clear interrupt
702 * @regs: MFI register set
705 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
710 * Check if it is our interrupt
712 status
= readl(®s
->outbound_intr_status
);
714 if (status
& MFI_GEN2_ENABLE_INTERRUPT_MASK
) {
715 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
717 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
) {
718 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
722 * Clear the interrupt by writing back the same value
725 writel(status
, ®s
->outbound_doorbell_clear
);
727 /* Dummy readl to force pci flush */
728 readl(®s
->outbound_intr_status
);
733 * megasas_fire_cmd_gen2 - Sends command to the FW
734 * @frame_phys_addr : Physical address of cmd
735 * @frame_count : Number of frames for the command
736 * @regs : MFI register set
739 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
740 dma_addr_t frame_phys_addr
,
742 struct megasas_register_set __iomem
*regs
)
745 spin_lock_irqsave(&instance
->hba_lock
, flags
);
746 writel((frame_phys_addr
| (frame_count
<<1))|1,
747 &(regs
)->inbound_queue_port
);
748 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
752 * megasas_adp_reset_gen2 - For controller reset
753 * @regs: MFI register set
756 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
757 struct megasas_register_set __iomem
*reg_set
)
761 u32
*seq_offset
= ®_set
->seq_offset
;
762 u32
*hostdiag_offset
= ®_set
->host_diag
;
764 if (instance
->instancet
== &megasas_instance_template_skinny
) {
765 seq_offset
= ®_set
->fusion_seq_offset
;
766 hostdiag_offset
= ®_set
->fusion_host_diag
;
769 writel(0, seq_offset
);
770 writel(4, seq_offset
);
771 writel(0xb, seq_offset
);
772 writel(2, seq_offset
);
773 writel(7, seq_offset
);
774 writel(0xd, seq_offset
);
778 HostDiag
= (u32
)readl(hostdiag_offset
);
780 while ( !( HostDiag
& DIAG_WRITE_ENABLE
) ) {
782 HostDiag
= (u32
)readl(hostdiag_offset
);
783 printk(KERN_NOTICE
"RESETGEN2: retry=%x, hostdiag=%x\n",
791 printk(KERN_NOTICE
"ADP_RESET_GEN2: HostDiag=%x\n", HostDiag
);
793 writel((HostDiag
| DIAG_RESET_ADAPTER
), hostdiag_offset
);
797 HostDiag
= (u32
)readl(hostdiag_offset
);
798 while ( ( HostDiag
& DIAG_RESET_ADAPTER
) ) {
800 HostDiag
= (u32
)readl(hostdiag_offset
);
801 printk(KERN_NOTICE
"RESET_GEN2: retry=%x, hostdiag=%x\n",
812 * megasas_check_reset_gen2 - For controller reset check
813 * @regs: MFI register set
816 megasas_check_reset_gen2(struct megasas_instance
*instance
,
817 struct megasas_register_set __iomem
*regs
)
819 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
826 static struct megasas_instance_template megasas_instance_template_gen2
= {
828 .fire_cmd
= megasas_fire_cmd_gen2
,
829 .enable_intr
= megasas_enable_intr_gen2
,
830 .disable_intr
= megasas_disable_intr_gen2
,
831 .clear_intr
= megasas_clear_intr_gen2
,
832 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
833 .adp_reset
= megasas_adp_reset_gen2
,
834 .check_reset
= megasas_check_reset_gen2
,
835 .service_isr
= megasas_isr
,
836 .tasklet
= megasas_complete_cmd_dpc
,
837 .init_adapter
= megasas_init_adapter_mfi
,
838 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
839 .issue_dcmd
= megasas_issue_dcmd
,
843 * This is the end of set of functions & definitions
844 * specific to gen2 (deviceid : 0x78, 0x79) controllers
848 * Template added for TB (Fusion)
850 extern struct megasas_instance_template megasas_instance_template_fusion
;
853 * megasas_issue_polled - Issues a polling command
854 * @instance: Adapter soft state
855 * @cmd: Command packet to be issued
857 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
860 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
863 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
865 frame_hdr
->cmd_status
= 0xFF;
866 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
869 * Issue the frame using inbound queue port
871 instance
->instancet
->issue_dcmd(instance
, cmd
);
874 * Wait for cmd_status to change
876 return wait_and_poll(instance
, cmd
);
880 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
881 * @instance: Adapter soft state
882 * @cmd: Command to be issued
884 * This function waits on an event for the command to be returned from ISR.
885 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
886 * Used to issue ioctl commands.
889 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
890 struct megasas_cmd
*cmd
)
892 cmd
->cmd_status
= ENODATA
;
894 instance
->instancet
->issue_dcmd(instance
, cmd
);
896 wait_event(instance
->int_cmd_wait_q
, cmd
->cmd_status
!= ENODATA
);
902 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
903 * @instance: Adapter soft state
904 * @cmd_to_abort: Previously issued cmd to be aborted
906 * MFI firmware can abort previously issued AEN command (automatic event
907 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
908 * cmd and waits for return status.
909 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
912 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
913 struct megasas_cmd
*cmd_to_abort
)
915 struct megasas_cmd
*cmd
;
916 struct megasas_abort_frame
*abort_fr
;
918 cmd
= megasas_get_cmd(instance
);
923 abort_fr
= &cmd
->frame
->abort
;
926 * Prepare and issue the abort frame
928 abort_fr
->cmd
= MFI_CMD_ABORT
;
929 abort_fr
->cmd_status
= 0xFF;
931 abort_fr
->abort_context
= cmd_to_abort
->index
;
932 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
933 abort_fr
->abort_mfi_phys_addr_hi
= 0;
936 cmd
->cmd_status
= 0xFF;
938 instance
->instancet
->issue_dcmd(instance
, cmd
);
941 * Wait for this cmd to complete
943 wait_event(instance
->abort_cmd_wait_q
, cmd
->cmd_status
!= 0xFF);
946 megasas_return_cmd(instance
, cmd
);
951 * megasas_make_sgl32 - Prepares 32-bit SGL
952 * @instance: Adapter soft state
953 * @scp: SCSI command from the mid-layer
954 * @mfi_sgl: SGL to be filled in
956 * If successful, this function returns the number of SG elements. Otherwise,
960 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
961 union megasas_sgl
*mfi_sgl
)
965 struct scatterlist
*os_sgl
;
967 sge_count
= scsi_dma_map(scp
);
968 BUG_ON(sge_count
< 0);
971 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
972 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
973 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
980 * megasas_make_sgl64 - Prepares 64-bit SGL
981 * @instance: Adapter soft state
982 * @scp: SCSI command from the mid-layer
983 * @mfi_sgl: SGL to be filled in
985 * If successful, this function returns the number of SG elements. Otherwise,
989 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
990 union megasas_sgl
*mfi_sgl
)
994 struct scatterlist
*os_sgl
;
996 sge_count
= scsi_dma_map(scp
);
997 BUG_ON(sge_count
< 0);
1000 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1001 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
1002 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
1009 * megasas_make_sgl_skinny - Prepares IEEE SGL
1010 * @instance: Adapter soft state
1011 * @scp: SCSI command from the mid-layer
1012 * @mfi_sgl: SGL to be filled in
1014 * If successful, this function returns the number of SG elements. Otherwise,
1018 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
1019 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
1023 struct scatterlist
*os_sgl
;
1025 sge_count
= scsi_dma_map(scp
);
1028 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1029 mfi_sgl
->sge_skinny
[i
].length
= sg_dma_len(os_sgl
);
1030 mfi_sgl
->sge_skinny
[i
].phys_addr
=
1031 sg_dma_address(os_sgl
);
1032 mfi_sgl
->sge_skinny
[i
].flag
= 0;
1039 * megasas_get_frame_count - Computes the number of frames
1040 * @frame_type : type of frame- io or pthru frame
1041 * @sge_count : number of sg elements
1043 * Returns the number of frames required for numnber of sge's (sge_count)
1046 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
1047 u8 sge_count
, u8 frame_type
)
1054 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1055 sizeof(struct megasas_sge32
);
1057 if (instance
->flag_ieee
) {
1058 sge_sz
= sizeof(struct megasas_sge_skinny
);
1062 * Main frame can contain 2 SGEs for 64-bit SGLs and
1063 * 3 SGEs for 32-bit SGLs for ldio &
1064 * 1 SGEs for 64-bit SGLs and
1065 * 2 SGEs for 32-bit SGLs for pthru frame
1067 if (unlikely(frame_type
== PTHRU_FRAME
)) {
1068 if (instance
->flag_ieee
== 1) {
1069 num_cnt
= sge_count
- 1;
1070 } else if (IS_DMA64
)
1071 num_cnt
= sge_count
- 1;
1073 num_cnt
= sge_count
- 2;
1075 if (instance
->flag_ieee
== 1) {
1076 num_cnt
= sge_count
- 1;
1077 } else if (IS_DMA64
)
1078 num_cnt
= sge_count
- 2;
1080 num_cnt
= sge_count
- 3;
1084 sge_bytes
= sge_sz
* num_cnt
;
1086 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
1087 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
1092 if (frame_count
> 7)
1098 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1099 * @instance: Adapter soft state
1100 * @scp: SCSI command
1101 * @cmd: Command to be prepared in
1103 * This function prepares CDB commands. These are typcially pass-through
1104 * commands to the devices.
1107 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1108 struct megasas_cmd
*cmd
)
1113 struct megasas_pthru_frame
*pthru
;
1115 is_logical
= MEGASAS_IS_LOGICAL(scp
);
1116 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1117 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
1119 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1120 flags
= MFI_FRAME_DIR_WRITE
;
1121 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1122 flags
= MFI_FRAME_DIR_READ
;
1123 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
1124 flags
= MFI_FRAME_DIR_NONE
;
1126 if (instance
->flag_ieee
== 1) {
1127 flags
|= MFI_FRAME_IEEE
;
1131 * Prepare the DCDB frame
1133 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
1134 pthru
->cmd_status
= 0x0;
1135 pthru
->scsi_status
= 0x0;
1136 pthru
->target_id
= device_id
;
1137 pthru
->lun
= scp
->device
->lun
;
1138 pthru
->cdb_len
= scp
->cmd_len
;
1141 pthru
->flags
= flags
;
1142 pthru
->data_xfer_len
= scsi_bufflen(scp
);
1144 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
1147 * If the command is for the tape device, set the
1148 * pthru timeout to the os layer timeout value.
1150 if (scp
->device
->type
== TYPE_TAPE
) {
1151 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
1152 pthru
->timeout
= 0xFFFF;
1154 pthru
->timeout
= scp
->request
->timeout
/ HZ
;
1160 if (instance
->flag_ieee
== 1) {
1161 pthru
->flags
|= MFI_FRAME_SGL64
;
1162 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1164 } else if (IS_DMA64
) {
1165 pthru
->flags
|= MFI_FRAME_SGL64
;
1166 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
1169 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
1172 if (pthru
->sge_count
> instance
->max_num_sge
) {
1173 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
1179 * Sense info specific
1181 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1182 pthru
->sense_buf_phys_addr_hi
= 0;
1183 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1186 * Compute the total number of frames this command consumes. FW uses
1187 * this number to pull sufficient number of frames from host memory.
1189 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
1192 return cmd
->frame_count
;
1196 * megasas_build_ldio - Prepares IOs to logical devices
1197 * @instance: Adapter soft state
1198 * @scp: SCSI command
1199 * @cmd: Command to be prepared
1201 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1204 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1205 struct megasas_cmd
*cmd
)
1208 u8 sc
= scp
->cmnd
[0];
1210 struct megasas_io_frame
*ldio
;
1212 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1213 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1215 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1216 flags
= MFI_FRAME_DIR_WRITE
;
1217 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1218 flags
= MFI_FRAME_DIR_READ
;
1220 if (instance
->flag_ieee
== 1) {
1221 flags
|= MFI_FRAME_IEEE
;
1225 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1227 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
1228 ldio
->cmd_status
= 0x0;
1229 ldio
->scsi_status
= 0x0;
1230 ldio
->target_id
= device_id
;
1232 ldio
->reserved_0
= 0;
1234 ldio
->flags
= flags
;
1235 ldio
->start_lba_hi
= 0;
1236 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
1239 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1241 if (scp
->cmd_len
== 6) {
1242 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
1243 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1244 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1246 ldio
->start_lba_lo
&= 0x1FFFFF;
1250 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1252 else if (scp
->cmd_len
== 10) {
1253 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
1254 ((u32
) scp
->cmnd
[7] << 8);
1255 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1256 ((u32
) scp
->cmnd
[3] << 16) |
1257 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1261 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1263 else if (scp
->cmd_len
== 12) {
1264 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
1265 ((u32
) scp
->cmnd
[7] << 16) |
1266 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1268 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1269 ((u32
) scp
->cmnd
[3] << 16) |
1270 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1274 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1276 else if (scp
->cmd_len
== 16) {
1277 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
1278 ((u32
) scp
->cmnd
[11] << 16) |
1279 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1281 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1282 ((u32
) scp
->cmnd
[7] << 16) |
1283 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1285 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1286 ((u32
) scp
->cmnd
[3] << 16) |
1287 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1294 if (instance
->flag_ieee
) {
1295 ldio
->flags
|= MFI_FRAME_SGL64
;
1296 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1298 } else if (IS_DMA64
) {
1299 ldio
->flags
|= MFI_FRAME_SGL64
;
1300 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1302 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1304 if (ldio
->sge_count
> instance
->max_num_sge
) {
1305 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1311 * Sense info specific
1313 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1314 ldio
->sense_buf_phys_addr_hi
= 0;
1315 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1318 * Compute the total number of frames this command consumes. FW uses
1319 * this number to pull sufficient number of frames from host memory.
1321 cmd
->frame_count
= megasas_get_frame_count(instance
,
1322 ldio
->sge_count
, IO_FRAME
);
1324 return cmd
->frame_count
;
1328 * megasas_is_ldio - Checks if the cmd is for logical drive
1329 * @scmd: SCSI command
1331 * Called by megasas_queue_command to find out if the command to be queued
1332 * is a logical drive command
1334 inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1336 if (!MEGASAS_IS_LOGICAL(cmd
))
1338 switch (cmd
->cmnd
[0]) {
1354 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1356 * @instance: Adapter soft state
1359 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1361 struct megasas_cmd
*cmd
;
1363 union megasas_sgl
*mfi_sgl
;
1364 struct megasas_io_frame
*ldio
;
1365 struct megasas_pthru_frame
*pthru
;
1367 u32 max_cmd
= instance
->max_fw_cmds
;
1369 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1370 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1372 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1374 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1376 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1377 for (i
= 0; i
< max_cmd
; i
++) {
1378 cmd
= instance
->cmd_list
[i
];
1381 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1382 if (megasas_is_ldio(cmd
->scmd
)){
1383 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1384 mfi_sgl
= &ldio
->sgl
;
1385 sgcount
= ldio
->sge_count
;
1386 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance
->host
->host_no
, cmd
->frame_count
,ldio
->cmd
,ldio
->target_id
, ldio
->start_lba_lo
,ldio
->start_lba_hi
,ldio
->sense_buf_phys_addr_lo
,sgcount
);
1389 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1390 mfi_sgl
= &pthru
->sgl
;
1391 sgcount
= pthru
->sge_count
;
1392 printk(KERN_ERR
"megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance
->host
->host_no
,cmd
->frame_count
,pthru
->cmd
,pthru
->target_id
,pthru
->lun
,pthru
->cdb_len
, pthru
->data_xfer_len
,pthru
->sense_buf_phys_addr_lo
,sgcount
);
1394 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1395 for (n
= 0; n
< sgcount
; n
++){
1397 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl
->sge64
[n
].length
, (unsigned long)mfi_sgl
->sge64
[n
].phys_addr
) ;
1399 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl
->sge32
[n
].length
, mfi_sgl
->sge32
[n
].phys_addr
) ;
1402 printk(KERN_ERR
"\n");
1404 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1405 for (i
= 0; i
< max_cmd
; i
++) {
1407 cmd
= instance
->cmd_list
[i
];
1409 if(cmd
->sync_cmd
== 1){
1410 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1413 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1417 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
1418 struct scsi_cmnd
*scmd
)
1420 struct megasas_cmd
*cmd
;
1423 cmd
= megasas_get_cmd(instance
);
1425 return SCSI_MLQUEUE_HOST_BUSY
;
1428 * Logical drive command
1430 if (megasas_is_ldio(scmd
))
1431 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1433 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1436 goto out_return_cmd
;
1439 scmd
->SCp
.ptr
= (char *)cmd
;
1442 * Issue the command to the FW
1444 atomic_inc(&instance
->fw_outstanding
);
1446 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1447 cmd
->frame_count
-1, instance
->reg_set
);
1451 megasas_return_cmd(instance
, cmd
);
1457 * megasas_queue_command - Queue entry point
1458 * @scmd: SCSI command to be queued
1459 * @done: Callback entry point
1462 megasas_queue_command_lck(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
1464 struct megasas_instance
*instance
;
1465 unsigned long flags
;
1467 instance
= (struct megasas_instance
*)
1468 scmd
->device
->host
->hostdata
;
1470 if (instance
->issuepend_done
== 0)
1471 return SCSI_MLQUEUE_HOST_BUSY
;
1473 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1474 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1475 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1476 return SCSI_MLQUEUE_HOST_BUSY
;
1479 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1481 scmd
->scsi_done
= done
;
1484 if (MEGASAS_IS_LOGICAL(scmd
) &&
1485 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
1486 scmd
->result
= DID_BAD_TARGET
<< 16;
1490 switch (scmd
->cmnd
[0]) {
1491 case SYNCHRONIZE_CACHE
:
1493 * FW takes care of flush cache on its own
1494 * No need to send it down
1496 scmd
->result
= DID_OK
<< 16;
1502 if (instance
->instancet
->build_and_issue_cmd(instance
, scmd
)) {
1503 printk(KERN_ERR
"megasas: Err returned from build_and_issue_cmd\n");
1504 return SCSI_MLQUEUE_HOST_BUSY
;
1514 static DEF_SCSI_QCMD(megasas_queue_command
)
1516 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1520 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1522 if ((megasas_mgmt_info
.instance
[i
]) &&
1523 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1524 return megasas_mgmt_info
.instance
[i
];
1530 static int megasas_slave_configure(struct scsi_device
*sdev
)
1533 struct megasas_instance
*instance
;
1535 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1538 * Don't export physical disk devices to the disk driver.
1540 * FIXME: Currently we don't export them to the midlayer at all.
1541 * That will be fixed once LSI engineers have audited the
1542 * firmware for possible issues.
1544 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1545 sdev
->type
== TYPE_DISK
) {
1546 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1548 if (instance
->pd_list
[pd_index
].driveState
==
1549 MR_PD_STATE_SYSTEM
) {
1550 blk_queue_rq_timeout(sdev
->request_queue
,
1551 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1558 * The RAID firmware may require extended timeouts.
1560 blk_queue_rq_timeout(sdev
->request_queue
,
1561 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1565 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1568 struct megasas_instance
*instance
;
1569 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1570 if ((sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) &&
1571 (sdev
->type
== TYPE_DISK
)) {
1573 * Open the OS scan to the SYSTEM PD
1576 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1578 if ((instance
->pd_list
[pd_index
].driveState
==
1579 MR_PD_STATE_SYSTEM
) &&
1580 (instance
->pd_list
[pd_index
].driveType
==
1589 void megaraid_sas_kill_hba(struct megasas_instance
*instance
)
1591 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1592 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
1593 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1594 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)) {
1595 writel(MFI_STOP_ADP
, &instance
->reg_set
->doorbell
);
1597 writel(MFI_STOP_ADP
, &instance
->reg_set
->inbound_doorbell
);
1602 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1603 * restored to max value
1604 * @instance: Adapter soft state
1608 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
)
1610 unsigned long flags
;
1611 if (instance
->flag
& MEGASAS_FW_BUSY
1612 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1613 && atomic_read(&instance
->fw_outstanding
) <
1614 instance
->throttlequeuedepth
+ 1) {
1616 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1617 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1618 if ((instance
->pdev
->device
==
1619 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1620 (instance
->pdev
->device
==
1621 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1622 instance
->host
->can_queue
=
1623 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1625 instance
->host
->can_queue
=
1626 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1628 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1633 * megasas_complete_cmd_dpc - Returns FW's controller structure
1634 * @instance_addr: Address of adapter soft state
1636 * Tasklet to complete cmds
1638 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1643 struct megasas_cmd
*cmd
;
1644 struct megasas_instance
*instance
=
1645 (struct megasas_instance
*)instance_addr
;
1646 unsigned long flags
;
1648 /* If we have already declared adapter dead, donot complete cmds */
1649 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1652 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1654 producer
= *instance
->producer
;
1655 consumer
= *instance
->consumer
;
1657 while (consumer
!= producer
) {
1658 context
= instance
->reply_queue
[consumer
];
1659 if (context
>= instance
->max_fw_cmds
) {
1660 printk(KERN_ERR
"Unexpected context value %x\n",
1665 cmd
= instance
->cmd_list
[context
];
1667 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1670 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1675 *instance
->consumer
= producer
;
1677 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1680 * Check if we can restore can_queue
1682 megasas_check_and_restore_queue_depth(instance
);
1686 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
);
1689 process_fw_state_change_wq(struct work_struct
*work
);
1691 void megasas_do_ocr(struct megasas_instance
*instance
)
1693 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
1694 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
1695 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
1696 *instance
->consumer
= MEGASAS_ADPRESET_INPROG_SIGN
;
1698 instance
->instancet
->disable_intr(instance
->reg_set
);
1699 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
1700 instance
->issuepend_done
= 0;
1702 atomic_set(&instance
->fw_outstanding
, 0);
1703 megasas_internal_reset_defer_cmds(instance
);
1704 process_fw_state_change_wq(&instance
->work_init
);
1708 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1709 * @instance: Adapter soft state
1711 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1712 * complete all its outstanding commands. Returns error if one or more IOs
1713 * are pending after this time period. It also marks the controller dead.
1715 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
1719 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
1721 unsigned long flags
;
1722 struct list_head clist_local
;
1723 struct megasas_cmd
*reset_cmd
;
1725 u8 kill_adapter_flag
;
1727 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1728 adprecovery
= instance
->adprecovery
;
1729 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1731 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1733 INIT_LIST_HEAD(&clist_local
);
1734 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1735 list_splice_init(&instance
->internal_reset_pending_q
,
1737 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1739 printk(KERN_NOTICE
"megasas: HBA reset wait ...\n");
1740 for (i
= 0; i
< wait_time
; i
++) {
1742 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1743 adprecovery
= instance
->adprecovery
;
1744 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1745 if (adprecovery
== MEGASAS_HBA_OPERATIONAL
)
1749 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1750 printk(KERN_NOTICE
"megasas: reset: Stopping HBA.\n");
1751 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1752 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1753 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1758 while (!list_empty(&clist_local
)) {
1759 reset_cmd
= list_entry((&clist_local
)->next
,
1760 struct megasas_cmd
, list
);
1761 list_del_init(&reset_cmd
->list
);
1762 if (reset_cmd
->scmd
) {
1763 reset_cmd
->scmd
->result
= DID_RESET
<< 16;
1764 printk(KERN_NOTICE
"%d:%p reset [%02x]\n",
1765 reset_index
, reset_cmd
,
1766 reset_cmd
->scmd
->cmnd
[0]);
1768 reset_cmd
->scmd
->scsi_done(reset_cmd
->scmd
);
1769 megasas_return_cmd(instance
, reset_cmd
);
1770 } else if (reset_cmd
->sync_cmd
) {
1771 printk(KERN_NOTICE
"megasas:%p synch cmds"
1775 reset_cmd
->cmd_status
= ENODATA
;
1776 instance
->instancet
->fire_cmd(instance
,
1777 reset_cmd
->frame_phys_addr
,
1778 0, instance
->reg_set
);
1780 printk(KERN_NOTICE
"megasas: %p unexpected"
1790 for (i
= 0; i
< resetwaittime
; i
++) {
1792 int outstanding
= atomic_read(&instance
->fw_outstanding
);
1797 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
1798 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
1799 "commands to complete\n",i
,outstanding
);
1801 * Call cmd completion routine. Cmd to be
1802 * be completed directly without depending on isr.
1804 megasas_complete_cmd_dpc((unsigned long)instance
);
1811 kill_adapter_flag
= 0;
1813 fw_state
= instance
->instancet
->read_fw_status_reg(
1814 instance
->reg_set
) & MFI_STATE_MASK
;
1815 if ((fw_state
== MFI_STATE_FAULT
) &&
1816 (instance
->disableOnlineCtrlReset
== 0)) {
1818 kill_adapter_flag
= 2;
1821 megasas_do_ocr(instance
);
1822 kill_adapter_flag
= 1;
1824 /* wait for 1 secs to let FW finish the pending cmds */
1830 if (atomic_read(&instance
->fw_outstanding
) &&
1831 !kill_adapter_flag
) {
1832 if (instance
->disableOnlineCtrlReset
== 0) {
1834 megasas_do_ocr(instance
);
1836 /* wait for 5 secs to let FW finish the pending cmds */
1837 for (i
= 0; i
< wait_time
; i
++) {
1839 atomic_read(&instance
->fw_outstanding
);
1847 if (atomic_read(&instance
->fw_outstanding
) ||
1848 (kill_adapter_flag
== 2)) {
1849 printk(KERN_NOTICE
"megaraid_sas: pending cmds after reset\n");
1851 * Send signal to FW to stop processing any pending cmds.
1852 * The controller will be taken offline by the OS now.
1854 if ((instance
->pdev
->device
==
1855 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1856 (instance
->pdev
->device
==
1857 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1858 writel(MFI_STOP_ADP
,
1859 &instance
->reg_set
->doorbell
);
1861 writel(MFI_STOP_ADP
,
1862 &instance
->reg_set
->inbound_doorbell
);
1864 megasas_dump_pending_frames(instance
);
1865 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1866 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1867 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1871 printk(KERN_NOTICE
"megaraid_sas: no pending cmds after reset\n");
1877 * megasas_generic_reset - Generic reset routine
1878 * @scmd: Mid-layer SCSI command
1880 * This routine implements a generic reset handler for device, bus and host
1881 * reset requests. Device, bus and host specific reset handlers can use this
1882 * function after they do their specific tasks.
1884 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
1887 struct megasas_instance
*instance
;
1889 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1891 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET cmd=%x retries=%x\n",
1892 scmd
->cmnd
[0], scmd
->retries
);
1894 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1895 printk(KERN_ERR
"megasas: cannot recover from previous reset "
1900 ret_val
= megasas_wait_for_outstanding(instance
);
1901 if (ret_val
== SUCCESS
)
1902 printk(KERN_NOTICE
"megasas: reset successful \n");
1904 printk(KERN_ERR
"megasas: failed to do reset\n");
1910 * megasas_reset_timer - quiesce the adapter if required
1913 * Sets the FW busy flag and reduces the host->can_queue if the
1914 * cmd has not been completed within the timeout period.
1917 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
1919 struct megasas_instance
*instance
;
1920 unsigned long flags
;
1922 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
1923 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
1924 return BLK_EH_NOT_HANDLED
;
1927 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1928 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
1929 /* FW is busy, throttle IO */
1930 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1932 instance
->host
->can_queue
= instance
->throttlequeuedepth
;
1933 instance
->last_time
= jiffies
;
1934 instance
->flag
|= MEGASAS_FW_BUSY
;
1936 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1938 return BLK_EH_RESET_TIMER
;
1942 * megasas_reset_device - Device reset handler entry point
1944 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
1949 * First wait for all commands to complete
1951 ret
= megasas_generic_reset(scmd
);
1957 * megasas_reset_bus_host - Bus & host reset handler entry point
1959 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
1962 struct megasas_instance
*instance
;
1963 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1966 * First wait for all commands to complete
1968 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1969 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
))
1970 ret
= megasas_reset_fusion(scmd
->device
->host
);
1972 ret
= megasas_generic_reset(scmd
);
1978 * megasas_bios_param - Returns disk geometry for a disk
1979 * @sdev: device handle
1980 * @bdev: block device
1981 * @capacity: drive capacity
1982 * @geom: geometry parameters
1985 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
1986 sector_t capacity
, int geom
[])
1992 /* Default heads (64) & sectors (32) */
1996 tmp
= heads
* sectors
;
1997 cylinders
= capacity
;
1999 sector_div(cylinders
, tmp
);
2002 * Handle extended translation size for logical drives > 1Gb
2005 if (capacity
>= 0x200000) {
2008 tmp
= heads
*sectors
;
2009 cylinders
= capacity
;
2010 sector_div(cylinders
, tmp
);
2015 geom
[2] = cylinders
;
2020 static void megasas_aen_polling(struct work_struct
*work
);
2023 * megasas_service_aen - Processes an event notification
2024 * @instance: Adapter soft state
2025 * @cmd: AEN command completed by the ISR
2027 * For AEN, driver sends a command down to FW that is held by the FW till an
2028 * event occurs. When an event of interest occurs, FW completes the command
2029 * that it was previously holding.
2031 * This routines sends SIGIO signal to processes that have registered with the
2035 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2037 unsigned long flags
;
2039 * Don't signal app if it is just an aborted previously registered aen
2041 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
2042 spin_lock_irqsave(&poll_aen_lock
, flags
);
2043 megasas_poll_wait_aen
= 1;
2044 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2045 wake_up(&megasas_poll_wait
);
2046 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
2051 instance
->aen_cmd
= NULL
;
2052 megasas_return_cmd(instance
, cmd
);
2054 if ((instance
->unload
== 0) &&
2055 ((instance
->issuepend_done
== 1))) {
2056 struct megasas_aen_event
*ev
;
2057 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
2059 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
2061 ev
->instance
= instance
;
2063 INIT_WORK(&ev
->hotplug_work
, megasas_aen_polling
);
2064 schedule_delayed_work(
2065 (struct delayed_work
*)&ev
->hotplug_work
, 0);
2070 static int megasas_change_queue_depth(struct scsi_device
*sdev
,
2071 int queue_depth
, int reason
)
2073 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2076 if (queue_depth
> sdev
->host
->can_queue
)
2077 queue_depth
= sdev
->host
->can_queue
;
2078 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
),
2085 * Scsi host template for megaraid_sas driver
2087 static struct scsi_host_template megasas_template
= {
2089 .module
= THIS_MODULE
,
2090 .name
= "LSI SAS based MegaRAID driver",
2091 .proc_name
= "megaraid_sas",
2092 .slave_configure
= megasas_slave_configure
,
2093 .slave_alloc
= megasas_slave_alloc
,
2094 .queuecommand
= megasas_queue_command
,
2095 .eh_device_reset_handler
= megasas_reset_device
,
2096 .eh_bus_reset_handler
= megasas_reset_bus_host
,
2097 .eh_host_reset_handler
= megasas_reset_bus_host
,
2098 .eh_timed_out
= megasas_reset_timer
,
2099 .bios_param
= megasas_bios_param
,
2100 .use_clustering
= ENABLE_CLUSTERING
,
2101 .change_queue_depth
= megasas_change_queue_depth
,
2105 * megasas_complete_int_cmd - Completes an internal command
2106 * @instance: Adapter soft state
2107 * @cmd: Command to be completed
2109 * The megasas_issue_blocked_cmd() function waits for a command to complete
2110 * after it issues a command. This function wakes up that waiting routine by
2111 * calling wake_up() on the wait queue.
2114 megasas_complete_int_cmd(struct megasas_instance
*instance
,
2115 struct megasas_cmd
*cmd
)
2117 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
2119 if (cmd
->cmd_status
== ENODATA
) {
2120 cmd
->cmd_status
= 0;
2122 wake_up(&instance
->int_cmd_wait_q
);
2126 * megasas_complete_abort - Completes aborting a command
2127 * @instance: Adapter soft state
2128 * @cmd: Cmd that was issued to abort another cmd
2130 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2131 * after it issues an abort on a previously issued command. This function
2132 * wakes up all functions waiting on the same wait queue.
2135 megasas_complete_abort(struct megasas_instance
*instance
,
2136 struct megasas_cmd
*cmd
)
2138 if (cmd
->sync_cmd
) {
2140 cmd
->cmd_status
= 0;
2141 wake_up(&instance
->abort_cmd_wait_q
);
2148 * megasas_complete_cmd - Completes a command
2149 * @instance: Adapter soft state
2150 * @cmd: Command to be completed
2151 * @alt_status: If non-zero, use this value as status to
2152 * SCSI mid-layer instead of the value returned
2153 * by the FW. This should be used if caller wants
2154 * an alternate status (as in the case of aborted
2158 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
2162 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
2163 unsigned long flags
;
2164 struct fusion_context
*fusion
= instance
->ctrl_context
;
2166 /* flag for the retry reset */
2167 cmd
->retry_for_fw_reset
= 0;
2170 cmd
->scmd
->SCp
.ptr
= NULL
;
2173 case MFI_CMD_INVALID
:
2174 /* Some older 1068 controller FW may keep a pended
2175 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2176 when booting the kdump kernel. Ignore this command to
2177 prevent a kernel panic on shutdown of the kdump kernel. */
2178 printk(KERN_WARNING
"megaraid_sas: MFI_CMD_INVALID command "
2180 printk(KERN_WARNING
"megaraid_sas: If you have a controller "
2181 "other than PERC5, please upgrade your firmware.\n");
2183 case MFI_CMD_PD_SCSI_IO
:
2184 case MFI_CMD_LD_SCSI_IO
:
2187 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2188 * issued either through an IO path or an IOCTL path. If it
2189 * was via IOCTL, we will send it to internal completion.
2191 if (cmd
->sync_cmd
) {
2193 megasas_complete_int_cmd(instance
, cmd
);
2197 case MFI_CMD_LD_READ
:
2198 case MFI_CMD_LD_WRITE
:
2201 cmd
->scmd
->result
= alt_status
<< 16;
2207 atomic_dec(&instance
->fw_outstanding
);
2209 scsi_dma_unmap(cmd
->scmd
);
2210 cmd
->scmd
->scsi_done(cmd
->scmd
);
2211 megasas_return_cmd(instance
, cmd
);
2216 switch (hdr
->cmd_status
) {
2219 cmd
->scmd
->result
= DID_OK
<< 16;
2222 case MFI_STAT_SCSI_IO_FAILED
:
2223 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2225 (DID_ERROR
<< 16) | hdr
->scsi_status
;
2228 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2230 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
2232 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
2233 memset(cmd
->scmd
->sense_buffer
, 0,
2234 SCSI_SENSE_BUFFERSIZE
);
2235 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
2238 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
2243 case MFI_STAT_LD_OFFLINE
:
2244 case MFI_STAT_DEVICE_NOT_FOUND
:
2245 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
2249 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
2251 cmd
->scmd
->result
= DID_ERROR
<< 16;
2255 atomic_dec(&instance
->fw_outstanding
);
2257 scsi_dma_unmap(cmd
->scmd
);
2258 cmd
->scmd
->scsi_done(cmd
->scmd
);
2259 megasas_return_cmd(instance
, cmd
);
2266 /* Check for LD map update */
2267 if ((cmd
->frame
->dcmd
.opcode
== MR_DCMD_LD_MAP_GET_INFO
) &&
2268 (cmd
->frame
->dcmd
.mbox
.b
[1] == 1)) {
2269 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2270 if (cmd
->frame
->hdr
.cmd_status
!= 0) {
2271 if (cmd
->frame
->hdr
.cmd_status
!=
2273 printk(KERN_WARNING
"megasas: map sync"
2274 "failed, status = 0x%x.\n",
2275 cmd
->frame
->hdr
.cmd_status
);
2277 megasas_return_cmd(instance
, cmd
);
2278 spin_unlock_irqrestore(
2279 instance
->host
->host_lock
,
2285 megasas_return_cmd(instance
, cmd
);
2286 if (MR_ValidateMapInfo(
2287 fusion
->ld_map
[(instance
->map_id
& 1)],
2288 fusion
->load_balance_info
))
2289 fusion
->fast_path_io
= 1;
2291 fusion
->fast_path_io
= 0;
2292 megasas_sync_map_info(instance
);
2293 spin_unlock_irqrestore(instance
->host
->host_lock
,
2297 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
2298 cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET
) {
2299 spin_lock_irqsave(&poll_aen_lock
, flags
);
2300 megasas_poll_wait_aen
= 0;
2301 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2305 * See if got an event notification
2307 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
2308 megasas_service_aen(instance
, cmd
);
2310 megasas_complete_int_cmd(instance
, cmd
);
2316 * Cmd issued to abort another cmd returned
2318 megasas_complete_abort(instance
, cmd
);
2322 printk("megasas: Unknown command completed! [0x%X]\n",
2329 * megasas_issue_pending_cmds_again - issue all pending cmds
2330 * in FW again because of the fw reset
2331 * @instance: Adapter soft state
2334 megasas_issue_pending_cmds_again(struct megasas_instance
*instance
)
2336 struct megasas_cmd
*cmd
;
2337 struct list_head clist_local
;
2338 union megasas_evt_class_locale class_locale
;
2339 unsigned long flags
;
2342 INIT_LIST_HEAD(&clist_local
);
2343 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2344 list_splice_init(&instance
->internal_reset_pending_q
, &clist_local
);
2345 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2347 while (!list_empty(&clist_local
)) {
2348 cmd
= list_entry((&clist_local
)->next
,
2349 struct megasas_cmd
, list
);
2350 list_del_init(&cmd
->list
);
2352 if (cmd
->sync_cmd
|| cmd
->scmd
) {
2353 printk(KERN_NOTICE
"megaraid_sas: command %p, %p:%d"
2354 "detected to be pending while HBA reset.\n",
2355 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2357 cmd
->retry_for_fw_reset
++;
2359 if (cmd
->retry_for_fw_reset
== 3) {
2360 printk(KERN_NOTICE
"megaraid_sas: cmd %p, %p:%d"
2361 "was tried multiple times during reset."
2362 "Shutting down the HBA\n",
2363 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2364 megaraid_sas_kill_hba(instance
);
2366 instance
->adprecovery
=
2367 MEGASAS_HW_CRITICAL_ERROR
;
2372 if (cmd
->sync_cmd
== 1) {
2374 printk(KERN_NOTICE
"megaraid_sas: unexpected"
2375 "cmd attached to internal command!\n");
2377 printk(KERN_NOTICE
"megasas: %p synchronous cmd"
2378 "on the internal reset queue,"
2379 "issue it again.\n", cmd
);
2380 cmd
->cmd_status
= ENODATA
;
2381 instance
->instancet
->fire_cmd(instance
,
2382 cmd
->frame_phys_addr
,
2383 0, instance
->reg_set
);
2384 } else if (cmd
->scmd
) {
2385 printk(KERN_NOTICE
"megasas: %p scsi cmd [%02x]"
2386 "detected on the internal queue, issue again.\n",
2387 cmd
, cmd
->scmd
->cmnd
[0]);
2389 atomic_inc(&instance
->fw_outstanding
);
2390 instance
->instancet
->fire_cmd(instance
,
2391 cmd
->frame_phys_addr
,
2392 cmd
->frame_count
-1, instance
->reg_set
);
2394 printk(KERN_NOTICE
"megasas: %p unexpected cmd on the"
2395 "internal reset defer list while re-issue!!\n",
2400 if (instance
->aen_cmd
) {
2401 printk(KERN_NOTICE
"megaraid_sas: aen_cmd in def process\n");
2402 megasas_return_cmd(instance
, instance
->aen_cmd
);
2404 instance
->aen_cmd
= NULL
;
2408 * Initiate AEN (Asynchronous Event Notification)
2410 seq_num
= instance
->last_seq_num
;
2411 class_locale
.members
.reserved
= 0;
2412 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2413 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2415 megasas_register_aen(instance
, seq_num
, class_locale
.word
);
2419 * Move the internal reset pending commands to a deferred queue.
2421 * We move the commands pending at internal reset time to a
2422 * pending queue. This queue would be flushed after successful
2423 * completion of the internal reset sequence. if the internal reset
2424 * did not complete in time, the kernel reset handler would flush
2428 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
)
2430 struct megasas_cmd
*cmd
;
2432 u32 max_cmd
= instance
->max_fw_cmds
;
2434 unsigned long flags
;
2437 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
2438 for (i
= 0; i
< max_cmd
; i
++) {
2439 cmd
= instance
->cmd_list
[i
];
2440 if (cmd
->sync_cmd
== 1 || cmd
->scmd
) {
2441 printk(KERN_NOTICE
"megasas: moving cmd[%d]:%p:%d:%p"
2442 "on the defer queue as internal\n",
2443 defer_index
, cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2445 if (!list_empty(&cmd
->list
)) {
2446 printk(KERN_NOTICE
"megaraid_sas: ERROR while"
2447 " moving this cmd:%p, %d %p, it was"
2448 "discovered on some list?\n",
2449 cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2451 list_del_init(&cmd
->list
);
2454 list_add_tail(&cmd
->list
,
2455 &instance
->internal_reset_pending_q
);
2458 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
2463 process_fw_state_change_wq(struct work_struct
*work
)
2465 struct megasas_instance
*instance
=
2466 container_of(work
, struct megasas_instance
, work_init
);
2468 unsigned long flags
;
2470 if (instance
->adprecovery
!= MEGASAS_ADPRESET_SM_INFAULT
) {
2471 printk(KERN_NOTICE
"megaraid_sas: error, recovery st %x \n",
2472 instance
->adprecovery
);
2476 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
2477 printk(KERN_NOTICE
"megaraid_sas: FW detected to be in fault"
2478 "state, restarting it...\n");
2480 instance
->instancet
->disable_intr(instance
->reg_set
);
2481 atomic_set(&instance
->fw_outstanding
, 0);
2483 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
2484 instance
->instancet
->adp_reset(instance
, instance
->reg_set
);
2485 atomic_set(&instance
->fw_reset_no_pci_access
, 0 );
2487 printk(KERN_NOTICE
"megaraid_sas: FW restarted successfully,"
2488 "initiating next stage...\n");
2490 printk(KERN_NOTICE
"megaraid_sas: HBA recovery state machine,"
2491 "state 2 starting...\n");
2493 /*waitting for about 20 second before start the second init*/
2494 for (wait
= 0; wait
< 30; wait
++) {
2498 if (megasas_transition_to_ready(instance
, 1)) {
2499 printk(KERN_NOTICE
"megaraid_sas:adapter not ready\n");
2501 megaraid_sas_kill_hba(instance
);
2502 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2506 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
2507 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
2508 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)
2510 *instance
->consumer
= *instance
->producer
;
2512 *instance
->consumer
= 0;
2513 *instance
->producer
= 0;
2516 megasas_issue_init_mfi(instance
);
2518 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2519 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2520 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2521 instance
->instancet
->enable_intr(instance
->reg_set
);
2523 megasas_issue_pending_cmds_again(instance
);
2524 instance
->issuepend_done
= 1;
2530 * megasas_deplete_reply_queue - Processes all completed commands
2531 * @instance: Adapter soft state
2532 * @alt_status: Alternate status to be returned to
2533 * SCSI mid-layer instead of the status
2534 * returned by the FW
2535 * Note: this must be called with hba lock held
2538 megasas_deplete_reply_queue(struct megasas_instance
*instance
,
2544 if ((mfiStatus
= instance
->instancet
->check_reset(instance
,
2545 instance
->reg_set
)) == 1) {
2549 if ((mfiStatus
= instance
->instancet
->clear_intr(
2552 /* Hardware may not set outbound_intr_status in MSI-X mode */
2553 if (!instance
->msix_vectors
)
2557 instance
->mfiStatus
= mfiStatus
;
2559 if ((mfiStatus
& MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
)) {
2560 fw_state
= instance
->instancet
->read_fw_status_reg(
2561 instance
->reg_set
) & MFI_STATE_MASK
;
2563 if (fw_state
!= MFI_STATE_FAULT
) {
2564 printk(KERN_NOTICE
"megaraid_sas: fw state:%x\n",
2568 if ((fw_state
== MFI_STATE_FAULT
) &&
2569 (instance
->disableOnlineCtrlReset
== 0)) {
2570 printk(KERN_NOTICE
"megaraid_sas: wait adp restart\n");
2572 if ((instance
->pdev
->device
==
2573 PCI_DEVICE_ID_LSI_SAS1064R
) ||
2574 (instance
->pdev
->device
==
2575 PCI_DEVICE_ID_DELL_PERC5
) ||
2576 (instance
->pdev
->device
==
2577 PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
2579 *instance
->consumer
=
2580 MEGASAS_ADPRESET_INPROG_SIGN
;
2584 instance
->instancet
->disable_intr(instance
->reg_set
);
2585 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2586 instance
->issuepend_done
= 0;
2588 atomic_set(&instance
->fw_outstanding
, 0);
2589 megasas_internal_reset_defer_cmds(instance
);
2591 printk(KERN_NOTICE
"megasas: fwState=%x, stage:%d\n",
2592 fw_state
, instance
->adprecovery
);
2594 schedule_work(&instance
->work_init
);
2598 printk(KERN_NOTICE
"megasas: fwstate:%x, dis_OCR=%x\n",
2599 fw_state
, instance
->disableOnlineCtrlReset
);
2603 tasklet_schedule(&instance
->isr_tasklet
);
2607 * megasas_isr - isr entry point
2609 static irqreturn_t
megasas_isr(int irq
, void *devp
)
2611 struct megasas_irq_context
*irq_context
= devp
;
2612 struct megasas_instance
*instance
= irq_context
->instance
;
2613 unsigned long flags
;
2616 if (atomic_read(&instance
->fw_reset_no_pci_access
))
2619 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2620 rc
= megasas_deplete_reply_queue(instance
, DID_OK
);
2621 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2627 * megasas_transition_to_ready - Move the FW to READY state
2628 * @instance: Adapter soft state
2630 * During the initialization, FW passes can potentially be in any one of
2631 * several possible states. If the FW in operational, waiting-for-handshake
2632 * states, driver must take steps to bring it to ready state. Otherwise, it
2633 * has to wait for the ready state.
2636 megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
)
2642 u32 abs_state
, curr_abs_state
;
2644 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
2646 if (fw_state
!= MFI_STATE_READY
)
2647 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
2650 while (fw_state
!= MFI_STATE_READY
) {
2653 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2657 case MFI_STATE_FAULT
:
2658 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
2660 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2661 cur_state
= MFI_STATE_FAULT
;
2666 case MFI_STATE_WAIT_HANDSHAKE
:
2668 * Set the CLR bit in inbound doorbell
2670 if ((instance
->pdev
->device
==
2671 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2672 (instance
->pdev
->device
==
2673 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2674 (instance
->pdev
->device
==
2675 PCI_DEVICE_ID_LSI_FUSION
) ||
2676 (instance
->pdev
->device
==
2677 PCI_DEVICE_ID_LSI_INVADER
)) {
2679 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2680 &instance
->reg_set
->doorbell
);
2683 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2684 &instance
->reg_set
->inbound_doorbell
);
2687 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2688 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
2691 case MFI_STATE_BOOT_MESSAGE_PENDING
:
2692 if ((instance
->pdev
->device
==
2693 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2694 (instance
->pdev
->device
==
2695 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2696 (instance
->pdev
->device
==
2697 PCI_DEVICE_ID_LSI_FUSION
) ||
2698 (instance
->pdev
->device
==
2699 PCI_DEVICE_ID_LSI_INVADER
)) {
2700 writel(MFI_INIT_HOTPLUG
,
2701 &instance
->reg_set
->doorbell
);
2703 writel(MFI_INIT_HOTPLUG
,
2704 &instance
->reg_set
->inbound_doorbell
);
2706 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2707 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
2710 case MFI_STATE_OPERATIONAL
:
2712 * Bring it to READY state; assuming max wait 10 secs
2714 instance
->instancet
->disable_intr(instance
->reg_set
);
2715 if ((instance
->pdev
->device
==
2716 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2717 (instance
->pdev
->device
==
2718 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2719 (instance
->pdev
->device
2720 == PCI_DEVICE_ID_LSI_FUSION
) ||
2721 (instance
->pdev
->device
2722 == PCI_DEVICE_ID_LSI_INVADER
)) {
2723 writel(MFI_RESET_FLAGS
,
2724 &instance
->reg_set
->doorbell
);
2725 if ((instance
->pdev
->device
==
2726 PCI_DEVICE_ID_LSI_FUSION
) ||
2727 (instance
->pdev
->device
==
2728 PCI_DEVICE_ID_LSI_INVADER
)) {
2729 for (i
= 0; i
< (10 * 1000); i
+= 20) {
2740 writel(MFI_RESET_FLAGS
,
2741 &instance
->reg_set
->inbound_doorbell
);
2743 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2744 cur_state
= MFI_STATE_OPERATIONAL
;
2747 case MFI_STATE_UNDEFINED
:
2749 * This state should not last for more than 2 seconds
2751 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2752 cur_state
= MFI_STATE_UNDEFINED
;
2755 case MFI_STATE_BB_INIT
:
2756 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2757 cur_state
= MFI_STATE_BB_INIT
;
2760 case MFI_STATE_FW_INIT
:
2761 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2762 cur_state
= MFI_STATE_FW_INIT
;
2765 case MFI_STATE_FW_INIT_2
:
2766 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2767 cur_state
= MFI_STATE_FW_INIT_2
;
2770 case MFI_STATE_DEVICE_SCAN
:
2771 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2772 cur_state
= MFI_STATE_DEVICE_SCAN
;
2775 case MFI_STATE_FLUSH_CACHE
:
2776 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2777 cur_state
= MFI_STATE_FLUSH_CACHE
;
2781 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
2787 * The cur_state should not last for more than max_wait secs
2789 for (i
= 0; i
< (max_wait
* 1000); i
++) {
2790 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
2793 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2795 if (abs_state
== curr_abs_state
) {
2802 * Return error if fw_state hasn't changed after max_wait
2804 if (curr_abs_state
== abs_state
) {
2805 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
2806 "in %d secs\n", fw_state
, max_wait
);
2810 printk(KERN_INFO
"megasas: FW now in Ready state\n");
2816 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2817 * @instance: Adapter soft state
2819 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
2822 u32 max_cmd
= instance
->max_mfi_cmds
;
2823 struct megasas_cmd
*cmd
;
2825 if (!instance
->frame_dma_pool
)
2829 * Return all frames to pool
2831 for (i
= 0; i
< max_cmd
; i
++) {
2833 cmd
= instance
->cmd_list
[i
];
2836 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
2837 cmd
->frame_phys_addr
);
2840 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
2841 cmd
->sense_phys_addr
);
2845 * Now destroy the pool itself
2847 pci_pool_destroy(instance
->frame_dma_pool
);
2848 pci_pool_destroy(instance
->sense_dma_pool
);
2850 instance
->frame_dma_pool
= NULL
;
2851 instance
->sense_dma_pool
= NULL
;
2855 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2856 * @instance: Adapter soft state
2858 * Each command packet has an embedded DMA memory buffer that is used for
2859 * filling MFI frame and the SG list that immediately follows the frame. This
2860 * function creates those DMA memory buffers for each command packet by using
2861 * PCI pool facility.
2863 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
2871 struct megasas_cmd
*cmd
;
2873 max_cmd
= instance
->max_mfi_cmds
;
2876 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2877 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2879 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
2880 sizeof(struct megasas_sge32
);
2882 if (instance
->flag_ieee
) {
2883 sge_sz
= sizeof(struct megasas_sge_skinny
);
2887 * Calculated the number of 64byte frames required for SGL
2889 sgl_sz
= sge_sz
* instance
->max_num_sge
;
2890 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
2894 * We need one extra frame for the MFI command
2898 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
2900 * Use DMA pool facility provided by PCI layer
2902 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
2903 instance
->pdev
, total_sz
, 64,
2906 if (!instance
->frame_dma_pool
) {
2907 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
2911 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
2912 instance
->pdev
, 128, 4, 0);
2914 if (!instance
->sense_dma_pool
) {
2915 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
2917 pci_pool_destroy(instance
->frame_dma_pool
);
2918 instance
->frame_dma_pool
= NULL
;
2924 * Allocate and attach a frame to each of the commands in cmd_list.
2925 * By making cmd->index as the context instead of the &cmd, we can
2926 * always use 32bit context regardless of the architecture
2928 for (i
= 0; i
< max_cmd
; i
++) {
2930 cmd
= instance
->cmd_list
[i
];
2932 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
2933 GFP_KERNEL
, &cmd
->frame_phys_addr
);
2935 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
2936 GFP_KERNEL
, &cmd
->sense_phys_addr
);
2939 * megasas_teardown_frame_pool() takes care of freeing
2940 * whatever has been allocated
2942 if (!cmd
->frame
|| !cmd
->sense
) {
2943 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
2944 megasas_teardown_frame_pool(instance
);
2948 memset(cmd
->frame
, 0, total_sz
);
2949 cmd
->frame
->io
.context
= cmd
->index
;
2950 cmd
->frame
->io
.pad_0
= 0;
2951 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
2952 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
2954 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
2961 * megasas_free_cmds - Free all the cmds in the free cmd pool
2962 * @instance: Adapter soft state
2964 void megasas_free_cmds(struct megasas_instance
*instance
)
2967 /* First free the MFI frame pool */
2968 megasas_teardown_frame_pool(instance
);
2970 /* Free all the commands in the cmd_list */
2971 for (i
= 0; i
< instance
->max_mfi_cmds
; i
++)
2973 kfree(instance
->cmd_list
[i
]);
2975 /* Free the cmd_list buffer itself */
2976 kfree(instance
->cmd_list
);
2977 instance
->cmd_list
= NULL
;
2979 INIT_LIST_HEAD(&instance
->cmd_pool
);
2983 * megasas_alloc_cmds - Allocates the command packets
2984 * @instance: Adapter soft state
2986 * Each command that is issued to the FW, whether IO commands from the OS or
2987 * internal commands like IOCTLs, are wrapped in local data structure called
2988 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2991 * Each frame has a 32-bit field called context (tag). This context is used
2992 * to get back the megasas_cmd from the frame when a frame gets completed in
2993 * the ISR. Typically the address of the megasas_cmd itself would be used as
2994 * the context. But we wanted to keep the differences between 32 and 64 bit
2995 * systems to the mininum. We always use 32 bit integers for the context. In
2996 * this driver, the 32 bit values are the indices into an array cmd_list.
2997 * This array is used only to look up the megasas_cmd given the context. The
2998 * free commands themselves are maintained in a linked list called cmd_pool.
3000 int megasas_alloc_cmds(struct megasas_instance
*instance
)
3005 struct megasas_cmd
*cmd
;
3007 max_cmd
= instance
->max_mfi_cmds
;
3010 * instance->cmd_list is an array of struct megasas_cmd pointers.
3011 * Allocate the dynamic array first and then allocate individual
3014 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
3016 if (!instance
->cmd_list
) {
3017 printk(KERN_DEBUG
"megasas: out of memory\n");
3021 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) *max_cmd
);
3023 for (i
= 0; i
< max_cmd
; i
++) {
3024 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
3027 if (!instance
->cmd_list
[i
]) {
3029 for (j
= 0; j
< i
; j
++)
3030 kfree(instance
->cmd_list
[j
]);
3032 kfree(instance
->cmd_list
);
3033 instance
->cmd_list
= NULL
;
3040 * Add all the commands to command pool (instance->cmd_pool)
3042 for (i
= 0; i
< max_cmd
; i
++) {
3043 cmd
= instance
->cmd_list
[i
];
3044 memset(cmd
, 0, sizeof(struct megasas_cmd
));
3047 cmd
->instance
= instance
;
3049 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
3053 * Create a frame pool and assign one frame to each cmd
3055 if (megasas_create_frame_pool(instance
)) {
3056 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
3057 megasas_free_cmds(instance
);
3064 * megasas_get_pd_list_info - Returns FW's pd_list structure
3065 * @instance: Adapter soft state
3066 * @pd_list: pd_list structure
3068 * Issues an internal command (DCMD) to get the FW's controller PD
3069 * list structure. This information is mainly used to find out SYSTEM
3070 * supported by the FW.
3073 megasas_get_pd_list(struct megasas_instance
*instance
)
3075 int ret
= 0, pd_index
= 0;
3076 struct megasas_cmd
*cmd
;
3077 struct megasas_dcmd_frame
*dcmd
;
3078 struct MR_PD_LIST
*ci
;
3079 struct MR_PD_ADDRESS
*pd_addr
;
3080 dma_addr_t ci_h
= 0;
3082 cmd
= megasas_get_cmd(instance
);
3085 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
3089 dcmd
= &cmd
->frame
->dcmd
;
3091 ci
= pci_alloc_consistent(instance
->pdev
,
3092 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
3095 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
3096 megasas_return_cmd(instance
, cmd
);
3100 memset(ci
, 0, sizeof(*ci
));
3101 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3103 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
3104 dcmd
->mbox
.b
[1] = 0;
3105 dcmd
->cmd
= MFI_CMD_DCMD
;
3106 dcmd
->cmd_status
= 0xFF;
3107 dcmd
->sge_count
= 1;
3108 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3111 dcmd
->data_xfer_len
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3112 dcmd
->opcode
= MR_DCMD_PD_LIST_QUERY
;
3113 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3114 dcmd
->sgl
.sge32
[0].length
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3116 if (!megasas_issue_polled(instance
, cmd
)) {
3123 * the following function will get the instance PD LIST.
3130 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
3132 memset(instance
->pd_list
, 0,
3133 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
3135 for (pd_index
= 0; pd_index
< ci
->count
; pd_index
++) {
3137 instance
->pd_list
[pd_addr
->deviceId
].tid
=
3139 instance
->pd_list
[pd_addr
->deviceId
].driveType
=
3140 pd_addr
->scsiDevType
;
3141 instance
->pd_list
[pd_addr
->deviceId
].driveState
=
3147 pci_free_consistent(instance
->pdev
,
3148 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
3150 megasas_return_cmd(instance
, cmd
);
3156 * megasas_get_ld_list_info - Returns FW's ld_list structure
3157 * @instance: Adapter soft state
3158 * @ld_list: ld_list structure
3160 * Issues an internal command (DCMD) to get the FW's controller PD
3161 * list structure. This information is mainly used to find out SYSTEM
3162 * supported by the FW.
3165 megasas_get_ld_list(struct megasas_instance
*instance
)
3167 int ret
= 0, ld_index
= 0, ids
= 0;
3168 struct megasas_cmd
*cmd
;
3169 struct megasas_dcmd_frame
*dcmd
;
3170 struct MR_LD_LIST
*ci
;
3171 dma_addr_t ci_h
= 0;
3173 cmd
= megasas_get_cmd(instance
);
3176 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
3180 dcmd
= &cmd
->frame
->dcmd
;
3182 ci
= pci_alloc_consistent(instance
->pdev
,
3183 sizeof(struct MR_LD_LIST
),
3187 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
3188 megasas_return_cmd(instance
, cmd
);
3192 memset(ci
, 0, sizeof(*ci
));
3193 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3195 dcmd
->cmd
= MFI_CMD_DCMD
;
3196 dcmd
->cmd_status
= 0xFF;
3197 dcmd
->sge_count
= 1;
3198 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3200 dcmd
->data_xfer_len
= sizeof(struct MR_LD_LIST
);
3201 dcmd
->opcode
= MR_DCMD_LD_GET_LIST
;
3202 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3203 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_LD_LIST
);
3206 if (!megasas_issue_polled(instance
, cmd
)) {
3212 /* the following function will get the instance PD LIST */
3214 if ((ret
== 0) && (ci
->ldCount
<= MAX_LOGICAL_DRIVES
)) {
3215 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3217 for (ld_index
= 0; ld_index
< ci
->ldCount
; ld_index
++) {
3218 if (ci
->ldList
[ld_index
].state
!= 0) {
3219 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
3220 instance
->ld_ids
[ids
] =
3221 ci
->ldList
[ld_index
].ref
.targetId
;
3226 pci_free_consistent(instance
->pdev
,
3227 sizeof(struct MR_LD_LIST
),
3231 megasas_return_cmd(instance
, cmd
);
3236 * megasas_get_controller_info - Returns FW's controller structure
3237 * @instance: Adapter soft state
3238 * @ctrl_info: Controller information structure
3240 * Issues an internal command (DCMD) to get the FW's controller structure.
3241 * This information is mainly used to find out the maximum IO transfer per
3242 * command supported by the FW.
3245 megasas_get_ctrl_info(struct megasas_instance
*instance
,
3246 struct megasas_ctrl_info
*ctrl_info
)
3249 struct megasas_cmd
*cmd
;
3250 struct megasas_dcmd_frame
*dcmd
;
3251 struct megasas_ctrl_info
*ci
;
3252 dma_addr_t ci_h
= 0;
3254 cmd
= megasas_get_cmd(instance
);
3257 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
3261 dcmd
= &cmd
->frame
->dcmd
;
3263 ci
= pci_alloc_consistent(instance
->pdev
,
3264 sizeof(struct megasas_ctrl_info
), &ci_h
);
3267 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
3268 megasas_return_cmd(instance
, cmd
);
3272 memset(ci
, 0, sizeof(*ci
));
3273 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3275 dcmd
->cmd
= MFI_CMD_DCMD
;
3276 dcmd
->cmd_status
= 0xFF;
3277 dcmd
->sge_count
= 1;
3278 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3281 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
3282 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
3283 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3284 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
3286 if (!megasas_issue_polled(instance
, cmd
)) {
3288 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
3293 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
3296 megasas_return_cmd(instance
, cmd
);
3301 * megasas_issue_init_mfi - Initializes the FW
3302 * @instance: Adapter soft state
3304 * Issues the INIT MFI cmd
3307 megasas_issue_init_mfi(struct megasas_instance
*instance
)
3311 struct megasas_cmd
*cmd
;
3313 struct megasas_init_frame
*init_frame
;
3314 struct megasas_init_queue_info
*initq_info
;
3315 dma_addr_t init_frame_h
;
3316 dma_addr_t initq_info_h
;
3319 * Prepare a init frame. Note the init frame points to queue info
3320 * structure. Each frame has SGL allocated after first 64 bytes. For
3321 * this frame - since we don't need any SGL - we use SGL's space as
3322 * queue info structure
3324 * We will not get a NULL command below. We just created the pool.
3326 cmd
= megasas_get_cmd(instance
);
3328 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
3329 initq_info
= (struct megasas_init_queue_info
*)
3330 ((unsigned long)init_frame
+ 64);
3332 init_frame_h
= cmd
->frame_phys_addr
;
3333 initq_info_h
= init_frame_h
+ 64;
3335 context
= init_frame
->context
;
3336 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
3337 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
3338 init_frame
->context
= context
;
3340 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
3341 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
3343 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
3344 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
3346 init_frame
->cmd
= MFI_CMD_INIT
;
3347 init_frame
->cmd_status
= 0xFF;
3348 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
3350 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
3353 * disable the intr before firing the init frame to FW
3355 instance
->instancet
->disable_intr(instance
->reg_set
);
3358 * Issue the init frame in polled mode
3361 if (megasas_issue_polled(instance
, cmd
)) {
3362 printk(KERN_ERR
"megasas: Failed to init firmware\n");
3363 megasas_return_cmd(instance
, cmd
);
3367 megasas_return_cmd(instance
, cmd
);
3376 megasas_init_adapter_mfi(struct megasas_instance
*instance
)
3378 struct megasas_register_set __iomem
*reg_set
;
3382 reg_set
= instance
->reg_set
;
3385 * Get various operational parameters from status register
3387 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
3389 * Reduce the max supported cmds by 1. This is to ensure that the
3390 * reply_q_sz (1 more than the max cmd that driver may send)
3391 * does not exceed max cmds that the FW can support
3393 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
3394 instance
->max_mfi_cmds
= instance
->max_fw_cmds
;
3395 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
3398 * Create a pool of commands
3400 if (megasas_alloc_cmds(instance
))
3401 goto fail_alloc_cmds
;
3404 * Allocate memory for reply queue. Length of reply queue should
3405 * be _one_ more than the maximum commands handled by the firmware.
3407 * Note: When FW completes commands, it places corresponding contex
3408 * values in this circular reply queue. This circular queue is a fairly
3409 * typical producer-consumer queue. FW is the producer (of completed
3410 * commands) and the driver is the consumer.
3412 context_sz
= sizeof(u32
);
3413 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
3415 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
3417 &instance
->reply_queue_h
);
3419 if (!instance
->reply_queue
) {
3420 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
3421 goto fail_reply_queue
;
3424 if (megasas_issue_init_mfi(instance
))
3427 instance
->fw_support_ieee
= 0;
3428 instance
->fw_support_ieee
=
3429 (instance
->instancet
->read_fw_status_reg(reg_set
) &
3432 printk(KERN_NOTICE
"megasas_init_mfi: fw_support_ieee=%d",
3433 instance
->fw_support_ieee
);
3435 if (instance
->fw_support_ieee
)
3436 instance
->flag_ieee
= 1;
3442 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3443 instance
->reply_queue
, instance
->reply_queue_h
);
3445 megasas_free_cmds(instance
);
3452 * megasas_init_fw - Initializes the FW
3453 * @instance: Adapter soft state
3455 * This is the main function for initializing firmware
3458 static int megasas_init_fw(struct megasas_instance
*instance
)
3462 u32 tmp_sectors
, msix_enable
;
3463 struct megasas_register_set __iomem
*reg_set
;
3464 struct megasas_ctrl_info
*ctrl_info
;
3465 unsigned long bar_list
;
3468 /* Find first memory bar */
3469 bar_list
= pci_select_bars(instance
->pdev
, IORESOURCE_MEM
);
3470 instance
->bar
= find_first_bit(&bar_list
, sizeof(unsigned long));
3471 instance
->base_addr
= pci_resource_start(instance
->pdev
, instance
->bar
);
3472 if (pci_request_selected_regions(instance
->pdev
, instance
->bar
,
3474 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
3478 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
3480 if (!instance
->reg_set
) {
3481 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
3485 reg_set
= instance
->reg_set
;
3487 switch (instance
->pdev
->device
) {
3488 case PCI_DEVICE_ID_LSI_FUSION
:
3489 case PCI_DEVICE_ID_LSI_INVADER
:
3490 instance
->instancet
= &megasas_instance_template_fusion
;
3492 case PCI_DEVICE_ID_LSI_SAS1078R
:
3493 case PCI_DEVICE_ID_LSI_SAS1078DE
:
3494 instance
->instancet
= &megasas_instance_template_ppc
;
3496 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
3497 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
3498 instance
->instancet
= &megasas_instance_template_gen2
;
3500 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
3501 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
3502 instance
->instancet
= &megasas_instance_template_skinny
;
3504 case PCI_DEVICE_ID_LSI_SAS1064R
:
3505 case PCI_DEVICE_ID_DELL_PERC5
:
3507 instance
->instancet
= &megasas_instance_template_xscale
;
3512 * We expect the FW state to be READY
3514 if (megasas_transition_to_ready(instance
, 0))
3515 goto fail_ready_state
;
3517 /* Check if MSI-X is supported while in ready state */
3518 msix_enable
= (instance
->instancet
->read_fw_status_reg(reg_set
) &
3520 if (msix_enable
&& !msix_disable
) {
3521 /* Check max MSI-X vectors */
3522 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
3523 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)) {
3524 instance
->msix_vectors
= (readl(&instance
->reg_set
->
3525 outbound_scratch_pad_2
3528 instance
->msix_vectors
=
3530 instance
->msix_vectors
);
3532 instance
->msix_vectors
= 1;
3533 /* Don't bother allocating more MSI-X vectors than cpus */
3534 instance
->msix_vectors
= min(instance
->msix_vectors
,
3535 (unsigned int)num_online_cpus());
3536 for (i
= 0; i
< instance
->msix_vectors
; i
++)
3537 instance
->msixentry
[i
].entry
= i
;
3538 i
= pci_enable_msix(instance
->pdev
, instance
->msixentry
,
3539 instance
->msix_vectors
);
3542 if (!pci_enable_msix(instance
->pdev
,
3543 instance
->msixentry
, i
))
3544 instance
->msix_vectors
= i
;
3546 instance
->msix_vectors
= 0;
3549 instance
->msix_vectors
= 0;
3552 /* Get operational params, sge flags, send init cmd to controller */
3553 if (instance
->instancet
->init_adapter(instance
))
3554 goto fail_init_adapter
;
3556 printk(KERN_ERR
"megasas: INIT adapter done\n");
3559 * the following function will get the PD LIST.
3562 memset(instance
->pd_list
, 0 ,
3563 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
3564 megasas_get_pd_list(instance
);
3566 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3567 megasas_get_ld_list(instance
);
3569 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
3572 * Compute the max allowed sectors per IO: The controller info has two
3573 * limits on max sectors. Driver should use the minimum of these two.
3575 * 1 << stripe_sz_ops.min = max sectors per strip
3577 * Note that older firmwares ( < FW ver 30) didn't report information
3578 * to calculate max_sectors_1. So the number ended up as zero always.
3581 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
3583 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
3584 ctrl_info
->max_strips_per_io
;
3585 max_sectors_2
= ctrl_info
->max_request_size
;
3587 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
3588 instance
->disableOnlineCtrlReset
=
3589 ctrl_info
->properties
.OnOffProperties
.disableOnlineCtrlReset
;
3592 instance
->max_sectors_per_req
= instance
->max_num_sge
*
3594 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
3595 instance
->max_sectors_per_req
= tmp_sectors
;
3599 /* Check for valid throttlequeuedepth module parameter */
3600 if (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
||
3601 instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) {
3602 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3603 MEGASAS_SKINNY_INT_CMDS
))
3604 instance
->throttlequeuedepth
=
3605 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3607 instance
->throttlequeuedepth
= throttlequeuedepth
;
3609 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3611 instance
->throttlequeuedepth
=
3612 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3614 instance
->throttlequeuedepth
= throttlequeuedepth
;
3618 * Setup tasklet for cmd completion
3621 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
3622 (unsigned long)instance
);
3628 iounmap(instance
->reg_set
);
3631 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3637 * megasas_release_mfi - Reverses the FW initialization
3638 * @intance: Adapter soft state
3640 static void megasas_release_mfi(struct megasas_instance
*instance
)
3642 u32 reply_q_sz
= sizeof(u32
) *(instance
->max_mfi_cmds
+ 1);
3644 if (instance
->reply_queue
)
3645 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3646 instance
->reply_queue
, instance
->reply_queue_h
);
3648 megasas_free_cmds(instance
);
3650 iounmap(instance
->reg_set
);
3652 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3656 * megasas_get_seq_num - Gets latest event sequence numbers
3657 * @instance: Adapter soft state
3658 * @eli: FW event log sequence numbers information
3660 * FW maintains a log of all events in a non-volatile area. Upper layers would
3661 * usually find out the latest sequence number of the events, the seq number at
3662 * the boot etc. They would "read" all the events below the latest seq number
3663 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3664 * number), they would subsribe to AEN (asynchronous event notification) and
3665 * wait for the events to happen.
3668 megasas_get_seq_num(struct megasas_instance
*instance
,
3669 struct megasas_evt_log_info
*eli
)
3671 struct megasas_cmd
*cmd
;
3672 struct megasas_dcmd_frame
*dcmd
;
3673 struct megasas_evt_log_info
*el_info
;
3674 dma_addr_t el_info_h
= 0;
3676 cmd
= megasas_get_cmd(instance
);
3682 dcmd
= &cmd
->frame
->dcmd
;
3683 el_info
= pci_alloc_consistent(instance
->pdev
,
3684 sizeof(struct megasas_evt_log_info
),
3688 megasas_return_cmd(instance
, cmd
);
3692 memset(el_info
, 0, sizeof(*el_info
));
3693 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3695 dcmd
->cmd
= MFI_CMD_DCMD
;
3696 dcmd
->cmd_status
= 0x0;
3697 dcmd
->sge_count
= 1;
3698 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3701 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
3702 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
3703 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
3704 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
3706 megasas_issue_blocked_cmd(instance
, cmd
);
3709 * Copy the data back into callers buffer
3711 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
3713 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
3714 el_info
, el_info_h
);
3716 megasas_return_cmd(instance
, cmd
);
3722 * megasas_register_aen - Registers for asynchronous event notification
3723 * @instance: Adapter soft state
3724 * @seq_num: The starting sequence number
3725 * @class_locale: Class of the event
3727 * This function subscribes for AEN for events beyond the @seq_num. It requests
3728 * to be notified if and only if the event is of type @class_locale
3731 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
3732 u32 class_locale_word
)
3735 struct megasas_cmd
*cmd
;
3736 struct megasas_dcmd_frame
*dcmd
;
3737 union megasas_evt_class_locale curr_aen
;
3738 union megasas_evt_class_locale prev_aen
;
3741 * If there an AEN pending already (aen_cmd), check if the
3742 * class_locale of that pending AEN is inclusive of the new
3743 * AEN request we currently have. If it is, then we don't have
3744 * to do anything. In other words, whichever events the current
3745 * AEN request is subscribing to, have already been subscribed
3748 * If the old_cmd is _not_ inclusive, then we have to abort
3749 * that command, form a class_locale that is superset of both
3750 * old and current and re-issue to the FW
3753 curr_aen
.word
= class_locale_word
;
3755 if (instance
->aen_cmd
) {
3757 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
3760 * A class whose enum value is smaller is inclusive of all
3761 * higher values. If a PROGRESS (= -1) was previously
3762 * registered, then a new registration requests for higher
3763 * classes need not be sent to FW. They are automatically
3766 * Locale numbers don't have such hierarchy. They are bitmap
3769 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
3770 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
3771 curr_aen
.members
.locale
)) {
3773 * Previously issued event registration includes
3774 * current request. Nothing to do.
3778 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
3780 if (prev_aen
.members
.class < curr_aen
.members
.class)
3781 curr_aen
.members
.class = prev_aen
.members
.class;
3783 instance
->aen_cmd
->abort_aen
= 1;
3784 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
3789 printk(KERN_DEBUG
"megasas: Failed to abort "
3790 "previous AEN command\n");
3796 cmd
= megasas_get_cmd(instance
);
3801 dcmd
= &cmd
->frame
->dcmd
;
3803 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
3806 * Prepare DCMD for aen registration
3808 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3810 dcmd
->cmd
= MFI_CMD_DCMD
;
3811 dcmd
->cmd_status
= 0x0;
3812 dcmd
->sge_count
= 1;
3813 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3816 instance
->last_seq_num
= seq_num
;
3817 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
3818 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
3819 dcmd
->mbox
.w
[0] = seq_num
;
3820 dcmd
->mbox
.w
[1] = curr_aen
.word
;
3821 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
3822 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
3824 if (instance
->aen_cmd
!= NULL
) {
3825 megasas_return_cmd(instance
, cmd
);
3830 * Store reference to the cmd used to register for AEN. When an
3831 * application wants us to register for AEN, we have to abort this
3832 * cmd and re-register with a new EVENT LOCALE supplied by that app
3834 instance
->aen_cmd
= cmd
;
3837 * Issue the aen registration frame
3839 instance
->instancet
->issue_dcmd(instance
, cmd
);
3845 * megasas_start_aen - Subscribes to AEN during driver load time
3846 * @instance: Adapter soft state
3848 static int megasas_start_aen(struct megasas_instance
*instance
)
3850 struct megasas_evt_log_info eli
;
3851 union megasas_evt_class_locale class_locale
;
3854 * Get the latest sequence number from FW
3856 memset(&eli
, 0, sizeof(eli
));
3858 if (megasas_get_seq_num(instance
, &eli
))
3862 * Register AEN with FW for latest sequence number plus 1
3864 class_locale
.members
.reserved
= 0;
3865 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
3866 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
3868 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
3873 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3874 * @instance: Adapter soft state
3876 static int megasas_io_attach(struct megasas_instance
*instance
)
3878 struct Scsi_Host
*host
= instance
->host
;
3881 * Export parameters required by SCSI mid-layer
3883 host
->irq
= instance
->pdev
->irq
;
3884 host
->unique_id
= instance
->unique_id
;
3885 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3886 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
3888 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
3891 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
3892 host
->this_id
= instance
->init_id
;
3893 host
->sg_tablesize
= instance
->max_num_sge
;
3895 if (instance
->fw_support_ieee
)
3896 instance
->max_sectors_per_req
= MEGASAS_MAX_SECTORS_IEEE
;
3899 * Check if the module parameter value for max_sectors can be used
3901 if (max_sectors
&& max_sectors
< instance
->max_sectors_per_req
)
3902 instance
->max_sectors_per_req
= max_sectors
;
3905 if (((instance
->pdev
->device
==
3906 PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
3907 (instance
->pdev
->device
==
3908 PCI_DEVICE_ID_LSI_SAS0079GEN2
)) &&
3909 (max_sectors
<= MEGASAS_MAX_SECTORS
)) {
3910 instance
->max_sectors_per_req
= max_sectors
;
3912 printk(KERN_INFO
"megasas: max_sectors should be > 0"
3913 "and <= %d (or < 1MB for GEN2 controller)\n",
3914 instance
->max_sectors_per_req
);
3919 host
->max_sectors
= instance
->max_sectors_per_req
;
3920 host
->cmd_per_lun
= MEGASAS_DEFAULT_CMD_PER_LUN
;
3921 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
3922 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
3923 host
->max_lun
= MEGASAS_MAX_LUN
;
3924 host
->max_cmd_len
= 16;
3926 /* Fusion only supports host reset */
3927 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
3928 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)) {
3929 host
->hostt
->eh_device_reset_handler
= NULL
;
3930 host
->hostt
->eh_bus_reset_handler
= NULL
;
3934 * Notify the mid-layer about the new controller
3936 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
3937 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
3942 * Trigger SCSI to scan our drives
3944 scsi_scan_host(host
);
3949 megasas_set_dma_mask(struct pci_dev
*pdev
)
3952 * All our contollers are capable of performing 64-bit DMA
3955 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
3957 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3958 goto fail_set_dma_mask
;
3961 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3962 goto fail_set_dma_mask
;
3971 * megasas_probe_one - PCI hotplug entry point
3972 * @pdev: PCI device structure
3973 * @id: PCI ids of supported hotplugged adapter
3975 static int __devinit
3976 megasas_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3978 int rval
, pos
, i
, j
;
3979 struct Scsi_Host
*host
;
3980 struct megasas_instance
*instance
;
3983 /* Reset MSI-X in the kdump kernel */
3984 if (reset_devices
) {
3985 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
3987 pci_read_config_word(pdev
, msi_control_reg(pos
),
3989 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
3990 dev_info(&pdev
->dev
, "resetting MSI-X\n");
3991 pci_write_config_word(pdev
,
3992 msi_control_reg(pos
),
3994 ~PCI_MSIX_FLAGS_ENABLE
);
4000 * Announce PCI information
4002 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4003 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
4004 pdev
->subsystem_device
);
4006 printk("bus %d:slot %d:func %d\n",
4007 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
4010 * PCI prepping: enable device set bus mastering and dma mask
4012 rval
= pci_enable_device_mem(pdev
);
4018 pci_set_master(pdev
);
4020 if (megasas_set_dma_mask(pdev
))
4021 goto fail_set_dma_mask
;
4023 host
= scsi_host_alloc(&megasas_template
,
4024 sizeof(struct megasas_instance
));
4027 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
4028 goto fail_alloc_instance
;
4031 instance
= (struct megasas_instance
*)host
->hostdata
;
4032 memset(instance
, 0, sizeof(*instance
));
4033 atomic_set( &instance
->fw_reset_no_pci_access
, 0 );
4034 instance
->pdev
= pdev
;
4036 switch (instance
->pdev
->device
) {
4037 case PCI_DEVICE_ID_LSI_FUSION
:
4038 case PCI_DEVICE_ID_LSI_INVADER
:
4040 struct fusion_context
*fusion
;
4042 instance
->ctrl_context
=
4043 kzalloc(sizeof(struct fusion_context
), GFP_KERNEL
);
4044 if (!instance
->ctrl_context
) {
4045 printk(KERN_DEBUG
"megasas: Failed to allocate "
4046 "memory for Fusion context info\n");
4047 goto fail_alloc_dma_buf
;
4049 fusion
= instance
->ctrl_context
;
4050 INIT_LIST_HEAD(&fusion
->cmd_pool
);
4051 spin_lock_init(&fusion
->cmd_pool_lock
);
4054 default: /* For all other supported controllers */
4056 instance
->producer
=
4057 pci_alloc_consistent(pdev
, sizeof(u32
),
4058 &instance
->producer_h
);
4059 instance
->consumer
=
4060 pci_alloc_consistent(pdev
, sizeof(u32
),
4061 &instance
->consumer_h
);
4063 if (!instance
->producer
|| !instance
->consumer
) {
4064 printk(KERN_DEBUG
"megasas: Failed to allocate"
4065 "memory for producer, consumer\n");
4066 goto fail_alloc_dma_buf
;
4069 *instance
->producer
= 0;
4070 *instance
->consumer
= 0;
4074 megasas_poll_wait_aen
= 0;
4075 instance
->flag_ieee
= 0;
4076 instance
->ev
= NULL
;
4077 instance
->issuepend_done
= 1;
4078 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
4079 megasas_poll_wait_aen
= 0;
4081 instance
->evt_detail
= pci_alloc_consistent(pdev
,
4083 megasas_evt_detail
),
4084 &instance
->evt_detail_h
);
4086 if (!instance
->evt_detail
) {
4087 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
4088 "event detail structure\n");
4089 goto fail_alloc_dma_buf
;
4093 * Initialize locks and queues
4095 INIT_LIST_HEAD(&instance
->cmd_pool
);
4096 INIT_LIST_HEAD(&instance
->internal_reset_pending_q
);
4098 atomic_set(&instance
->fw_outstanding
,0);
4100 init_waitqueue_head(&instance
->int_cmd_wait_q
);
4101 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
4103 spin_lock_init(&instance
->cmd_pool_lock
);
4104 spin_lock_init(&instance
->hba_lock
);
4105 spin_lock_init(&instance
->completion_lock
);
4107 mutex_init(&instance
->aen_mutex
);
4108 mutex_init(&instance
->reset_mutex
);
4111 * Initialize PCI related and misc parameters
4113 instance
->host
= host
;
4114 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
4115 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
4117 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
4118 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
4119 instance
->flag_ieee
= 1;
4120 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
4122 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
4124 megasas_dbg_lvl
= 0;
4126 instance
->unload
= 1;
4127 instance
->last_time
= 0;
4128 instance
->disableOnlineCtrlReset
= 1;
4130 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4131 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
))
4132 INIT_WORK(&instance
->work_init
, megasas_fusion_ocr_wq
);
4134 INIT_WORK(&instance
->work_init
, process_fw_state_change_wq
);
4137 * Initialize MFI Firmware
4139 if (megasas_init_fw(instance
))
4145 if (instance
->msix_vectors
) {
4146 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4147 instance
->irq_context
[i
].instance
= instance
;
4148 instance
->irq_context
[i
].MSIxIndex
= i
;
4149 if (request_irq(instance
->msixentry
[i
].vector
,
4150 instance
->instancet
->service_isr
, 0,
4152 &instance
->irq_context
[i
])) {
4153 printk(KERN_DEBUG
"megasas: Failed to "
4154 "register IRQ for vector %d.\n", i
);
4155 for (j
= 0 ; j
< i
; j
++)
4157 instance
->msixentry
[j
].vector
,
4158 &instance
->irq_context
[j
]);
4163 instance
->irq_context
[0].instance
= instance
;
4164 instance
->irq_context
[0].MSIxIndex
= 0;
4165 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4166 IRQF_SHARED
, "megasas",
4167 &instance
->irq_context
[0])) {
4168 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4173 instance
->instancet
->enable_intr(instance
->reg_set
);
4176 * Store instance in PCI softstate
4178 pci_set_drvdata(pdev
, instance
);
4181 * Add this controller to megasas_mgmt_info structure so that it
4182 * can be exported to management applications
4184 megasas_mgmt_info
.count
++;
4185 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
4186 megasas_mgmt_info
.max_index
++;
4189 * Register with SCSI mid-layer
4191 if (megasas_io_attach(instance
))
4192 goto fail_io_attach
;
4194 instance
->unload
= 0;
4197 * Initiate AEN (Asynchronous Event Notification)
4199 if (megasas_start_aen(instance
)) {
4200 printk(KERN_DEBUG
"megasas: start aen failed\n");
4201 goto fail_start_aen
;
4208 megasas_mgmt_info
.count
--;
4209 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
4210 megasas_mgmt_info
.max_index
--;
4212 pci_set_drvdata(pdev
, NULL
);
4213 instance
->instancet
->disable_intr(instance
->reg_set
);
4214 if (instance
->msix_vectors
)
4215 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4216 free_irq(instance
->msixentry
[i
].vector
,
4217 &instance
->irq_context
[i
]);
4219 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4221 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4222 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
))
4223 megasas_release_fusion(instance
);
4225 megasas_release_mfi(instance
);
4227 if (instance
->msix_vectors
)
4228 pci_disable_msix(instance
->pdev
);
4230 if (instance
->evt_detail
)
4231 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4232 instance
->evt_detail
,
4233 instance
->evt_detail_h
);
4235 if (instance
->producer
)
4236 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4237 instance
->producer_h
);
4238 if (instance
->consumer
)
4239 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4240 instance
->consumer_h
);
4241 scsi_host_put(host
);
4243 fail_alloc_instance
:
4245 pci_disable_device(pdev
);
4251 * megasas_flush_cache - Requests FW to flush all its caches
4252 * @instance: Adapter soft state
4254 static void megasas_flush_cache(struct megasas_instance
*instance
)
4256 struct megasas_cmd
*cmd
;
4257 struct megasas_dcmd_frame
*dcmd
;
4259 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4262 cmd
= megasas_get_cmd(instance
);
4267 dcmd
= &cmd
->frame
->dcmd
;
4269 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4271 dcmd
->cmd
= MFI_CMD_DCMD
;
4272 dcmd
->cmd_status
= 0x0;
4273 dcmd
->sge_count
= 0;
4274 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4277 dcmd
->data_xfer_len
= 0;
4278 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
4279 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
4281 megasas_issue_blocked_cmd(instance
, cmd
);
4283 megasas_return_cmd(instance
, cmd
);
4289 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4290 * @instance: Adapter soft state
4291 * @opcode: Shutdown/Hibernate
4293 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
4296 struct megasas_cmd
*cmd
;
4297 struct megasas_dcmd_frame
*dcmd
;
4299 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4302 cmd
= megasas_get_cmd(instance
);
4307 if (instance
->aen_cmd
)
4308 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
4309 if (instance
->map_update_cmd
)
4310 megasas_issue_blocked_abort_cmd(instance
,
4311 instance
->map_update_cmd
);
4312 dcmd
= &cmd
->frame
->dcmd
;
4314 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4316 dcmd
->cmd
= MFI_CMD_DCMD
;
4317 dcmd
->cmd_status
= 0x0;
4318 dcmd
->sge_count
= 0;
4319 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4322 dcmd
->data_xfer_len
= 0;
4323 dcmd
->opcode
= opcode
;
4325 megasas_issue_blocked_cmd(instance
, cmd
);
4327 megasas_return_cmd(instance
, cmd
);
4334 * megasas_suspend - driver suspend entry point
4335 * @pdev: PCI device structure
4336 * @state: PCI power state to suspend routine
4339 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
4341 struct Scsi_Host
*host
;
4342 struct megasas_instance
*instance
;
4345 instance
= pci_get_drvdata(pdev
);
4346 host
= instance
->host
;
4347 instance
->unload
= 1;
4349 megasas_flush_cache(instance
);
4350 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
4352 /* cancel the delayed work if this work still in queue */
4353 if (instance
->ev
!= NULL
) {
4354 struct megasas_aen_event
*ev
= instance
->ev
;
4355 cancel_delayed_work_sync(
4356 (struct delayed_work
*)&ev
->hotplug_work
);
4357 instance
->ev
= NULL
;
4360 tasklet_kill(&instance
->isr_tasklet
);
4362 pci_set_drvdata(instance
->pdev
, instance
);
4363 instance
->instancet
->disable_intr(instance
->reg_set
);
4365 if (instance
->msix_vectors
)
4366 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4367 free_irq(instance
->msixentry
[i
].vector
,
4368 &instance
->irq_context
[i
]);
4370 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4371 if (instance
->msix_vectors
)
4372 pci_disable_msix(instance
->pdev
);
4374 pci_save_state(pdev
);
4375 pci_disable_device(pdev
);
4377 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
4383 * megasas_resume- driver resume entry point
4384 * @pdev: PCI device structure
4387 megasas_resume(struct pci_dev
*pdev
)
4390 struct Scsi_Host
*host
;
4391 struct megasas_instance
*instance
;
4393 instance
= pci_get_drvdata(pdev
);
4394 host
= instance
->host
;
4395 pci_set_power_state(pdev
, PCI_D0
);
4396 pci_enable_wake(pdev
, PCI_D0
, 0);
4397 pci_restore_state(pdev
);
4400 * PCI prepping: enable device set bus mastering and dma mask
4402 rval
= pci_enable_device_mem(pdev
);
4405 printk(KERN_ERR
"megasas: Enable device failed\n");
4409 pci_set_master(pdev
);
4411 if (megasas_set_dma_mask(pdev
))
4412 goto fail_set_dma_mask
;
4415 * Initialize MFI Firmware
4418 atomic_set(&instance
->fw_outstanding
, 0);
4421 * We expect the FW state to be READY
4423 if (megasas_transition_to_ready(instance
, 0))
4424 goto fail_ready_state
;
4426 /* Now re-enable MSI-X */
4427 if (instance
->msix_vectors
)
4428 pci_enable_msix(instance
->pdev
, instance
->msixentry
,
4429 instance
->msix_vectors
);
4431 switch (instance
->pdev
->device
) {
4432 case PCI_DEVICE_ID_LSI_FUSION
:
4433 case PCI_DEVICE_ID_LSI_INVADER
:
4435 megasas_reset_reply_desc(instance
);
4436 if (megasas_ioc_init_fusion(instance
)) {
4437 megasas_free_cmds(instance
);
4438 megasas_free_cmds_fusion(instance
);
4441 if (!megasas_get_map_info(instance
))
4442 megasas_sync_map_info(instance
);
4446 *instance
->producer
= 0;
4447 *instance
->consumer
= 0;
4448 if (megasas_issue_init_mfi(instance
))
4453 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
4454 (unsigned long)instance
);
4459 if (instance
->msix_vectors
) {
4460 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4461 instance
->irq_context
[i
].instance
= instance
;
4462 instance
->irq_context
[i
].MSIxIndex
= i
;
4463 if (request_irq(instance
->msixentry
[i
].vector
,
4464 instance
->instancet
->service_isr
, 0,
4466 &instance
->irq_context
[i
])) {
4467 printk(KERN_DEBUG
"megasas: Failed to "
4468 "register IRQ for vector %d.\n", i
);
4469 for (j
= 0 ; j
< i
; j
++)
4471 instance
->msixentry
[j
].vector
,
4472 &instance
->irq_context
[j
]);
4477 instance
->irq_context
[0].instance
= instance
;
4478 instance
->irq_context
[0].MSIxIndex
= 0;
4479 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4480 IRQF_SHARED
, "megasas",
4481 &instance
->irq_context
[0])) {
4482 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4487 instance
->instancet
->enable_intr(instance
->reg_set
);
4488 instance
->unload
= 0;
4491 * Initiate AEN (Asynchronous Event Notification)
4493 if (megasas_start_aen(instance
))
4494 printk(KERN_ERR
"megasas: Start AEN failed\n");
4500 if (instance
->evt_detail
)
4501 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4502 instance
->evt_detail
,
4503 instance
->evt_detail_h
);
4505 if (instance
->producer
)
4506 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4507 instance
->producer_h
);
4508 if (instance
->consumer
)
4509 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4510 instance
->consumer_h
);
4511 scsi_host_put(host
);
4516 pci_disable_device(pdev
);
4521 #define megasas_suspend NULL
4522 #define megasas_resume NULL
4526 * megasas_detach_one - PCI hot"un"plug entry point
4527 * @pdev: PCI device structure
4529 static void __devexit
megasas_detach_one(struct pci_dev
*pdev
)
4532 struct Scsi_Host
*host
;
4533 struct megasas_instance
*instance
;
4534 struct fusion_context
*fusion
;
4536 instance
= pci_get_drvdata(pdev
);
4537 instance
->unload
= 1;
4538 host
= instance
->host
;
4539 fusion
= instance
->ctrl_context
;
4541 scsi_remove_host(instance
->host
);
4542 megasas_flush_cache(instance
);
4543 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4545 /* cancel the delayed work if this work still in queue*/
4546 if (instance
->ev
!= NULL
) {
4547 struct megasas_aen_event
*ev
= instance
->ev
;
4548 cancel_delayed_work_sync(
4549 (struct delayed_work
*)&ev
->hotplug_work
);
4550 instance
->ev
= NULL
;
4553 tasklet_kill(&instance
->isr_tasklet
);
4556 * Take the instance off the instance array. Note that we will not
4557 * decrement the max_index. We let this array be sparse array
4559 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
4560 if (megasas_mgmt_info
.instance
[i
] == instance
) {
4561 megasas_mgmt_info
.count
--;
4562 megasas_mgmt_info
.instance
[i
] = NULL
;
4568 pci_set_drvdata(instance
->pdev
, NULL
);
4570 instance
->instancet
->disable_intr(instance
->reg_set
);
4572 if (instance
->msix_vectors
)
4573 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4574 free_irq(instance
->msixentry
[i
].vector
,
4575 &instance
->irq_context
[i
]);
4577 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4578 if (instance
->msix_vectors
)
4579 pci_disable_msix(instance
->pdev
);
4581 switch (instance
->pdev
->device
) {
4582 case PCI_DEVICE_ID_LSI_FUSION
:
4583 case PCI_DEVICE_ID_LSI_INVADER
:
4584 megasas_release_fusion(instance
);
4585 for (i
= 0; i
< 2 ; i
++)
4586 if (fusion
->ld_map
[i
])
4587 dma_free_coherent(&instance
->pdev
->dev
,
4592 kfree(instance
->ctrl_context
);
4595 megasas_release_mfi(instance
);
4596 pci_free_consistent(pdev
,
4597 sizeof(struct megasas_evt_detail
),
4598 instance
->evt_detail
,
4599 instance
->evt_detail_h
);
4600 pci_free_consistent(pdev
, sizeof(u32
),
4602 instance
->producer_h
);
4603 pci_free_consistent(pdev
, sizeof(u32
),
4605 instance
->consumer_h
);
4609 scsi_host_put(host
);
4611 pci_set_drvdata(pdev
, NULL
);
4613 pci_disable_device(pdev
);
4619 * megasas_shutdown - Shutdown entry point
4620 * @device: Generic device structure
4622 static void megasas_shutdown(struct pci_dev
*pdev
)
4625 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
4627 instance
->unload
= 1;
4628 megasas_flush_cache(instance
);
4629 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4630 instance
->instancet
->disable_intr(instance
->reg_set
);
4631 if (instance
->msix_vectors
)
4632 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4633 free_irq(instance
->msixentry
[i
].vector
,
4634 &instance
->irq_context
[i
]);
4636 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4637 if (instance
->msix_vectors
)
4638 pci_disable_msix(instance
->pdev
);
4642 * megasas_mgmt_open - char node "open" entry point
4644 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
4647 * Allow only those users with admin rights
4649 if (!capable(CAP_SYS_ADMIN
))
4656 * megasas_mgmt_fasync - Async notifier registration from applications
4658 * This function adds the calling process to a driver global queue. When an
4659 * event occurs, SIGIO will be sent to all processes in this queue.
4661 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
4665 mutex_lock(&megasas_async_queue_mutex
);
4667 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
4669 mutex_unlock(&megasas_async_queue_mutex
);
4672 /* For sanity check when we get ioctl */
4673 filep
->private_data
= filep
;
4677 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
4683 * megasas_mgmt_poll - char node "poll" entry point
4685 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
4688 unsigned long flags
;
4689 poll_wait(file
, &megasas_poll_wait
, wait
);
4690 spin_lock_irqsave(&poll_aen_lock
, flags
);
4691 if (megasas_poll_wait_aen
)
4692 mask
= (POLLIN
| POLLRDNORM
);
4695 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
4700 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4701 * @instance: Adapter soft state
4702 * @argp: User's ioctl packet
4705 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
4706 struct megasas_iocpacket __user
* user_ioc
,
4707 struct megasas_iocpacket
*ioc
)
4709 struct megasas_sge32
*kern_sge32
;
4710 struct megasas_cmd
*cmd
;
4711 void *kbuff_arr
[MAX_IOCTL_SGE
];
4712 dma_addr_t buf_handle
= 0;
4715 dma_addr_t sense_handle
;
4716 unsigned long *sense_ptr
;
4718 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
4720 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
4721 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
4722 ioc
->sge_count
, MAX_IOCTL_SGE
);
4726 cmd
= megasas_get_cmd(instance
);
4728 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
4733 * User's IOCTL packet has 2 frames (maximum). Copy those two
4734 * frames into our cmd's frames. cmd->frame's context will get
4735 * overwritten when we copy from user's frames. So set that value
4738 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
4739 cmd
->frame
->hdr
.context
= cmd
->index
;
4740 cmd
->frame
->hdr
.pad_0
= 0;
4741 cmd
->frame
->hdr
.flags
&= ~(MFI_FRAME_IEEE
| MFI_FRAME_SGL64
|
4745 * The management interface between applications and the fw uses
4746 * MFI frames. E.g, RAID configuration changes, LD property changes
4747 * etc are accomplishes through different kinds of MFI frames. The
4748 * driver needs to care only about substituting user buffers with
4749 * kernel buffers in SGLs. The location of SGL is embedded in the
4750 * struct iocpacket itself.
4752 kern_sge32
= (struct megasas_sge32
*)
4753 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
4756 * For each user buffer, create a mirror buffer and copy in
4758 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4759 if (!ioc
->sgl
[i
].iov_len
)
4762 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
4763 ioc
->sgl
[i
].iov_len
,
4764 &buf_handle
, GFP_KERNEL
);
4765 if (!kbuff_arr
[i
]) {
4766 printk(KERN_DEBUG
"megasas: Failed to alloc "
4767 "kernel SGL buffer for IOCTL \n");
4773 * We don't change the dma_coherent_mask, so
4774 * pci_alloc_consistent only returns 32bit addresses
4776 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
4777 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
4780 * We created a kernel buffer corresponding to the
4781 * user buffer. Now copy in from the user buffer
4783 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
4784 (u32
) (ioc
->sgl
[i
].iov_len
))) {
4790 if (ioc
->sense_len
) {
4791 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4792 &sense_handle
, GFP_KERNEL
);
4799 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
4800 *sense_ptr
= sense_handle
;
4804 * Set the sync_cmd flag so that the ISR knows not to complete this
4805 * cmd to the SCSI mid-layer
4808 megasas_issue_blocked_cmd(instance
, cmd
);
4812 * copy out the kernel buffers to user buffers
4814 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4815 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
4816 ioc
->sgl
[i
].iov_len
)) {
4823 * copy out the sense
4825 if (ioc
->sense_len
) {
4827 * sense_ptr points to the location that has the user
4828 * sense buffer address
4830 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
4833 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
4834 sense
, ioc
->sense_len
)) {
4835 printk(KERN_ERR
"megasas: Failed to copy out to user "
4843 * copy the status codes returned by the fw
4845 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
4846 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
4847 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
4853 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4854 sense
, sense_handle
);
4857 for (i
= 0; i
< ioc
->sge_count
&& kbuff_arr
[i
]; i
++) {
4858 dma_free_coherent(&instance
->pdev
->dev
,
4859 kern_sge32
[i
].length
,
4860 kbuff_arr
[i
], kern_sge32
[i
].phys_addr
);
4863 megasas_return_cmd(instance
, cmd
);
4867 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
4869 struct megasas_iocpacket __user
*user_ioc
=
4870 (struct megasas_iocpacket __user
*)arg
;
4871 struct megasas_iocpacket
*ioc
;
4872 struct megasas_instance
*instance
;
4875 unsigned long flags
;
4876 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
4878 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
4882 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
4887 instance
= megasas_lookup_instance(ioc
->host_no
);
4893 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
4894 printk(KERN_ERR
"Controller in crit error\n");
4899 if (instance
->unload
== 1) {
4905 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4907 if (down_interruptible(&instance
->ioctl_sem
)) {
4908 error
= -ERESTARTSYS
;
4912 for (i
= 0; i
< wait_time
; i
++) {
4914 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4915 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
4916 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4919 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4921 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
4922 printk(KERN_NOTICE
"megasas: waiting"
4923 "for controller reset to finish\n");
4929 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4930 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
4931 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4933 printk(KERN_ERR
"megaraid_sas: timed out while"
4934 "waiting for HBA to recover\n");
4938 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4940 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
4941 up(&instance
->ioctl_sem
);
4948 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
4950 struct megasas_instance
*instance
;
4951 struct megasas_aen aen
;
4954 unsigned long flags
;
4955 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
4957 if (file
->private_data
!= file
) {
4958 printk(KERN_DEBUG
"megasas: fasync_helper was not "
4963 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
4966 instance
= megasas_lookup_instance(aen
.host_no
);
4971 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
4975 if (instance
->unload
== 1) {
4979 for (i
= 0; i
< wait_time
; i
++) {
4981 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4982 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
4983 spin_unlock_irqrestore(&instance
->hba_lock
,
4988 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4990 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
4991 printk(KERN_NOTICE
"megasas: waiting for"
4992 "controller reset to finish\n");
4998 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4999 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5000 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5001 printk(KERN_ERR
"megaraid_sas: timed out while waiting"
5002 "for HBA to recover.\n");
5005 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5007 mutex_lock(&instance
->aen_mutex
);
5008 error
= megasas_register_aen(instance
, aen
.seq_num
,
5009 aen
.class_locale_word
);
5010 mutex_unlock(&instance
->aen_mutex
);
5015 * megasas_mgmt_ioctl - char node ioctl entry point
5018 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5021 case MEGASAS_IOC_FIRMWARE
:
5022 return megasas_mgmt_ioctl_fw(file
, arg
);
5024 case MEGASAS_IOC_GET_AEN
:
5025 return megasas_mgmt_ioctl_aen(file
, arg
);
5031 #ifdef CONFIG_COMPAT
5032 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
5034 struct compat_megasas_iocpacket __user
*cioc
=
5035 (struct compat_megasas_iocpacket __user
*)arg
;
5036 struct megasas_iocpacket __user
*ioc
=
5037 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
5042 if (clear_user(ioc
, sizeof(*ioc
)))
5045 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
5046 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
5047 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
5048 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
5049 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
5050 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
5054 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5055 * sense_len is not null, so prepare the 64bit value under
5056 * the same condition.
5058 if (ioc
->sense_len
) {
5059 void __user
**sense_ioc_ptr
=
5060 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
5061 compat_uptr_t
*sense_cioc_ptr
=
5062 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
5063 if (get_user(ptr
, sense_cioc_ptr
) ||
5064 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
5068 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
5069 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
5070 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
5071 copy_in_user(&ioc
->sgl
[i
].iov_len
,
5072 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
5076 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
5078 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
5079 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
5080 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
5087 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
5091 case MEGASAS_IOC_FIRMWARE32
:
5092 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
5093 case MEGASAS_IOC_GET_AEN
:
5094 return megasas_mgmt_ioctl_aen(file
, arg
);
5102 * File operations structure for management interface
5104 static const struct file_operations megasas_mgmt_fops
= {
5105 .owner
= THIS_MODULE
,
5106 .open
= megasas_mgmt_open
,
5107 .fasync
= megasas_mgmt_fasync
,
5108 .unlocked_ioctl
= megasas_mgmt_ioctl
,
5109 .poll
= megasas_mgmt_poll
,
5110 #ifdef CONFIG_COMPAT
5111 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
5113 .llseek
= noop_llseek
,
5117 * PCI hotplug support registration structure
5119 static struct pci_driver megasas_pci_driver
= {
5121 .name
= "megaraid_sas",
5122 .id_table
= megasas_pci_table
,
5123 .probe
= megasas_probe_one
,
5124 .remove
= __devexit_p(megasas_detach_one
),
5125 .suspend
= megasas_suspend
,
5126 .resume
= megasas_resume
,
5127 .shutdown
= megasas_shutdown
,
5131 * Sysfs driver attributes
5133 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
5135 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
5139 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
5142 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
5144 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
5148 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
5152 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
5154 return sprintf(buf
, "%u\n", support_poll_for_event
);
5157 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
5158 megasas_sysfs_show_support_poll_for_event
, NULL
);
5161 megasas_sysfs_show_support_device_change(struct device_driver
*dd
, char *buf
)
5163 return sprintf(buf
, "%u\n", support_device_change
);
5166 static DRIVER_ATTR(support_device_change
, S_IRUGO
,
5167 megasas_sysfs_show_support_device_change
, NULL
);
5170 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
5172 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
5176 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
5179 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
5180 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
5186 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
5187 megasas_sysfs_set_dbg_lvl
);
5190 megasas_aen_polling(struct work_struct
*work
)
5192 struct megasas_aen_event
*ev
=
5193 container_of(work
, struct megasas_aen_event
, hotplug_work
);
5194 struct megasas_instance
*instance
= ev
->instance
;
5195 union megasas_evt_class_locale class_locale
;
5196 struct Scsi_Host
*host
;
5197 struct scsi_device
*sdev1
;
5200 int i
, j
, doscan
= 0;
5205 printk(KERN_ERR
"invalid instance!\n");
5209 instance
->ev
= NULL
;
5210 host
= instance
->host
;
5211 if (instance
->evt_detail
) {
5213 switch (instance
->evt_detail
->code
) {
5214 case MR_EVT_PD_INSERTED
:
5215 if (megasas_get_pd_list(instance
) == 0) {
5216 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5218 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5222 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5225 scsi_device_lookup(host
, i
, j
, 0);
5227 if (instance
->pd_list
[pd_index
].driveState
5228 == MR_PD_STATE_SYSTEM
) {
5230 scsi_add_device(host
, i
, j
, 0);
5234 scsi_device_put(sdev1
);
5242 case MR_EVT_PD_REMOVED
:
5243 if (megasas_get_pd_list(instance
) == 0) {
5244 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5246 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5250 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5253 scsi_device_lookup(host
, i
, j
, 0);
5255 if (instance
->pd_list
[pd_index
].driveState
5256 == MR_PD_STATE_SYSTEM
) {
5258 scsi_device_put(sdev1
);
5262 scsi_remove_device(sdev1
);
5263 scsi_device_put(sdev1
);
5272 case MR_EVT_LD_OFFLINE
:
5273 case MR_EVT_CFG_CLEARED
:
5274 case MR_EVT_LD_DELETED
:
5275 megasas_get_ld_list(instance
);
5276 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5278 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5282 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5284 sdev1
= scsi_device_lookup(host
,
5285 i
+ MEGASAS_MAX_LD_CHANNELS
,
5289 if (instance
->ld_ids
[ld_index
] != 0xff) {
5291 scsi_device_put(sdev1
);
5295 scsi_remove_device(sdev1
);
5296 scsi_device_put(sdev1
);
5303 case MR_EVT_LD_CREATED
:
5304 megasas_get_ld_list(instance
);
5305 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5307 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5310 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5312 sdev1
= scsi_device_lookup(host
,
5313 i
+MEGASAS_MAX_LD_CHANNELS
,
5316 if (instance
->ld_ids
[ld_index
] !=
5319 scsi_add_device(host
,
5325 scsi_device_put(sdev1
);
5331 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
5332 case MR_EVT_FOREIGN_CFG_IMPORTED
:
5333 case MR_EVT_LD_STATE_CHANGE
:
5341 printk(KERN_ERR
"invalid evt_detail!\n");
5347 printk(KERN_INFO
"scanning ...\n");
5348 megasas_get_pd_list(instance
);
5349 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5350 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5351 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
5352 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
5353 if (instance
->pd_list
[pd_index
].driveState
==
5354 MR_PD_STATE_SYSTEM
) {
5356 scsi_add_device(host
, i
, j
, 0);
5359 scsi_device_put(sdev1
);
5362 scsi_remove_device(sdev1
);
5363 scsi_device_put(sdev1
);
5369 megasas_get_ld_list(instance
);
5370 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5371 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5373 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5375 sdev1
= scsi_device_lookup(host
,
5376 i
+MEGASAS_MAX_LD_CHANNELS
, j
, 0);
5377 if (instance
->ld_ids
[ld_index
] != 0xff) {
5379 scsi_add_device(host
,
5383 scsi_device_put(sdev1
);
5387 scsi_remove_device(sdev1
);
5388 scsi_device_put(sdev1
);
5395 if ( instance
->aen_cmd
!= NULL
) {
5400 seq_num
= instance
->evt_detail
->seq_num
+ 1;
5402 /* Register AEN with FW for latest sequence number plus 1 */
5403 class_locale
.members
.reserved
= 0;
5404 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
5405 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
5406 mutex_lock(&instance
->aen_mutex
);
5407 error
= megasas_register_aen(instance
, seq_num
,
5409 mutex_unlock(&instance
->aen_mutex
);
5412 printk(KERN_ERR
"register aen failed error %x\n", error
);
5418 * megasas_init - Driver load entry point
5420 static int __init
megasas_init(void)
5425 * Announce driver version and other information
5427 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
5428 MEGASAS_EXT_VERSION
);
5430 spin_lock_init(&poll_aen_lock
);
5432 support_poll_for_event
= 2;
5433 support_device_change
= 1;
5435 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
5438 * Register character device node
5440 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
5443 printk(KERN_DEBUG
"megasas: failed to open device node\n");
5447 megasas_mgmt_majorno
= rval
;
5450 * Register ourselves as PCI hotplug module
5452 rval
= pci_register_driver(&megasas_pci_driver
);
5455 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
5459 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5460 &driver_attr_version
);
5462 goto err_dcf_attr_ver
;
5463 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5464 &driver_attr_release_date
);
5466 goto err_dcf_rel_date
;
5468 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5469 &driver_attr_support_poll_for_event
);
5471 goto err_dcf_support_poll_for_event
;
5473 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5474 &driver_attr_dbg_lvl
);
5476 goto err_dcf_dbg_lvl
;
5477 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5478 &driver_attr_support_device_change
);
5480 goto err_dcf_support_device_change
;
5484 err_dcf_support_device_change
:
5485 driver_remove_file(&megasas_pci_driver
.driver
,
5486 &driver_attr_dbg_lvl
);
5488 driver_remove_file(&megasas_pci_driver
.driver
,
5489 &driver_attr_support_poll_for_event
);
5491 err_dcf_support_poll_for_event
:
5492 driver_remove_file(&megasas_pci_driver
.driver
,
5493 &driver_attr_release_date
);
5496 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5498 pci_unregister_driver(&megasas_pci_driver
);
5500 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5505 * megasas_exit - Driver unload entry point
5507 static void __exit
megasas_exit(void)
5509 driver_remove_file(&megasas_pci_driver
.driver
,
5510 &driver_attr_dbg_lvl
);
5511 driver_remove_file(&megasas_pci_driver
.driver
,
5512 &driver_attr_support_poll_for_event
);
5513 driver_remove_file(&megasas_pci_driver
.driver
,
5514 &driver_attr_support_device_change
);
5515 driver_remove_file(&megasas_pci_driver
.driver
,
5516 &driver_attr_release_date
);
5517 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5519 pci_unregister_driver(&megasas_pci_driver
);
5520 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5523 module_init(megasas_init
);
5524 module_exit(megasas_exit
);