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.506.00.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
)},
125 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_FURY
)},
130 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
132 static int megasas_mgmt_majorno
;
133 static struct megasas_mgmt_info megasas_mgmt_info
;
134 static struct fasync_struct
*megasas_async_queue
;
135 static DEFINE_MUTEX(megasas_async_queue_mutex
);
137 static int megasas_poll_wait_aen
;
138 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait
);
139 static u32 support_poll_for_event
;
141 static u32 support_device_change
;
143 /* define lock for aen poll */
144 spinlock_t poll_aen_lock
;
147 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
150 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
);
152 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
153 struct megasas_register_set __iomem
*reg_set
);
154 static irqreturn_t
megasas_isr(int irq
, void *devp
);
156 megasas_init_adapter_mfi(struct megasas_instance
*instance
);
158 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
159 struct scsi_cmnd
*scmd
);
160 static void megasas_complete_cmd_dpc(unsigned long instance_addr
);
162 megasas_release_fusion(struct megasas_instance
*instance
);
164 megasas_ioc_init_fusion(struct megasas_instance
*instance
);
166 megasas_free_cmds_fusion(struct megasas_instance
*instance
);
168 megasas_get_map_info(struct megasas_instance
*instance
);
170 megasas_sync_map_info(struct megasas_instance
*instance
);
172 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
);
173 void megasas_reset_reply_desc(struct megasas_instance
*instance
);
174 u8
MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL
*map
,
175 struct LD_LOAD_BALANCE_INFO
*lbInfo
);
176 int megasas_reset_fusion(struct Scsi_Host
*shost
);
177 void megasas_fusion_ocr_wq(struct work_struct
*work
);
180 megasas_issue_dcmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
182 instance
->instancet
->fire_cmd(instance
,
183 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
187 * megasas_get_cmd - Get a command from the free pool
188 * @instance: Adapter soft state
190 * Returns a free command from the pool
192 struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
196 struct megasas_cmd
*cmd
= NULL
;
198 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
200 if (!list_empty(&instance
->cmd_pool
)) {
201 cmd
= list_entry((&instance
->cmd_pool
)->next
,
202 struct megasas_cmd
, list
);
203 list_del_init(&cmd
->list
);
205 printk(KERN_ERR
"megasas: Command pool empty!\n");
208 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
213 * megasas_return_cmd - Return a cmd to free command pool
214 * @instance: Adapter soft state
215 * @cmd: Command packet to be returned to free command pool
218 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
222 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
225 cmd
->frame_count
= 0;
226 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
227 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
228 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
230 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
231 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
233 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
238 * The following functions are defined for xscale
239 * (deviceid : 1064R, PERC5) controllers
243 * megasas_enable_intr_xscale - Enables interrupts
244 * @regs: MFI register set
247 megasas_enable_intr_xscale(struct megasas_register_set __iomem
* regs
)
249 writel(0, &(regs
)->outbound_intr_mask
);
251 /* Dummy readl to force pci flush */
252 readl(®s
->outbound_intr_mask
);
256 * megasas_disable_intr_xscale -Disables interrupt
257 * @regs: MFI register set
260 megasas_disable_intr_xscale(struct megasas_register_set __iomem
* regs
)
263 writel(mask
, ®s
->outbound_intr_mask
);
264 /* Dummy readl to force pci flush */
265 readl(®s
->outbound_intr_mask
);
269 * megasas_read_fw_status_reg_xscale - returns the current FW status value
270 * @regs: MFI register set
273 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
275 return readl(&(regs
)->outbound_msg_0
);
278 * megasas_clear_interrupt_xscale - Check & clear interrupt
279 * @regs: MFI register set
282 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
287 * Check if it is our interrupt
289 status
= readl(®s
->outbound_intr_status
);
291 if (status
& MFI_OB_INTR_STATUS_MASK
)
292 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
293 if (status
& MFI_XSCALE_OMR0_CHANGE_INTERRUPT
)
294 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
297 * Clear the interrupt by writing back the same value
300 writel(status
, ®s
->outbound_intr_status
);
302 /* Dummy readl to force pci flush */
303 readl(®s
->outbound_intr_status
);
309 * megasas_fire_cmd_xscale - Sends command to the FW
310 * @frame_phys_addr : Physical address of cmd
311 * @frame_count : Number of frames for the command
312 * @regs : MFI register set
315 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
316 dma_addr_t frame_phys_addr
,
318 struct megasas_register_set __iomem
*regs
)
321 spin_lock_irqsave(&instance
->hba_lock
, flags
);
322 writel((frame_phys_addr
>> 3)|(frame_count
),
323 &(regs
)->inbound_queue_port
);
324 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
328 * megasas_adp_reset_xscale - For controller reset
329 * @regs: MFI register set
332 megasas_adp_reset_xscale(struct megasas_instance
*instance
,
333 struct megasas_register_set __iomem
*regs
)
337 writel(MFI_ADP_RESET
, ®s
->inbound_doorbell
);
339 for (i
= 0; i
< 3; i
++)
340 msleep(1000); /* sleep for 3 secs */
342 pci_read_config_dword(instance
->pdev
, MFI_1068_PCSR_OFFSET
, &pcidata
);
343 printk(KERN_NOTICE
"pcidata = %x\n", pcidata
);
345 printk(KERN_NOTICE
"mfi 1068 offset read=%x\n", pcidata
);
347 pci_write_config_dword(instance
->pdev
,
348 MFI_1068_PCSR_OFFSET
, pcidata
);
350 for (i
= 0; i
< 2; i
++)
351 msleep(1000); /* need to wait 2 secs again */
354 pci_read_config_dword(instance
->pdev
,
355 MFI_1068_FW_HANDSHAKE_OFFSET
, &pcidata
);
356 printk(KERN_NOTICE
"1068 offset handshake read=%x\n", pcidata
);
357 if ((pcidata
& 0xffff0000) == MFI_1068_FW_READY
) {
358 printk(KERN_NOTICE
"1068 offset pcidt=%x\n", pcidata
);
360 pci_write_config_dword(instance
->pdev
,
361 MFI_1068_FW_HANDSHAKE_OFFSET
, pcidata
);
368 * megasas_check_reset_xscale - For controller reset check
369 * @regs: MFI register set
372 megasas_check_reset_xscale(struct megasas_instance
*instance
,
373 struct megasas_register_set __iomem
*regs
)
376 consumer
= *instance
->consumer
;
378 if ((instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) &&
379 (*instance
->consumer
== MEGASAS_ADPRESET_INPROG_SIGN
)) {
385 static struct megasas_instance_template megasas_instance_template_xscale
= {
387 .fire_cmd
= megasas_fire_cmd_xscale
,
388 .enable_intr
= megasas_enable_intr_xscale
,
389 .disable_intr
= megasas_disable_intr_xscale
,
390 .clear_intr
= megasas_clear_intr_xscale
,
391 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
392 .adp_reset
= megasas_adp_reset_xscale
,
393 .check_reset
= megasas_check_reset_xscale
,
394 .service_isr
= megasas_isr
,
395 .tasklet
= megasas_complete_cmd_dpc
,
396 .init_adapter
= megasas_init_adapter_mfi
,
397 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
398 .issue_dcmd
= megasas_issue_dcmd
,
402 * This is the end of set of functions & definitions specific
403 * to xscale (deviceid : 1064R, PERC5) controllers
407 * The following functions are defined for ppc (deviceid : 0x60)
412 * megasas_enable_intr_ppc - Enables interrupts
413 * @regs: MFI register set
416 megasas_enable_intr_ppc(struct megasas_register_set __iomem
* regs
)
418 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
420 writel(~0x80000000, &(regs
)->outbound_intr_mask
);
422 /* Dummy readl to force pci flush */
423 readl(®s
->outbound_intr_mask
);
427 * megasas_disable_intr_ppc - Disable interrupt
428 * @regs: MFI register set
431 megasas_disable_intr_ppc(struct megasas_register_set __iomem
* regs
)
433 u32 mask
= 0xFFFFFFFF;
434 writel(mask
, ®s
->outbound_intr_mask
);
435 /* Dummy readl to force pci flush */
436 readl(®s
->outbound_intr_mask
);
440 * megasas_read_fw_status_reg_ppc - returns the current FW status value
441 * @regs: MFI register set
444 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
446 return readl(&(regs
)->outbound_scratch_pad
);
450 * megasas_clear_interrupt_ppc - Check & clear interrupt
451 * @regs: MFI register set
454 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
456 u32 status
, mfiStatus
= 0;
459 * Check if it is our interrupt
461 status
= readl(®s
->outbound_intr_status
);
463 if (status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)
464 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
466 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
)
467 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
470 * Clear the interrupt by writing back the same value
472 writel(status
, ®s
->outbound_doorbell_clear
);
474 /* Dummy readl to force pci flush */
475 readl(®s
->outbound_doorbell_clear
);
481 * megasas_fire_cmd_ppc - Sends command to the FW
482 * @frame_phys_addr : Physical address of cmd
483 * @frame_count : Number of frames for the command
484 * @regs : MFI register set
487 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
488 dma_addr_t frame_phys_addr
,
490 struct megasas_register_set __iomem
*regs
)
493 spin_lock_irqsave(&instance
->hba_lock
, flags
);
494 writel((frame_phys_addr
| (frame_count
<<1))|1,
495 &(regs
)->inbound_queue_port
);
496 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
500 * megasas_check_reset_ppc - For controller reset check
501 * @regs: MFI register set
504 megasas_check_reset_ppc(struct megasas_instance
*instance
,
505 struct megasas_register_set __iomem
*regs
)
507 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
513 static struct megasas_instance_template megasas_instance_template_ppc
= {
515 .fire_cmd
= megasas_fire_cmd_ppc
,
516 .enable_intr
= megasas_enable_intr_ppc
,
517 .disable_intr
= megasas_disable_intr_ppc
,
518 .clear_intr
= megasas_clear_intr_ppc
,
519 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
520 .adp_reset
= megasas_adp_reset_xscale
,
521 .check_reset
= megasas_check_reset_ppc
,
522 .service_isr
= megasas_isr
,
523 .tasklet
= megasas_complete_cmd_dpc
,
524 .init_adapter
= megasas_init_adapter_mfi
,
525 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
526 .issue_dcmd
= megasas_issue_dcmd
,
530 * megasas_enable_intr_skinny - Enables interrupts
531 * @regs: MFI register set
534 megasas_enable_intr_skinny(struct megasas_register_set __iomem
*regs
)
536 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
538 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
540 /* Dummy readl to force pci flush */
541 readl(®s
->outbound_intr_mask
);
545 * megasas_disable_intr_skinny - Disables interrupt
546 * @regs: MFI register set
549 megasas_disable_intr_skinny(struct megasas_register_set __iomem
*regs
)
551 u32 mask
= 0xFFFFFFFF;
552 writel(mask
, ®s
->outbound_intr_mask
);
553 /* Dummy readl to force pci flush */
554 readl(®s
->outbound_intr_mask
);
558 * megasas_read_fw_status_reg_skinny - returns the current FW status value
559 * @regs: MFI register set
562 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
564 return readl(&(regs
)->outbound_scratch_pad
);
568 * megasas_clear_interrupt_skinny - Check & clear interrupt
569 * @regs: MFI register set
572 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
578 * Check if it is our interrupt
580 status
= readl(®s
->outbound_intr_status
);
582 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
587 * Check if it is our interrupt
589 if ((megasas_read_fw_status_reg_gen2(regs
) & MFI_STATE_MASK
) ==
591 mfiStatus
= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
593 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
596 * Clear the interrupt by writing back the same value
598 writel(status
, ®s
->outbound_intr_status
);
601 * dummy read to flush PCI
603 readl(®s
->outbound_intr_status
);
609 * megasas_fire_cmd_skinny - Sends command to the FW
610 * @frame_phys_addr : Physical address of cmd
611 * @frame_count : Number of frames for the command
612 * @regs : MFI register set
615 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
616 dma_addr_t frame_phys_addr
,
618 struct megasas_register_set __iomem
*regs
)
621 spin_lock_irqsave(&instance
->hba_lock
, flags
);
622 writel(0, &(regs
)->inbound_high_queue_port
);
623 writel((frame_phys_addr
| (frame_count
<<1))|1,
624 &(regs
)->inbound_low_queue_port
);
625 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
629 * megasas_check_reset_skinny - For controller reset check
630 * @regs: MFI register set
633 megasas_check_reset_skinny(struct megasas_instance
*instance
,
634 struct megasas_register_set __iomem
*regs
)
636 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
642 static struct megasas_instance_template megasas_instance_template_skinny
= {
644 .fire_cmd
= megasas_fire_cmd_skinny
,
645 .enable_intr
= megasas_enable_intr_skinny
,
646 .disable_intr
= megasas_disable_intr_skinny
,
647 .clear_intr
= megasas_clear_intr_skinny
,
648 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
649 .adp_reset
= megasas_adp_reset_gen2
,
650 .check_reset
= megasas_check_reset_skinny
,
651 .service_isr
= megasas_isr
,
652 .tasklet
= megasas_complete_cmd_dpc
,
653 .init_adapter
= megasas_init_adapter_mfi
,
654 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
655 .issue_dcmd
= megasas_issue_dcmd
,
660 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
665 * megasas_enable_intr_gen2 - Enables interrupts
666 * @regs: MFI register set
669 megasas_enable_intr_gen2(struct megasas_register_set __iomem
*regs
)
671 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
673 /* write ~0x00000005 (4 & 1) to the intr mask*/
674 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
676 /* Dummy readl to force pci flush */
677 readl(®s
->outbound_intr_mask
);
681 * megasas_disable_intr_gen2 - Disables interrupt
682 * @regs: MFI register set
685 megasas_disable_intr_gen2(struct megasas_register_set __iomem
*regs
)
687 u32 mask
= 0xFFFFFFFF;
688 writel(mask
, ®s
->outbound_intr_mask
);
689 /* Dummy readl to force pci flush */
690 readl(®s
->outbound_intr_mask
);
694 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
695 * @regs: MFI register set
698 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
700 return readl(&(regs
)->outbound_scratch_pad
);
704 * megasas_clear_interrupt_gen2 - Check & clear interrupt
705 * @regs: MFI register set
708 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
713 * Check if it is our interrupt
715 status
= readl(®s
->outbound_intr_status
);
717 if (status
& MFI_INTR_FLAG_REPLY_MESSAGE
) {
718 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
720 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
) {
721 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
725 * Clear the interrupt by writing back the same value
728 writel(status
, ®s
->outbound_doorbell_clear
);
730 /* Dummy readl to force pci flush */
731 readl(®s
->outbound_intr_status
);
736 * megasas_fire_cmd_gen2 - Sends command to the FW
737 * @frame_phys_addr : Physical address of cmd
738 * @frame_count : Number of frames for the command
739 * @regs : MFI register set
742 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
743 dma_addr_t frame_phys_addr
,
745 struct megasas_register_set __iomem
*regs
)
748 spin_lock_irqsave(&instance
->hba_lock
, flags
);
749 writel((frame_phys_addr
| (frame_count
<<1))|1,
750 &(regs
)->inbound_queue_port
);
751 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
755 * megasas_adp_reset_gen2 - For controller reset
756 * @regs: MFI register set
759 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
760 struct megasas_register_set __iomem
*reg_set
)
764 u32
*seq_offset
= ®_set
->seq_offset
;
765 u32
*hostdiag_offset
= ®_set
->host_diag
;
767 if (instance
->instancet
== &megasas_instance_template_skinny
) {
768 seq_offset
= ®_set
->fusion_seq_offset
;
769 hostdiag_offset
= ®_set
->fusion_host_diag
;
772 writel(0, seq_offset
);
773 writel(4, seq_offset
);
774 writel(0xb, seq_offset
);
775 writel(2, seq_offset
);
776 writel(7, seq_offset
);
777 writel(0xd, seq_offset
);
781 HostDiag
= (u32
)readl(hostdiag_offset
);
783 while ( !( HostDiag
& DIAG_WRITE_ENABLE
) ) {
785 HostDiag
= (u32
)readl(hostdiag_offset
);
786 printk(KERN_NOTICE
"RESETGEN2: retry=%x, hostdiag=%x\n",
794 printk(KERN_NOTICE
"ADP_RESET_GEN2: HostDiag=%x\n", HostDiag
);
796 writel((HostDiag
| DIAG_RESET_ADAPTER
), hostdiag_offset
);
800 HostDiag
= (u32
)readl(hostdiag_offset
);
801 while ( ( HostDiag
& DIAG_RESET_ADAPTER
) ) {
803 HostDiag
= (u32
)readl(hostdiag_offset
);
804 printk(KERN_NOTICE
"RESET_GEN2: retry=%x, hostdiag=%x\n",
815 * megasas_check_reset_gen2 - For controller reset check
816 * @regs: MFI register set
819 megasas_check_reset_gen2(struct megasas_instance
*instance
,
820 struct megasas_register_set __iomem
*regs
)
822 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
829 static struct megasas_instance_template megasas_instance_template_gen2
= {
831 .fire_cmd
= megasas_fire_cmd_gen2
,
832 .enable_intr
= megasas_enable_intr_gen2
,
833 .disable_intr
= megasas_disable_intr_gen2
,
834 .clear_intr
= megasas_clear_intr_gen2
,
835 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
836 .adp_reset
= megasas_adp_reset_gen2
,
837 .check_reset
= megasas_check_reset_gen2
,
838 .service_isr
= megasas_isr
,
839 .tasklet
= megasas_complete_cmd_dpc
,
840 .init_adapter
= megasas_init_adapter_mfi
,
841 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
842 .issue_dcmd
= megasas_issue_dcmd
,
846 * This is the end of set of functions & definitions
847 * specific to gen2 (deviceid : 0x78, 0x79) controllers
851 * Template added for TB (Fusion)
853 extern struct megasas_instance_template megasas_instance_template_fusion
;
856 * megasas_issue_polled - Issues a polling command
857 * @instance: Adapter soft state
858 * @cmd: Command packet to be issued
860 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
863 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
866 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
868 frame_hdr
->cmd_status
= 0xFF;
869 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
872 * Issue the frame using inbound queue port
874 instance
->instancet
->issue_dcmd(instance
, cmd
);
877 * Wait for cmd_status to change
879 return wait_and_poll(instance
, cmd
);
883 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
884 * @instance: Adapter soft state
885 * @cmd: Command to be issued
887 * This function waits on an event for the command to be returned from ISR.
888 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
889 * Used to issue ioctl commands.
892 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
893 struct megasas_cmd
*cmd
)
895 cmd
->cmd_status
= ENODATA
;
897 instance
->instancet
->issue_dcmd(instance
, cmd
);
899 wait_event(instance
->int_cmd_wait_q
, cmd
->cmd_status
!= ENODATA
);
905 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
906 * @instance: Adapter soft state
907 * @cmd_to_abort: Previously issued cmd to be aborted
909 * MFI firmware can abort previously issued AEN command (automatic event
910 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
911 * cmd and waits for return status.
912 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
915 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
916 struct megasas_cmd
*cmd_to_abort
)
918 struct megasas_cmd
*cmd
;
919 struct megasas_abort_frame
*abort_fr
;
921 cmd
= megasas_get_cmd(instance
);
926 abort_fr
= &cmd
->frame
->abort
;
929 * Prepare and issue the abort frame
931 abort_fr
->cmd
= MFI_CMD_ABORT
;
932 abort_fr
->cmd_status
= 0xFF;
934 abort_fr
->abort_context
= cmd_to_abort
->index
;
935 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
936 abort_fr
->abort_mfi_phys_addr_hi
= 0;
939 cmd
->cmd_status
= 0xFF;
941 instance
->instancet
->issue_dcmd(instance
, cmd
);
944 * Wait for this cmd to complete
946 wait_event(instance
->abort_cmd_wait_q
, cmd
->cmd_status
!= 0xFF);
949 megasas_return_cmd(instance
, cmd
);
954 * megasas_make_sgl32 - Prepares 32-bit SGL
955 * @instance: Adapter soft state
956 * @scp: SCSI command from the mid-layer
957 * @mfi_sgl: SGL to be filled in
959 * If successful, this function returns the number of SG elements. Otherwise,
963 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
964 union megasas_sgl
*mfi_sgl
)
968 struct scatterlist
*os_sgl
;
970 sge_count
= scsi_dma_map(scp
);
971 BUG_ON(sge_count
< 0);
974 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
975 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
976 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
983 * megasas_make_sgl64 - Prepares 64-bit SGL
984 * @instance: Adapter soft state
985 * @scp: SCSI command from the mid-layer
986 * @mfi_sgl: SGL to be filled in
988 * If successful, this function returns the number of SG elements. Otherwise,
992 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
993 union megasas_sgl
*mfi_sgl
)
997 struct scatterlist
*os_sgl
;
999 sge_count
= scsi_dma_map(scp
);
1000 BUG_ON(sge_count
< 0);
1003 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1004 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
1005 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
1012 * megasas_make_sgl_skinny - Prepares IEEE SGL
1013 * @instance: Adapter soft state
1014 * @scp: SCSI command from the mid-layer
1015 * @mfi_sgl: SGL to be filled in
1017 * If successful, this function returns the number of SG elements. Otherwise,
1021 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
1022 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
1026 struct scatterlist
*os_sgl
;
1028 sge_count
= scsi_dma_map(scp
);
1031 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1032 mfi_sgl
->sge_skinny
[i
].length
= sg_dma_len(os_sgl
);
1033 mfi_sgl
->sge_skinny
[i
].phys_addr
=
1034 sg_dma_address(os_sgl
);
1035 mfi_sgl
->sge_skinny
[i
].flag
= 0;
1042 * megasas_get_frame_count - Computes the number of frames
1043 * @frame_type : type of frame- io or pthru frame
1044 * @sge_count : number of sg elements
1046 * Returns the number of frames required for numnber of sge's (sge_count)
1049 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
1050 u8 sge_count
, u8 frame_type
)
1057 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1058 sizeof(struct megasas_sge32
);
1060 if (instance
->flag_ieee
) {
1061 sge_sz
= sizeof(struct megasas_sge_skinny
);
1065 * Main frame can contain 2 SGEs for 64-bit SGLs and
1066 * 3 SGEs for 32-bit SGLs for ldio &
1067 * 1 SGEs for 64-bit SGLs and
1068 * 2 SGEs for 32-bit SGLs for pthru frame
1070 if (unlikely(frame_type
== PTHRU_FRAME
)) {
1071 if (instance
->flag_ieee
== 1) {
1072 num_cnt
= sge_count
- 1;
1073 } else if (IS_DMA64
)
1074 num_cnt
= sge_count
- 1;
1076 num_cnt
= sge_count
- 2;
1078 if (instance
->flag_ieee
== 1) {
1079 num_cnt
= sge_count
- 1;
1080 } else if (IS_DMA64
)
1081 num_cnt
= sge_count
- 2;
1083 num_cnt
= sge_count
- 3;
1087 sge_bytes
= sge_sz
* num_cnt
;
1089 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
1090 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
1095 if (frame_count
> 7)
1101 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1102 * @instance: Adapter soft state
1103 * @scp: SCSI command
1104 * @cmd: Command to be prepared in
1106 * This function prepares CDB commands. These are typcially pass-through
1107 * commands to the devices.
1110 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1111 struct megasas_cmd
*cmd
)
1116 struct megasas_pthru_frame
*pthru
;
1118 is_logical
= MEGASAS_IS_LOGICAL(scp
);
1119 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1120 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
1122 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1123 flags
= MFI_FRAME_DIR_WRITE
;
1124 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1125 flags
= MFI_FRAME_DIR_READ
;
1126 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
1127 flags
= MFI_FRAME_DIR_NONE
;
1129 if (instance
->flag_ieee
== 1) {
1130 flags
|= MFI_FRAME_IEEE
;
1134 * Prepare the DCDB frame
1136 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
1137 pthru
->cmd_status
= 0x0;
1138 pthru
->scsi_status
= 0x0;
1139 pthru
->target_id
= device_id
;
1140 pthru
->lun
= scp
->device
->lun
;
1141 pthru
->cdb_len
= scp
->cmd_len
;
1144 pthru
->flags
= flags
;
1145 pthru
->data_xfer_len
= scsi_bufflen(scp
);
1147 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
1150 * If the command is for the tape device, set the
1151 * pthru timeout to the os layer timeout value.
1153 if (scp
->device
->type
== TYPE_TAPE
) {
1154 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
1155 pthru
->timeout
= 0xFFFF;
1157 pthru
->timeout
= scp
->request
->timeout
/ HZ
;
1163 if (instance
->flag_ieee
== 1) {
1164 pthru
->flags
|= MFI_FRAME_SGL64
;
1165 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1167 } else if (IS_DMA64
) {
1168 pthru
->flags
|= MFI_FRAME_SGL64
;
1169 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
1172 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
1175 if (pthru
->sge_count
> instance
->max_num_sge
) {
1176 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
1182 * Sense info specific
1184 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1185 pthru
->sense_buf_phys_addr_hi
= 0;
1186 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1189 * Compute the total number of frames this command consumes. FW uses
1190 * this number to pull sufficient number of frames from host memory.
1192 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
1195 return cmd
->frame_count
;
1199 * megasas_build_ldio - Prepares IOs to logical devices
1200 * @instance: Adapter soft state
1201 * @scp: SCSI command
1202 * @cmd: Command to be prepared
1204 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1207 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1208 struct megasas_cmd
*cmd
)
1211 u8 sc
= scp
->cmnd
[0];
1213 struct megasas_io_frame
*ldio
;
1215 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1216 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1218 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1219 flags
= MFI_FRAME_DIR_WRITE
;
1220 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1221 flags
= MFI_FRAME_DIR_READ
;
1223 if (instance
->flag_ieee
== 1) {
1224 flags
|= MFI_FRAME_IEEE
;
1228 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1230 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
1231 ldio
->cmd_status
= 0x0;
1232 ldio
->scsi_status
= 0x0;
1233 ldio
->target_id
= device_id
;
1235 ldio
->reserved_0
= 0;
1237 ldio
->flags
= flags
;
1238 ldio
->start_lba_hi
= 0;
1239 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
1242 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1244 if (scp
->cmd_len
== 6) {
1245 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
1246 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1247 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1249 ldio
->start_lba_lo
&= 0x1FFFFF;
1253 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1255 else if (scp
->cmd_len
== 10) {
1256 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
1257 ((u32
) scp
->cmnd
[7] << 8);
1258 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1259 ((u32
) scp
->cmnd
[3] << 16) |
1260 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1264 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1266 else if (scp
->cmd_len
== 12) {
1267 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
1268 ((u32
) scp
->cmnd
[7] << 16) |
1269 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1271 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1272 ((u32
) scp
->cmnd
[3] << 16) |
1273 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1277 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1279 else if (scp
->cmd_len
== 16) {
1280 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
1281 ((u32
) scp
->cmnd
[11] << 16) |
1282 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1284 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1285 ((u32
) scp
->cmnd
[7] << 16) |
1286 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1288 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1289 ((u32
) scp
->cmnd
[3] << 16) |
1290 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1297 if (instance
->flag_ieee
) {
1298 ldio
->flags
|= MFI_FRAME_SGL64
;
1299 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1301 } else if (IS_DMA64
) {
1302 ldio
->flags
|= MFI_FRAME_SGL64
;
1303 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1305 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1307 if (ldio
->sge_count
> instance
->max_num_sge
) {
1308 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1314 * Sense info specific
1316 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1317 ldio
->sense_buf_phys_addr_hi
= 0;
1318 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1321 * Compute the total number of frames this command consumes. FW uses
1322 * this number to pull sufficient number of frames from host memory.
1324 cmd
->frame_count
= megasas_get_frame_count(instance
,
1325 ldio
->sge_count
, IO_FRAME
);
1327 return cmd
->frame_count
;
1331 * megasas_is_ldio - Checks if the cmd is for logical drive
1332 * @scmd: SCSI command
1334 * Called by megasas_queue_command to find out if the command to be queued
1335 * is a logical drive command
1337 inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1339 if (!MEGASAS_IS_LOGICAL(cmd
))
1341 switch (cmd
->cmnd
[0]) {
1357 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1359 * @instance: Adapter soft state
1362 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1364 struct megasas_cmd
*cmd
;
1366 union megasas_sgl
*mfi_sgl
;
1367 struct megasas_io_frame
*ldio
;
1368 struct megasas_pthru_frame
*pthru
;
1370 u32 max_cmd
= instance
->max_fw_cmds
;
1372 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1373 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1375 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1377 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1379 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1380 for (i
= 0; i
< max_cmd
; i
++) {
1381 cmd
= instance
->cmd_list
[i
];
1384 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1385 if (megasas_is_ldio(cmd
->scmd
)){
1386 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1387 mfi_sgl
= &ldio
->sgl
;
1388 sgcount
= ldio
->sge_count
;
1389 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
);
1392 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1393 mfi_sgl
= &pthru
->sgl
;
1394 sgcount
= pthru
->sge_count
;
1395 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
);
1397 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1398 for (n
= 0; n
< sgcount
; n
++){
1400 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
) ;
1402 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl
->sge32
[n
].length
, mfi_sgl
->sge32
[n
].phys_addr
) ;
1405 printk(KERN_ERR
"\n");
1407 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1408 for (i
= 0; i
< max_cmd
; i
++) {
1410 cmd
= instance
->cmd_list
[i
];
1412 if(cmd
->sync_cmd
== 1){
1413 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1416 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1420 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
1421 struct scsi_cmnd
*scmd
)
1423 struct megasas_cmd
*cmd
;
1426 cmd
= megasas_get_cmd(instance
);
1428 return SCSI_MLQUEUE_HOST_BUSY
;
1431 * Logical drive command
1433 if (megasas_is_ldio(scmd
))
1434 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1436 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1439 goto out_return_cmd
;
1442 scmd
->SCp
.ptr
= (char *)cmd
;
1445 * Issue the command to the FW
1447 atomic_inc(&instance
->fw_outstanding
);
1449 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1450 cmd
->frame_count
-1, instance
->reg_set
);
1454 megasas_return_cmd(instance
, cmd
);
1460 * megasas_queue_command - Queue entry point
1461 * @scmd: SCSI command to be queued
1462 * @done: Callback entry point
1465 megasas_queue_command_lck(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
1467 struct megasas_instance
*instance
;
1468 unsigned long flags
;
1470 instance
= (struct megasas_instance
*)
1471 scmd
->device
->host
->hostdata
;
1473 if (instance
->issuepend_done
== 0)
1474 return SCSI_MLQUEUE_HOST_BUSY
;
1476 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1478 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1479 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1480 scmd
->result
= DID_ERROR
<< 16;
1485 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1486 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1487 return SCSI_MLQUEUE_HOST_BUSY
;
1490 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1492 scmd
->scsi_done
= done
;
1495 if (MEGASAS_IS_LOGICAL(scmd
) &&
1496 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
1497 scmd
->result
= DID_BAD_TARGET
<< 16;
1501 switch (scmd
->cmnd
[0]) {
1502 case SYNCHRONIZE_CACHE
:
1504 * FW takes care of flush cache on its own
1505 * No need to send it down
1507 scmd
->result
= DID_OK
<< 16;
1513 if (instance
->instancet
->build_and_issue_cmd(instance
, scmd
)) {
1514 printk(KERN_ERR
"megasas: Err returned from build_and_issue_cmd\n");
1515 return SCSI_MLQUEUE_HOST_BUSY
;
1525 static DEF_SCSI_QCMD(megasas_queue_command
)
1527 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1531 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1533 if ((megasas_mgmt_info
.instance
[i
]) &&
1534 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1535 return megasas_mgmt_info
.instance
[i
];
1541 static int megasas_slave_configure(struct scsi_device
*sdev
)
1544 struct megasas_instance
*instance
;
1546 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1549 * Don't export physical disk devices to the disk driver.
1551 * FIXME: Currently we don't export them to the midlayer at all.
1552 * That will be fixed once LSI engineers have audited the
1553 * firmware for possible issues.
1555 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1556 sdev
->type
== TYPE_DISK
) {
1557 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1559 if (instance
->pd_list
[pd_index
].driveState
==
1560 MR_PD_STATE_SYSTEM
) {
1561 blk_queue_rq_timeout(sdev
->request_queue
,
1562 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1569 * The RAID firmware may require extended timeouts.
1571 blk_queue_rq_timeout(sdev
->request_queue
,
1572 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1576 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1579 struct megasas_instance
*instance
;
1580 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1581 if ((sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) &&
1582 (sdev
->type
== TYPE_DISK
)) {
1584 * Open the OS scan to the SYSTEM PD
1587 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1589 if ((instance
->pd_list
[pd_index
].driveState
==
1590 MR_PD_STATE_SYSTEM
) &&
1591 (instance
->pd_list
[pd_index
].driveType
==
1600 void megaraid_sas_kill_hba(struct megasas_instance
*instance
)
1602 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1603 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
1604 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1605 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1606 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1607 writel(MFI_STOP_ADP
, &instance
->reg_set
->doorbell
);
1609 writel(MFI_STOP_ADP
, &instance
->reg_set
->inbound_doorbell
);
1614 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1615 * restored to max value
1616 * @instance: Adapter soft state
1620 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
)
1622 unsigned long flags
;
1623 if (instance
->flag
& MEGASAS_FW_BUSY
1624 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1625 && atomic_read(&instance
->fw_outstanding
) <
1626 instance
->throttlequeuedepth
+ 1) {
1628 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1629 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1630 if ((instance
->pdev
->device
==
1631 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1632 (instance
->pdev
->device
==
1633 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1634 instance
->host
->can_queue
=
1635 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1637 instance
->host
->can_queue
=
1638 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1640 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1645 * megasas_complete_cmd_dpc - Returns FW's controller structure
1646 * @instance_addr: Address of adapter soft state
1648 * Tasklet to complete cmds
1650 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1655 struct megasas_cmd
*cmd
;
1656 struct megasas_instance
*instance
=
1657 (struct megasas_instance
*)instance_addr
;
1658 unsigned long flags
;
1660 /* If we have already declared adapter dead, donot complete cmds */
1661 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1664 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1666 producer
= *instance
->producer
;
1667 consumer
= *instance
->consumer
;
1669 while (consumer
!= producer
) {
1670 context
= instance
->reply_queue
[consumer
];
1671 if (context
>= instance
->max_fw_cmds
) {
1672 printk(KERN_ERR
"Unexpected context value %x\n",
1677 cmd
= instance
->cmd_list
[context
];
1679 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1682 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1687 *instance
->consumer
= producer
;
1689 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1692 * Check if we can restore can_queue
1694 megasas_check_and_restore_queue_depth(instance
);
1698 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
);
1701 process_fw_state_change_wq(struct work_struct
*work
);
1703 void megasas_do_ocr(struct megasas_instance
*instance
)
1705 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
1706 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
1707 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
1708 *instance
->consumer
= MEGASAS_ADPRESET_INPROG_SIGN
;
1710 instance
->instancet
->disable_intr(instance
->reg_set
);
1711 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
1712 instance
->issuepend_done
= 0;
1714 atomic_set(&instance
->fw_outstanding
, 0);
1715 megasas_internal_reset_defer_cmds(instance
);
1716 process_fw_state_change_wq(&instance
->work_init
);
1720 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1721 * @instance: Adapter soft state
1723 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1724 * complete all its outstanding commands. Returns error if one or more IOs
1725 * are pending after this time period. It also marks the controller dead.
1727 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
1731 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
1733 unsigned long flags
;
1734 struct list_head clist_local
;
1735 struct megasas_cmd
*reset_cmd
;
1737 u8 kill_adapter_flag
;
1739 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1740 adprecovery
= instance
->adprecovery
;
1741 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1743 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1745 INIT_LIST_HEAD(&clist_local
);
1746 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1747 list_splice_init(&instance
->internal_reset_pending_q
,
1749 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1751 printk(KERN_NOTICE
"megasas: HBA reset wait ...\n");
1752 for (i
= 0; i
< wait_time
; i
++) {
1754 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1755 adprecovery
= instance
->adprecovery
;
1756 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1757 if (adprecovery
== MEGASAS_HBA_OPERATIONAL
)
1761 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1762 printk(KERN_NOTICE
"megasas: reset: Stopping HBA.\n");
1763 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1764 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1765 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1770 while (!list_empty(&clist_local
)) {
1771 reset_cmd
= list_entry((&clist_local
)->next
,
1772 struct megasas_cmd
, list
);
1773 list_del_init(&reset_cmd
->list
);
1774 if (reset_cmd
->scmd
) {
1775 reset_cmd
->scmd
->result
= DID_RESET
<< 16;
1776 printk(KERN_NOTICE
"%d:%p reset [%02x]\n",
1777 reset_index
, reset_cmd
,
1778 reset_cmd
->scmd
->cmnd
[0]);
1780 reset_cmd
->scmd
->scsi_done(reset_cmd
->scmd
);
1781 megasas_return_cmd(instance
, reset_cmd
);
1782 } else if (reset_cmd
->sync_cmd
) {
1783 printk(KERN_NOTICE
"megasas:%p synch cmds"
1787 reset_cmd
->cmd_status
= ENODATA
;
1788 instance
->instancet
->fire_cmd(instance
,
1789 reset_cmd
->frame_phys_addr
,
1790 0, instance
->reg_set
);
1792 printk(KERN_NOTICE
"megasas: %p unexpected"
1802 for (i
= 0; i
< resetwaittime
; i
++) {
1804 int outstanding
= atomic_read(&instance
->fw_outstanding
);
1809 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
1810 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
1811 "commands to complete\n",i
,outstanding
);
1813 * Call cmd completion routine. Cmd to be
1814 * be completed directly without depending on isr.
1816 megasas_complete_cmd_dpc((unsigned long)instance
);
1823 kill_adapter_flag
= 0;
1825 fw_state
= instance
->instancet
->read_fw_status_reg(
1826 instance
->reg_set
) & MFI_STATE_MASK
;
1827 if ((fw_state
== MFI_STATE_FAULT
) &&
1828 (instance
->disableOnlineCtrlReset
== 0)) {
1830 kill_adapter_flag
= 2;
1833 megasas_do_ocr(instance
);
1834 kill_adapter_flag
= 1;
1836 /* wait for 1 secs to let FW finish the pending cmds */
1842 if (atomic_read(&instance
->fw_outstanding
) &&
1843 !kill_adapter_flag
) {
1844 if (instance
->disableOnlineCtrlReset
== 0) {
1846 megasas_do_ocr(instance
);
1848 /* wait for 5 secs to let FW finish the pending cmds */
1849 for (i
= 0; i
< wait_time
; i
++) {
1851 atomic_read(&instance
->fw_outstanding
);
1859 if (atomic_read(&instance
->fw_outstanding
) ||
1860 (kill_adapter_flag
== 2)) {
1861 printk(KERN_NOTICE
"megaraid_sas: pending cmds after reset\n");
1863 * Send signal to FW to stop processing any pending cmds.
1864 * The controller will be taken offline by the OS now.
1866 if ((instance
->pdev
->device
==
1867 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1868 (instance
->pdev
->device
==
1869 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1870 writel(MFI_STOP_ADP
,
1871 &instance
->reg_set
->doorbell
);
1873 writel(MFI_STOP_ADP
,
1874 &instance
->reg_set
->inbound_doorbell
);
1876 megasas_dump_pending_frames(instance
);
1877 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1878 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1879 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1883 printk(KERN_NOTICE
"megaraid_sas: no pending cmds after reset\n");
1889 * megasas_generic_reset - Generic reset routine
1890 * @scmd: Mid-layer SCSI command
1892 * This routine implements a generic reset handler for device, bus and host
1893 * reset requests. Device, bus and host specific reset handlers can use this
1894 * function after they do their specific tasks.
1896 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
1899 struct megasas_instance
*instance
;
1901 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1903 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET cmd=%x retries=%x\n",
1904 scmd
->cmnd
[0], scmd
->retries
);
1906 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1907 printk(KERN_ERR
"megasas: cannot recover from previous reset "
1912 ret_val
= megasas_wait_for_outstanding(instance
);
1913 if (ret_val
== SUCCESS
)
1914 printk(KERN_NOTICE
"megasas: reset successful \n");
1916 printk(KERN_ERR
"megasas: failed to do reset\n");
1922 * megasas_reset_timer - quiesce the adapter if required
1925 * Sets the FW busy flag and reduces the host->can_queue if the
1926 * cmd has not been completed within the timeout period.
1929 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
1931 struct megasas_instance
*instance
;
1932 unsigned long flags
;
1934 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
1935 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
1936 return BLK_EH_NOT_HANDLED
;
1939 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1940 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
1941 /* FW is busy, throttle IO */
1942 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1944 instance
->host
->can_queue
= instance
->throttlequeuedepth
;
1945 instance
->last_time
= jiffies
;
1946 instance
->flag
|= MEGASAS_FW_BUSY
;
1948 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1950 return BLK_EH_RESET_TIMER
;
1954 * megasas_reset_device - Device reset handler entry point
1956 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
1961 * First wait for all commands to complete
1963 ret
= megasas_generic_reset(scmd
);
1969 * megasas_reset_bus_host - Bus & host reset handler entry point
1971 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
1974 struct megasas_instance
*instance
;
1975 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1978 * First wait for all commands to complete
1980 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1981 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1982 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1983 ret
= megasas_reset_fusion(scmd
->device
->host
);
1985 ret
= megasas_generic_reset(scmd
);
1991 * megasas_bios_param - Returns disk geometry for a disk
1992 * @sdev: device handle
1993 * @bdev: block device
1994 * @capacity: drive capacity
1995 * @geom: geometry parameters
1998 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
1999 sector_t capacity
, int geom
[])
2005 /* Default heads (64) & sectors (32) */
2009 tmp
= heads
* sectors
;
2010 cylinders
= capacity
;
2012 sector_div(cylinders
, tmp
);
2015 * Handle extended translation size for logical drives > 1Gb
2018 if (capacity
>= 0x200000) {
2021 tmp
= heads
*sectors
;
2022 cylinders
= capacity
;
2023 sector_div(cylinders
, tmp
);
2028 geom
[2] = cylinders
;
2033 static void megasas_aen_polling(struct work_struct
*work
);
2036 * megasas_service_aen - Processes an event notification
2037 * @instance: Adapter soft state
2038 * @cmd: AEN command completed by the ISR
2040 * For AEN, driver sends a command down to FW that is held by the FW till an
2041 * event occurs. When an event of interest occurs, FW completes the command
2042 * that it was previously holding.
2044 * This routines sends SIGIO signal to processes that have registered with the
2048 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2050 unsigned long flags
;
2052 * Don't signal app if it is just an aborted previously registered aen
2054 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
2055 spin_lock_irqsave(&poll_aen_lock
, flags
);
2056 megasas_poll_wait_aen
= 1;
2057 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2058 wake_up(&megasas_poll_wait
);
2059 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
2064 instance
->aen_cmd
= NULL
;
2065 megasas_return_cmd(instance
, cmd
);
2067 if ((instance
->unload
== 0) &&
2068 ((instance
->issuepend_done
== 1))) {
2069 struct megasas_aen_event
*ev
;
2070 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
2072 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
2074 ev
->instance
= instance
;
2076 INIT_DELAYED_WORK(&ev
->hotplug_work
,
2077 megasas_aen_polling
);
2078 schedule_delayed_work(&ev
->hotplug_work
, 0);
2083 static int megasas_change_queue_depth(struct scsi_device
*sdev
,
2084 int queue_depth
, int reason
)
2086 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2089 if (queue_depth
> sdev
->host
->can_queue
)
2090 queue_depth
= sdev
->host
->can_queue
;
2091 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
),
2098 * Scsi host template for megaraid_sas driver
2100 static struct scsi_host_template megasas_template
= {
2102 .module
= THIS_MODULE
,
2103 .name
= "LSI SAS based MegaRAID driver",
2104 .proc_name
= "megaraid_sas",
2105 .slave_configure
= megasas_slave_configure
,
2106 .slave_alloc
= megasas_slave_alloc
,
2107 .queuecommand
= megasas_queue_command
,
2108 .eh_device_reset_handler
= megasas_reset_device
,
2109 .eh_bus_reset_handler
= megasas_reset_bus_host
,
2110 .eh_host_reset_handler
= megasas_reset_bus_host
,
2111 .eh_timed_out
= megasas_reset_timer
,
2112 .bios_param
= megasas_bios_param
,
2113 .use_clustering
= ENABLE_CLUSTERING
,
2114 .change_queue_depth
= megasas_change_queue_depth
,
2118 * megasas_complete_int_cmd - Completes an internal command
2119 * @instance: Adapter soft state
2120 * @cmd: Command to be completed
2122 * The megasas_issue_blocked_cmd() function waits for a command to complete
2123 * after it issues a command. This function wakes up that waiting routine by
2124 * calling wake_up() on the wait queue.
2127 megasas_complete_int_cmd(struct megasas_instance
*instance
,
2128 struct megasas_cmd
*cmd
)
2130 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
2132 if (cmd
->cmd_status
== ENODATA
) {
2133 cmd
->cmd_status
= 0;
2135 wake_up(&instance
->int_cmd_wait_q
);
2139 * megasas_complete_abort - Completes aborting a command
2140 * @instance: Adapter soft state
2141 * @cmd: Cmd that was issued to abort another cmd
2143 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2144 * after it issues an abort on a previously issued command. This function
2145 * wakes up all functions waiting on the same wait queue.
2148 megasas_complete_abort(struct megasas_instance
*instance
,
2149 struct megasas_cmd
*cmd
)
2151 if (cmd
->sync_cmd
) {
2153 cmd
->cmd_status
= 0;
2154 wake_up(&instance
->abort_cmd_wait_q
);
2161 * megasas_complete_cmd - Completes a command
2162 * @instance: Adapter soft state
2163 * @cmd: Command to be completed
2164 * @alt_status: If non-zero, use this value as status to
2165 * SCSI mid-layer instead of the value returned
2166 * by the FW. This should be used if caller wants
2167 * an alternate status (as in the case of aborted
2171 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
2175 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
2176 unsigned long flags
;
2177 struct fusion_context
*fusion
= instance
->ctrl_context
;
2179 /* flag for the retry reset */
2180 cmd
->retry_for_fw_reset
= 0;
2183 cmd
->scmd
->SCp
.ptr
= NULL
;
2186 case MFI_CMD_INVALID
:
2187 /* Some older 1068 controller FW may keep a pended
2188 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2189 when booting the kdump kernel. Ignore this command to
2190 prevent a kernel panic on shutdown of the kdump kernel. */
2191 printk(KERN_WARNING
"megaraid_sas: MFI_CMD_INVALID command "
2193 printk(KERN_WARNING
"megaraid_sas: If you have a controller "
2194 "other than PERC5, please upgrade your firmware.\n");
2196 case MFI_CMD_PD_SCSI_IO
:
2197 case MFI_CMD_LD_SCSI_IO
:
2200 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2201 * issued either through an IO path or an IOCTL path. If it
2202 * was via IOCTL, we will send it to internal completion.
2204 if (cmd
->sync_cmd
) {
2206 megasas_complete_int_cmd(instance
, cmd
);
2210 case MFI_CMD_LD_READ
:
2211 case MFI_CMD_LD_WRITE
:
2214 cmd
->scmd
->result
= alt_status
<< 16;
2220 atomic_dec(&instance
->fw_outstanding
);
2222 scsi_dma_unmap(cmd
->scmd
);
2223 cmd
->scmd
->scsi_done(cmd
->scmd
);
2224 megasas_return_cmd(instance
, cmd
);
2229 switch (hdr
->cmd_status
) {
2232 cmd
->scmd
->result
= DID_OK
<< 16;
2235 case MFI_STAT_SCSI_IO_FAILED
:
2236 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2238 (DID_ERROR
<< 16) | hdr
->scsi_status
;
2241 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2243 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
2245 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
2246 memset(cmd
->scmd
->sense_buffer
, 0,
2247 SCSI_SENSE_BUFFERSIZE
);
2248 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
2251 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
2256 case MFI_STAT_LD_OFFLINE
:
2257 case MFI_STAT_DEVICE_NOT_FOUND
:
2258 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
2262 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
2264 cmd
->scmd
->result
= DID_ERROR
<< 16;
2268 atomic_dec(&instance
->fw_outstanding
);
2270 scsi_dma_unmap(cmd
->scmd
);
2271 cmd
->scmd
->scsi_done(cmd
->scmd
);
2272 megasas_return_cmd(instance
, cmd
);
2279 /* Check for LD map update */
2280 if ((cmd
->frame
->dcmd
.opcode
== MR_DCMD_LD_MAP_GET_INFO
) &&
2281 (cmd
->frame
->dcmd
.mbox
.b
[1] == 1)) {
2282 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2283 if (cmd
->frame
->hdr
.cmd_status
!= 0) {
2284 if (cmd
->frame
->hdr
.cmd_status
!=
2286 printk(KERN_WARNING
"megasas: map sync"
2287 "failed, status = 0x%x.\n",
2288 cmd
->frame
->hdr
.cmd_status
);
2290 megasas_return_cmd(instance
, cmd
);
2291 spin_unlock_irqrestore(
2292 instance
->host
->host_lock
,
2298 megasas_return_cmd(instance
, cmd
);
2299 if (MR_ValidateMapInfo(
2300 fusion
->ld_map
[(instance
->map_id
& 1)],
2301 fusion
->load_balance_info
))
2302 fusion
->fast_path_io
= 1;
2304 fusion
->fast_path_io
= 0;
2305 megasas_sync_map_info(instance
);
2306 spin_unlock_irqrestore(instance
->host
->host_lock
,
2310 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
2311 cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET
) {
2312 spin_lock_irqsave(&poll_aen_lock
, flags
);
2313 megasas_poll_wait_aen
= 0;
2314 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2318 * See if got an event notification
2320 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
2321 megasas_service_aen(instance
, cmd
);
2323 megasas_complete_int_cmd(instance
, cmd
);
2329 * Cmd issued to abort another cmd returned
2331 megasas_complete_abort(instance
, cmd
);
2335 printk("megasas: Unknown command completed! [0x%X]\n",
2342 * megasas_issue_pending_cmds_again - issue all pending cmds
2343 * in FW again because of the fw reset
2344 * @instance: Adapter soft state
2347 megasas_issue_pending_cmds_again(struct megasas_instance
*instance
)
2349 struct megasas_cmd
*cmd
;
2350 struct list_head clist_local
;
2351 union megasas_evt_class_locale class_locale
;
2352 unsigned long flags
;
2355 INIT_LIST_HEAD(&clist_local
);
2356 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2357 list_splice_init(&instance
->internal_reset_pending_q
, &clist_local
);
2358 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2360 while (!list_empty(&clist_local
)) {
2361 cmd
= list_entry((&clist_local
)->next
,
2362 struct megasas_cmd
, list
);
2363 list_del_init(&cmd
->list
);
2365 if (cmd
->sync_cmd
|| cmd
->scmd
) {
2366 printk(KERN_NOTICE
"megaraid_sas: command %p, %p:%d"
2367 "detected to be pending while HBA reset.\n",
2368 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2370 cmd
->retry_for_fw_reset
++;
2372 if (cmd
->retry_for_fw_reset
== 3) {
2373 printk(KERN_NOTICE
"megaraid_sas: cmd %p, %p:%d"
2374 "was tried multiple times during reset."
2375 "Shutting down the HBA\n",
2376 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2377 megaraid_sas_kill_hba(instance
);
2379 instance
->adprecovery
=
2380 MEGASAS_HW_CRITICAL_ERROR
;
2385 if (cmd
->sync_cmd
== 1) {
2387 printk(KERN_NOTICE
"megaraid_sas: unexpected"
2388 "cmd attached to internal command!\n");
2390 printk(KERN_NOTICE
"megasas: %p synchronous cmd"
2391 "on the internal reset queue,"
2392 "issue it again.\n", cmd
);
2393 cmd
->cmd_status
= ENODATA
;
2394 instance
->instancet
->fire_cmd(instance
,
2395 cmd
->frame_phys_addr
,
2396 0, instance
->reg_set
);
2397 } else if (cmd
->scmd
) {
2398 printk(KERN_NOTICE
"megasas: %p scsi cmd [%02x]"
2399 "detected on the internal queue, issue again.\n",
2400 cmd
, cmd
->scmd
->cmnd
[0]);
2402 atomic_inc(&instance
->fw_outstanding
);
2403 instance
->instancet
->fire_cmd(instance
,
2404 cmd
->frame_phys_addr
,
2405 cmd
->frame_count
-1, instance
->reg_set
);
2407 printk(KERN_NOTICE
"megasas: %p unexpected cmd on the"
2408 "internal reset defer list while re-issue!!\n",
2413 if (instance
->aen_cmd
) {
2414 printk(KERN_NOTICE
"megaraid_sas: aen_cmd in def process\n");
2415 megasas_return_cmd(instance
, instance
->aen_cmd
);
2417 instance
->aen_cmd
= NULL
;
2421 * Initiate AEN (Asynchronous Event Notification)
2423 seq_num
= instance
->last_seq_num
;
2424 class_locale
.members
.reserved
= 0;
2425 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2426 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2428 megasas_register_aen(instance
, seq_num
, class_locale
.word
);
2432 * Move the internal reset pending commands to a deferred queue.
2434 * We move the commands pending at internal reset time to a
2435 * pending queue. This queue would be flushed after successful
2436 * completion of the internal reset sequence. if the internal reset
2437 * did not complete in time, the kernel reset handler would flush
2441 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
)
2443 struct megasas_cmd
*cmd
;
2445 u32 max_cmd
= instance
->max_fw_cmds
;
2447 unsigned long flags
;
2450 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
2451 for (i
= 0; i
< max_cmd
; i
++) {
2452 cmd
= instance
->cmd_list
[i
];
2453 if (cmd
->sync_cmd
== 1 || cmd
->scmd
) {
2454 printk(KERN_NOTICE
"megasas: moving cmd[%d]:%p:%d:%p"
2455 "on the defer queue as internal\n",
2456 defer_index
, cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2458 if (!list_empty(&cmd
->list
)) {
2459 printk(KERN_NOTICE
"megaraid_sas: ERROR while"
2460 " moving this cmd:%p, %d %p, it was"
2461 "discovered on some list?\n",
2462 cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2464 list_del_init(&cmd
->list
);
2467 list_add_tail(&cmd
->list
,
2468 &instance
->internal_reset_pending_q
);
2471 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
2476 process_fw_state_change_wq(struct work_struct
*work
)
2478 struct megasas_instance
*instance
=
2479 container_of(work
, struct megasas_instance
, work_init
);
2481 unsigned long flags
;
2483 if (instance
->adprecovery
!= MEGASAS_ADPRESET_SM_INFAULT
) {
2484 printk(KERN_NOTICE
"megaraid_sas: error, recovery st %x \n",
2485 instance
->adprecovery
);
2489 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
2490 printk(KERN_NOTICE
"megaraid_sas: FW detected to be in fault"
2491 "state, restarting it...\n");
2493 instance
->instancet
->disable_intr(instance
->reg_set
);
2494 atomic_set(&instance
->fw_outstanding
, 0);
2496 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
2497 instance
->instancet
->adp_reset(instance
, instance
->reg_set
);
2498 atomic_set(&instance
->fw_reset_no_pci_access
, 0 );
2500 printk(KERN_NOTICE
"megaraid_sas: FW restarted successfully,"
2501 "initiating next stage...\n");
2503 printk(KERN_NOTICE
"megaraid_sas: HBA recovery state machine,"
2504 "state 2 starting...\n");
2506 /*waitting for about 20 second before start the second init*/
2507 for (wait
= 0; wait
< 30; wait
++) {
2511 if (megasas_transition_to_ready(instance
, 1)) {
2512 printk(KERN_NOTICE
"megaraid_sas:adapter not ready\n");
2514 megaraid_sas_kill_hba(instance
);
2515 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2519 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
2520 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
2521 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)
2523 *instance
->consumer
= *instance
->producer
;
2525 *instance
->consumer
= 0;
2526 *instance
->producer
= 0;
2529 megasas_issue_init_mfi(instance
);
2531 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2532 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2533 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2534 instance
->instancet
->enable_intr(instance
->reg_set
);
2536 megasas_issue_pending_cmds_again(instance
);
2537 instance
->issuepend_done
= 1;
2543 * megasas_deplete_reply_queue - Processes all completed commands
2544 * @instance: Adapter soft state
2545 * @alt_status: Alternate status to be returned to
2546 * SCSI mid-layer instead of the status
2547 * returned by the FW
2548 * Note: this must be called with hba lock held
2551 megasas_deplete_reply_queue(struct megasas_instance
*instance
,
2557 if ((mfiStatus
= instance
->instancet
->check_reset(instance
,
2558 instance
->reg_set
)) == 1) {
2562 if ((mfiStatus
= instance
->instancet
->clear_intr(
2565 /* Hardware may not set outbound_intr_status in MSI-X mode */
2566 if (!instance
->msix_vectors
)
2570 instance
->mfiStatus
= mfiStatus
;
2572 if ((mfiStatus
& MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
)) {
2573 fw_state
= instance
->instancet
->read_fw_status_reg(
2574 instance
->reg_set
) & MFI_STATE_MASK
;
2576 if (fw_state
!= MFI_STATE_FAULT
) {
2577 printk(KERN_NOTICE
"megaraid_sas: fw state:%x\n",
2581 if ((fw_state
== MFI_STATE_FAULT
) &&
2582 (instance
->disableOnlineCtrlReset
== 0)) {
2583 printk(KERN_NOTICE
"megaraid_sas: wait adp restart\n");
2585 if ((instance
->pdev
->device
==
2586 PCI_DEVICE_ID_LSI_SAS1064R
) ||
2587 (instance
->pdev
->device
==
2588 PCI_DEVICE_ID_DELL_PERC5
) ||
2589 (instance
->pdev
->device
==
2590 PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
2592 *instance
->consumer
=
2593 MEGASAS_ADPRESET_INPROG_SIGN
;
2597 instance
->instancet
->disable_intr(instance
->reg_set
);
2598 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2599 instance
->issuepend_done
= 0;
2601 atomic_set(&instance
->fw_outstanding
, 0);
2602 megasas_internal_reset_defer_cmds(instance
);
2604 printk(KERN_NOTICE
"megasas: fwState=%x, stage:%d\n",
2605 fw_state
, instance
->adprecovery
);
2607 schedule_work(&instance
->work_init
);
2611 printk(KERN_NOTICE
"megasas: fwstate:%x, dis_OCR=%x\n",
2612 fw_state
, instance
->disableOnlineCtrlReset
);
2616 tasklet_schedule(&instance
->isr_tasklet
);
2620 * megasas_isr - isr entry point
2622 static irqreturn_t
megasas_isr(int irq
, void *devp
)
2624 struct megasas_irq_context
*irq_context
= devp
;
2625 struct megasas_instance
*instance
= irq_context
->instance
;
2626 unsigned long flags
;
2629 if (atomic_read(&instance
->fw_reset_no_pci_access
))
2632 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2633 rc
= megasas_deplete_reply_queue(instance
, DID_OK
);
2634 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2640 * megasas_transition_to_ready - Move the FW to READY state
2641 * @instance: Adapter soft state
2643 * During the initialization, FW passes can potentially be in any one of
2644 * several possible states. If the FW in operational, waiting-for-handshake
2645 * states, driver must take steps to bring it to ready state. Otherwise, it
2646 * has to wait for the ready state.
2649 megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
)
2655 u32 abs_state
, curr_abs_state
;
2657 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
2659 if (fw_state
!= MFI_STATE_READY
)
2660 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
2663 while (fw_state
!= MFI_STATE_READY
) {
2666 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2670 case MFI_STATE_FAULT
:
2671 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
2673 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2674 cur_state
= MFI_STATE_FAULT
;
2679 case MFI_STATE_WAIT_HANDSHAKE
:
2681 * Set the CLR bit in inbound doorbell
2683 if ((instance
->pdev
->device
==
2684 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2685 (instance
->pdev
->device
==
2686 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2687 (instance
->pdev
->device
==
2688 PCI_DEVICE_ID_LSI_FUSION
) ||
2689 (instance
->pdev
->device
==
2690 PCI_DEVICE_ID_LSI_INVADER
) ||
2691 (instance
->pdev
->device
==
2692 PCI_DEVICE_ID_LSI_FURY
)) {
2694 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2695 &instance
->reg_set
->doorbell
);
2698 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2699 &instance
->reg_set
->inbound_doorbell
);
2702 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2703 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
2706 case MFI_STATE_BOOT_MESSAGE_PENDING
:
2707 if ((instance
->pdev
->device
==
2708 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2709 (instance
->pdev
->device
==
2710 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2711 (instance
->pdev
->device
==
2712 PCI_DEVICE_ID_LSI_FUSION
) ||
2713 (instance
->pdev
->device
==
2714 PCI_DEVICE_ID_LSI_INVADER
) ||
2715 (instance
->pdev
->device
==
2716 PCI_DEVICE_ID_LSI_FURY
)) {
2717 writel(MFI_INIT_HOTPLUG
,
2718 &instance
->reg_set
->doorbell
);
2720 writel(MFI_INIT_HOTPLUG
,
2721 &instance
->reg_set
->inbound_doorbell
);
2723 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2724 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
2727 case MFI_STATE_OPERATIONAL
:
2729 * Bring it to READY state; assuming max wait 10 secs
2731 instance
->instancet
->disable_intr(instance
->reg_set
);
2732 if ((instance
->pdev
->device
==
2733 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2734 (instance
->pdev
->device
==
2735 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2736 (instance
->pdev
->device
2737 == PCI_DEVICE_ID_LSI_FUSION
) ||
2738 (instance
->pdev
->device
2739 == PCI_DEVICE_ID_LSI_INVADER
) ||
2740 (instance
->pdev
->device
2741 == PCI_DEVICE_ID_LSI_FURY
)) {
2742 writel(MFI_RESET_FLAGS
,
2743 &instance
->reg_set
->doorbell
);
2744 if ((instance
->pdev
->device
==
2745 PCI_DEVICE_ID_LSI_FUSION
) ||
2746 (instance
->pdev
->device
==
2747 PCI_DEVICE_ID_LSI_INVADER
) ||
2748 (instance
->pdev
->device
==
2749 PCI_DEVICE_ID_LSI_FURY
)) {
2750 for (i
= 0; i
< (10 * 1000); i
+= 20) {
2761 writel(MFI_RESET_FLAGS
,
2762 &instance
->reg_set
->inbound_doorbell
);
2764 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2765 cur_state
= MFI_STATE_OPERATIONAL
;
2768 case MFI_STATE_UNDEFINED
:
2770 * This state should not last for more than 2 seconds
2772 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2773 cur_state
= MFI_STATE_UNDEFINED
;
2776 case MFI_STATE_BB_INIT
:
2777 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2778 cur_state
= MFI_STATE_BB_INIT
;
2781 case MFI_STATE_FW_INIT
:
2782 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2783 cur_state
= MFI_STATE_FW_INIT
;
2786 case MFI_STATE_FW_INIT_2
:
2787 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2788 cur_state
= MFI_STATE_FW_INIT_2
;
2791 case MFI_STATE_DEVICE_SCAN
:
2792 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2793 cur_state
= MFI_STATE_DEVICE_SCAN
;
2796 case MFI_STATE_FLUSH_CACHE
:
2797 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2798 cur_state
= MFI_STATE_FLUSH_CACHE
;
2802 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
2808 * The cur_state should not last for more than max_wait secs
2810 for (i
= 0; i
< (max_wait
* 1000); i
++) {
2811 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
2814 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2816 if (abs_state
== curr_abs_state
) {
2823 * Return error if fw_state hasn't changed after max_wait
2825 if (curr_abs_state
== abs_state
) {
2826 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
2827 "in %d secs\n", fw_state
, max_wait
);
2831 printk(KERN_INFO
"megasas: FW now in Ready state\n");
2837 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2838 * @instance: Adapter soft state
2840 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
2843 u32 max_cmd
= instance
->max_mfi_cmds
;
2844 struct megasas_cmd
*cmd
;
2846 if (!instance
->frame_dma_pool
)
2850 * Return all frames to pool
2852 for (i
= 0; i
< max_cmd
; i
++) {
2854 cmd
= instance
->cmd_list
[i
];
2857 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
2858 cmd
->frame_phys_addr
);
2861 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
2862 cmd
->sense_phys_addr
);
2866 * Now destroy the pool itself
2868 pci_pool_destroy(instance
->frame_dma_pool
);
2869 pci_pool_destroy(instance
->sense_dma_pool
);
2871 instance
->frame_dma_pool
= NULL
;
2872 instance
->sense_dma_pool
= NULL
;
2876 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2877 * @instance: Adapter soft state
2879 * Each command packet has an embedded DMA memory buffer that is used for
2880 * filling MFI frame and the SG list that immediately follows the frame. This
2881 * function creates those DMA memory buffers for each command packet by using
2882 * PCI pool facility.
2884 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
2892 struct megasas_cmd
*cmd
;
2894 max_cmd
= instance
->max_mfi_cmds
;
2897 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2898 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2900 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
2901 sizeof(struct megasas_sge32
);
2903 if (instance
->flag_ieee
) {
2904 sge_sz
= sizeof(struct megasas_sge_skinny
);
2908 * Calculated the number of 64byte frames required for SGL
2910 sgl_sz
= sge_sz
* instance
->max_num_sge
;
2911 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
2915 * We need one extra frame for the MFI command
2919 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
2921 * Use DMA pool facility provided by PCI layer
2923 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
2924 instance
->pdev
, total_sz
, 64,
2927 if (!instance
->frame_dma_pool
) {
2928 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
2932 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
2933 instance
->pdev
, 128, 4, 0);
2935 if (!instance
->sense_dma_pool
) {
2936 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
2938 pci_pool_destroy(instance
->frame_dma_pool
);
2939 instance
->frame_dma_pool
= NULL
;
2945 * Allocate and attach a frame to each of the commands in cmd_list.
2946 * By making cmd->index as the context instead of the &cmd, we can
2947 * always use 32bit context regardless of the architecture
2949 for (i
= 0; i
< max_cmd
; i
++) {
2951 cmd
= instance
->cmd_list
[i
];
2953 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
2954 GFP_KERNEL
, &cmd
->frame_phys_addr
);
2956 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
2957 GFP_KERNEL
, &cmd
->sense_phys_addr
);
2960 * megasas_teardown_frame_pool() takes care of freeing
2961 * whatever has been allocated
2963 if (!cmd
->frame
|| !cmd
->sense
) {
2964 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
2965 megasas_teardown_frame_pool(instance
);
2969 memset(cmd
->frame
, 0, total_sz
);
2970 cmd
->frame
->io
.context
= cmd
->index
;
2971 cmd
->frame
->io
.pad_0
= 0;
2972 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
2973 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
2974 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
2976 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
2983 * megasas_free_cmds - Free all the cmds in the free cmd pool
2984 * @instance: Adapter soft state
2986 void megasas_free_cmds(struct megasas_instance
*instance
)
2989 /* First free the MFI frame pool */
2990 megasas_teardown_frame_pool(instance
);
2992 /* Free all the commands in the cmd_list */
2993 for (i
= 0; i
< instance
->max_mfi_cmds
; i
++)
2995 kfree(instance
->cmd_list
[i
]);
2997 /* Free the cmd_list buffer itself */
2998 kfree(instance
->cmd_list
);
2999 instance
->cmd_list
= NULL
;
3001 INIT_LIST_HEAD(&instance
->cmd_pool
);
3005 * megasas_alloc_cmds - Allocates the command packets
3006 * @instance: Adapter soft state
3008 * Each command that is issued to the FW, whether IO commands from the OS or
3009 * internal commands like IOCTLs, are wrapped in local data structure called
3010 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3013 * Each frame has a 32-bit field called context (tag). This context is used
3014 * to get back the megasas_cmd from the frame when a frame gets completed in
3015 * the ISR. Typically the address of the megasas_cmd itself would be used as
3016 * the context. But we wanted to keep the differences between 32 and 64 bit
3017 * systems to the mininum. We always use 32 bit integers for the context. In
3018 * this driver, the 32 bit values are the indices into an array cmd_list.
3019 * This array is used only to look up the megasas_cmd given the context. The
3020 * free commands themselves are maintained in a linked list called cmd_pool.
3022 int megasas_alloc_cmds(struct megasas_instance
*instance
)
3027 struct megasas_cmd
*cmd
;
3029 max_cmd
= instance
->max_mfi_cmds
;
3032 * instance->cmd_list is an array of struct megasas_cmd pointers.
3033 * Allocate the dynamic array first and then allocate individual
3036 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
3038 if (!instance
->cmd_list
) {
3039 printk(KERN_DEBUG
"megasas: out of memory\n");
3043 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) *max_cmd
);
3045 for (i
= 0; i
< max_cmd
; i
++) {
3046 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
3049 if (!instance
->cmd_list
[i
]) {
3051 for (j
= 0; j
< i
; j
++)
3052 kfree(instance
->cmd_list
[j
]);
3054 kfree(instance
->cmd_list
);
3055 instance
->cmd_list
= NULL
;
3062 * Add all the commands to command pool (instance->cmd_pool)
3064 for (i
= 0; i
< max_cmd
; i
++) {
3065 cmd
= instance
->cmd_list
[i
];
3066 memset(cmd
, 0, sizeof(struct megasas_cmd
));
3069 cmd
->instance
= instance
;
3071 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
3075 * Create a frame pool and assign one frame to each cmd
3077 if (megasas_create_frame_pool(instance
)) {
3078 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
3079 megasas_free_cmds(instance
);
3086 * megasas_get_pd_list_info - Returns FW's pd_list structure
3087 * @instance: Adapter soft state
3088 * @pd_list: pd_list structure
3090 * Issues an internal command (DCMD) to get the FW's controller PD
3091 * list structure. This information is mainly used to find out SYSTEM
3092 * supported by the FW.
3095 megasas_get_pd_list(struct megasas_instance
*instance
)
3097 int ret
= 0, pd_index
= 0;
3098 struct megasas_cmd
*cmd
;
3099 struct megasas_dcmd_frame
*dcmd
;
3100 struct MR_PD_LIST
*ci
;
3101 struct MR_PD_ADDRESS
*pd_addr
;
3102 dma_addr_t ci_h
= 0;
3104 cmd
= megasas_get_cmd(instance
);
3107 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
3111 dcmd
= &cmd
->frame
->dcmd
;
3113 ci
= pci_alloc_consistent(instance
->pdev
,
3114 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
3117 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
3118 megasas_return_cmd(instance
, cmd
);
3122 memset(ci
, 0, sizeof(*ci
));
3123 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3125 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
3126 dcmd
->mbox
.b
[1] = 0;
3127 dcmd
->cmd
= MFI_CMD_DCMD
;
3128 dcmd
->cmd_status
= 0xFF;
3129 dcmd
->sge_count
= 1;
3130 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3133 dcmd
->data_xfer_len
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3134 dcmd
->opcode
= MR_DCMD_PD_LIST_QUERY
;
3135 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3136 dcmd
->sgl
.sge32
[0].length
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3138 if (!megasas_issue_polled(instance
, cmd
)) {
3145 * the following function will get the instance PD LIST.
3152 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
3154 memset(instance
->pd_list
, 0,
3155 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
3157 for (pd_index
= 0; pd_index
< ci
->count
; pd_index
++) {
3159 instance
->pd_list
[pd_addr
->deviceId
].tid
=
3161 instance
->pd_list
[pd_addr
->deviceId
].driveType
=
3162 pd_addr
->scsiDevType
;
3163 instance
->pd_list
[pd_addr
->deviceId
].driveState
=
3169 pci_free_consistent(instance
->pdev
,
3170 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
3172 megasas_return_cmd(instance
, cmd
);
3178 * megasas_get_ld_list_info - Returns FW's ld_list structure
3179 * @instance: Adapter soft state
3180 * @ld_list: ld_list structure
3182 * Issues an internal command (DCMD) to get the FW's controller PD
3183 * list structure. This information is mainly used to find out SYSTEM
3184 * supported by the FW.
3187 megasas_get_ld_list(struct megasas_instance
*instance
)
3189 int ret
= 0, ld_index
= 0, ids
= 0;
3190 struct megasas_cmd
*cmd
;
3191 struct megasas_dcmd_frame
*dcmd
;
3192 struct MR_LD_LIST
*ci
;
3193 dma_addr_t ci_h
= 0;
3195 cmd
= megasas_get_cmd(instance
);
3198 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
3202 dcmd
= &cmd
->frame
->dcmd
;
3204 ci
= pci_alloc_consistent(instance
->pdev
,
3205 sizeof(struct MR_LD_LIST
),
3209 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
3210 megasas_return_cmd(instance
, cmd
);
3214 memset(ci
, 0, sizeof(*ci
));
3215 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3217 dcmd
->cmd
= MFI_CMD_DCMD
;
3218 dcmd
->cmd_status
= 0xFF;
3219 dcmd
->sge_count
= 1;
3220 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3222 dcmd
->data_xfer_len
= sizeof(struct MR_LD_LIST
);
3223 dcmd
->opcode
= MR_DCMD_LD_GET_LIST
;
3224 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3225 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_LD_LIST
);
3228 if (!megasas_issue_polled(instance
, cmd
)) {
3234 /* the following function will get the instance PD LIST */
3236 if ((ret
== 0) && (ci
->ldCount
<= MAX_LOGICAL_DRIVES
)) {
3237 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3239 for (ld_index
= 0; ld_index
< ci
->ldCount
; ld_index
++) {
3240 if (ci
->ldList
[ld_index
].state
!= 0) {
3241 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
3242 instance
->ld_ids
[ids
] =
3243 ci
->ldList
[ld_index
].ref
.targetId
;
3248 pci_free_consistent(instance
->pdev
,
3249 sizeof(struct MR_LD_LIST
),
3253 megasas_return_cmd(instance
, cmd
);
3258 * megasas_get_controller_info - Returns FW's controller structure
3259 * @instance: Adapter soft state
3260 * @ctrl_info: Controller information structure
3262 * Issues an internal command (DCMD) to get the FW's controller structure.
3263 * This information is mainly used to find out the maximum IO transfer per
3264 * command supported by the FW.
3267 megasas_get_ctrl_info(struct megasas_instance
*instance
,
3268 struct megasas_ctrl_info
*ctrl_info
)
3271 struct megasas_cmd
*cmd
;
3272 struct megasas_dcmd_frame
*dcmd
;
3273 struct megasas_ctrl_info
*ci
;
3274 dma_addr_t ci_h
= 0;
3276 cmd
= megasas_get_cmd(instance
);
3279 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
3283 dcmd
= &cmd
->frame
->dcmd
;
3285 ci
= pci_alloc_consistent(instance
->pdev
,
3286 sizeof(struct megasas_ctrl_info
), &ci_h
);
3289 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
3290 megasas_return_cmd(instance
, cmd
);
3294 memset(ci
, 0, sizeof(*ci
));
3295 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3297 dcmd
->cmd
= MFI_CMD_DCMD
;
3298 dcmd
->cmd_status
= 0xFF;
3299 dcmd
->sge_count
= 1;
3300 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3303 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
3304 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
3305 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3306 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
3308 if (!megasas_issue_polled(instance
, cmd
)) {
3310 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
3315 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
3318 megasas_return_cmd(instance
, cmd
);
3323 * megasas_issue_init_mfi - Initializes the FW
3324 * @instance: Adapter soft state
3326 * Issues the INIT MFI cmd
3329 megasas_issue_init_mfi(struct megasas_instance
*instance
)
3333 struct megasas_cmd
*cmd
;
3335 struct megasas_init_frame
*init_frame
;
3336 struct megasas_init_queue_info
*initq_info
;
3337 dma_addr_t init_frame_h
;
3338 dma_addr_t initq_info_h
;
3341 * Prepare a init frame. Note the init frame points to queue info
3342 * structure. Each frame has SGL allocated after first 64 bytes. For
3343 * this frame - since we don't need any SGL - we use SGL's space as
3344 * queue info structure
3346 * We will not get a NULL command below. We just created the pool.
3348 cmd
= megasas_get_cmd(instance
);
3350 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
3351 initq_info
= (struct megasas_init_queue_info
*)
3352 ((unsigned long)init_frame
+ 64);
3354 init_frame_h
= cmd
->frame_phys_addr
;
3355 initq_info_h
= init_frame_h
+ 64;
3357 context
= init_frame
->context
;
3358 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
3359 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
3360 init_frame
->context
= context
;
3362 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
3363 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
3365 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
3366 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
3368 init_frame
->cmd
= MFI_CMD_INIT
;
3369 init_frame
->cmd_status
= 0xFF;
3370 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
3372 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
3375 * disable the intr before firing the init frame to FW
3377 instance
->instancet
->disable_intr(instance
->reg_set
);
3380 * Issue the init frame in polled mode
3383 if (megasas_issue_polled(instance
, cmd
)) {
3384 printk(KERN_ERR
"megasas: Failed to init firmware\n");
3385 megasas_return_cmd(instance
, cmd
);
3389 megasas_return_cmd(instance
, cmd
);
3398 megasas_init_adapter_mfi(struct megasas_instance
*instance
)
3400 struct megasas_register_set __iomem
*reg_set
;
3404 reg_set
= instance
->reg_set
;
3407 * Get various operational parameters from status register
3409 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
3411 * Reduce the max supported cmds by 1. This is to ensure that the
3412 * reply_q_sz (1 more than the max cmd that driver may send)
3413 * does not exceed max cmds that the FW can support
3415 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
3416 instance
->max_mfi_cmds
= instance
->max_fw_cmds
;
3417 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
3420 * Create a pool of commands
3422 if (megasas_alloc_cmds(instance
))
3423 goto fail_alloc_cmds
;
3426 * Allocate memory for reply queue. Length of reply queue should
3427 * be _one_ more than the maximum commands handled by the firmware.
3429 * Note: When FW completes commands, it places corresponding contex
3430 * values in this circular reply queue. This circular queue is a fairly
3431 * typical producer-consumer queue. FW is the producer (of completed
3432 * commands) and the driver is the consumer.
3434 context_sz
= sizeof(u32
);
3435 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
3437 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
3439 &instance
->reply_queue_h
);
3441 if (!instance
->reply_queue
) {
3442 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
3443 goto fail_reply_queue
;
3446 if (megasas_issue_init_mfi(instance
))
3449 instance
->fw_support_ieee
= 0;
3450 instance
->fw_support_ieee
=
3451 (instance
->instancet
->read_fw_status_reg(reg_set
) &
3454 printk(KERN_NOTICE
"megasas_init_mfi: fw_support_ieee=%d",
3455 instance
->fw_support_ieee
);
3457 if (instance
->fw_support_ieee
)
3458 instance
->flag_ieee
= 1;
3464 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3465 instance
->reply_queue
, instance
->reply_queue_h
);
3467 megasas_free_cmds(instance
);
3474 * megasas_init_fw - Initializes the FW
3475 * @instance: Adapter soft state
3477 * This is the main function for initializing firmware
3480 static int megasas_init_fw(struct megasas_instance
*instance
)
3484 u32 tmp_sectors
, msix_enable
;
3485 struct megasas_register_set __iomem
*reg_set
;
3486 struct megasas_ctrl_info
*ctrl_info
;
3487 unsigned long bar_list
;
3490 /* Find first memory bar */
3491 bar_list
= pci_select_bars(instance
->pdev
, IORESOURCE_MEM
);
3492 instance
->bar
= find_first_bit(&bar_list
, sizeof(unsigned long));
3493 instance
->base_addr
= pci_resource_start(instance
->pdev
, instance
->bar
);
3494 if (pci_request_selected_regions(instance
->pdev
, instance
->bar
,
3496 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
3500 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
3502 if (!instance
->reg_set
) {
3503 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
3507 reg_set
= instance
->reg_set
;
3509 switch (instance
->pdev
->device
) {
3510 case PCI_DEVICE_ID_LSI_FUSION
:
3511 case PCI_DEVICE_ID_LSI_INVADER
:
3512 case PCI_DEVICE_ID_LSI_FURY
:
3513 instance
->instancet
= &megasas_instance_template_fusion
;
3515 case PCI_DEVICE_ID_LSI_SAS1078R
:
3516 case PCI_DEVICE_ID_LSI_SAS1078DE
:
3517 instance
->instancet
= &megasas_instance_template_ppc
;
3519 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
3520 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
3521 instance
->instancet
= &megasas_instance_template_gen2
;
3523 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
3524 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
3525 instance
->instancet
= &megasas_instance_template_skinny
;
3527 case PCI_DEVICE_ID_LSI_SAS1064R
:
3528 case PCI_DEVICE_ID_DELL_PERC5
:
3530 instance
->instancet
= &megasas_instance_template_xscale
;
3535 * We expect the FW state to be READY
3537 if (megasas_transition_to_ready(instance
, 0))
3538 goto fail_ready_state
;
3540 /* Check if MSI-X is supported while in ready state */
3541 msix_enable
= (instance
->instancet
->read_fw_status_reg(reg_set
) &
3543 if (msix_enable
&& !msix_disable
) {
3544 /* Check max MSI-X vectors */
3545 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
3546 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
3547 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
3548 instance
->msix_vectors
= (readl(&instance
->reg_set
->
3549 outbound_scratch_pad_2
3552 instance
->msix_vectors
=
3554 instance
->msix_vectors
);
3556 instance
->msix_vectors
= 1;
3557 /* Don't bother allocating more MSI-X vectors than cpus */
3558 instance
->msix_vectors
= min(instance
->msix_vectors
,
3559 (unsigned int)num_online_cpus());
3560 for (i
= 0; i
< instance
->msix_vectors
; i
++)
3561 instance
->msixentry
[i
].entry
= i
;
3562 i
= pci_enable_msix(instance
->pdev
, instance
->msixentry
,
3563 instance
->msix_vectors
);
3566 if (!pci_enable_msix(instance
->pdev
,
3567 instance
->msixentry
, i
))
3568 instance
->msix_vectors
= i
;
3570 instance
->msix_vectors
= 0;
3573 instance
->msix_vectors
= 0;
3576 /* Get operational params, sge flags, send init cmd to controller */
3577 if (instance
->instancet
->init_adapter(instance
))
3578 goto fail_init_adapter
;
3580 printk(KERN_ERR
"megasas: INIT adapter done\n");
3583 * the following function will get the PD LIST.
3586 memset(instance
->pd_list
, 0 ,
3587 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
3588 megasas_get_pd_list(instance
);
3590 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3591 megasas_get_ld_list(instance
);
3593 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
3596 * Compute the max allowed sectors per IO: The controller info has two
3597 * limits on max sectors. Driver should use the minimum of these two.
3599 * 1 << stripe_sz_ops.min = max sectors per strip
3601 * Note that older firmwares ( < FW ver 30) didn't report information
3602 * to calculate max_sectors_1. So the number ended up as zero always.
3605 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
3607 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
3608 ctrl_info
->max_strips_per_io
;
3609 max_sectors_2
= ctrl_info
->max_request_size
;
3611 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
3612 instance
->disableOnlineCtrlReset
=
3613 ctrl_info
->properties
.OnOffProperties
.disableOnlineCtrlReset
;
3616 instance
->max_sectors_per_req
= instance
->max_num_sge
*
3618 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
3619 instance
->max_sectors_per_req
= tmp_sectors
;
3623 /* Check for valid throttlequeuedepth module parameter */
3624 if (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
||
3625 instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) {
3626 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3627 MEGASAS_SKINNY_INT_CMDS
))
3628 instance
->throttlequeuedepth
=
3629 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3631 instance
->throttlequeuedepth
= throttlequeuedepth
;
3633 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3635 instance
->throttlequeuedepth
=
3636 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3638 instance
->throttlequeuedepth
= throttlequeuedepth
;
3642 * Setup tasklet for cmd completion
3645 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
3646 (unsigned long)instance
);
3652 iounmap(instance
->reg_set
);
3655 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3661 * megasas_release_mfi - Reverses the FW initialization
3662 * @intance: Adapter soft state
3664 static void megasas_release_mfi(struct megasas_instance
*instance
)
3666 u32 reply_q_sz
= sizeof(u32
) *(instance
->max_mfi_cmds
+ 1);
3668 if (instance
->reply_queue
)
3669 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3670 instance
->reply_queue
, instance
->reply_queue_h
);
3672 megasas_free_cmds(instance
);
3674 iounmap(instance
->reg_set
);
3676 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3680 * megasas_get_seq_num - Gets latest event sequence numbers
3681 * @instance: Adapter soft state
3682 * @eli: FW event log sequence numbers information
3684 * FW maintains a log of all events in a non-volatile area. Upper layers would
3685 * usually find out the latest sequence number of the events, the seq number at
3686 * the boot etc. They would "read" all the events below the latest seq number
3687 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3688 * number), they would subsribe to AEN (asynchronous event notification) and
3689 * wait for the events to happen.
3692 megasas_get_seq_num(struct megasas_instance
*instance
,
3693 struct megasas_evt_log_info
*eli
)
3695 struct megasas_cmd
*cmd
;
3696 struct megasas_dcmd_frame
*dcmd
;
3697 struct megasas_evt_log_info
*el_info
;
3698 dma_addr_t el_info_h
= 0;
3700 cmd
= megasas_get_cmd(instance
);
3706 dcmd
= &cmd
->frame
->dcmd
;
3707 el_info
= pci_alloc_consistent(instance
->pdev
,
3708 sizeof(struct megasas_evt_log_info
),
3712 megasas_return_cmd(instance
, cmd
);
3716 memset(el_info
, 0, sizeof(*el_info
));
3717 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3719 dcmd
->cmd
= MFI_CMD_DCMD
;
3720 dcmd
->cmd_status
= 0x0;
3721 dcmd
->sge_count
= 1;
3722 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3725 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
3726 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
3727 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
3728 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
3730 megasas_issue_blocked_cmd(instance
, cmd
);
3733 * Copy the data back into callers buffer
3735 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
3737 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
3738 el_info
, el_info_h
);
3740 megasas_return_cmd(instance
, cmd
);
3746 * megasas_register_aen - Registers for asynchronous event notification
3747 * @instance: Adapter soft state
3748 * @seq_num: The starting sequence number
3749 * @class_locale: Class of the event
3751 * This function subscribes for AEN for events beyond the @seq_num. It requests
3752 * to be notified if and only if the event is of type @class_locale
3755 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
3756 u32 class_locale_word
)
3759 struct megasas_cmd
*cmd
;
3760 struct megasas_dcmd_frame
*dcmd
;
3761 union megasas_evt_class_locale curr_aen
;
3762 union megasas_evt_class_locale prev_aen
;
3765 * If there an AEN pending already (aen_cmd), check if the
3766 * class_locale of that pending AEN is inclusive of the new
3767 * AEN request we currently have. If it is, then we don't have
3768 * to do anything. In other words, whichever events the current
3769 * AEN request is subscribing to, have already been subscribed
3772 * If the old_cmd is _not_ inclusive, then we have to abort
3773 * that command, form a class_locale that is superset of both
3774 * old and current and re-issue to the FW
3777 curr_aen
.word
= class_locale_word
;
3779 if (instance
->aen_cmd
) {
3781 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
3784 * A class whose enum value is smaller is inclusive of all
3785 * higher values. If a PROGRESS (= -1) was previously
3786 * registered, then a new registration requests for higher
3787 * classes need not be sent to FW. They are automatically
3790 * Locale numbers don't have such hierarchy. They are bitmap
3793 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
3794 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
3795 curr_aen
.members
.locale
)) {
3797 * Previously issued event registration includes
3798 * current request. Nothing to do.
3802 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
3804 if (prev_aen
.members
.class < curr_aen
.members
.class)
3805 curr_aen
.members
.class = prev_aen
.members
.class;
3807 instance
->aen_cmd
->abort_aen
= 1;
3808 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
3813 printk(KERN_DEBUG
"megasas: Failed to abort "
3814 "previous AEN command\n");
3820 cmd
= megasas_get_cmd(instance
);
3825 dcmd
= &cmd
->frame
->dcmd
;
3827 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
3830 * Prepare DCMD for aen registration
3832 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3834 dcmd
->cmd
= MFI_CMD_DCMD
;
3835 dcmd
->cmd_status
= 0x0;
3836 dcmd
->sge_count
= 1;
3837 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3840 instance
->last_seq_num
= seq_num
;
3841 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
3842 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
3843 dcmd
->mbox
.w
[0] = seq_num
;
3844 dcmd
->mbox
.w
[1] = curr_aen
.word
;
3845 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
3846 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
3848 if (instance
->aen_cmd
!= NULL
) {
3849 megasas_return_cmd(instance
, cmd
);
3854 * Store reference to the cmd used to register for AEN. When an
3855 * application wants us to register for AEN, we have to abort this
3856 * cmd and re-register with a new EVENT LOCALE supplied by that app
3858 instance
->aen_cmd
= cmd
;
3861 * Issue the aen registration frame
3863 instance
->instancet
->issue_dcmd(instance
, cmd
);
3869 * megasas_start_aen - Subscribes to AEN during driver load time
3870 * @instance: Adapter soft state
3872 static int megasas_start_aen(struct megasas_instance
*instance
)
3874 struct megasas_evt_log_info eli
;
3875 union megasas_evt_class_locale class_locale
;
3878 * Get the latest sequence number from FW
3880 memset(&eli
, 0, sizeof(eli
));
3882 if (megasas_get_seq_num(instance
, &eli
))
3886 * Register AEN with FW for latest sequence number plus 1
3888 class_locale
.members
.reserved
= 0;
3889 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
3890 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
3892 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
3897 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3898 * @instance: Adapter soft state
3900 static int megasas_io_attach(struct megasas_instance
*instance
)
3902 struct Scsi_Host
*host
= instance
->host
;
3905 * Export parameters required by SCSI mid-layer
3907 host
->irq
= instance
->pdev
->irq
;
3908 host
->unique_id
= instance
->unique_id
;
3909 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3910 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
3912 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
3915 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
3916 host
->this_id
= instance
->init_id
;
3917 host
->sg_tablesize
= instance
->max_num_sge
;
3919 if (instance
->fw_support_ieee
)
3920 instance
->max_sectors_per_req
= MEGASAS_MAX_SECTORS_IEEE
;
3923 * Check if the module parameter value for max_sectors can be used
3925 if (max_sectors
&& max_sectors
< instance
->max_sectors_per_req
)
3926 instance
->max_sectors_per_req
= max_sectors
;
3929 if (((instance
->pdev
->device
==
3930 PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
3931 (instance
->pdev
->device
==
3932 PCI_DEVICE_ID_LSI_SAS0079GEN2
)) &&
3933 (max_sectors
<= MEGASAS_MAX_SECTORS
)) {
3934 instance
->max_sectors_per_req
= max_sectors
;
3936 printk(KERN_INFO
"megasas: max_sectors should be > 0"
3937 "and <= %d (or < 1MB for GEN2 controller)\n",
3938 instance
->max_sectors_per_req
);
3943 host
->max_sectors
= instance
->max_sectors_per_req
;
3944 host
->cmd_per_lun
= MEGASAS_DEFAULT_CMD_PER_LUN
;
3945 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
3946 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
3947 host
->max_lun
= MEGASAS_MAX_LUN
;
3948 host
->max_cmd_len
= 16;
3950 /* Fusion only supports host reset */
3951 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
3952 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
3953 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
3954 host
->hostt
->eh_device_reset_handler
= NULL
;
3955 host
->hostt
->eh_bus_reset_handler
= NULL
;
3959 * Notify the mid-layer about the new controller
3961 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
3962 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
3967 * Trigger SCSI to scan our drives
3969 scsi_scan_host(host
);
3974 megasas_set_dma_mask(struct pci_dev
*pdev
)
3977 * All our contollers are capable of performing 64-bit DMA
3980 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
3982 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3983 goto fail_set_dma_mask
;
3986 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3987 goto fail_set_dma_mask
;
3996 * megasas_probe_one - PCI hotplug entry point
3997 * @pdev: PCI device structure
3998 * @id: PCI ids of supported hotplugged adapter
4000 static int megasas_probe_one(struct pci_dev
*pdev
,
4001 const struct pci_device_id
*id
)
4003 int rval
, pos
, i
, j
;
4004 struct Scsi_Host
*host
;
4005 struct megasas_instance
*instance
;
4008 /* Reset MSI-X in the kdump kernel */
4009 if (reset_devices
) {
4010 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
4012 pci_read_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
,
4014 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
4015 dev_info(&pdev
->dev
, "resetting MSI-X\n");
4016 pci_write_config_word(pdev
,
4017 pos
+ PCI_MSIX_FLAGS
,
4019 ~PCI_MSIX_FLAGS_ENABLE
);
4025 * Announce PCI information
4027 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4028 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
4029 pdev
->subsystem_device
);
4031 printk("bus %d:slot %d:func %d\n",
4032 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
4035 * PCI prepping: enable device set bus mastering and dma mask
4037 rval
= pci_enable_device_mem(pdev
);
4043 pci_set_master(pdev
);
4045 if (megasas_set_dma_mask(pdev
))
4046 goto fail_set_dma_mask
;
4048 host
= scsi_host_alloc(&megasas_template
,
4049 sizeof(struct megasas_instance
));
4052 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
4053 goto fail_alloc_instance
;
4056 instance
= (struct megasas_instance
*)host
->hostdata
;
4057 memset(instance
, 0, sizeof(*instance
));
4058 atomic_set( &instance
->fw_reset_no_pci_access
, 0 );
4059 instance
->pdev
= pdev
;
4061 switch (instance
->pdev
->device
) {
4062 case PCI_DEVICE_ID_LSI_FUSION
:
4063 case PCI_DEVICE_ID_LSI_INVADER
:
4064 case PCI_DEVICE_ID_LSI_FURY
:
4066 struct fusion_context
*fusion
;
4068 instance
->ctrl_context
=
4069 kzalloc(sizeof(struct fusion_context
), GFP_KERNEL
);
4070 if (!instance
->ctrl_context
) {
4071 printk(KERN_DEBUG
"megasas: Failed to allocate "
4072 "memory for Fusion context info\n");
4073 goto fail_alloc_dma_buf
;
4075 fusion
= instance
->ctrl_context
;
4076 INIT_LIST_HEAD(&fusion
->cmd_pool
);
4077 spin_lock_init(&fusion
->cmd_pool_lock
);
4080 default: /* For all other supported controllers */
4082 instance
->producer
=
4083 pci_alloc_consistent(pdev
, sizeof(u32
),
4084 &instance
->producer_h
);
4085 instance
->consumer
=
4086 pci_alloc_consistent(pdev
, sizeof(u32
),
4087 &instance
->consumer_h
);
4089 if (!instance
->producer
|| !instance
->consumer
) {
4090 printk(KERN_DEBUG
"megasas: Failed to allocate"
4091 "memory for producer, consumer\n");
4092 goto fail_alloc_dma_buf
;
4095 *instance
->producer
= 0;
4096 *instance
->consumer
= 0;
4100 megasas_poll_wait_aen
= 0;
4101 instance
->flag_ieee
= 0;
4102 instance
->ev
= NULL
;
4103 instance
->issuepend_done
= 1;
4104 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
4105 megasas_poll_wait_aen
= 0;
4107 instance
->evt_detail
= pci_alloc_consistent(pdev
,
4109 megasas_evt_detail
),
4110 &instance
->evt_detail_h
);
4112 if (!instance
->evt_detail
) {
4113 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
4114 "event detail structure\n");
4115 goto fail_alloc_dma_buf
;
4119 * Initialize locks and queues
4121 INIT_LIST_HEAD(&instance
->cmd_pool
);
4122 INIT_LIST_HEAD(&instance
->internal_reset_pending_q
);
4124 atomic_set(&instance
->fw_outstanding
,0);
4126 init_waitqueue_head(&instance
->int_cmd_wait_q
);
4127 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
4129 spin_lock_init(&instance
->cmd_pool_lock
);
4130 spin_lock_init(&instance
->hba_lock
);
4131 spin_lock_init(&instance
->completion_lock
);
4133 mutex_init(&instance
->aen_mutex
);
4134 mutex_init(&instance
->reset_mutex
);
4137 * Initialize PCI related and misc parameters
4139 instance
->host
= host
;
4140 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
4141 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
4143 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
4144 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
4145 instance
->flag_ieee
= 1;
4146 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
4148 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
4150 megasas_dbg_lvl
= 0;
4152 instance
->unload
= 1;
4153 instance
->last_time
= 0;
4154 instance
->disableOnlineCtrlReset
= 1;
4156 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4157 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4158 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4159 INIT_WORK(&instance
->work_init
, megasas_fusion_ocr_wq
);
4161 INIT_WORK(&instance
->work_init
, process_fw_state_change_wq
);
4164 * Initialize MFI Firmware
4166 if (megasas_init_fw(instance
))
4172 if (instance
->msix_vectors
) {
4173 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4174 instance
->irq_context
[i
].instance
= instance
;
4175 instance
->irq_context
[i
].MSIxIndex
= i
;
4176 if (request_irq(instance
->msixentry
[i
].vector
,
4177 instance
->instancet
->service_isr
, 0,
4179 &instance
->irq_context
[i
])) {
4180 printk(KERN_DEBUG
"megasas: Failed to "
4181 "register IRQ for vector %d.\n", i
);
4182 for (j
= 0 ; j
< i
; j
++)
4184 instance
->msixentry
[j
].vector
,
4185 &instance
->irq_context
[j
]);
4190 instance
->irq_context
[0].instance
= instance
;
4191 instance
->irq_context
[0].MSIxIndex
= 0;
4192 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4193 IRQF_SHARED
, "megasas",
4194 &instance
->irq_context
[0])) {
4195 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4200 instance
->instancet
->enable_intr(instance
->reg_set
);
4203 * Store instance in PCI softstate
4205 pci_set_drvdata(pdev
, instance
);
4208 * Add this controller to megasas_mgmt_info structure so that it
4209 * can be exported to management applications
4211 megasas_mgmt_info
.count
++;
4212 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
4213 megasas_mgmt_info
.max_index
++;
4216 * Register with SCSI mid-layer
4218 if (megasas_io_attach(instance
))
4219 goto fail_io_attach
;
4221 instance
->unload
= 0;
4224 * Initiate AEN (Asynchronous Event Notification)
4226 if (megasas_start_aen(instance
)) {
4227 printk(KERN_DEBUG
"megasas: start aen failed\n");
4228 goto fail_start_aen
;
4235 megasas_mgmt_info
.count
--;
4236 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
4237 megasas_mgmt_info
.max_index
--;
4239 pci_set_drvdata(pdev
, NULL
);
4240 instance
->instancet
->disable_intr(instance
->reg_set
);
4241 if (instance
->msix_vectors
)
4242 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4243 free_irq(instance
->msixentry
[i
].vector
,
4244 &instance
->irq_context
[i
]);
4246 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4248 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4249 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4250 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4251 megasas_release_fusion(instance
);
4253 megasas_release_mfi(instance
);
4255 if (instance
->msix_vectors
)
4256 pci_disable_msix(instance
->pdev
);
4258 if (instance
->evt_detail
)
4259 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4260 instance
->evt_detail
,
4261 instance
->evt_detail_h
);
4263 if (instance
->producer
)
4264 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4265 instance
->producer_h
);
4266 if (instance
->consumer
)
4267 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4268 instance
->consumer_h
);
4269 scsi_host_put(host
);
4271 fail_alloc_instance
:
4273 pci_disable_device(pdev
);
4279 * megasas_flush_cache - Requests FW to flush all its caches
4280 * @instance: Adapter soft state
4282 static void megasas_flush_cache(struct megasas_instance
*instance
)
4284 struct megasas_cmd
*cmd
;
4285 struct megasas_dcmd_frame
*dcmd
;
4287 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4290 cmd
= megasas_get_cmd(instance
);
4295 dcmd
= &cmd
->frame
->dcmd
;
4297 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4299 dcmd
->cmd
= MFI_CMD_DCMD
;
4300 dcmd
->cmd_status
= 0x0;
4301 dcmd
->sge_count
= 0;
4302 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4305 dcmd
->data_xfer_len
= 0;
4306 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
4307 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
4309 megasas_issue_blocked_cmd(instance
, cmd
);
4311 megasas_return_cmd(instance
, cmd
);
4317 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4318 * @instance: Adapter soft state
4319 * @opcode: Shutdown/Hibernate
4321 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
4324 struct megasas_cmd
*cmd
;
4325 struct megasas_dcmd_frame
*dcmd
;
4327 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4330 cmd
= megasas_get_cmd(instance
);
4335 if (instance
->aen_cmd
)
4336 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
4337 if (instance
->map_update_cmd
)
4338 megasas_issue_blocked_abort_cmd(instance
,
4339 instance
->map_update_cmd
);
4340 dcmd
= &cmd
->frame
->dcmd
;
4342 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4344 dcmd
->cmd
= MFI_CMD_DCMD
;
4345 dcmd
->cmd_status
= 0x0;
4346 dcmd
->sge_count
= 0;
4347 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4350 dcmd
->data_xfer_len
= 0;
4351 dcmd
->opcode
= opcode
;
4353 megasas_issue_blocked_cmd(instance
, cmd
);
4355 megasas_return_cmd(instance
, cmd
);
4362 * megasas_suspend - driver suspend entry point
4363 * @pdev: PCI device structure
4364 * @state: PCI power state to suspend routine
4367 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
4369 struct Scsi_Host
*host
;
4370 struct megasas_instance
*instance
;
4373 instance
= pci_get_drvdata(pdev
);
4374 host
= instance
->host
;
4375 instance
->unload
= 1;
4377 megasas_flush_cache(instance
);
4378 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
4380 /* cancel the delayed work if this work still in queue */
4381 if (instance
->ev
!= NULL
) {
4382 struct megasas_aen_event
*ev
= instance
->ev
;
4383 cancel_delayed_work_sync(&ev
->hotplug_work
);
4384 instance
->ev
= NULL
;
4387 tasklet_kill(&instance
->isr_tasklet
);
4389 pci_set_drvdata(instance
->pdev
, instance
);
4390 instance
->instancet
->disable_intr(instance
->reg_set
);
4392 if (instance
->msix_vectors
)
4393 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4394 free_irq(instance
->msixentry
[i
].vector
,
4395 &instance
->irq_context
[i
]);
4397 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4398 if (instance
->msix_vectors
)
4399 pci_disable_msix(instance
->pdev
);
4401 pci_save_state(pdev
);
4402 pci_disable_device(pdev
);
4404 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
4410 * megasas_resume- driver resume entry point
4411 * @pdev: PCI device structure
4414 megasas_resume(struct pci_dev
*pdev
)
4417 struct Scsi_Host
*host
;
4418 struct megasas_instance
*instance
;
4420 instance
= pci_get_drvdata(pdev
);
4421 host
= instance
->host
;
4422 pci_set_power_state(pdev
, PCI_D0
);
4423 pci_enable_wake(pdev
, PCI_D0
, 0);
4424 pci_restore_state(pdev
);
4427 * PCI prepping: enable device set bus mastering and dma mask
4429 rval
= pci_enable_device_mem(pdev
);
4432 printk(KERN_ERR
"megasas: Enable device failed\n");
4436 pci_set_master(pdev
);
4438 if (megasas_set_dma_mask(pdev
))
4439 goto fail_set_dma_mask
;
4442 * Initialize MFI Firmware
4445 atomic_set(&instance
->fw_outstanding
, 0);
4448 * We expect the FW state to be READY
4450 if (megasas_transition_to_ready(instance
, 0))
4451 goto fail_ready_state
;
4453 /* Now re-enable MSI-X */
4454 if (instance
->msix_vectors
)
4455 pci_enable_msix(instance
->pdev
, instance
->msixentry
,
4456 instance
->msix_vectors
);
4458 switch (instance
->pdev
->device
) {
4459 case PCI_DEVICE_ID_LSI_FUSION
:
4460 case PCI_DEVICE_ID_LSI_INVADER
:
4461 case PCI_DEVICE_ID_LSI_FURY
:
4463 megasas_reset_reply_desc(instance
);
4464 if (megasas_ioc_init_fusion(instance
)) {
4465 megasas_free_cmds(instance
);
4466 megasas_free_cmds_fusion(instance
);
4469 if (!megasas_get_map_info(instance
))
4470 megasas_sync_map_info(instance
);
4474 *instance
->producer
= 0;
4475 *instance
->consumer
= 0;
4476 if (megasas_issue_init_mfi(instance
))
4481 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
4482 (unsigned long)instance
);
4487 if (instance
->msix_vectors
) {
4488 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4489 instance
->irq_context
[i
].instance
= instance
;
4490 instance
->irq_context
[i
].MSIxIndex
= i
;
4491 if (request_irq(instance
->msixentry
[i
].vector
,
4492 instance
->instancet
->service_isr
, 0,
4494 &instance
->irq_context
[i
])) {
4495 printk(KERN_DEBUG
"megasas: Failed to "
4496 "register IRQ for vector %d.\n", i
);
4497 for (j
= 0 ; j
< i
; j
++)
4499 instance
->msixentry
[j
].vector
,
4500 &instance
->irq_context
[j
]);
4505 instance
->irq_context
[0].instance
= instance
;
4506 instance
->irq_context
[0].MSIxIndex
= 0;
4507 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4508 IRQF_SHARED
, "megasas",
4509 &instance
->irq_context
[0])) {
4510 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4515 instance
->instancet
->enable_intr(instance
->reg_set
);
4516 instance
->unload
= 0;
4519 * Initiate AEN (Asynchronous Event Notification)
4521 if (megasas_start_aen(instance
))
4522 printk(KERN_ERR
"megasas: Start AEN failed\n");
4528 if (instance
->evt_detail
)
4529 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4530 instance
->evt_detail
,
4531 instance
->evt_detail_h
);
4533 if (instance
->producer
)
4534 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4535 instance
->producer_h
);
4536 if (instance
->consumer
)
4537 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4538 instance
->consumer_h
);
4539 scsi_host_put(host
);
4544 pci_disable_device(pdev
);
4549 #define megasas_suspend NULL
4550 #define megasas_resume NULL
4554 * megasas_detach_one - PCI hot"un"plug entry point
4555 * @pdev: PCI device structure
4557 static void megasas_detach_one(struct pci_dev
*pdev
)
4560 struct Scsi_Host
*host
;
4561 struct megasas_instance
*instance
;
4562 struct fusion_context
*fusion
;
4564 instance
= pci_get_drvdata(pdev
);
4565 instance
->unload
= 1;
4566 host
= instance
->host
;
4567 fusion
= instance
->ctrl_context
;
4569 scsi_remove_host(instance
->host
);
4570 megasas_flush_cache(instance
);
4571 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4573 /* cancel the delayed work if this work still in queue*/
4574 if (instance
->ev
!= NULL
) {
4575 struct megasas_aen_event
*ev
= instance
->ev
;
4576 cancel_delayed_work_sync(&ev
->hotplug_work
);
4577 instance
->ev
= NULL
;
4580 tasklet_kill(&instance
->isr_tasklet
);
4583 * Take the instance off the instance array. Note that we will not
4584 * decrement the max_index. We let this array be sparse array
4586 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
4587 if (megasas_mgmt_info
.instance
[i
] == instance
) {
4588 megasas_mgmt_info
.count
--;
4589 megasas_mgmt_info
.instance
[i
] = NULL
;
4595 pci_set_drvdata(instance
->pdev
, NULL
);
4597 instance
->instancet
->disable_intr(instance
->reg_set
);
4599 if (instance
->msix_vectors
)
4600 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4601 free_irq(instance
->msixentry
[i
].vector
,
4602 &instance
->irq_context
[i
]);
4604 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4605 if (instance
->msix_vectors
)
4606 pci_disable_msix(instance
->pdev
);
4608 switch (instance
->pdev
->device
) {
4609 case PCI_DEVICE_ID_LSI_FUSION
:
4610 case PCI_DEVICE_ID_LSI_INVADER
:
4611 case PCI_DEVICE_ID_LSI_FURY
:
4612 megasas_release_fusion(instance
);
4613 for (i
= 0; i
< 2 ; i
++)
4614 if (fusion
->ld_map
[i
])
4615 dma_free_coherent(&instance
->pdev
->dev
,
4620 kfree(instance
->ctrl_context
);
4623 megasas_release_mfi(instance
);
4624 pci_free_consistent(pdev
, sizeof(u32
),
4626 instance
->producer_h
);
4627 pci_free_consistent(pdev
, sizeof(u32
),
4629 instance
->consumer_h
);
4633 if (instance
->evt_detail
)
4634 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4635 instance
->evt_detail
, instance
->evt_detail_h
);
4636 scsi_host_put(host
);
4638 pci_set_drvdata(pdev
, NULL
);
4640 pci_disable_device(pdev
);
4646 * megasas_shutdown - Shutdown entry point
4647 * @device: Generic device structure
4649 static void megasas_shutdown(struct pci_dev
*pdev
)
4652 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
4654 instance
->unload
= 1;
4655 megasas_flush_cache(instance
);
4656 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4657 instance
->instancet
->disable_intr(instance
->reg_set
);
4658 if (instance
->msix_vectors
)
4659 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4660 free_irq(instance
->msixentry
[i
].vector
,
4661 &instance
->irq_context
[i
]);
4663 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4664 if (instance
->msix_vectors
)
4665 pci_disable_msix(instance
->pdev
);
4669 * megasas_mgmt_open - char node "open" entry point
4671 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
4674 * Allow only those users with admin rights
4676 if (!capable(CAP_SYS_ADMIN
))
4683 * megasas_mgmt_fasync - Async notifier registration from applications
4685 * This function adds the calling process to a driver global queue. When an
4686 * event occurs, SIGIO will be sent to all processes in this queue.
4688 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
4692 mutex_lock(&megasas_async_queue_mutex
);
4694 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
4696 mutex_unlock(&megasas_async_queue_mutex
);
4699 /* For sanity check when we get ioctl */
4700 filep
->private_data
= filep
;
4704 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
4710 * megasas_mgmt_poll - char node "poll" entry point
4712 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
4715 unsigned long flags
;
4716 poll_wait(file
, &megasas_poll_wait
, wait
);
4717 spin_lock_irqsave(&poll_aen_lock
, flags
);
4718 if (megasas_poll_wait_aen
)
4719 mask
= (POLLIN
| POLLRDNORM
);
4722 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
4727 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4728 * @instance: Adapter soft state
4729 * @argp: User's ioctl packet
4732 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
4733 struct megasas_iocpacket __user
* user_ioc
,
4734 struct megasas_iocpacket
*ioc
)
4736 struct megasas_sge32
*kern_sge32
;
4737 struct megasas_cmd
*cmd
;
4738 void *kbuff_arr
[MAX_IOCTL_SGE
];
4739 dma_addr_t buf_handle
= 0;
4742 dma_addr_t sense_handle
;
4743 unsigned long *sense_ptr
;
4745 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
4747 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
4748 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
4749 ioc
->sge_count
, MAX_IOCTL_SGE
);
4753 cmd
= megasas_get_cmd(instance
);
4755 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
4760 * User's IOCTL packet has 2 frames (maximum). Copy those two
4761 * frames into our cmd's frames. cmd->frame's context will get
4762 * overwritten when we copy from user's frames. So set that value
4765 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
4766 cmd
->frame
->hdr
.context
= cmd
->index
;
4767 cmd
->frame
->hdr
.pad_0
= 0;
4768 cmd
->frame
->hdr
.flags
&= ~(MFI_FRAME_IEEE
| MFI_FRAME_SGL64
|
4772 * The management interface between applications and the fw uses
4773 * MFI frames. E.g, RAID configuration changes, LD property changes
4774 * etc are accomplishes through different kinds of MFI frames. The
4775 * driver needs to care only about substituting user buffers with
4776 * kernel buffers in SGLs. The location of SGL is embedded in the
4777 * struct iocpacket itself.
4779 kern_sge32
= (struct megasas_sge32
*)
4780 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
4783 * For each user buffer, create a mirror buffer and copy in
4785 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4786 if (!ioc
->sgl
[i
].iov_len
)
4789 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
4790 ioc
->sgl
[i
].iov_len
,
4791 &buf_handle
, GFP_KERNEL
);
4792 if (!kbuff_arr
[i
]) {
4793 printk(KERN_DEBUG
"megasas: Failed to alloc "
4794 "kernel SGL buffer for IOCTL \n");
4800 * We don't change the dma_coherent_mask, so
4801 * pci_alloc_consistent only returns 32bit addresses
4803 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
4804 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
4807 * We created a kernel buffer corresponding to the
4808 * user buffer. Now copy in from the user buffer
4810 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
4811 (u32
) (ioc
->sgl
[i
].iov_len
))) {
4817 if (ioc
->sense_len
) {
4818 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4819 &sense_handle
, GFP_KERNEL
);
4826 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
4827 *sense_ptr
= sense_handle
;
4831 * Set the sync_cmd flag so that the ISR knows not to complete this
4832 * cmd to the SCSI mid-layer
4835 megasas_issue_blocked_cmd(instance
, cmd
);
4839 * copy out the kernel buffers to user buffers
4841 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4842 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
4843 ioc
->sgl
[i
].iov_len
)) {
4850 * copy out the sense
4852 if (ioc
->sense_len
) {
4854 * sense_ptr points to the location that has the user
4855 * sense buffer address
4857 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
4860 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
4861 sense
, ioc
->sense_len
)) {
4862 printk(KERN_ERR
"megasas: Failed to copy out to user "
4870 * copy the status codes returned by the fw
4872 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
4873 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
4874 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
4880 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4881 sense
, sense_handle
);
4884 for (i
= 0; i
< ioc
->sge_count
&& kbuff_arr
[i
]; i
++) {
4885 dma_free_coherent(&instance
->pdev
->dev
,
4886 kern_sge32
[i
].length
,
4887 kbuff_arr
[i
], kern_sge32
[i
].phys_addr
);
4890 megasas_return_cmd(instance
, cmd
);
4894 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
4896 struct megasas_iocpacket __user
*user_ioc
=
4897 (struct megasas_iocpacket __user
*)arg
;
4898 struct megasas_iocpacket
*ioc
;
4899 struct megasas_instance
*instance
;
4902 unsigned long flags
;
4903 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
4905 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
4909 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
4914 instance
= megasas_lookup_instance(ioc
->host_no
);
4920 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
4921 printk(KERN_ERR
"Controller in crit error\n");
4926 if (instance
->unload
== 1) {
4932 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4934 if (down_interruptible(&instance
->ioctl_sem
)) {
4935 error
= -ERESTARTSYS
;
4939 for (i
= 0; i
< wait_time
; i
++) {
4941 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4942 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
4943 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4946 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4948 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
4949 printk(KERN_NOTICE
"megasas: waiting"
4950 "for controller reset to finish\n");
4956 spin_lock_irqsave(&instance
->hba_lock
, flags
);
4957 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
4958 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4960 printk(KERN_ERR
"megaraid_sas: timed out while"
4961 "waiting for HBA to recover\n");
4965 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
4967 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
4969 up(&instance
->ioctl_sem
);
4976 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
4978 struct megasas_instance
*instance
;
4979 struct megasas_aen aen
;
4982 unsigned long flags
;
4983 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
4985 if (file
->private_data
!= file
) {
4986 printk(KERN_DEBUG
"megasas: fasync_helper was not "
4991 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
4994 instance
= megasas_lookup_instance(aen
.host_no
);
4999 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
5003 if (instance
->unload
== 1) {
5007 for (i
= 0; i
< wait_time
; i
++) {
5009 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5010 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
5011 spin_unlock_irqrestore(&instance
->hba_lock
,
5016 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5018 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5019 printk(KERN_NOTICE
"megasas: waiting for"
5020 "controller reset to finish\n");
5026 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5027 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5028 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5029 printk(KERN_ERR
"megaraid_sas: timed out while waiting"
5030 "for HBA to recover.\n");
5033 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5035 mutex_lock(&instance
->aen_mutex
);
5036 error
= megasas_register_aen(instance
, aen
.seq_num
,
5037 aen
.class_locale_word
);
5038 mutex_unlock(&instance
->aen_mutex
);
5043 * megasas_mgmt_ioctl - char node ioctl entry point
5046 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5049 case MEGASAS_IOC_FIRMWARE
:
5050 return megasas_mgmt_ioctl_fw(file
, arg
);
5052 case MEGASAS_IOC_GET_AEN
:
5053 return megasas_mgmt_ioctl_aen(file
, arg
);
5059 #ifdef CONFIG_COMPAT
5060 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
5062 struct compat_megasas_iocpacket __user
*cioc
=
5063 (struct compat_megasas_iocpacket __user
*)arg
;
5064 struct megasas_iocpacket __user
*ioc
=
5065 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
5070 if (clear_user(ioc
, sizeof(*ioc
)))
5073 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
5074 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
5075 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
5076 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
5077 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
5078 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
5082 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5083 * sense_len is not null, so prepare the 64bit value under
5084 * the same condition.
5086 if (ioc
->sense_len
) {
5087 void __user
**sense_ioc_ptr
=
5088 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
5089 compat_uptr_t
*sense_cioc_ptr
=
5090 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
5091 if (get_user(ptr
, sense_cioc_ptr
) ||
5092 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
5096 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
5097 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
5098 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
5099 copy_in_user(&ioc
->sgl
[i
].iov_len
,
5100 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
5104 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
5106 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
5107 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
5108 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
5115 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
5119 case MEGASAS_IOC_FIRMWARE32
:
5120 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
5121 case MEGASAS_IOC_GET_AEN
:
5122 return megasas_mgmt_ioctl_aen(file
, arg
);
5130 * File operations structure for management interface
5132 static const struct file_operations megasas_mgmt_fops
= {
5133 .owner
= THIS_MODULE
,
5134 .open
= megasas_mgmt_open
,
5135 .fasync
= megasas_mgmt_fasync
,
5136 .unlocked_ioctl
= megasas_mgmt_ioctl
,
5137 .poll
= megasas_mgmt_poll
,
5138 #ifdef CONFIG_COMPAT
5139 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
5141 .llseek
= noop_llseek
,
5145 * PCI hotplug support registration structure
5147 static struct pci_driver megasas_pci_driver
= {
5149 .name
= "megaraid_sas",
5150 .id_table
= megasas_pci_table
,
5151 .probe
= megasas_probe_one
,
5152 .remove
= megasas_detach_one
,
5153 .suspend
= megasas_suspend
,
5154 .resume
= megasas_resume
,
5155 .shutdown
= megasas_shutdown
,
5159 * Sysfs driver attributes
5161 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
5163 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
5167 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
5170 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
5172 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
5176 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
5180 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
5182 return sprintf(buf
, "%u\n", support_poll_for_event
);
5185 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
5186 megasas_sysfs_show_support_poll_for_event
, NULL
);
5189 megasas_sysfs_show_support_device_change(struct device_driver
*dd
, char *buf
)
5191 return sprintf(buf
, "%u\n", support_device_change
);
5194 static DRIVER_ATTR(support_device_change
, S_IRUGO
,
5195 megasas_sysfs_show_support_device_change
, NULL
);
5198 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
5200 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
5204 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
5207 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
5208 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
5214 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
5215 megasas_sysfs_set_dbg_lvl
);
5218 megasas_aen_polling(struct work_struct
*work
)
5220 struct megasas_aen_event
*ev
=
5221 container_of(work
, struct megasas_aen_event
, hotplug_work
.work
);
5222 struct megasas_instance
*instance
= ev
->instance
;
5223 union megasas_evt_class_locale class_locale
;
5224 struct Scsi_Host
*host
;
5225 struct scsi_device
*sdev1
;
5228 int i
, j
, doscan
= 0;
5233 printk(KERN_ERR
"invalid instance!\n");
5237 instance
->ev
= NULL
;
5238 host
= instance
->host
;
5239 if (instance
->evt_detail
) {
5241 switch (instance
->evt_detail
->code
) {
5242 case MR_EVT_PD_INSERTED
:
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_add_device(host
, i
, j
, 0);
5262 scsi_device_put(sdev1
);
5270 case MR_EVT_PD_REMOVED
:
5271 if (megasas_get_pd_list(instance
) == 0) {
5272 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5274 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5278 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5281 scsi_device_lookup(host
, i
, j
, 0);
5283 if (instance
->pd_list
[pd_index
].driveState
5284 == MR_PD_STATE_SYSTEM
) {
5286 scsi_device_put(sdev1
);
5290 scsi_remove_device(sdev1
);
5291 scsi_device_put(sdev1
);
5300 case MR_EVT_LD_OFFLINE
:
5301 case MR_EVT_CFG_CLEARED
:
5302 case MR_EVT_LD_DELETED
:
5303 megasas_get_ld_list(instance
);
5304 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5306 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
,
5317 if (instance
->ld_ids
[ld_index
] != 0xff) {
5319 scsi_device_put(sdev1
);
5323 scsi_remove_device(sdev1
);
5324 scsi_device_put(sdev1
);
5331 case MR_EVT_LD_CREATED
:
5332 megasas_get_ld_list(instance
);
5333 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5335 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5338 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5340 sdev1
= scsi_device_lookup(host
,
5341 i
+MEGASAS_MAX_LD_CHANNELS
,
5344 if (instance
->ld_ids
[ld_index
] !=
5347 scsi_add_device(host
,
5353 scsi_device_put(sdev1
);
5359 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
5360 case MR_EVT_FOREIGN_CFG_IMPORTED
:
5361 case MR_EVT_LD_STATE_CHANGE
:
5369 printk(KERN_ERR
"invalid evt_detail!\n");
5375 printk(KERN_INFO
"scanning ...\n");
5376 megasas_get_pd_list(instance
);
5377 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5378 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5379 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
5380 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
5381 if (instance
->pd_list
[pd_index
].driveState
==
5382 MR_PD_STATE_SYSTEM
) {
5384 scsi_add_device(host
, i
, j
, 0);
5387 scsi_device_put(sdev1
);
5390 scsi_remove_device(sdev1
);
5391 scsi_device_put(sdev1
);
5397 megasas_get_ld_list(instance
);
5398 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5399 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5401 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5403 sdev1
= scsi_device_lookup(host
,
5404 i
+MEGASAS_MAX_LD_CHANNELS
, j
, 0);
5405 if (instance
->ld_ids
[ld_index
] != 0xff) {
5407 scsi_add_device(host
,
5411 scsi_device_put(sdev1
);
5415 scsi_remove_device(sdev1
);
5416 scsi_device_put(sdev1
);
5423 if ( instance
->aen_cmd
!= NULL
) {
5428 seq_num
= instance
->evt_detail
->seq_num
+ 1;
5430 /* Register AEN with FW for latest sequence number plus 1 */
5431 class_locale
.members
.reserved
= 0;
5432 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
5433 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
5434 mutex_lock(&instance
->aen_mutex
);
5435 error
= megasas_register_aen(instance
, seq_num
,
5437 mutex_unlock(&instance
->aen_mutex
);
5440 printk(KERN_ERR
"register aen failed error %x\n", error
);
5446 * megasas_init - Driver load entry point
5448 static int __init
megasas_init(void)
5453 * Announce driver version and other information
5455 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
5456 MEGASAS_EXT_VERSION
);
5458 spin_lock_init(&poll_aen_lock
);
5460 support_poll_for_event
= 2;
5461 support_device_change
= 1;
5463 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
5466 * Register character device node
5468 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
5471 printk(KERN_DEBUG
"megasas: failed to open device node\n");
5475 megasas_mgmt_majorno
= rval
;
5478 * Register ourselves as PCI hotplug module
5480 rval
= pci_register_driver(&megasas_pci_driver
);
5483 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
5487 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5488 &driver_attr_version
);
5490 goto err_dcf_attr_ver
;
5491 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5492 &driver_attr_release_date
);
5494 goto err_dcf_rel_date
;
5496 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5497 &driver_attr_support_poll_for_event
);
5499 goto err_dcf_support_poll_for_event
;
5501 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5502 &driver_attr_dbg_lvl
);
5504 goto err_dcf_dbg_lvl
;
5505 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5506 &driver_attr_support_device_change
);
5508 goto err_dcf_support_device_change
;
5512 err_dcf_support_device_change
:
5513 driver_remove_file(&megasas_pci_driver
.driver
,
5514 &driver_attr_dbg_lvl
);
5516 driver_remove_file(&megasas_pci_driver
.driver
,
5517 &driver_attr_support_poll_for_event
);
5519 err_dcf_support_poll_for_event
:
5520 driver_remove_file(&megasas_pci_driver
.driver
,
5521 &driver_attr_release_date
);
5524 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5526 pci_unregister_driver(&megasas_pci_driver
);
5528 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5533 * megasas_exit - Driver unload entry point
5535 static void __exit
megasas_exit(void)
5537 driver_remove_file(&megasas_pci_driver
.driver
,
5538 &driver_attr_dbg_lvl
);
5539 driver_remove_file(&megasas_pci_driver
.driver
,
5540 &driver_attr_support_poll_for_event
);
5541 driver_remove_file(&megasas_pci_driver
.driver
,
5542 &driver_attr_support_device_change
);
5543 driver_remove_file(&megasas_pci_driver
.driver
,
5544 &driver_attr_release_date
);
5545 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5547 pci_unregister_driver(&megasas_pci_driver
);
5548 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5551 module_init(megasas_init
);
5552 module_exit(megasas_exit
);