2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2012 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_base.c
21 * Version : 06.600.18.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 int megasas_reset_fusion(struct Scsi_Host
*shost
);
175 void megasas_fusion_ocr_wq(struct work_struct
*work
);
178 megasas_issue_dcmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
180 instance
->instancet
->fire_cmd(instance
,
181 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
185 * megasas_get_cmd - Get a command from the free pool
186 * @instance: Adapter soft state
188 * Returns a free command from the pool
190 struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
194 struct megasas_cmd
*cmd
= NULL
;
196 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
198 if (!list_empty(&instance
->cmd_pool
)) {
199 cmd
= list_entry((&instance
->cmd_pool
)->next
,
200 struct megasas_cmd
, list
);
201 list_del_init(&cmd
->list
);
203 printk(KERN_ERR
"megasas: Command pool empty!\n");
206 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
211 * megasas_return_cmd - Return a cmd to free command pool
212 * @instance: Adapter soft state
213 * @cmd: Command packet to be returned to free command pool
216 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
220 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
223 cmd
->frame_count
= 0;
224 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
225 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
226 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
228 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
229 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
231 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
236 * The following functions are defined for xscale
237 * (deviceid : 1064R, PERC5) controllers
241 * megasas_enable_intr_xscale - Enables interrupts
242 * @regs: MFI register set
245 megasas_enable_intr_xscale(struct megasas_instance
*instance
)
247 struct megasas_register_set __iomem
*regs
;
248 regs
= instance
->reg_set
;
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_instance
*instance
)
262 struct megasas_register_set __iomem
*regs
;
264 regs
= instance
->reg_set
;
265 writel(mask
, ®s
->outbound_intr_mask
);
266 /* Dummy readl to force pci flush */
267 readl(®s
->outbound_intr_mask
);
271 * megasas_read_fw_status_reg_xscale - returns the current FW status value
272 * @regs: MFI register set
275 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
277 return readl(&(regs
)->outbound_msg_0
);
280 * megasas_clear_interrupt_xscale - Check & clear interrupt
281 * @regs: MFI register set
284 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
289 * Check if it is our interrupt
291 status
= readl(®s
->outbound_intr_status
);
293 if (status
& MFI_OB_INTR_STATUS_MASK
)
294 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
295 if (status
& MFI_XSCALE_OMR0_CHANGE_INTERRUPT
)
296 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
299 * Clear the interrupt by writing back the same value
302 writel(status
, ®s
->outbound_intr_status
);
304 /* Dummy readl to force pci flush */
305 readl(®s
->outbound_intr_status
);
311 * megasas_fire_cmd_xscale - Sends command to the FW
312 * @frame_phys_addr : Physical address of cmd
313 * @frame_count : Number of frames for the command
314 * @regs : MFI register set
317 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
318 dma_addr_t frame_phys_addr
,
320 struct megasas_register_set __iomem
*regs
)
323 spin_lock_irqsave(&instance
->hba_lock
, flags
);
324 writel((frame_phys_addr
>> 3)|(frame_count
),
325 &(regs
)->inbound_queue_port
);
326 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
330 * megasas_adp_reset_xscale - For controller reset
331 * @regs: MFI register set
334 megasas_adp_reset_xscale(struct megasas_instance
*instance
,
335 struct megasas_register_set __iomem
*regs
)
339 writel(MFI_ADP_RESET
, ®s
->inbound_doorbell
);
341 for (i
= 0; i
< 3; i
++)
342 msleep(1000); /* sleep for 3 secs */
344 pci_read_config_dword(instance
->pdev
, MFI_1068_PCSR_OFFSET
, &pcidata
);
345 printk(KERN_NOTICE
"pcidata = %x\n", pcidata
);
347 printk(KERN_NOTICE
"mfi 1068 offset read=%x\n", pcidata
);
349 pci_write_config_dword(instance
->pdev
,
350 MFI_1068_PCSR_OFFSET
, pcidata
);
352 for (i
= 0; i
< 2; i
++)
353 msleep(1000); /* need to wait 2 secs again */
356 pci_read_config_dword(instance
->pdev
,
357 MFI_1068_FW_HANDSHAKE_OFFSET
, &pcidata
);
358 printk(KERN_NOTICE
"1068 offset handshake read=%x\n", pcidata
);
359 if ((pcidata
& 0xffff0000) == MFI_1068_FW_READY
) {
360 printk(KERN_NOTICE
"1068 offset pcidt=%x\n", pcidata
);
362 pci_write_config_dword(instance
->pdev
,
363 MFI_1068_FW_HANDSHAKE_OFFSET
, pcidata
);
370 * megasas_check_reset_xscale - For controller reset check
371 * @regs: MFI register set
374 megasas_check_reset_xscale(struct megasas_instance
*instance
,
375 struct megasas_register_set __iomem
*regs
)
378 consumer
= *instance
->consumer
;
380 if ((instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) &&
381 (*instance
->consumer
== MEGASAS_ADPRESET_INPROG_SIGN
)) {
387 static struct megasas_instance_template megasas_instance_template_xscale
= {
389 .fire_cmd
= megasas_fire_cmd_xscale
,
390 .enable_intr
= megasas_enable_intr_xscale
,
391 .disable_intr
= megasas_disable_intr_xscale
,
392 .clear_intr
= megasas_clear_intr_xscale
,
393 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
394 .adp_reset
= megasas_adp_reset_xscale
,
395 .check_reset
= megasas_check_reset_xscale
,
396 .service_isr
= megasas_isr
,
397 .tasklet
= megasas_complete_cmd_dpc
,
398 .init_adapter
= megasas_init_adapter_mfi
,
399 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
400 .issue_dcmd
= megasas_issue_dcmd
,
404 * This is the end of set of functions & definitions specific
405 * to xscale (deviceid : 1064R, PERC5) controllers
409 * The following functions are defined for ppc (deviceid : 0x60)
414 * megasas_enable_intr_ppc - Enables interrupts
415 * @regs: MFI register set
418 megasas_enable_intr_ppc(struct megasas_instance
*instance
)
420 struct megasas_register_set __iomem
*regs
;
421 regs
= instance
->reg_set
;
422 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
424 writel(~0x80000000, &(regs
)->outbound_intr_mask
);
426 /* Dummy readl to force pci flush */
427 readl(®s
->outbound_intr_mask
);
431 * megasas_disable_intr_ppc - Disable interrupt
432 * @regs: MFI register set
435 megasas_disable_intr_ppc(struct megasas_instance
*instance
)
437 struct megasas_register_set __iomem
*regs
;
438 u32 mask
= 0xFFFFFFFF;
439 regs
= instance
->reg_set
;
440 writel(mask
, ®s
->outbound_intr_mask
);
441 /* Dummy readl to force pci flush */
442 readl(®s
->outbound_intr_mask
);
446 * megasas_read_fw_status_reg_ppc - returns the current FW status value
447 * @regs: MFI register set
450 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
452 return readl(&(regs
)->outbound_scratch_pad
);
456 * megasas_clear_interrupt_ppc - Check & clear interrupt
457 * @regs: MFI register set
460 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
462 u32 status
, mfiStatus
= 0;
465 * Check if it is our interrupt
467 status
= readl(®s
->outbound_intr_status
);
469 if (status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)
470 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
472 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
)
473 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
476 * Clear the interrupt by writing back the same value
478 writel(status
, ®s
->outbound_doorbell_clear
);
480 /* Dummy readl to force pci flush */
481 readl(®s
->outbound_doorbell_clear
);
487 * megasas_fire_cmd_ppc - Sends command to the FW
488 * @frame_phys_addr : Physical address of cmd
489 * @frame_count : Number of frames for the command
490 * @regs : MFI register set
493 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
494 dma_addr_t frame_phys_addr
,
496 struct megasas_register_set __iomem
*regs
)
499 spin_lock_irqsave(&instance
->hba_lock
, flags
);
500 writel((frame_phys_addr
| (frame_count
<<1))|1,
501 &(regs
)->inbound_queue_port
);
502 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
506 * megasas_check_reset_ppc - For controller reset check
507 * @regs: MFI register set
510 megasas_check_reset_ppc(struct megasas_instance
*instance
,
511 struct megasas_register_set __iomem
*regs
)
513 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
519 static struct megasas_instance_template megasas_instance_template_ppc
= {
521 .fire_cmd
= megasas_fire_cmd_ppc
,
522 .enable_intr
= megasas_enable_intr_ppc
,
523 .disable_intr
= megasas_disable_intr_ppc
,
524 .clear_intr
= megasas_clear_intr_ppc
,
525 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
526 .adp_reset
= megasas_adp_reset_xscale
,
527 .check_reset
= megasas_check_reset_ppc
,
528 .service_isr
= megasas_isr
,
529 .tasklet
= megasas_complete_cmd_dpc
,
530 .init_adapter
= megasas_init_adapter_mfi
,
531 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
532 .issue_dcmd
= megasas_issue_dcmd
,
536 * megasas_enable_intr_skinny - Enables interrupts
537 * @regs: MFI register set
540 megasas_enable_intr_skinny(struct megasas_instance
*instance
)
542 struct megasas_register_set __iomem
*regs
;
543 regs
= instance
->reg_set
;
544 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
546 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
548 /* Dummy readl to force pci flush */
549 readl(®s
->outbound_intr_mask
);
553 * megasas_disable_intr_skinny - Disables interrupt
554 * @regs: MFI register set
557 megasas_disable_intr_skinny(struct megasas_instance
*instance
)
559 struct megasas_register_set __iomem
*regs
;
560 u32 mask
= 0xFFFFFFFF;
561 regs
= instance
->reg_set
;
562 writel(mask
, ®s
->outbound_intr_mask
);
563 /* Dummy readl to force pci flush */
564 readl(®s
->outbound_intr_mask
);
568 * megasas_read_fw_status_reg_skinny - returns the current FW status value
569 * @regs: MFI register set
572 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
574 return readl(&(regs
)->outbound_scratch_pad
);
578 * megasas_clear_interrupt_skinny - Check & clear interrupt
579 * @regs: MFI register set
582 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
588 * Check if it is our interrupt
590 status
= readl(®s
->outbound_intr_status
);
592 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
597 * Check if it is our interrupt
599 if ((megasas_read_fw_status_reg_skinny(regs
) & MFI_STATE_MASK
) ==
601 mfiStatus
= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
603 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
606 * Clear the interrupt by writing back the same value
608 writel(status
, ®s
->outbound_intr_status
);
611 * dummy read to flush PCI
613 readl(®s
->outbound_intr_status
);
619 * megasas_fire_cmd_skinny - Sends command to the FW
620 * @frame_phys_addr : Physical address of cmd
621 * @frame_count : Number of frames for the command
622 * @regs : MFI register set
625 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
626 dma_addr_t frame_phys_addr
,
628 struct megasas_register_set __iomem
*regs
)
631 spin_lock_irqsave(&instance
->hba_lock
, flags
);
632 writel(0, &(regs
)->inbound_high_queue_port
);
633 writel((frame_phys_addr
| (frame_count
<<1))|1,
634 &(regs
)->inbound_low_queue_port
);
635 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
639 * megasas_check_reset_skinny - For controller reset check
640 * @regs: MFI register set
643 megasas_check_reset_skinny(struct megasas_instance
*instance
,
644 struct megasas_register_set __iomem
*regs
)
646 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
)
652 static struct megasas_instance_template megasas_instance_template_skinny
= {
654 .fire_cmd
= megasas_fire_cmd_skinny
,
655 .enable_intr
= megasas_enable_intr_skinny
,
656 .disable_intr
= megasas_disable_intr_skinny
,
657 .clear_intr
= megasas_clear_intr_skinny
,
658 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
659 .adp_reset
= megasas_adp_reset_gen2
,
660 .check_reset
= megasas_check_reset_skinny
,
661 .service_isr
= megasas_isr
,
662 .tasklet
= megasas_complete_cmd_dpc
,
663 .init_adapter
= megasas_init_adapter_mfi
,
664 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
665 .issue_dcmd
= megasas_issue_dcmd
,
670 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
675 * megasas_enable_intr_gen2 - Enables interrupts
676 * @regs: MFI register set
679 megasas_enable_intr_gen2(struct megasas_instance
*instance
)
681 struct megasas_register_set __iomem
*regs
;
682 regs
= instance
->reg_set
;
683 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
685 /* write ~0x00000005 (4 & 1) to the intr mask*/
686 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
688 /* Dummy readl to force pci flush */
689 readl(®s
->outbound_intr_mask
);
693 * megasas_disable_intr_gen2 - Disables interrupt
694 * @regs: MFI register set
697 megasas_disable_intr_gen2(struct megasas_instance
*instance
)
699 struct megasas_register_set __iomem
*regs
;
700 u32 mask
= 0xFFFFFFFF;
701 regs
= instance
->reg_set
;
702 writel(mask
, ®s
->outbound_intr_mask
);
703 /* Dummy readl to force pci flush */
704 readl(®s
->outbound_intr_mask
);
708 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
709 * @regs: MFI register set
712 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
714 return readl(&(regs
)->outbound_scratch_pad
);
718 * megasas_clear_interrupt_gen2 - Check & clear interrupt
719 * @regs: MFI register set
722 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
727 * Check if it is our interrupt
729 status
= readl(®s
->outbound_intr_status
);
731 if (status
& MFI_INTR_FLAG_REPLY_MESSAGE
) {
732 mfiStatus
= MFI_INTR_FLAG_REPLY_MESSAGE
;
734 if (status
& MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT
) {
735 mfiStatus
|= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
;
739 * Clear the interrupt by writing back the same value
742 writel(status
, ®s
->outbound_doorbell_clear
);
744 /* Dummy readl to force pci flush */
745 readl(®s
->outbound_intr_status
);
750 * megasas_fire_cmd_gen2 - Sends command to the FW
751 * @frame_phys_addr : Physical address of cmd
752 * @frame_count : Number of frames for the command
753 * @regs : MFI register set
756 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
757 dma_addr_t frame_phys_addr
,
759 struct megasas_register_set __iomem
*regs
)
762 spin_lock_irqsave(&instance
->hba_lock
, flags
);
763 writel((frame_phys_addr
| (frame_count
<<1))|1,
764 &(regs
)->inbound_queue_port
);
765 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
769 * megasas_adp_reset_gen2 - For controller reset
770 * @regs: MFI register set
773 megasas_adp_reset_gen2(struct megasas_instance
*instance
,
774 struct megasas_register_set __iomem
*reg_set
)
778 u32
*seq_offset
= ®_set
->seq_offset
;
779 u32
*hostdiag_offset
= ®_set
->host_diag
;
781 if (instance
->instancet
== &megasas_instance_template_skinny
) {
782 seq_offset
= ®_set
->fusion_seq_offset
;
783 hostdiag_offset
= ®_set
->fusion_host_diag
;
786 writel(0, seq_offset
);
787 writel(4, seq_offset
);
788 writel(0xb, seq_offset
);
789 writel(2, seq_offset
);
790 writel(7, seq_offset
);
791 writel(0xd, seq_offset
);
795 HostDiag
= (u32
)readl(hostdiag_offset
);
797 while ( !( HostDiag
& DIAG_WRITE_ENABLE
) ) {
799 HostDiag
= (u32
)readl(hostdiag_offset
);
800 printk(KERN_NOTICE
"RESETGEN2: retry=%x, hostdiag=%x\n",
808 printk(KERN_NOTICE
"ADP_RESET_GEN2: HostDiag=%x\n", HostDiag
);
810 writel((HostDiag
| DIAG_RESET_ADAPTER
), hostdiag_offset
);
814 HostDiag
= (u32
)readl(hostdiag_offset
);
815 while ( ( HostDiag
& DIAG_RESET_ADAPTER
) ) {
817 HostDiag
= (u32
)readl(hostdiag_offset
);
818 printk(KERN_NOTICE
"RESET_GEN2: retry=%x, hostdiag=%x\n",
829 * megasas_check_reset_gen2 - For controller reset check
830 * @regs: MFI register set
833 megasas_check_reset_gen2(struct megasas_instance
*instance
,
834 struct megasas_register_set __iomem
*regs
)
836 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
843 static struct megasas_instance_template megasas_instance_template_gen2
= {
845 .fire_cmd
= megasas_fire_cmd_gen2
,
846 .enable_intr
= megasas_enable_intr_gen2
,
847 .disable_intr
= megasas_disable_intr_gen2
,
848 .clear_intr
= megasas_clear_intr_gen2
,
849 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
850 .adp_reset
= megasas_adp_reset_gen2
,
851 .check_reset
= megasas_check_reset_gen2
,
852 .service_isr
= megasas_isr
,
853 .tasklet
= megasas_complete_cmd_dpc
,
854 .init_adapter
= megasas_init_adapter_mfi
,
855 .build_and_issue_cmd
= megasas_build_and_issue_cmd
,
856 .issue_dcmd
= megasas_issue_dcmd
,
860 * This is the end of set of functions & definitions
861 * specific to gen2 (deviceid : 0x78, 0x79) controllers
865 * Template added for TB (Fusion)
867 extern struct megasas_instance_template megasas_instance_template_fusion
;
870 * megasas_issue_polled - Issues a polling command
871 * @instance: Adapter soft state
872 * @cmd: Command packet to be issued
874 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
877 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
880 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
882 frame_hdr
->cmd_status
= 0xFF;
883 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
886 * Issue the frame using inbound queue port
888 instance
->instancet
->issue_dcmd(instance
, cmd
);
891 * Wait for cmd_status to change
893 return wait_and_poll(instance
, cmd
);
897 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
898 * @instance: Adapter soft state
899 * @cmd: Command to be issued
901 * This function waits on an event for the command to be returned from ISR.
902 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
903 * Used to issue ioctl commands.
906 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
907 struct megasas_cmd
*cmd
)
909 cmd
->cmd_status
= ENODATA
;
911 instance
->instancet
->issue_dcmd(instance
, cmd
);
913 wait_event(instance
->int_cmd_wait_q
, cmd
->cmd_status
!= ENODATA
);
919 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
920 * @instance: Adapter soft state
921 * @cmd_to_abort: Previously issued cmd to be aborted
923 * MFI firmware can abort previously issued AEN command (automatic event
924 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
925 * cmd and waits for return status.
926 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
929 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
930 struct megasas_cmd
*cmd_to_abort
)
932 struct megasas_cmd
*cmd
;
933 struct megasas_abort_frame
*abort_fr
;
935 cmd
= megasas_get_cmd(instance
);
940 abort_fr
= &cmd
->frame
->abort
;
943 * Prepare and issue the abort frame
945 abort_fr
->cmd
= MFI_CMD_ABORT
;
946 abort_fr
->cmd_status
= 0xFF;
948 abort_fr
->abort_context
= cmd_to_abort
->index
;
949 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
950 abort_fr
->abort_mfi_phys_addr_hi
= 0;
953 cmd
->cmd_status
= 0xFF;
955 instance
->instancet
->issue_dcmd(instance
, cmd
);
958 * Wait for this cmd to complete
960 wait_event(instance
->abort_cmd_wait_q
, cmd
->cmd_status
!= 0xFF);
963 megasas_return_cmd(instance
, cmd
);
968 * megasas_make_sgl32 - Prepares 32-bit SGL
969 * @instance: Adapter soft state
970 * @scp: SCSI command from the mid-layer
971 * @mfi_sgl: SGL to be filled in
973 * If successful, this function returns the number of SG elements. Otherwise,
977 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
978 union megasas_sgl
*mfi_sgl
)
982 struct scatterlist
*os_sgl
;
984 sge_count
= scsi_dma_map(scp
);
985 BUG_ON(sge_count
< 0);
988 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
989 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
990 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
997 * megasas_make_sgl64 - Prepares 64-bit SGL
998 * @instance: Adapter soft state
999 * @scp: SCSI command from the mid-layer
1000 * @mfi_sgl: SGL to be filled in
1002 * If successful, this function returns the number of SG elements. Otherwise,
1006 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1007 union megasas_sgl
*mfi_sgl
)
1011 struct scatterlist
*os_sgl
;
1013 sge_count
= scsi_dma_map(scp
);
1014 BUG_ON(sge_count
< 0);
1017 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1018 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
1019 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
1026 * megasas_make_sgl_skinny - Prepares IEEE SGL
1027 * @instance: Adapter soft state
1028 * @scp: SCSI command from the mid-layer
1029 * @mfi_sgl: SGL to be filled in
1031 * If successful, this function returns the number of SG elements. Otherwise,
1035 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
1036 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
1040 struct scatterlist
*os_sgl
;
1042 sge_count
= scsi_dma_map(scp
);
1045 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1046 mfi_sgl
->sge_skinny
[i
].length
= sg_dma_len(os_sgl
);
1047 mfi_sgl
->sge_skinny
[i
].phys_addr
=
1048 sg_dma_address(os_sgl
);
1049 mfi_sgl
->sge_skinny
[i
].flag
= 0;
1056 * megasas_get_frame_count - Computes the number of frames
1057 * @frame_type : type of frame- io or pthru frame
1058 * @sge_count : number of sg elements
1060 * Returns the number of frames required for numnber of sge's (sge_count)
1063 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
1064 u8 sge_count
, u8 frame_type
)
1071 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1072 sizeof(struct megasas_sge32
);
1074 if (instance
->flag_ieee
) {
1075 sge_sz
= sizeof(struct megasas_sge_skinny
);
1079 * Main frame can contain 2 SGEs for 64-bit SGLs and
1080 * 3 SGEs for 32-bit SGLs for ldio &
1081 * 1 SGEs for 64-bit SGLs and
1082 * 2 SGEs for 32-bit SGLs for pthru frame
1084 if (unlikely(frame_type
== PTHRU_FRAME
)) {
1085 if (instance
->flag_ieee
== 1) {
1086 num_cnt
= sge_count
- 1;
1087 } else if (IS_DMA64
)
1088 num_cnt
= sge_count
- 1;
1090 num_cnt
= sge_count
- 2;
1092 if (instance
->flag_ieee
== 1) {
1093 num_cnt
= sge_count
- 1;
1094 } else if (IS_DMA64
)
1095 num_cnt
= sge_count
- 2;
1097 num_cnt
= sge_count
- 3;
1101 sge_bytes
= sge_sz
* num_cnt
;
1103 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
1104 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
1109 if (frame_count
> 7)
1115 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1116 * @instance: Adapter soft state
1117 * @scp: SCSI command
1118 * @cmd: Command to be prepared in
1120 * This function prepares CDB commands. These are typcially pass-through
1121 * commands to the devices.
1124 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1125 struct megasas_cmd
*cmd
)
1130 struct megasas_pthru_frame
*pthru
;
1132 is_logical
= MEGASAS_IS_LOGICAL(scp
);
1133 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1134 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
1136 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1137 flags
= MFI_FRAME_DIR_WRITE
;
1138 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1139 flags
= MFI_FRAME_DIR_READ
;
1140 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
1141 flags
= MFI_FRAME_DIR_NONE
;
1143 if (instance
->flag_ieee
== 1) {
1144 flags
|= MFI_FRAME_IEEE
;
1148 * Prepare the DCDB frame
1150 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
1151 pthru
->cmd_status
= 0x0;
1152 pthru
->scsi_status
= 0x0;
1153 pthru
->target_id
= device_id
;
1154 pthru
->lun
= scp
->device
->lun
;
1155 pthru
->cdb_len
= scp
->cmd_len
;
1158 pthru
->flags
= flags
;
1159 pthru
->data_xfer_len
= scsi_bufflen(scp
);
1161 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
1164 * If the command is for the tape device, set the
1165 * pthru timeout to the os layer timeout value.
1167 if (scp
->device
->type
== TYPE_TAPE
) {
1168 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
1169 pthru
->timeout
= 0xFFFF;
1171 pthru
->timeout
= scp
->request
->timeout
/ HZ
;
1177 if (instance
->flag_ieee
== 1) {
1178 pthru
->flags
|= MFI_FRAME_SGL64
;
1179 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1181 } else if (IS_DMA64
) {
1182 pthru
->flags
|= MFI_FRAME_SGL64
;
1183 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
1186 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
1189 if (pthru
->sge_count
> instance
->max_num_sge
) {
1190 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
1196 * Sense info specific
1198 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1199 pthru
->sense_buf_phys_addr_hi
= 0;
1200 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1203 * Compute the total number of frames this command consumes. FW uses
1204 * this number to pull sufficient number of frames from host memory.
1206 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
1209 return cmd
->frame_count
;
1213 * megasas_build_ldio - Prepares IOs to logical devices
1214 * @instance: Adapter soft state
1215 * @scp: SCSI command
1216 * @cmd: Command to be prepared
1218 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1221 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
1222 struct megasas_cmd
*cmd
)
1225 u8 sc
= scp
->cmnd
[0];
1227 struct megasas_io_frame
*ldio
;
1229 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1230 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1232 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1233 flags
= MFI_FRAME_DIR_WRITE
;
1234 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1235 flags
= MFI_FRAME_DIR_READ
;
1237 if (instance
->flag_ieee
== 1) {
1238 flags
|= MFI_FRAME_IEEE
;
1242 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1244 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
1245 ldio
->cmd_status
= 0x0;
1246 ldio
->scsi_status
= 0x0;
1247 ldio
->target_id
= device_id
;
1249 ldio
->reserved_0
= 0;
1251 ldio
->flags
= flags
;
1252 ldio
->start_lba_hi
= 0;
1253 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
1256 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1258 if (scp
->cmd_len
== 6) {
1259 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
1260 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1261 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1263 ldio
->start_lba_lo
&= 0x1FFFFF;
1267 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1269 else if (scp
->cmd_len
== 10) {
1270 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
1271 ((u32
) scp
->cmnd
[7] << 8);
1272 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1273 ((u32
) scp
->cmnd
[3] << 16) |
1274 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1278 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1280 else if (scp
->cmd_len
== 12) {
1281 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
1282 ((u32
) scp
->cmnd
[7] << 16) |
1283 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1285 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1286 ((u32
) scp
->cmnd
[3] << 16) |
1287 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1291 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1293 else if (scp
->cmd_len
== 16) {
1294 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
1295 ((u32
) scp
->cmnd
[11] << 16) |
1296 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1298 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1299 ((u32
) scp
->cmnd
[7] << 16) |
1300 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1302 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1303 ((u32
) scp
->cmnd
[3] << 16) |
1304 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1311 if (instance
->flag_ieee
) {
1312 ldio
->flags
|= MFI_FRAME_SGL64
;
1313 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1315 } else if (IS_DMA64
) {
1316 ldio
->flags
|= MFI_FRAME_SGL64
;
1317 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1319 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1321 if (ldio
->sge_count
> instance
->max_num_sge
) {
1322 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1328 * Sense info specific
1330 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1331 ldio
->sense_buf_phys_addr_hi
= 0;
1332 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1335 * Compute the total number of frames this command consumes. FW uses
1336 * this number to pull sufficient number of frames from host memory.
1338 cmd
->frame_count
= megasas_get_frame_count(instance
,
1339 ldio
->sge_count
, IO_FRAME
);
1341 return cmd
->frame_count
;
1345 * megasas_is_ldio - Checks if the cmd is for logical drive
1346 * @scmd: SCSI command
1348 * Called by megasas_queue_command to find out if the command to be queued
1349 * is a logical drive command
1351 inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1353 if (!MEGASAS_IS_LOGICAL(cmd
))
1355 switch (cmd
->cmnd
[0]) {
1371 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1373 * @instance: Adapter soft state
1376 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1378 struct megasas_cmd
*cmd
;
1380 union megasas_sgl
*mfi_sgl
;
1381 struct megasas_io_frame
*ldio
;
1382 struct megasas_pthru_frame
*pthru
;
1384 u32 max_cmd
= instance
->max_fw_cmds
;
1386 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1387 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1389 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1391 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1393 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1394 for (i
= 0; i
< max_cmd
; i
++) {
1395 cmd
= instance
->cmd_list
[i
];
1398 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1399 if (megasas_is_ldio(cmd
->scmd
)){
1400 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1401 mfi_sgl
= &ldio
->sgl
;
1402 sgcount
= ldio
->sge_count
;
1403 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
);
1406 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1407 mfi_sgl
= &pthru
->sgl
;
1408 sgcount
= pthru
->sge_count
;
1409 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
);
1411 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1412 for (n
= 0; n
< sgcount
; n
++){
1414 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
) ;
1416 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl
->sge32
[n
].length
, mfi_sgl
->sge32
[n
].phys_addr
) ;
1419 printk(KERN_ERR
"\n");
1421 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1422 for (i
= 0; i
< max_cmd
; i
++) {
1424 cmd
= instance
->cmd_list
[i
];
1426 if(cmd
->sync_cmd
== 1){
1427 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1430 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1434 megasas_build_and_issue_cmd(struct megasas_instance
*instance
,
1435 struct scsi_cmnd
*scmd
)
1437 struct megasas_cmd
*cmd
;
1440 cmd
= megasas_get_cmd(instance
);
1442 return SCSI_MLQUEUE_HOST_BUSY
;
1445 * Logical drive command
1447 if (megasas_is_ldio(scmd
))
1448 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1450 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1453 goto out_return_cmd
;
1456 scmd
->SCp
.ptr
= (char *)cmd
;
1459 * Issue the command to the FW
1461 atomic_inc(&instance
->fw_outstanding
);
1463 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1464 cmd
->frame_count
-1, instance
->reg_set
);
1468 megasas_return_cmd(instance
, cmd
);
1474 * megasas_queue_command - Queue entry point
1475 * @scmd: SCSI command to be queued
1476 * @done: Callback entry point
1479 megasas_queue_command_lck(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
1481 struct megasas_instance
*instance
;
1482 unsigned long flags
;
1484 instance
= (struct megasas_instance
*)
1485 scmd
->device
->host
->hostdata
;
1487 if (instance
->issuepend_done
== 0)
1488 return SCSI_MLQUEUE_HOST_BUSY
;
1490 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1492 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1493 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1494 scmd
->result
= DID_ERROR
<< 16;
1499 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1500 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1501 return SCSI_MLQUEUE_HOST_BUSY
;
1504 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1506 scmd
->scsi_done
= done
;
1509 if (MEGASAS_IS_LOGICAL(scmd
) &&
1510 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
1511 scmd
->result
= DID_BAD_TARGET
<< 16;
1515 switch (scmd
->cmnd
[0]) {
1516 case SYNCHRONIZE_CACHE
:
1518 * FW takes care of flush cache on its own
1519 * No need to send it down
1521 scmd
->result
= DID_OK
<< 16;
1527 if (instance
->instancet
->build_and_issue_cmd(instance
, scmd
)) {
1528 printk(KERN_ERR
"megasas: Err returned from build_and_issue_cmd\n");
1529 return SCSI_MLQUEUE_HOST_BUSY
;
1539 static DEF_SCSI_QCMD(megasas_queue_command
)
1541 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1545 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1547 if ((megasas_mgmt_info
.instance
[i
]) &&
1548 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1549 return megasas_mgmt_info
.instance
[i
];
1555 static int megasas_slave_configure(struct scsi_device
*sdev
)
1558 struct megasas_instance
*instance
;
1560 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1563 * Don't export physical disk devices to the disk driver.
1565 * FIXME: Currently we don't export them to the midlayer at all.
1566 * That will be fixed once LSI engineers have audited the
1567 * firmware for possible issues.
1569 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1570 sdev
->type
== TYPE_DISK
) {
1571 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1573 if (instance
->pd_list
[pd_index
].driveState
==
1574 MR_PD_STATE_SYSTEM
) {
1575 blk_queue_rq_timeout(sdev
->request_queue
,
1576 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1583 * The RAID firmware may require extended timeouts.
1585 blk_queue_rq_timeout(sdev
->request_queue
,
1586 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1590 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1593 struct megasas_instance
*instance
;
1594 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1595 if ((sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) &&
1596 (sdev
->type
== TYPE_DISK
)) {
1598 * Open the OS scan to the SYSTEM PD
1601 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1603 if ((instance
->pd_list
[pd_index
].driveState
==
1604 MR_PD_STATE_SYSTEM
) &&
1605 (instance
->pd_list
[pd_index
].driveType
==
1614 void megaraid_sas_kill_hba(struct megasas_instance
*instance
)
1616 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1617 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
1618 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1619 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1620 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1621 writel(MFI_STOP_ADP
, &instance
->reg_set
->doorbell
);
1623 writel(MFI_STOP_ADP
, &instance
->reg_set
->inbound_doorbell
);
1628 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1629 * restored to max value
1630 * @instance: Adapter soft state
1634 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
)
1636 unsigned long flags
;
1637 if (instance
->flag
& MEGASAS_FW_BUSY
1638 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1639 && atomic_read(&instance
->fw_outstanding
) <
1640 instance
->throttlequeuedepth
+ 1) {
1642 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1643 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1644 if (instance
->is_imr
) {
1645 instance
->host
->can_queue
=
1646 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1648 instance
->host
->can_queue
=
1649 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1651 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1656 * megasas_complete_cmd_dpc - Returns FW's controller structure
1657 * @instance_addr: Address of adapter soft state
1659 * Tasklet to complete cmds
1661 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1666 struct megasas_cmd
*cmd
;
1667 struct megasas_instance
*instance
=
1668 (struct megasas_instance
*)instance_addr
;
1669 unsigned long flags
;
1671 /* If we have already declared adapter dead, donot complete cmds */
1672 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1675 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1677 producer
= *instance
->producer
;
1678 consumer
= *instance
->consumer
;
1680 while (consumer
!= producer
) {
1681 context
= instance
->reply_queue
[consumer
];
1682 if (context
>= instance
->max_fw_cmds
) {
1683 printk(KERN_ERR
"Unexpected context value %x\n",
1688 cmd
= instance
->cmd_list
[context
];
1690 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1693 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1698 *instance
->consumer
= producer
;
1700 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1703 * Check if we can restore can_queue
1705 megasas_check_and_restore_queue_depth(instance
);
1709 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
);
1712 process_fw_state_change_wq(struct work_struct
*work
);
1714 void megasas_do_ocr(struct megasas_instance
*instance
)
1716 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
1717 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
1718 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
1719 *instance
->consumer
= MEGASAS_ADPRESET_INPROG_SIGN
;
1721 instance
->instancet
->disable_intr(instance
);
1722 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
1723 instance
->issuepend_done
= 0;
1725 atomic_set(&instance
->fw_outstanding
, 0);
1726 megasas_internal_reset_defer_cmds(instance
);
1727 process_fw_state_change_wq(&instance
->work_init
);
1731 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1732 * @instance: Adapter soft state
1734 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1735 * complete all its outstanding commands. Returns error if one or more IOs
1736 * are pending after this time period. It also marks the controller dead.
1738 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
1742 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
1744 unsigned long flags
;
1745 struct list_head clist_local
;
1746 struct megasas_cmd
*reset_cmd
;
1748 u8 kill_adapter_flag
;
1750 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1751 adprecovery
= instance
->adprecovery
;
1752 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1754 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1756 INIT_LIST_HEAD(&clist_local
);
1757 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1758 list_splice_init(&instance
->internal_reset_pending_q
,
1760 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1762 printk(KERN_NOTICE
"megasas: HBA reset wait ...\n");
1763 for (i
= 0; i
< wait_time
; i
++) {
1765 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1766 adprecovery
= instance
->adprecovery
;
1767 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1768 if (adprecovery
== MEGASAS_HBA_OPERATIONAL
)
1772 if (adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
1773 printk(KERN_NOTICE
"megasas: reset: Stopping HBA.\n");
1774 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1775 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1776 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1781 while (!list_empty(&clist_local
)) {
1782 reset_cmd
= list_entry((&clist_local
)->next
,
1783 struct megasas_cmd
, list
);
1784 list_del_init(&reset_cmd
->list
);
1785 if (reset_cmd
->scmd
) {
1786 reset_cmd
->scmd
->result
= DID_RESET
<< 16;
1787 printk(KERN_NOTICE
"%d:%p reset [%02x]\n",
1788 reset_index
, reset_cmd
,
1789 reset_cmd
->scmd
->cmnd
[0]);
1791 reset_cmd
->scmd
->scsi_done(reset_cmd
->scmd
);
1792 megasas_return_cmd(instance
, reset_cmd
);
1793 } else if (reset_cmd
->sync_cmd
) {
1794 printk(KERN_NOTICE
"megasas:%p synch cmds"
1798 reset_cmd
->cmd_status
= ENODATA
;
1799 instance
->instancet
->fire_cmd(instance
,
1800 reset_cmd
->frame_phys_addr
,
1801 0, instance
->reg_set
);
1803 printk(KERN_NOTICE
"megasas: %p unexpected"
1813 for (i
= 0; i
< resetwaittime
; i
++) {
1815 int outstanding
= atomic_read(&instance
->fw_outstanding
);
1820 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
1821 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
1822 "commands to complete\n",i
,outstanding
);
1824 * Call cmd completion routine. Cmd to be
1825 * be completed directly without depending on isr.
1827 megasas_complete_cmd_dpc((unsigned long)instance
);
1834 kill_adapter_flag
= 0;
1836 fw_state
= instance
->instancet
->read_fw_status_reg(
1837 instance
->reg_set
) & MFI_STATE_MASK
;
1838 if ((fw_state
== MFI_STATE_FAULT
) &&
1839 (instance
->disableOnlineCtrlReset
== 0)) {
1841 kill_adapter_flag
= 2;
1844 megasas_do_ocr(instance
);
1845 kill_adapter_flag
= 1;
1847 /* wait for 1 secs to let FW finish the pending cmds */
1853 if (atomic_read(&instance
->fw_outstanding
) &&
1854 !kill_adapter_flag
) {
1855 if (instance
->disableOnlineCtrlReset
== 0) {
1857 megasas_do_ocr(instance
);
1859 /* wait for 5 secs to let FW finish the pending cmds */
1860 for (i
= 0; i
< wait_time
; i
++) {
1862 atomic_read(&instance
->fw_outstanding
);
1870 if (atomic_read(&instance
->fw_outstanding
) ||
1871 (kill_adapter_flag
== 2)) {
1872 printk(KERN_NOTICE
"megaraid_sas: pending cmds after reset\n");
1874 * Send signal to FW to stop processing any pending cmds.
1875 * The controller will be taken offline by the OS now.
1877 if ((instance
->pdev
->device
==
1878 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1879 (instance
->pdev
->device
==
1880 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1881 writel(MFI_STOP_ADP
,
1882 &instance
->reg_set
->doorbell
);
1884 writel(MFI_STOP_ADP
,
1885 &instance
->reg_set
->inbound_doorbell
);
1887 megasas_dump_pending_frames(instance
);
1888 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1889 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
1890 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1894 printk(KERN_NOTICE
"megaraid_sas: no pending cmds after reset\n");
1900 * megasas_generic_reset - Generic reset routine
1901 * @scmd: Mid-layer SCSI command
1903 * This routine implements a generic reset handler for device, bus and host
1904 * reset requests. Device, bus and host specific reset handlers can use this
1905 * function after they do their specific tasks.
1907 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
1910 struct megasas_instance
*instance
;
1912 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1914 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET cmd=%x retries=%x\n",
1915 scmd
->cmnd
[0], scmd
->retries
);
1917 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1918 printk(KERN_ERR
"megasas: cannot recover from previous reset "
1923 ret_val
= megasas_wait_for_outstanding(instance
);
1924 if (ret_val
== SUCCESS
)
1925 printk(KERN_NOTICE
"megasas: reset successful \n");
1927 printk(KERN_ERR
"megasas: failed to do reset\n");
1933 * megasas_reset_timer - quiesce the adapter if required
1936 * Sets the FW busy flag and reduces the host->can_queue if the
1937 * cmd has not been completed within the timeout period.
1940 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
1942 struct megasas_instance
*instance
;
1943 unsigned long flags
;
1945 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
1946 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
1947 return BLK_EH_NOT_HANDLED
;
1950 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1951 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
1952 /* FW is busy, throttle IO */
1953 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1955 instance
->host
->can_queue
= instance
->throttlequeuedepth
;
1956 instance
->last_time
= jiffies
;
1957 instance
->flag
|= MEGASAS_FW_BUSY
;
1959 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1961 return BLK_EH_RESET_TIMER
;
1965 * megasas_reset_device - Device reset handler entry point
1967 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
1972 * First wait for all commands to complete
1974 ret
= megasas_generic_reset(scmd
);
1980 * megasas_reset_bus_host - Bus & host reset handler entry point
1982 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
1985 struct megasas_instance
*instance
;
1986 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1989 * First wait for all commands to complete
1991 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
1992 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1993 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1994 ret
= megasas_reset_fusion(scmd
->device
->host
);
1996 ret
= megasas_generic_reset(scmd
);
2002 * megasas_bios_param - Returns disk geometry for a disk
2003 * @sdev: device handle
2004 * @bdev: block device
2005 * @capacity: drive capacity
2006 * @geom: geometry parameters
2009 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
2010 sector_t capacity
, int geom
[])
2016 /* Default heads (64) & sectors (32) */
2020 tmp
= heads
* sectors
;
2021 cylinders
= capacity
;
2023 sector_div(cylinders
, tmp
);
2026 * Handle extended translation size for logical drives > 1Gb
2029 if (capacity
>= 0x200000) {
2032 tmp
= heads
*sectors
;
2033 cylinders
= capacity
;
2034 sector_div(cylinders
, tmp
);
2039 geom
[2] = cylinders
;
2044 static void megasas_aen_polling(struct work_struct
*work
);
2047 * megasas_service_aen - Processes an event notification
2048 * @instance: Adapter soft state
2049 * @cmd: AEN command completed by the ISR
2051 * For AEN, driver sends a command down to FW that is held by the FW till an
2052 * event occurs. When an event of interest occurs, FW completes the command
2053 * that it was previously holding.
2055 * This routines sends SIGIO signal to processes that have registered with the
2059 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2061 unsigned long flags
;
2063 * Don't signal app if it is just an aborted previously registered aen
2065 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
2066 spin_lock_irqsave(&poll_aen_lock
, flags
);
2067 megasas_poll_wait_aen
= 1;
2068 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2069 wake_up(&megasas_poll_wait
);
2070 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
2075 instance
->aen_cmd
= NULL
;
2076 megasas_return_cmd(instance
, cmd
);
2078 if ((instance
->unload
== 0) &&
2079 ((instance
->issuepend_done
== 1))) {
2080 struct megasas_aen_event
*ev
;
2081 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
2083 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
2085 ev
->instance
= instance
;
2087 INIT_DELAYED_WORK(&ev
->hotplug_work
,
2088 megasas_aen_polling
);
2089 schedule_delayed_work(&ev
->hotplug_work
, 0);
2094 static int megasas_change_queue_depth(struct scsi_device
*sdev
,
2095 int queue_depth
, int reason
)
2097 if (reason
!= SCSI_QDEPTH_DEFAULT
)
2100 if (queue_depth
> sdev
->host
->can_queue
)
2101 queue_depth
= sdev
->host
->can_queue
;
2102 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
),
2109 * Scsi host template for megaraid_sas driver
2111 static struct scsi_host_template megasas_template
= {
2113 .module
= THIS_MODULE
,
2114 .name
= "LSI SAS based MegaRAID driver",
2115 .proc_name
= "megaraid_sas",
2116 .slave_configure
= megasas_slave_configure
,
2117 .slave_alloc
= megasas_slave_alloc
,
2118 .queuecommand
= megasas_queue_command
,
2119 .eh_device_reset_handler
= megasas_reset_device
,
2120 .eh_bus_reset_handler
= megasas_reset_bus_host
,
2121 .eh_host_reset_handler
= megasas_reset_bus_host
,
2122 .eh_timed_out
= megasas_reset_timer
,
2123 .bios_param
= megasas_bios_param
,
2124 .use_clustering
= ENABLE_CLUSTERING
,
2125 .change_queue_depth
= megasas_change_queue_depth
,
2129 * megasas_complete_int_cmd - Completes an internal command
2130 * @instance: Adapter soft state
2131 * @cmd: Command to be completed
2133 * The megasas_issue_blocked_cmd() function waits for a command to complete
2134 * after it issues a command. This function wakes up that waiting routine by
2135 * calling wake_up() on the wait queue.
2138 megasas_complete_int_cmd(struct megasas_instance
*instance
,
2139 struct megasas_cmd
*cmd
)
2141 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
2143 if (cmd
->cmd_status
== ENODATA
) {
2144 cmd
->cmd_status
= 0;
2146 wake_up(&instance
->int_cmd_wait_q
);
2150 * megasas_complete_abort - Completes aborting a command
2151 * @instance: Adapter soft state
2152 * @cmd: Cmd that was issued to abort another cmd
2154 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2155 * after it issues an abort on a previously issued command. This function
2156 * wakes up all functions waiting on the same wait queue.
2159 megasas_complete_abort(struct megasas_instance
*instance
,
2160 struct megasas_cmd
*cmd
)
2162 if (cmd
->sync_cmd
) {
2164 cmd
->cmd_status
= 0;
2165 wake_up(&instance
->abort_cmd_wait_q
);
2172 * megasas_complete_cmd - Completes a command
2173 * @instance: Adapter soft state
2174 * @cmd: Command to be completed
2175 * @alt_status: If non-zero, use this value as status to
2176 * SCSI mid-layer instead of the value returned
2177 * by the FW. This should be used if caller wants
2178 * an alternate status (as in the case of aborted
2182 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
2186 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
2187 unsigned long flags
;
2188 struct fusion_context
*fusion
= instance
->ctrl_context
;
2190 /* flag for the retry reset */
2191 cmd
->retry_for_fw_reset
= 0;
2194 cmd
->scmd
->SCp
.ptr
= NULL
;
2197 case MFI_CMD_INVALID
:
2198 /* Some older 1068 controller FW may keep a pended
2199 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2200 when booting the kdump kernel. Ignore this command to
2201 prevent a kernel panic on shutdown of the kdump kernel. */
2202 printk(KERN_WARNING
"megaraid_sas: MFI_CMD_INVALID command "
2204 printk(KERN_WARNING
"megaraid_sas: If you have a controller "
2205 "other than PERC5, please upgrade your firmware.\n");
2207 case MFI_CMD_PD_SCSI_IO
:
2208 case MFI_CMD_LD_SCSI_IO
:
2211 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2212 * issued either through an IO path or an IOCTL path. If it
2213 * was via IOCTL, we will send it to internal completion.
2215 if (cmd
->sync_cmd
) {
2217 megasas_complete_int_cmd(instance
, cmd
);
2221 case MFI_CMD_LD_READ
:
2222 case MFI_CMD_LD_WRITE
:
2225 cmd
->scmd
->result
= alt_status
<< 16;
2231 atomic_dec(&instance
->fw_outstanding
);
2233 scsi_dma_unmap(cmd
->scmd
);
2234 cmd
->scmd
->scsi_done(cmd
->scmd
);
2235 megasas_return_cmd(instance
, cmd
);
2240 switch (hdr
->cmd_status
) {
2243 cmd
->scmd
->result
= DID_OK
<< 16;
2246 case MFI_STAT_SCSI_IO_FAILED
:
2247 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2249 (DID_ERROR
<< 16) | hdr
->scsi_status
;
2252 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2254 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
2256 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
2257 memset(cmd
->scmd
->sense_buffer
, 0,
2258 SCSI_SENSE_BUFFERSIZE
);
2259 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
2262 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
2267 case MFI_STAT_LD_OFFLINE
:
2268 case MFI_STAT_DEVICE_NOT_FOUND
:
2269 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
2273 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
2275 cmd
->scmd
->result
= DID_ERROR
<< 16;
2279 atomic_dec(&instance
->fw_outstanding
);
2281 scsi_dma_unmap(cmd
->scmd
);
2282 cmd
->scmd
->scsi_done(cmd
->scmd
);
2283 megasas_return_cmd(instance
, cmd
);
2290 /* Check for LD map update */
2291 if ((cmd
->frame
->dcmd
.opcode
== MR_DCMD_LD_MAP_GET_INFO
) &&
2292 (cmd
->frame
->dcmd
.mbox
.b
[1] == 1)) {
2293 fusion
->fast_path_io
= 0;
2294 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
2295 if (cmd
->frame
->hdr
.cmd_status
!= 0) {
2296 if (cmd
->frame
->hdr
.cmd_status
!=
2298 printk(KERN_WARNING
"megasas: map sync"
2299 "failed, status = 0x%x.\n",
2300 cmd
->frame
->hdr
.cmd_status
);
2302 megasas_return_cmd(instance
, cmd
);
2303 spin_unlock_irqrestore(
2304 instance
->host
->host_lock
,
2310 megasas_return_cmd(instance
, cmd
);
2313 * Set fast path IO to ZERO.
2314 * Validate Map will set proper value.
2315 * Meanwhile all IOs will go as LD IO.
2317 if (MR_ValidateMapInfo(instance
))
2318 fusion
->fast_path_io
= 1;
2320 fusion
->fast_path_io
= 0;
2321 megasas_sync_map_info(instance
);
2322 spin_unlock_irqrestore(instance
->host
->host_lock
,
2326 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
2327 cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET
) {
2328 spin_lock_irqsave(&poll_aen_lock
, flags
);
2329 megasas_poll_wait_aen
= 0;
2330 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
2334 * See if got an event notification
2336 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
2337 megasas_service_aen(instance
, cmd
);
2339 megasas_complete_int_cmd(instance
, cmd
);
2345 * Cmd issued to abort another cmd returned
2347 megasas_complete_abort(instance
, cmd
);
2351 printk("megasas: Unknown command completed! [0x%X]\n",
2358 * megasas_issue_pending_cmds_again - issue all pending cmds
2359 * in FW again because of the fw reset
2360 * @instance: Adapter soft state
2363 megasas_issue_pending_cmds_again(struct megasas_instance
*instance
)
2365 struct megasas_cmd
*cmd
;
2366 struct list_head clist_local
;
2367 union megasas_evt_class_locale class_locale
;
2368 unsigned long flags
;
2371 INIT_LIST_HEAD(&clist_local
);
2372 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2373 list_splice_init(&instance
->internal_reset_pending_q
, &clist_local
);
2374 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2376 while (!list_empty(&clist_local
)) {
2377 cmd
= list_entry((&clist_local
)->next
,
2378 struct megasas_cmd
, list
);
2379 list_del_init(&cmd
->list
);
2381 if (cmd
->sync_cmd
|| cmd
->scmd
) {
2382 printk(KERN_NOTICE
"megaraid_sas: command %p, %p:%d"
2383 "detected to be pending while HBA reset.\n",
2384 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2386 cmd
->retry_for_fw_reset
++;
2388 if (cmd
->retry_for_fw_reset
== 3) {
2389 printk(KERN_NOTICE
"megaraid_sas: cmd %p, %p:%d"
2390 "was tried multiple times during reset."
2391 "Shutting down the HBA\n",
2392 cmd
, cmd
->scmd
, cmd
->sync_cmd
);
2393 megaraid_sas_kill_hba(instance
);
2395 instance
->adprecovery
=
2396 MEGASAS_HW_CRITICAL_ERROR
;
2401 if (cmd
->sync_cmd
== 1) {
2403 printk(KERN_NOTICE
"megaraid_sas: unexpected"
2404 "cmd attached to internal command!\n");
2406 printk(KERN_NOTICE
"megasas: %p synchronous cmd"
2407 "on the internal reset queue,"
2408 "issue it again.\n", cmd
);
2409 cmd
->cmd_status
= ENODATA
;
2410 instance
->instancet
->fire_cmd(instance
,
2411 cmd
->frame_phys_addr
,
2412 0, instance
->reg_set
);
2413 } else if (cmd
->scmd
) {
2414 printk(KERN_NOTICE
"megasas: %p scsi cmd [%02x]"
2415 "detected on the internal queue, issue again.\n",
2416 cmd
, cmd
->scmd
->cmnd
[0]);
2418 atomic_inc(&instance
->fw_outstanding
);
2419 instance
->instancet
->fire_cmd(instance
,
2420 cmd
->frame_phys_addr
,
2421 cmd
->frame_count
-1, instance
->reg_set
);
2423 printk(KERN_NOTICE
"megasas: %p unexpected cmd on the"
2424 "internal reset defer list while re-issue!!\n",
2429 if (instance
->aen_cmd
) {
2430 printk(KERN_NOTICE
"megaraid_sas: aen_cmd in def process\n");
2431 megasas_return_cmd(instance
, instance
->aen_cmd
);
2433 instance
->aen_cmd
= NULL
;
2437 * Initiate AEN (Asynchronous Event Notification)
2439 seq_num
= instance
->last_seq_num
;
2440 class_locale
.members
.reserved
= 0;
2441 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2442 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2444 megasas_register_aen(instance
, seq_num
, class_locale
.word
);
2448 * Move the internal reset pending commands to a deferred queue.
2450 * We move the commands pending at internal reset time to a
2451 * pending queue. This queue would be flushed after successful
2452 * completion of the internal reset sequence. if the internal reset
2453 * did not complete in time, the kernel reset handler would flush
2457 megasas_internal_reset_defer_cmds(struct megasas_instance
*instance
)
2459 struct megasas_cmd
*cmd
;
2461 u32 max_cmd
= instance
->max_fw_cmds
;
2463 unsigned long flags
;
2466 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
2467 for (i
= 0; i
< max_cmd
; i
++) {
2468 cmd
= instance
->cmd_list
[i
];
2469 if (cmd
->sync_cmd
== 1 || cmd
->scmd
) {
2470 printk(KERN_NOTICE
"megasas: moving cmd[%d]:%p:%d:%p"
2471 "on the defer queue as internal\n",
2472 defer_index
, cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2474 if (!list_empty(&cmd
->list
)) {
2475 printk(KERN_NOTICE
"megaraid_sas: ERROR while"
2476 " moving this cmd:%p, %d %p, it was"
2477 "discovered on some list?\n",
2478 cmd
, cmd
->sync_cmd
, cmd
->scmd
);
2480 list_del_init(&cmd
->list
);
2483 list_add_tail(&cmd
->list
,
2484 &instance
->internal_reset_pending_q
);
2487 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
2492 process_fw_state_change_wq(struct work_struct
*work
)
2494 struct megasas_instance
*instance
=
2495 container_of(work
, struct megasas_instance
, work_init
);
2497 unsigned long flags
;
2499 if (instance
->adprecovery
!= MEGASAS_ADPRESET_SM_INFAULT
) {
2500 printk(KERN_NOTICE
"megaraid_sas: error, recovery st %x \n",
2501 instance
->adprecovery
);
2505 if (instance
->adprecovery
== MEGASAS_ADPRESET_SM_INFAULT
) {
2506 printk(KERN_NOTICE
"megaraid_sas: FW detected to be in fault"
2507 "state, restarting it...\n");
2509 instance
->instancet
->disable_intr(instance
);
2510 atomic_set(&instance
->fw_outstanding
, 0);
2512 atomic_set(&instance
->fw_reset_no_pci_access
, 1);
2513 instance
->instancet
->adp_reset(instance
, instance
->reg_set
);
2514 atomic_set(&instance
->fw_reset_no_pci_access
, 0 );
2516 printk(KERN_NOTICE
"megaraid_sas: FW restarted successfully,"
2517 "initiating next stage...\n");
2519 printk(KERN_NOTICE
"megaraid_sas: HBA recovery state machine,"
2520 "state 2 starting...\n");
2522 /*waitting for about 20 second before start the second init*/
2523 for (wait
= 0; wait
< 30; wait
++) {
2527 if (megasas_transition_to_ready(instance
, 1)) {
2528 printk(KERN_NOTICE
"megaraid_sas:adapter not ready\n");
2530 megaraid_sas_kill_hba(instance
);
2531 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2535 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1064R
) ||
2536 (instance
->pdev
->device
== PCI_DEVICE_ID_DELL_PERC5
) ||
2537 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_VERDE_ZCR
)
2539 *instance
->consumer
= *instance
->producer
;
2541 *instance
->consumer
= 0;
2542 *instance
->producer
= 0;
2545 megasas_issue_init_mfi(instance
);
2547 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2548 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2549 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2550 instance
->instancet
->enable_intr(instance
);
2552 megasas_issue_pending_cmds_again(instance
);
2553 instance
->issuepend_done
= 1;
2559 * megasas_deplete_reply_queue - Processes all completed commands
2560 * @instance: Adapter soft state
2561 * @alt_status: Alternate status to be returned to
2562 * SCSI mid-layer instead of the status
2563 * returned by the FW
2564 * Note: this must be called with hba lock held
2567 megasas_deplete_reply_queue(struct megasas_instance
*instance
,
2573 if ((mfiStatus
= instance
->instancet
->check_reset(instance
,
2574 instance
->reg_set
)) == 1) {
2578 if ((mfiStatus
= instance
->instancet
->clear_intr(
2581 /* Hardware may not set outbound_intr_status in MSI-X mode */
2582 if (!instance
->msix_vectors
)
2586 instance
->mfiStatus
= mfiStatus
;
2588 if ((mfiStatus
& MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE
)) {
2589 fw_state
= instance
->instancet
->read_fw_status_reg(
2590 instance
->reg_set
) & MFI_STATE_MASK
;
2592 if (fw_state
!= MFI_STATE_FAULT
) {
2593 printk(KERN_NOTICE
"megaraid_sas: fw state:%x\n",
2597 if ((fw_state
== MFI_STATE_FAULT
) &&
2598 (instance
->disableOnlineCtrlReset
== 0)) {
2599 printk(KERN_NOTICE
"megaraid_sas: wait adp restart\n");
2601 if ((instance
->pdev
->device
==
2602 PCI_DEVICE_ID_LSI_SAS1064R
) ||
2603 (instance
->pdev
->device
==
2604 PCI_DEVICE_ID_DELL_PERC5
) ||
2605 (instance
->pdev
->device
==
2606 PCI_DEVICE_ID_LSI_VERDE_ZCR
)) {
2608 *instance
->consumer
=
2609 MEGASAS_ADPRESET_INPROG_SIGN
;
2613 instance
->instancet
->disable_intr(instance
);
2614 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2615 instance
->issuepend_done
= 0;
2617 atomic_set(&instance
->fw_outstanding
, 0);
2618 megasas_internal_reset_defer_cmds(instance
);
2620 printk(KERN_NOTICE
"megasas: fwState=%x, stage:%d\n",
2621 fw_state
, instance
->adprecovery
);
2623 schedule_work(&instance
->work_init
);
2627 printk(KERN_NOTICE
"megasas: fwstate:%x, dis_OCR=%x\n",
2628 fw_state
, instance
->disableOnlineCtrlReset
);
2632 tasklet_schedule(&instance
->isr_tasklet
);
2636 * megasas_isr - isr entry point
2638 static irqreturn_t
megasas_isr(int irq
, void *devp
)
2640 struct megasas_irq_context
*irq_context
= devp
;
2641 struct megasas_instance
*instance
= irq_context
->instance
;
2642 unsigned long flags
;
2645 if (atomic_read(&instance
->fw_reset_no_pci_access
))
2648 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2649 rc
= megasas_deplete_reply_queue(instance
, DID_OK
);
2650 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2656 * megasas_transition_to_ready - Move the FW to READY state
2657 * @instance: Adapter soft state
2659 * During the initialization, FW passes can potentially be in any one of
2660 * several possible states. If the FW in operational, waiting-for-handshake
2661 * states, driver must take steps to bring it to ready state. Otherwise, it
2662 * has to wait for the ready state.
2665 megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
)
2671 u32 abs_state
, curr_abs_state
;
2673 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
2675 if (fw_state
!= MFI_STATE_READY
)
2676 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
2679 while (fw_state
!= MFI_STATE_READY
) {
2682 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2686 case MFI_STATE_FAULT
:
2687 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
2689 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2690 cur_state
= MFI_STATE_FAULT
;
2695 case MFI_STATE_WAIT_HANDSHAKE
:
2697 * Set the CLR bit in inbound doorbell
2699 if ((instance
->pdev
->device
==
2700 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2701 (instance
->pdev
->device
==
2702 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2703 (instance
->pdev
->device
==
2704 PCI_DEVICE_ID_LSI_FUSION
) ||
2705 (instance
->pdev
->device
==
2706 PCI_DEVICE_ID_LSI_INVADER
) ||
2707 (instance
->pdev
->device
==
2708 PCI_DEVICE_ID_LSI_FURY
)) {
2710 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2711 &instance
->reg_set
->doorbell
);
2714 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
2715 &instance
->reg_set
->inbound_doorbell
);
2718 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2719 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
2722 case MFI_STATE_BOOT_MESSAGE_PENDING
:
2723 if ((instance
->pdev
->device
==
2724 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2725 (instance
->pdev
->device
==
2726 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2727 (instance
->pdev
->device
==
2728 PCI_DEVICE_ID_LSI_FUSION
) ||
2729 (instance
->pdev
->device
==
2730 PCI_DEVICE_ID_LSI_INVADER
) ||
2731 (instance
->pdev
->device
==
2732 PCI_DEVICE_ID_LSI_FURY
)) {
2733 writel(MFI_INIT_HOTPLUG
,
2734 &instance
->reg_set
->doorbell
);
2736 writel(MFI_INIT_HOTPLUG
,
2737 &instance
->reg_set
->inbound_doorbell
);
2739 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2740 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
2743 case MFI_STATE_OPERATIONAL
:
2745 * Bring it to READY state; assuming max wait 10 secs
2747 instance
->instancet
->disable_intr(instance
);
2748 if ((instance
->pdev
->device
==
2749 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2750 (instance
->pdev
->device
==
2751 PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2752 (instance
->pdev
->device
2753 == PCI_DEVICE_ID_LSI_FUSION
) ||
2754 (instance
->pdev
->device
2755 == PCI_DEVICE_ID_LSI_INVADER
) ||
2756 (instance
->pdev
->device
2757 == PCI_DEVICE_ID_LSI_FURY
)) {
2758 writel(MFI_RESET_FLAGS
,
2759 &instance
->reg_set
->doorbell
);
2760 if ((instance
->pdev
->device
==
2761 PCI_DEVICE_ID_LSI_FUSION
) ||
2762 (instance
->pdev
->device
==
2763 PCI_DEVICE_ID_LSI_INVADER
) ||
2764 (instance
->pdev
->device
==
2765 PCI_DEVICE_ID_LSI_FURY
)) {
2766 for (i
= 0; i
< (10 * 1000); i
+= 20) {
2777 writel(MFI_RESET_FLAGS
,
2778 &instance
->reg_set
->inbound_doorbell
);
2780 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2781 cur_state
= MFI_STATE_OPERATIONAL
;
2784 case MFI_STATE_UNDEFINED
:
2786 * This state should not last for more than 2 seconds
2788 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2789 cur_state
= MFI_STATE_UNDEFINED
;
2792 case MFI_STATE_BB_INIT
:
2793 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2794 cur_state
= MFI_STATE_BB_INIT
;
2797 case MFI_STATE_FW_INIT
:
2798 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2799 cur_state
= MFI_STATE_FW_INIT
;
2802 case MFI_STATE_FW_INIT_2
:
2803 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2804 cur_state
= MFI_STATE_FW_INIT_2
;
2807 case MFI_STATE_DEVICE_SCAN
:
2808 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2809 cur_state
= MFI_STATE_DEVICE_SCAN
;
2812 case MFI_STATE_FLUSH_CACHE
:
2813 max_wait
= MEGASAS_RESET_WAIT_TIME
;
2814 cur_state
= MFI_STATE_FLUSH_CACHE
;
2818 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
2824 * The cur_state should not last for more than max_wait secs
2826 for (i
= 0; i
< (max_wait
* 1000); i
++) {
2827 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
2830 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
2832 if (abs_state
== curr_abs_state
) {
2839 * Return error if fw_state hasn't changed after max_wait
2841 if (curr_abs_state
== abs_state
) {
2842 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
2843 "in %d secs\n", fw_state
, max_wait
);
2847 printk(KERN_INFO
"megasas: FW now in Ready state\n");
2853 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2854 * @instance: Adapter soft state
2856 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
2859 u32 max_cmd
= instance
->max_mfi_cmds
;
2860 struct megasas_cmd
*cmd
;
2862 if (!instance
->frame_dma_pool
)
2866 * Return all frames to pool
2868 for (i
= 0; i
< max_cmd
; i
++) {
2870 cmd
= instance
->cmd_list
[i
];
2873 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
2874 cmd
->frame_phys_addr
);
2877 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
2878 cmd
->sense_phys_addr
);
2882 * Now destroy the pool itself
2884 pci_pool_destroy(instance
->frame_dma_pool
);
2885 pci_pool_destroy(instance
->sense_dma_pool
);
2887 instance
->frame_dma_pool
= NULL
;
2888 instance
->sense_dma_pool
= NULL
;
2892 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2893 * @instance: Adapter soft state
2895 * Each command packet has an embedded DMA memory buffer that is used for
2896 * filling MFI frame and the SG list that immediately follows the frame. This
2897 * function creates those DMA memory buffers for each command packet by using
2898 * PCI pool facility.
2900 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
2908 struct megasas_cmd
*cmd
;
2910 max_cmd
= instance
->max_mfi_cmds
;
2913 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2914 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2916 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
2917 sizeof(struct megasas_sge32
);
2919 if (instance
->flag_ieee
) {
2920 sge_sz
= sizeof(struct megasas_sge_skinny
);
2924 * Calculated the number of 64byte frames required for SGL
2926 sgl_sz
= sge_sz
* instance
->max_num_sge
;
2927 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
2931 * We need one extra frame for the MFI command
2935 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
2937 * Use DMA pool facility provided by PCI layer
2939 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
2940 instance
->pdev
, total_sz
, 64,
2943 if (!instance
->frame_dma_pool
) {
2944 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
2948 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
2949 instance
->pdev
, 128, 4, 0);
2951 if (!instance
->sense_dma_pool
) {
2952 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
2954 pci_pool_destroy(instance
->frame_dma_pool
);
2955 instance
->frame_dma_pool
= NULL
;
2961 * Allocate and attach a frame to each of the commands in cmd_list.
2962 * By making cmd->index as the context instead of the &cmd, we can
2963 * always use 32bit context regardless of the architecture
2965 for (i
= 0; i
< max_cmd
; i
++) {
2967 cmd
= instance
->cmd_list
[i
];
2969 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
2970 GFP_KERNEL
, &cmd
->frame_phys_addr
);
2972 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
2973 GFP_KERNEL
, &cmd
->sense_phys_addr
);
2976 * megasas_teardown_frame_pool() takes care of freeing
2977 * whatever has been allocated
2979 if (!cmd
->frame
|| !cmd
->sense
) {
2980 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
2981 megasas_teardown_frame_pool(instance
);
2985 memset(cmd
->frame
, 0, total_sz
);
2986 cmd
->frame
->io
.context
= cmd
->index
;
2987 cmd
->frame
->io
.pad_0
= 0;
2988 if ((instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FUSION
) &&
2989 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_INVADER
) &&
2990 (instance
->pdev
->device
!= PCI_DEVICE_ID_LSI_FURY
) &&
2992 cmd
->frame
->hdr
.cmd
= MFI_CMD_INVALID
;
2999 * megasas_free_cmds - Free all the cmds in the free cmd pool
3000 * @instance: Adapter soft state
3002 void megasas_free_cmds(struct megasas_instance
*instance
)
3005 /* First free the MFI frame pool */
3006 megasas_teardown_frame_pool(instance
);
3008 /* Free all the commands in the cmd_list */
3009 for (i
= 0; i
< instance
->max_mfi_cmds
; i
++)
3011 kfree(instance
->cmd_list
[i
]);
3013 /* Free the cmd_list buffer itself */
3014 kfree(instance
->cmd_list
);
3015 instance
->cmd_list
= NULL
;
3017 INIT_LIST_HEAD(&instance
->cmd_pool
);
3021 * megasas_alloc_cmds - Allocates the command packets
3022 * @instance: Adapter soft state
3024 * Each command that is issued to the FW, whether IO commands from the OS or
3025 * internal commands like IOCTLs, are wrapped in local data structure called
3026 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3029 * Each frame has a 32-bit field called context (tag). This context is used
3030 * to get back the megasas_cmd from the frame when a frame gets completed in
3031 * the ISR. Typically the address of the megasas_cmd itself would be used as
3032 * the context. But we wanted to keep the differences between 32 and 64 bit
3033 * systems to the mininum. We always use 32 bit integers for the context. In
3034 * this driver, the 32 bit values are the indices into an array cmd_list.
3035 * This array is used only to look up the megasas_cmd given the context. The
3036 * free commands themselves are maintained in a linked list called cmd_pool.
3038 int megasas_alloc_cmds(struct megasas_instance
*instance
)
3043 struct megasas_cmd
*cmd
;
3045 max_cmd
= instance
->max_mfi_cmds
;
3048 * instance->cmd_list is an array of struct megasas_cmd pointers.
3049 * Allocate the dynamic array first and then allocate individual
3052 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
3054 if (!instance
->cmd_list
) {
3055 printk(KERN_DEBUG
"megasas: out of memory\n");
3059 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) *max_cmd
);
3061 for (i
= 0; i
< max_cmd
; i
++) {
3062 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
3065 if (!instance
->cmd_list
[i
]) {
3067 for (j
= 0; j
< i
; j
++)
3068 kfree(instance
->cmd_list
[j
]);
3070 kfree(instance
->cmd_list
);
3071 instance
->cmd_list
= NULL
;
3078 * Add all the commands to command pool (instance->cmd_pool)
3080 for (i
= 0; i
< max_cmd
; i
++) {
3081 cmd
= instance
->cmd_list
[i
];
3082 memset(cmd
, 0, sizeof(struct megasas_cmd
));
3085 cmd
->instance
= instance
;
3087 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
3091 * Create a frame pool and assign one frame to each cmd
3093 if (megasas_create_frame_pool(instance
)) {
3094 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
3095 megasas_free_cmds(instance
);
3102 * megasas_get_pd_list_info - Returns FW's pd_list structure
3103 * @instance: Adapter soft state
3104 * @pd_list: pd_list structure
3106 * Issues an internal command (DCMD) to get the FW's controller PD
3107 * list structure. This information is mainly used to find out SYSTEM
3108 * supported by the FW.
3111 megasas_get_pd_list(struct megasas_instance
*instance
)
3113 int ret
= 0, pd_index
= 0;
3114 struct megasas_cmd
*cmd
;
3115 struct megasas_dcmd_frame
*dcmd
;
3116 struct MR_PD_LIST
*ci
;
3117 struct MR_PD_ADDRESS
*pd_addr
;
3118 dma_addr_t ci_h
= 0;
3120 cmd
= megasas_get_cmd(instance
);
3123 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
3127 dcmd
= &cmd
->frame
->dcmd
;
3129 ci
= pci_alloc_consistent(instance
->pdev
,
3130 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
3133 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
3134 megasas_return_cmd(instance
, cmd
);
3138 memset(ci
, 0, sizeof(*ci
));
3139 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3141 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
3142 dcmd
->mbox
.b
[1] = 0;
3143 dcmd
->cmd
= MFI_CMD_DCMD
;
3144 dcmd
->cmd_status
= 0xFF;
3145 dcmd
->sge_count
= 1;
3146 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3149 dcmd
->data_xfer_len
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3150 dcmd
->opcode
= MR_DCMD_PD_LIST_QUERY
;
3151 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3152 dcmd
->sgl
.sge32
[0].length
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
3154 if (!megasas_issue_polled(instance
, cmd
)) {
3161 * the following function will get the instance PD LIST.
3168 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
3170 memset(instance
->pd_list
, 0,
3171 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
3173 for (pd_index
= 0; pd_index
< ci
->count
; pd_index
++) {
3175 instance
->pd_list
[pd_addr
->deviceId
].tid
=
3177 instance
->pd_list
[pd_addr
->deviceId
].driveType
=
3178 pd_addr
->scsiDevType
;
3179 instance
->pd_list
[pd_addr
->deviceId
].driveState
=
3185 pci_free_consistent(instance
->pdev
,
3186 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
3188 megasas_return_cmd(instance
, cmd
);
3194 * megasas_get_ld_list_info - Returns FW's ld_list structure
3195 * @instance: Adapter soft state
3196 * @ld_list: ld_list structure
3198 * Issues an internal command (DCMD) to get the FW's controller PD
3199 * list structure. This information is mainly used to find out SYSTEM
3200 * supported by the FW.
3203 megasas_get_ld_list(struct megasas_instance
*instance
)
3205 int ret
= 0, ld_index
= 0, ids
= 0;
3206 struct megasas_cmd
*cmd
;
3207 struct megasas_dcmd_frame
*dcmd
;
3208 struct MR_LD_LIST
*ci
;
3209 dma_addr_t ci_h
= 0;
3211 cmd
= megasas_get_cmd(instance
);
3214 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
3218 dcmd
= &cmd
->frame
->dcmd
;
3220 ci
= pci_alloc_consistent(instance
->pdev
,
3221 sizeof(struct MR_LD_LIST
),
3225 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
3226 megasas_return_cmd(instance
, cmd
);
3230 memset(ci
, 0, sizeof(*ci
));
3231 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3233 dcmd
->cmd
= MFI_CMD_DCMD
;
3234 dcmd
->cmd_status
= 0xFF;
3235 dcmd
->sge_count
= 1;
3236 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3238 dcmd
->data_xfer_len
= sizeof(struct MR_LD_LIST
);
3239 dcmd
->opcode
= MR_DCMD_LD_GET_LIST
;
3240 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3241 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_LD_LIST
);
3244 if (!megasas_issue_polled(instance
, cmd
)) {
3250 /* the following function will get the instance PD LIST */
3252 if ((ret
== 0) && (ci
->ldCount
<= MAX_LOGICAL_DRIVES
)) {
3253 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3255 for (ld_index
= 0; ld_index
< ci
->ldCount
; ld_index
++) {
3256 if (ci
->ldList
[ld_index
].state
!= 0) {
3257 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
3258 instance
->ld_ids
[ids
] =
3259 ci
->ldList
[ld_index
].ref
.targetId
;
3264 pci_free_consistent(instance
->pdev
,
3265 sizeof(struct MR_LD_LIST
),
3269 megasas_return_cmd(instance
, cmd
);
3274 * megasas_get_controller_info - Returns FW's controller structure
3275 * @instance: Adapter soft state
3276 * @ctrl_info: Controller information structure
3278 * Issues an internal command (DCMD) to get the FW's controller structure.
3279 * This information is mainly used to find out the maximum IO transfer per
3280 * command supported by the FW.
3283 megasas_get_ctrl_info(struct megasas_instance
*instance
,
3284 struct megasas_ctrl_info
*ctrl_info
)
3287 struct megasas_cmd
*cmd
;
3288 struct megasas_dcmd_frame
*dcmd
;
3289 struct megasas_ctrl_info
*ci
;
3290 dma_addr_t ci_h
= 0;
3292 cmd
= megasas_get_cmd(instance
);
3295 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
3299 dcmd
= &cmd
->frame
->dcmd
;
3301 ci
= pci_alloc_consistent(instance
->pdev
,
3302 sizeof(struct megasas_ctrl_info
), &ci_h
);
3305 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
3306 megasas_return_cmd(instance
, cmd
);
3310 memset(ci
, 0, sizeof(*ci
));
3311 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3313 dcmd
->cmd
= MFI_CMD_DCMD
;
3314 dcmd
->cmd_status
= 0xFF;
3315 dcmd
->sge_count
= 1;
3316 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3319 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
3320 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
3321 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
3322 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
3324 if (!megasas_issue_polled(instance
, cmd
)) {
3326 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
3331 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
3334 megasas_return_cmd(instance
, cmd
);
3339 * megasas_issue_init_mfi - Initializes the FW
3340 * @instance: Adapter soft state
3342 * Issues the INIT MFI cmd
3345 megasas_issue_init_mfi(struct megasas_instance
*instance
)
3349 struct megasas_cmd
*cmd
;
3351 struct megasas_init_frame
*init_frame
;
3352 struct megasas_init_queue_info
*initq_info
;
3353 dma_addr_t init_frame_h
;
3354 dma_addr_t initq_info_h
;
3357 * Prepare a init frame. Note the init frame points to queue info
3358 * structure. Each frame has SGL allocated after first 64 bytes. For
3359 * this frame - since we don't need any SGL - we use SGL's space as
3360 * queue info structure
3362 * We will not get a NULL command below. We just created the pool.
3364 cmd
= megasas_get_cmd(instance
);
3366 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
3367 initq_info
= (struct megasas_init_queue_info
*)
3368 ((unsigned long)init_frame
+ 64);
3370 init_frame_h
= cmd
->frame_phys_addr
;
3371 initq_info_h
= init_frame_h
+ 64;
3373 context
= init_frame
->context
;
3374 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
3375 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
3376 init_frame
->context
= context
;
3378 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
3379 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
3381 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
3382 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
3384 init_frame
->cmd
= MFI_CMD_INIT
;
3385 init_frame
->cmd_status
= 0xFF;
3386 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
3388 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
3391 * disable the intr before firing the init frame to FW
3393 instance
->instancet
->disable_intr(instance
);
3396 * Issue the init frame in polled mode
3399 if (megasas_issue_polled(instance
, cmd
)) {
3400 printk(KERN_ERR
"megasas: Failed to init firmware\n");
3401 megasas_return_cmd(instance
, cmd
);
3405 megasas_return_cmd(instance
, cmd
);
3414 megasas_init_adapter_mfi(struct megasas_instance
*instance
)
3416 struct megasas_register_set __iomem
*reg_set
;
3420 reg_set
= instance
->reg_set
;
3423 * Get various operational parameters from status register
3425 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
3427 * Reduce the max supported cmds by 1. This is to ensure that the
3428 * reply_q_sz (1 more than the max cmd that driver may send)
3429 * does not exceed max cmds that the FW can support
3431 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
3432 instance
->max_mfi_cmds
= instance
->max_fw_cmds
;
3433 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
3436 * Create a pool of commands
3438 if (megasas_alloc_cmds(instance
))
3439 goto fail_alloc_cmds
;
3442 * Allocate memory for reply queue. Length of reply queue should
3443 * be _one_ more than the maximum commands handled by the firmware.
3445 * Note: When FW completes commands, it places corresponding contex
3446 * values in this circular reply queue. This circular queue is a fairly
3447 * typical producer-consumer queue. FW is the producer (of completed
3448 * commands) and the driver is the consumer.
3450 context_sz
= sizeof(u32
);
3451 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
3453 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
3455 &instance
->reply_queue_h
);
3457 if (!instance
->reply_queue
) {
3458 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
3459 goto fail_reply_queue
;
3462 if (megasas_issue_init_mfi(instance
))
3465 instance
->fw_support_ieee
= 0;
3466 instance
->fw_support_ieee
=
3467 (instance
->instancet
->read_fw_status_reg(reg_set
) &
3470 printk(KERN_NOTICE
"megasas_init_mfi: fw_support_ieee=%d",
3471 instance
->fw_support_ieee
);
3473 if (instance
->fw_support_ieee
)
3474 instance
->flag_ieee
= 1;
3480 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3481 instance
->reply_queue
, instance
->reply_queue_h
);
3483 megasas_free_cmds(instance
);
3490 * megasas_init_fw - Initializes the FW
3491 * @instance: Adapter soft state
3493 * This is the main function for initializing firmware
3496 static int megasas_init_fw(struct megasas_instance
*instance
)
3500 u32 tmp_sectors
, msix_enable
, scratch_pad_2
;
3501 struct megasas_register_set __iomem
*reg_set
;
3502 struct megasas_ctrl_info
*ctrl_info
;
3503 unsigned long bar_list
;
3504 int i
, loop
, fw_msix_count
= 0;
3506 /* Find first memory bar */
3507 bar_list
= pci_select_bars(instance
->pdev
, IORESOURCE_MEM
);
3508 instance
->bar
= find_first_bit(&bar_list
, sizeof(unsigned long));
3509 instance
->base_addr
= pci_resource_start(instance
->pdev
, instance
->bar
);
3510 if (pci_request_selected_regions(instance
->pdev
, instance
->bar
,
3512 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
3516 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
3518 if (!instance
->reg_set
) {
3519 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
3523 reg_set
= instance
->reg_set
;
3525 switch (instance
->pdev
->device
) {
3526 case PCI_DEVICE_ID_LSI_FUSION
:
3527 case PCI_DEVICE_ID_LSI_INVADER
:
3528 case PCI_DEVICE_ID_LSI_FURY
:
3529 instance
->instancet
= &megasas_instance_template_fusion
;
3531 case PCI_DEVICE_ID_LSI_SAS1078R
:
3532 case PCI_DEVICE_ID_LSI_SAS1078DE
:
3533 instance
->instancet
= &megasas_instance_template_ppc
;
3535 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
3536 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
3537 instance
->instancet
= &megasas_instance_template_gen2
;
3539 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
3540 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
3541 instance
->instancet
= &megasas_instance_template_skinny
;
3543 case PCI_DEVICE_ID_LSI_SAS1064R
:
3544 case PCI_DEVICE_ID_DELL_PERC5
:
3546 instance
->instancet
= &megasas_instance_template_xscale
;
3551 * We expect the FW state to be READY
3553 if (megasas_transition_to_ready(instance
, 0))
3554 goto fail_ready_state
;
3557 * MSI-X host index 0 is common for all adapter.
3558 * It is used for all MPT based Adapters.
3560 instance
->reply_post_host_index_addr
[0] =
3561 (u32
*)((u8
*)instance
->reg_set
+
3562 MPI2_REPLY_POST_HOST_INDEX_OFFSET
);
3564 /* Check if MSI-X is supported while in ready state */
3565 msix_enable
= (instance
->instancet
->read_fw_status_reg(reg_set
) &
3567 if (msix_enable
&& !msix_disable
) {
3568 scratch_pad_2
= readl
3569 (&instance
->reg_set
->outbound_scratch_pad_2
);
3570 /* Check max MSI-X vectors */
3571 if (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) {
3572 instance
->msix_vectors
= (scratch_pad_2
3573 & MR_MAX_REPLY_QUEUES_OFFSET
) + 1;
3574 fw_msix_count
= instance
->msix_vectors
;
3576 instance
->msix_vectors
=
3578 instance
->msix_vectors
);
3579 } else if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
)
3580 || (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
3581 /* Invader/Fury supports more than 8 MSI-X */
3582 instance
->msix_vectors
= ((scratch_pad_2
3583 & MR_MAX_REPLY_QUEUES_EXT_OFFSET
)
3584 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT
) + 1;
3585 fw_msix_count
= instance
->msix_vectors
;
3586 /* Save 1-15 reply post index address to local memory
3587 * Index 0 is already saved from reg offset
3588 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
3590 for (loop
= 1; loop
< MR_MAX_MSIX_REG_ARRAY
; loop
++) {
3591 instance
->reply_post_host_index_addr
[loop
] =
3592 (u32
*)((u8
*)instance
->reg_set
+
3593 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
3597 instance
->msix_vectors
= min(msix_vectors
,
3598 instance
->msix_vectors
);
3600 instance
->msix_vectors
= 1;
3601 /* Don't bother allocating more MSI-X vectors than cpus */
3602 instance
->msix_vectors
= min(instance
->msix_vectors
,
3603 (unsigned int)num_online_cpus());
3604 for (i
= 0; i
< instance
->msix_vectors
; i
++)
3605 instance
->msixentry
[i
].entry
= i
;
3606 i
= pci_enable_msix(instance
->pdev
, instance
->msixentry
,
3607 instance
->msix_vectors
);
3610 if (!pci_enable_msix(instance
->pdev
,
3611 instance
->msixentry
, i
))
3612 instance
->msix_vectors
= i
;
3614 instance
->msix_vectors
= 0;
3617 instance
->msix_vectors
= 0;
3619 dev_info(&instance
->pdev
->dev
, "[scsi%d]: FW supports"
3620 "<%d> MSIX vector,Online CPUs: <%d>,"
3621 "Current MSIX <%d>\n", instance
->host
->host_no
,
3622 fw_msix_count
, (unsigned int)num_online_cpus(),
3623 instance
->msix_vectors
);
3626 /* Get operational params, sge flags, send init cmd to controller */
3627 if (instance
->instancet
->init_adapter(instance
))
3628 goto fail_init_adapter
;
3630 printk(KERN_ERR
"megasas: INIT adapter done\n");
3633 * the following function will get the PD LIST.
3636 memset(instance
->pd_list
, 0 ,
3637 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
3638 megasas_get_pd_list(instance
);
3640 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
3641 megasas_get_ld_list(instance
);
3643 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
3646 * Compute the max allowed sectors per IO: The controller info has two
3647 * limits on max sectors. Driver should use the minimum of these two.
3649 * 1 << stripe_sz_ops.min = max sectors per strip
3651 * Note that older firmwares ( < FW ver 30) didn't report information
3652 * to calculate max_sectors_1. So the number ended up as zero always.
3655 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
3657 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
3658 ctrl_info
->max_strips_per_io
;
3659 max_sectors_2
= ctrl_info
->max_request_size
;
3661 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
3663 /*Check whether controller is iMR or MR */
3664 if (ctrl_info
->memory_size
) {
3665 instance
->is_imr
= 0;
3666 dev_info(&instance
->pdev
->dev
, "Controller type: MR,"
3667 "Memory size is: %dMB\n",
3668 ctrl_info
->memory_size
);
3670 instance
->is_imr
= 1;
3671 dev_info(&instance
->pdev
->dev
,
3672 "Controller type: iMR\n");
3674 instance
->disableOnlineCtrlReset
=
3675 ctrl_info
->properties
.OnOffProperties
.disableOnlineCtrlReset
;
3676 instance
->UnevenSpanSupport
=
3677 ctrl_info
->adapterOperations2
.supportUnevenSpans
;
3678 if (instance
->UnevenSpanSupport
) {
3679 struct fusion_context
*fusion
= instance
->ctrl_context
;
3680 dev_info(&instance
->pdev
->dev
, "FW supports: "
3681 "UnevenSpanSupport=%x\n", instance
->UnevenSpanSupport
);
3682 if (MR_ValidateMapInfo(instance
))
3683 fusion
->fast_path_io
= 1;
3685 fusion
->fast_path_io
= 0;
3690 instance
->max_sectors_per_req
= instance
->max_num_sge
*
3692 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
3693 instance
->max_sectors_per_req
= tmp_sectors
;
3697 /* Check for valid throttlequeuedepth module parameter */
3698 if (instance
->is_imr
) {
3699 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3700 MEGASAS_SKINNY_INT_CMDS
))
3701 instance
->throttlequeuedepth
=
3702 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3704 instance
->throttlequeuedepth
= throttlequeuedepth
;
3706 if (throttlequeuedepth
> (instance
->max_fw_cmds
-
3708 instance
->throttlequeuedepth
=
3709 MEGASAS_THROTTLE_QUEUE_DEPTH
;
3711 instance
->throttlequeuedepth
= throttlequeuedepth
;
3715 * Setup tasklet for cmd completion
3718 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
3719 (unsigned long)instance
);
3725 iounmap(instance
->reg_set
);
3728 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3734 * megasas_release_mfi - Reverses the FW initialization
3735 * @intance: Adapter soft state
3737 static void megasas_release_mfi(struct megasas_instance
*instance
)
3739 u32 reply_q_sz
= sizeof(u32
) *(instance
->max_mfi_cmds
+ 1);
3741 if (instance
->reply_queue
)
3742 pci_free_consistent(instance
->pdev
, reply_q_sz
,
3743 instance
->reply_queue
, instance
->reply_queue_h
);
3745 megasas_free_cmds(instance
);
3747 iounmap(instance
->reg_set
);
3749 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
3753 * megasas_get_seq_num - Gets latest event sequence numbers
3754 * @instance: Adapter soft state
3755 * @eli: FW event log sequence numbers information
3757 * FW maintains a log of all events in a non-volatile area. Upper layers would
3758 * usually find out the latest sequence number of the events, the seq number at
3759 * the boot etc. They would "read" all the events below the latest seq number
3760 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3761 * number), they would subsribe to AEN (asynchronous event notification) and
3762 * wait for the events to happen.
3765 megasas_get_seq_num(struct megasas_instance
*instance
,
3766 struct megasas_evt_log_info
*eli
)
3768 struct megasas_cmd
*cmd
;
3769 struct megasas_dcmd_frame
*dcmd
;
3770 struct megasas_evt_log_info
*el_info
;
3771 dma_addr_t el_info_h
= 0;
3773 cmd
= megasas_get_cmd(instance
);
3779 dcmd
= &cmd
->frame
->dcmd
;
3780 el_info
= pci_alloc_consistent(instance
->pdev
,
3781 sizeof(struct megasas_evt_log_info
),
3785 megasas_return_cmd(instance
, cmd
);
3789 memset(el_info
, 0, sizeof(*el_info
));
3790 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3792 dcmd
->cmd
= MFI_CMD_DCMD
;
3793 dcmd
->cmd_status
= 0x0;
3794 dcmd
->sge_count
= 1;
3795 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3798 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
3799 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
3800 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
3801 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
3803 megasas_issue_blocked_cmd(instance
, cmd
);
3806 * Copy the data back into callers buffer
3808 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
3810 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
3811 el_info
, el_info_h
);
3813 megasas_return_cmd(instance
, cmd
);
3819 * megasas_register_aen - Registers for asynchronous event notification
3820 * @instance: Adapter soft state
3821 * @seq_num: The starting sequence number
3822 * @class_locale: Class of the event
3824 * This function subscribes for AEN for events beyond the @seq_num. It requests
3825 * to be notified if and only if the event is of type @class_locale
3828 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
3829 u32 class_locale_word
)
3832 struct megasas_cmd
*cmd
;
3833 struct megasas_dcmd_frame
*dcmd
;
3834 union megasas_evt_class_locale curr_aen
;
3835 union megasas_evt_class_locale prev_aen
;
3838 * If there an AEN pending already (aen_cmd), check if the
3839 * class_locale of that pending AEN is inclusive of the new
3840 * AEN request we currently have. If it is, then we don't have
3841 * to do anything. In other words, whichever events the current
3842 * AEN request is subscribing to, have already been subscribed
3845 * If the old_cmd is _not_ inclusive, then we have to abort
3846 * that command, form a class_locale that is superset of both
3847 * old and current and re-issue to the FW
3850 curr_aen
.word
= class_locale_word
;
3852 if (instance
->aen_cmd
) {
3854 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
3857 * A class whose enum value is smaller is inclusive of all
3858 * higher values. If a PROGRESS (= -1) was previously
3859 * registered, then a new registration requests for higher
3860 * classes need not be sent to FW. They are automatically
3863 * Locale numbers don't have such hierarchy. They are bitmap
3866 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
3867 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
3868 curr_aen
.members
.locale
)) {
3870 * Previously issued event registration includes
3871 * current request. Nothing to do.
3875 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
3877 if (prev_aen
.members
.class < curr_aen
.members
.class)
3878 curr_aen
.members
.class = prev_aen
.members
.class;
3880 instance
->aen_cmd
->abort_aen
= 1;
3881 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
3886 printk(KERN_DEBUG
"megasas: Failed to abort "
3887 "previous AEN command\n");
3893 cmd
= megasas_get_cmd(instance
);
3898 dcmd
= &cmd
->frame
->dcmd
;
3900 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
3903 * Prepare DCMD for aen registration
3905 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3907 dcmd
->cmd
= MFI_CMD_DCMD
;
3908 dcmd
->cmd_status
= 0x0;
3909 dcmd
->sge_count
= 1;
3910 dcmd
->flags
= MFI_FRAME_DIR_READ
;
3913 instance
->last_seq_num
= seq_num
;
3914 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
3915 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
3916 dcmd
->mbox
.w
[0] = seq_num
;
3917 dcmd
->mbox
.w
[1] = curr_aen
.word
;
3918 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
3919 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
3921 if (instance
->aen_cmd
!= NULL
) {
3922 megasas_return_cmd(instance
, cmd
);
3927 * Store reference to the cmd used to register for AEN. When an
3928 * application wants us to register for AEN, we have to abort this
3929 * cmd and re-register with a new EVENT LOCALE supplied by that app
3931 instance
->aen_cmd
= cmd
;
3934 * Issue the aen registration frame
3936 instance
->instancet
->issue_dcmd(instance
, cmd
);
3942 * megasas_start_aen - Subscribes to AEN during driver load time
3943 * @instance: Adapter soft state
3945 static int megasas_start_aen(struct megasas_instance
*instance
)
3947 struct megasas_evt_log_info eli
;
3948 union megasas_evt_class_locale class_locale
;
3951 * Get the latest sequence number from FW
3953 memset(&eli
, 0, sizeof(eli
));
3955 if (megasas_get_seq_num(instance
, &eli
))
3959 * Register AEN with FW for latest sequence number plus 1
3961 class_locale
.members
.reserved
= 0;
3962 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
3963 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
3965 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
3970 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3971 * @instance: Adapter soft state
3973 static int megasas_io_attach(struct megasas_instance
*instance
)
3975 struct Scsi_Host
*host
= instance
->host
;
3978 * Export parameters required by SCSI mid-layer
3980 host
->irq
= instance
->pdev
->irq
;
3981 host
->unique_id
= instance
->unique_id
;
3982 if (instance
->is_imr
) {
3984 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
3987 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
3988 host
->this_id
= instance
->init_id
;
3989 host
->sg_tablesize
= instance
->max_num_sge
;
3991 if (instance
->fw_support_ieee
)
3992 instance
->max_sectors_per_req
= MEGASAS_MAX_SECTORS_IEEE
;
3995 * Check if the module parameter value for max_sectors can be used
3997 if (max_sectors
&& max_sectors
< instance
->max_sectors_per_req
)
3998 instance
->max_sectors_per_req
= max_sectors
;
4001 if (((instance
->pdev
->device
==
4002 PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
4003 (instance
->pdev
->device
==
4004 PCI_DEVICE_ID_LSI_SAS0079GEN2
)) &&
4005 (max_sectors
<= MEGASAS_MAX_SECTORS
)) {
4006 instance
->max_sectors_per_req
= max_sectors
;
4008 printk(KERN_INFO
"megasas: max_sectors should be > 0"
4009 "and <= %d (or < 1MB for GEN2 controller)\n",
4010 instance
->max_sectors_per_req
);
4015 host
->max_sectors
= instance
->max_sectors_per_req
;
4016 host
->cmd_per_lun
= MEGASAS_DEFAULT_CMD_PER_LUN
;
4017 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
4018 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
4019 host
->max_lun
= MEGASAS_MAX_LUN
;
4020 host
->max_cmd_len
= 16;
4022 /* Fusion only supports host reset */
4023 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4024 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4025 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
4026 host
->hostt
->eh_device_reset_handler
= NULL
;
4027 host
->hostt
->eh_bus_reset_handler
= NULL
;
4031 * Notify the mid-layer about the new controller
4033 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
4034 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
4039 * Trigger SCSI to scan our drives
4041 scsi_scan_host(host
);
4046 megasas_set_dma_mask(struct pci_dev
*pdev
)
4049 * All our contollers are capable of performing 64-bit DMA
4052 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
4054 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
4055 goto fail_set_dma_mask
;
4058 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
4059 goto fail_set_dma_mask
;
4068 * megasas_probe_one - PCI hotplug entry point
4069 * @pdev: PCI device structure
4070 * @id: PCI ids of supported hotplugged adapter
4072 static int megasas_probe_one(struct pci_dev
*pdev
,
4073 const struct pci_device_id
*id
)
4075 int rval
, pos
, i
, j
;
4076 struct Scsi_Host
*host
;
4077 struct megasas_instance
*instance
;
4080 /* Reset MSI-X in the kdump kernel */
4081 if (reset_devices
) {
4082 pos
= pci_find_capability(pdev
, PCI_CAP_ID_MSIX
);
4084 pci_read_config_word(pdev
, pos
+ PCI_MSIX_FLAGS
,
4086 if (control
& PCI_MSIX_FLAGS_ENABLE
) {
4087 dev_info(&pdev
->dev
, "resetting MSI-X\n");
4088 pci_write_config_word(pdev
,
4089 pos
+ PCI_MSIX_FLAGS
,
4091 ~PCI_MSIX_FLAGS_ENABLE
);
4097 * Announce PCI information
4099 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4100 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
4101 pdev
->subsystem_device
);
4103 printk("bus %d:slot %d:func %d\n",
4104 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
4107 * PCI prepping: enable device set bus mastering and dma mask
4109 rval
= pci_enable_device_mem(pdev
);
4115 pci_set_master(pdev
);
4117 if (megasas_set_dma_mask(pdev
))
4118 goto fail_set_dma_mask
;
4120 host
= scsi_host_alloc(&megasas_template
,
4121 sizeof(struct megasas_instance
));
4124 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
4125 goto fail_alloc_instance
;
4128 instance
= (struct megasas_instance
*)host
->hostdata
;
4129 memset(instance
, 0, sizeof(*instance
));
4130 atomic_set( &instance
->fw_reset_no_pci_access
, 0 );
4131 instance
->pdev
= pdev
;
4133 switch (instance
->pdev
->device
) {
4134 case PCI_DEVICE_ID_LSI_FUSION
:
4135 case PCI_DEVICE_ID_LSI_INVADER
:
4136 case PCI_DEVICE_ID_LSI_FURY
:
4138 struct fusion_context
*fusion
;
4140 instance
->ctrl_context
=
4141 kzalloc(sizeof(struct fusion_context
), GFP_KERNEL
);
4142 if (!instance
->ctrl_context
) {
4143 printk(KERN_DEBUG
"megasas: Failed to allocate "
4144 "memory for Fusion context info\n");
4145 goto fail_alloc_dma_buf
;
4147 fusion
= instance
->ctrl_context
;
4148 INIT_LIST_HEAD(&fusion
->cmd_pool
);
4149 spin_lock_init(&fusion
->cmd_pool_lock
);
4152 default: /* For all other supported controllers */
4154 instance
->producer
=
4155 pci_alloc_consistent(pdev
, sizeof(u32
),
4156 &instance
->producer_h
);
4157 instance
->consumer
=
4158 pci_alloc_consistent(pdev
, sizeof(u32
),
4159 &instance
->consumer_h
);
4161 if (!instance
->producer
|| !instance
->consumer
) {
4162 printk(KERN_DEBUG
"megasas: Failed to allocate"
4163 "memory for producer, consumer\n");
4164 goto fail_alloc_dma_buf
;
4167 *instance
->producer
= 0;
4168 *instance
->consumer
= 0;
4172 megasas_poll_wait_aen
= 0;
4173 instance
->flag_ieee
= 0;
4174 instance
->ev
= NULL
;
4175 instance
->issuepend_done
= 1;
4176 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
4177 instance
->is_imr
= 0;
4178 megasas_poll_wait_aen
= 0;
4180 instance
->evt_detail
= pci_alloc_consistent(pdev
,
4182 megasas_evt_detail
),
4183 &instance
->evt_detail_h
);
4185 if (!instance
->evt_detail
) {
4186 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
4187 "event detail structure\n");
4188 goto fail_alloc_dma_buf
;
4192 * Initialize locks and queues
4194 INIT_LIST_HEAD(&instance
->cmd_pool
);
4195 INIT_LIST_HEAD(&instance
->internal_reset_pending_q
);
4197 atomic_set(&instance
->fw_outstanding
,0);
4199 init_waitqueue_head(&instance
->int_cmd_wait_q
);
4200 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
4202 spin_lock_init(&instance
->cmd_pool_lock
);
4203 spin_lock_init(&instance
->hba_lock
);
4204 spin_lock_init(&instance
->completion_lock
);
4206 mutex_init(&instance
->aen_mutex
);
4207 mutex_init(&instance
->reset_mutex
);
4210 * Initialize PCI related and misc parameters
4212 instance
->host
= host
;
4213 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
4214 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
4216 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
4217 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
4218 instance
->flag_ieee
= 1;
4219 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
4221 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
4223 megasas_dbg_lvl
= 0;
4225 instance
->unload
= 1;
4226 instance
->last_time
= 0;
4227 instance
->disableOnlineCtrlReset
= 1;
4228 instance
->UnevenSpanSupport
= 0;
4230 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4231 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4232 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4233 INIT_WORK(&instance
->work_init
, megasas_fusion_ocr_wq
);
4235 INIT_WORK(&instance
->work_init
, process_fw_state_change_wq
);
4238 * Initialize MFI Firmware
4240 if (megasas_init_fw(instance
))
4247 if (instance
->msix_vectors
) {
4248 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4249 instance
->irq_context
[i
].instance
= instance
;
4250 instance
->irq_context
[i
].MSIxIndex
= i
;
4251 if (request_irq(instance
->msixentry
[i
].vector
,
4252 instance
->instancet
->service_isr
, 0,
4254 &instance
->irq_context
[i
])) {
4255 printk(KERN_DEBUG
"megasas: Failed to "
4256 "register IRQ for vector %d.\n", i
);
4257 for (j
= 0 ; j
< i
; j
++)
4259 instance
->msixentry
[j
].vector
,
4260 &instance
->irq_context
[j
]);
4261 /* Retry irq register for IO_APIC */
4262 instance
->msix_vectors
= 0;
4263 goto retry_irq_register
;
4267 instance
->irq_context
[0].instance
= instance
;
4268 instance
->irq_context
[0].MSIxIndex
= 0;
4269 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4270 IRQF_SHARED
, "megasas",
4271 &instance
->irq_context
[0])) {
4272 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4277 instance
->instancet
->enable_intr(instance
);
4280 * Store instance in PCI softstate
4282 pci_set_drvdata(pdev
, instance
);
4285 * Add this controller to megasas_mgmt_info structure so that it
4286 * can be exported to management applications
4288 megasas_mgmt_info
.count
++;
4289 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
4290 megasas_mgmt_info
.max_index
++;
4293 * Register with SCSI mid-layer
4295 if (megasas_io_attach(instance
))
4296 goto fail_io_attach
;
4298 instance
->unload
= 0;
4301 * Initiate AEN (Asynchronous Event Notification)
4303 if (megasas_start_aen(instance
)) {
4304 printk(KERN_DEBUG
"megasas: start aen failed\n");
4305 goto fail_start_aen
;
4312 megasas_mgmt_info
.count
--;
4313 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
4314 megasas_mgmt_info
.max_index
--;
4316 pci_set_drvdata(pdev
, NULL
);
4317 instance
->instancet
->disable_intr(instance
);
4318 if (instance
->msix_vectors
)
4319 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4320 free_irq(instance
->msixentry
[i
].vector
,
4321 &instance
->irq_context
[i
]);
4323 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4325 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FUSION
) ||
4326 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
4327 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
4328 megasas_release_fusion(instance
);
4330 megasas_release_mfi(instance
);
4332 if (instance
->msix_vectors
)
4333 pci_disable_msix(instance
->pdev
);
4335 if (instance
->evt_detail
)
4336 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4337 instance
->evt_detail
,
4338 instance
->evt_detail_h
);
4340 if (instance
->producer
)
4341 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4342 instance
->producer_h
);
4343 if (instance
->consumer
)
4344 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4345 instance
->consumer_h
);
4346 scsi_host_put(host
);
4348 fail_alloc_instance
:
4350 pci_disable_device(pdev
);
4356 * megasas_flush_cache - Requests FW to flush all its caches
4357 * @instance: Adapter soft state
4359 static void megasas_flush_cache(struct megasas_instance
*instance
)
4361 struct megasas_cmd
*cmd
;
4362 struct megasas_dcmd_frame
*dcmd
;
4364 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4367 cmd
= megasas_get_cmd(instance
);
4372 dcmd
= &cmd
->frame
->dcmd
;
4374 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4376 dcmd
->cmd
= MFI_CMD_DCMD
;
4377 dcmd
->cmd_status
= 0x0;
4378 dcmd
->sge_count
= 0;
4379 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4382 dcmd
->data_xfer_len
= 0;
4383 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
4384 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
4386 megasas_issue_blocked_cmd(instance
, cmd
);
4388 megasas_return_cmd(instance
, cmd
);
4394 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4395 * @instance: Adapter soft state
4396 * @opcode: Shutdown/Hibernate
4398 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
4401 struct megasas_cmd
*cmd
;
4402 struct megasas_dcmd_frame
*dcmd
;
4404 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
4407 cmd
= megasas_get_cmd(instance
);
4412 if (instance
->aen_cmd
)
4413 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
4414 if (instance
->map_update_cmd
)
4415 megasas_issue_blocked_abort_cmd(instance
,
4416 instance
->map_update_cmd
);
4417 dcmd
= &cmd
->frame
->dcmd
;
4419 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
4421 dcmd
->cmd
= MFI_CMD_DCMD
;
4422 dcmd
->cmd_status
= 0x0;
4423 dcmd
->sge_count
= 0;
4424 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
4427 dcmd
->data_xfer_len
= 0;
4428 dcmd
->opcode
= opcode
;
4430 megasas_issue_blocked_cmd(instance
, cmd
);
4432 megasas_return_cmd(instance
, cmd
);
4439 * megasas_suspend - driver suspend entry point
4440 * @pdev: PCI device structure
4441 * @state: PCI power state to suspend routine
4444 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
4446 struct Scsi_Host
*host
;
4447 struct megasas_instance
*instance
;
4450 instance
= pci_get_drvdata(pdev
);
4451 host
= instance
->host
;
4452 instance
->unload
= 1;
4454 megasas_flush_cache(instance
);
4455 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
4457 /* cancel the delayed work if this work still in queue */
4458 if (instance
->ev
!= NULL
) {
4459 struct megasas_aen_event
*ev
= instance
->ev
;
4460 cancel_delayed_work_sync(&ev
->hotplug_work
);
4461 instance
->ev
= NULL
;
4464 tasklet_kill(&instance
->isr_tasklet
);
4466 pci_set_drvdata(instance
->pdev
, instance
);
4467 instance
->instancet
->disable_intr(instance
);
4469 if (instance
->msix_vectors
)
4470 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4471 free_irq(instance
->msixentry
[i
].vector
,
4472 &instance
->irq_context
[i
]);
4474 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4475 if (instance
->msix_vectors
)
4476 pci_disable_msix(instance
->pdev
);
4478 pci_save_state(pdev
);
4479 pci_disable_device(pdev
);
4481 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
4487 * megasas_resume- driver resume entry point
4488 * @pdev: PCI device structure
4491 megasas_resume(struct pci_dev
*pdev
)
4494 struct Scsi_Host
*host
;
4495 struct megasas_instance
*instance
;
4497 instance
= pci_get_drvdata(pdev
);
4498 host
= instance
->host
;
4499 pci_set_power_state(pdev
, PCI_D0
);
4500 pci_enable_wake(pdev
, PCI_D0
, 0);
4501 pci_restore_state(pdev
);
4504 * PCI prepping: enable device set bus mastering and dma mask
4506 rval
= pci_enable_device_mem(pdev
);
4509 printk(KERN_ERR
"megasas: Enable device failed\n");
4513 pci_set_master(pdev
);
4515 if (megasas_set_dma_mask(pdev
))
4516 goto fail_set_dma_mask
;
4519 * Initialize MFI Firmware
4522 atomic_set(&instance
->fw_outstanding
, 0);
4525 * We expect the FW state to be READY
4527 if (megasas_transition_to_ready(instance
, 0))
4528 goto fail_ready_state
;
4530 /* Now re-enable MSI-X */
4531 if (instance
->msix_vectors
)
4532 pci_enable_msix(instance
->pdev
, instance
->msixentry
,
4533 instance
->msix_vectors
);
4535 switch (instance
->pdev
->device
) {
4536 case PCI_DEVICE_ID_LSI_FUSION
:
4537 case PCI_DEVICE_ID_LSI_INVADER
:
4538 case PCI_DEVICE_ID_LSI_FURY
:
4540 megasas_reset_reply_desc(instance
);
4541 if (megasas_ioc_init_fusion(instance
)) {
4542 megasas_free_cmds(instance
);
4543 megasas_free_cmds_fusion(instance
);
4546 if (!megasas_get_map_info(instance
))
4547 megasas_sync_map_info(instance
);
4551 *instance
->producer
= 0;
4552 *instance
->consumer
= 0;
4553 if (megasas_issue_init_mfi(instance
))
4558 tasklet_init(&instance
->isr_tasklet
, instance
->instancet
->tasklet
,
4559 (unsigned long)instance
);
4564 if (instance
->msix_vectors
) {
4565 for (i
= 0 ; i
< instance
->msix_vectors
; i
++) {
4566 instance
->irq_context
[i
].instance
= instance
;
4567 instance
->irq_context
[i
].MSIxIndex
= i
;
4568 if (request_irq(instance
->msixentry
[i
].vector
,
4569 instance
->instancet
->service_isr
, 0,
4571 &instance
->irq_context
[i
])) {
4572 printk(KERN_DEBUG
"megasas: Failed to "
4573 "register IRQ for vector %d.\n", i
);
4574 for (j
= 0 ; j
< i
; j
++)
4576 instance
->msixentry
[j
].vector
,
4577 &instance
->irq_context
[j
]);
4582 instance
->irq_context
[0].instance
= instance
;
4583 instance
->irq_context
[0].MSIxIndex
= 0;
4584 if (request_irq(pdev
->irq
, instance
->instancet
->service_isr
,
4585 IRQF_SHARED
, "megasas",
4586 &instance
->irq_context
[0])) {
4587 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
4592 instance
->instancet
->enable_intr(instance
);
4593 instance
->unload
= 0;
4596 * Initiate AEN (Asynchronous Event Notification)
4598 if (megasas_start_aen(instance
))
4599 printk(KERN_ERR
"megasas: Start AEN failed\n");
4605 if (instance
->evt_detail
)
4606 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4607 instance
->evt_detail
,
4608 instance
->evt_detail_h
);
4610 if (instance
->producer
)
4611 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
4612 instance
->producer_h
);
4613 if (instance
->consumer
)
4614 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
4615 instance
->consumer_h
);
4616 scsi_host_put(host
);
4621 pci_disable_device(pdev
);
4626 #define megasas_suspend NULL
4627 #define megasas_resume NULL
4631 * megasas_detach_one - PCI hot"un"plug entry point
4632 * @pdev: PCI device structure
4634 static void megasas_detach_one(struct pci_dev
*pdev
)
4637 struct Scsi_Host
*host
;
4638 struct megasas_instance
*instance
;
4639 struct fusion_context
*fusion
;
4641 instance
= pci_get_drvdata(pdev
);
4642 instance
->unload
= 1;
4643 host
= instance
->host
;
4644 fusion
= instance
->ctrl_context
;
4646 scsi_remove_host(instance
->host
);
4647 megasas_flush_cache(instance
);
4648 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4650 /* cancel the delayed work if this work still in queue*/
4651 if (instance
->ev
!= NULL
) {
4652 struct megasas_aen_event
*ev
= instance
->ev
;
4653 cancel_delayed_work_sync(&ev
->hotplug_work
);
4654 instance
->ev
= NULL
;
4657 tasklet_kill(&instance
->isr_tasklet
);
4660 * Take the instance off the instance array. Note that we will not
4661 * decrement the max_index. We let this array be sparse array
4663 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
4664 if (megasas_mgmt_info
.instance
[i
] == instance
) {
4665 megasas_mgmt_info
.count
--;
4666 megasas_mgmt_info
.instance
[i
] = NULL
;
4672 pci_set_drvdata(instance
->pdev
, NULL
);
4674 instance
->instancet
->disable_intr(instance
);
4676 if (instance
->msix_vectors
)
4677 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4678 free_irq(instance
->msixentry
[i
].vector
,
4679 &instance
->irq_context
[i
]);
4681 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4682 if (instance
->msix_vectors
)
4683 pci_disable_msix(instance
->pdev
);
4685 switch (instance
->pdev
->device
) {
4686 case PCI_DEVICE_ID_LSI_FUSION
:
4687 case PCI_DEVICE_ID_LSI_INVADER
:
4688 case PCI_DEVICE_ID_LSI_FURY
:
4689 megasas_release_fusion(instance
);
4690 for (i
= 0; i
< 2 ; i
++)
4691 if (fusion
->ld_map
[i
])
4692 dma_free_coherent(&instance
->pdev
->dev
,
4697 kfree(instance
->ctrl_context
);
4700 megasas_release_mfi(instance
);
4701 pci_free_consistent(pdev
, sizeof(u32
),
4703 instance
->producer_h
);
4704 pci_free_consistent(pdev
, sizeof(u32
),
4706 instance
->consumer_h
);
4710 if (instance
->evt_detail
)
4711 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
4712 instance
->evt_detail
, instance
->evt_detail_h
);
4713 scsi_host_put(host
);
4715 pci_set_drvdata(pdev
, NULL
);
4717 pci_disable_device(pdev
);
4723 * megasas_shutdown - Shutdown entry point
4724 * @device: Generic device structure
4726 static void megasas_shutdown(struct pci_dev
*pdev
)
4729 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
4731 instance
->unload
= 1;
4732 megasas_flush_cache(instance
);
4733 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
4734 instance
->instancet
->disable_intr(instance
);
4735 if (instance
->msix_vectors
)
4736 for (i
= 0 ; i
< instance
->msix_vectors
; i
++)
4737 free_irq(instance
->msixentry
[i
].vector
,
4738 &instance
->irq_context
[i
]);
4740 free_irq(instance
->pdev
->irq
, &instance
->irq_context
[0]);
4741 if (instance
->msix_vectors
)
4742 pci_disable_msix(instance
->pdev
);
4746 * megasas_mgmt_open - char node "open" entry point
4748 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
4751 * Allow only those users with admin rights
4753 if (!capable(CAP_SYS_ADMIN
))
4760 * megasas_mgmt_fasync - Async notifier registration from applications
4762 * This function adds the calling process to a driver global queue. When an
4763 * event occurs, SIGIO will be sent to all processes in this queue.
4765 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
4769 mutex_lock(&megasas_async_queue_mutex
);
4771 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
4773 mutex_unlock(&megasas_async_queue_mutex
);
4776 /* For sanity check when we get ioctl */
4777 filep
->private_data
= filep
;
4781 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
4787 * megasas_mgmt_poll - char node "poll" entry point
4789 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
4792 unsigned long flags
;
4793 poll_wait(file
, &megasas_poll_wait
, wait
);
4794 spin_lock_irqsave(&poll_aen_lock
, flags
);
4795 if (megasas_poll_wait_aen
)
4796 mask
= (POLLIN
| POLLRDNORM
);
4799 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
4804 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4805 * @instance: Adapter soft state
4806 * @argp: User's ioctl packet
4809 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
4810 struct megasas_iocpacket __user
* user_ioc
,
4811 struct megasas_iocpacket
*ioc
)
4813 struct megasas_sge32
*kern_sge32
;
4814 struct megasas_cmd
*cmd
;
4815 void *kbuff_arr
[MAX_IOCTL_SGE
];
4816 dma_addr_t buf_handle
= 0;
4819 dma_addr_t sense_handle
;
4820 unsigned long *sense_ptr
;
4822 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
4824 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
4825 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
4826 ioc
->sge_count
, MAX_IOCTL_SGE
);
4830 cmd
= megasas_get_cmd(instance
);
4832 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
4837 * User's IOCTL packet has 2 frames (maximum). Copy those two
4838 * frames into our cmd's frames. cmd->frame's context will get
4839 * overwritten when we copy from user's frames. So set that value
4842 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
4843 cmd
->frame
->hdr
.context
= cmd
->index
;
4844 cmd
->frame
->hdr
.pad_0
= 0;
4845 cmd
->frame
->hdr
.flags
&= ~(MFI_FRAME_IEEE
| MFI_FRAME_SGL64
|
4849 * The management interface between applications and the fw uses
4850 * MFI frames. E.g, RAID configuration changes, LD property changes
4851 * etc are accomplishes through different kinds of MFI frames. The
4852 * driver needs to care only about substituting user buffers with
4853 * kernel buffers in SGLs. The location of SGL is embedded in the
4854 * struct iocpacket itself.
4856 kern_sge32
= (struct megasas_sge32
*)
4857 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
4860 * For each user buffer, create a mirror buffer and copy in
4862 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4863 if (!ioc
->sgl
[i
].iov_len
)
4866 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
4867 ioc
->sgl
[i
].iov_len
,
4868 &buf_handle
, GFP_KERNEL
);
4869 if (!kbuff_arr
[i
]) {
4870 printk(KERN_DEBUG
"megasas: Failed to alloc "
4871 "kernel SGL buffer for IOCTL \n");
4877 * We don't change the dma_coherent_mask, so
4878 * pci_alloc_consistent only returns 32bit addresses
4880 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
4881 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
4884 * We created a kernel buffer corresponding to the
4885 * user buffer. Now copy in from the user buffer
4887 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
4888 (u32
) (ioc
->sgl
[i
].iov_len
))) {
4894 if (ioc
->sense_len
) {
4895 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4896 &sense_handle
, GFP_KERNEL
);
4903 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
4904 *sense_ptr
= sense_handle
;
4908 * Set the sync_cmd flag so that the ISR knows not to complete this
4909 * cmd to the SCSI mid-layer
4912 megasas_issue_blocked_cmd(instance
, cmd
);
4916 * copy out the kernel buffers to user buffers
4918 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4919 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
4920 ioc
->sgl
[i
].iov_len
)) {
4927 * copy out the sense
4929 if (ioc
->sense_len
) {
4931 * sense_ptr points to the location that has the user
4932 * sense buffer address
4934 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
4937 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
4938 sense
, ioc
->sense_len
)) {
4939 printk(KERN_ERR
"megasas: Failed to copy out to user "
4947 * copy the status codes returned by the fw
4949 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
4950 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
4951 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
4957 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
4958 sense
, sense_handle
);
4961 for (i
= 0; i
< ioc
->sge_count
; i
++) {
4963 dma_free_coherent(&instance
->pdev
->dev
,
4964 kern_sge32
[i
].length
,
4966 kern_sge32
[i
].phys_addr
);
4969 megasas_return_cmd(instance
, cmd
);
4973 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
4975 struct megasas_iocpacket __user
*user_ioc
=
4976 (struct megasas_iocpacket __user
*)arg
;
4977 struct megasas_iocpacket
*ioc
;
4978 struct megasas_instance
*instance
;
4981 unsigned long flags
;
4982 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
4984 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
4988 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
4993 instance
= megasas_lookup_instance(ioc
->host_no
);
4999 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
5000 printk(KERN_ERR
"Controller in crit error\n");
5005 if (instance
->unload
== 1) {
5011 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
5013 if (down_interruptible(&instance
->ioctl_sem
)) {
5014 error
= -ERESTARTSYS
;
5018 for (i
= 0; i
< wait_time
; i
++) {
5020 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5021 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
5022 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5025 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5027 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5028 printk(KERN_NOTICE
"megasas: waiting"
5029 "for controller reset to finish\n");
5035 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5036 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5037 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5039 printk(KERN_ERR
"megaraid_sas: timed out while"
5040 "waiting for HBA to recover\n");
5044 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5046 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
5048 up(&instance
->ioctl_sem
);
5055 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
5057 struct megasas_instance
*instance
;
5058 struct megasas_aen aen
;
5061 unsigned long flags
;
5062 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
5064 if (file
->private_data
!= file
) {
5065 printk(KERN_DEBUG
"megasas: fasync_helper was not "
5070 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
5073 instance
= megasas_lookup_instance(aen
.host_no
);
5078 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
5082 if (instance
->unload
== 1) {
5086 for (i
= 0; i
< wait_time
; i
++) {
5088 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5089 if (instance
->adprecovery
== MEGASAS_HBA_OPERATIONAL
) {
5090 spin_unlock_irqrestore(&instance
->hba_lock
,
5095 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5097 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
5098 printk(KERN_NOTICE
"megasas: waiting for"
5099 "controller reset to finish\n");
5105 spin_lock_irqsave(&instance
->hba_lock
, flags
);
5106 if (instance
->adprecovery
!= MEGASAS_HBA_OPERATIONAL
) {
5107 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5108 printk(KERN_ERR
"megaraid_sas: timed out while waiting"
5109 "for HBA to recover.\n");
5112 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
5114 mutex_lock(&instance
->aen_mutex
);
5115 error
= megasas_register_aen(instance
, aen
.seq_num
,
5116 aen
.class_locale_word
);
5117 mutex_unlock(&instance
->aen_mutex
);
5122 * megasas_mgmt_ioctl - char node ioctl entry point
5125 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
5128 case MEGASAS_IOC_FIRMWARE
:
5129 return megasas_mgmt_ioctl_fw(file
, arg
);
5131 case MEGASAS_IOC_GET_AEN
:
5132 return megasas_mgmt_ioctl_aen(file
, arg
);
5138 #ifdef CONFIG_COMPAT
5139 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
5141 struct compat_megasas_iocpacket __user
*cioc
=
5142 (struct compat_megasas_iocpacket __user
*)arg
;
5143 struct megasas_iocpacket __user
*ioc
=
5144 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
5149 if (clear_user(ioc
, sizeof(*ioc
)))
5152 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
5153 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
5154 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
5155 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
5156 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
5157 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
5161 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5162 * sense_len is not null, so prepare the 64bit value under
5163 * the same condition.
5165 if (ioc
->sense_len
) {
5166 void __user
**sense_ioc_ptr
=
5167 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
5168 compat_uptr_t
*sense_cioc_ptr
=
5169 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
5170 if (get_user(ptr
, sense_cioc_ptr
) ||
5171 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
5175 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
5176 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
5177 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
5178 copy_in_user(&ioc
->sgl
[i
].iov_len
,
5179 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
5183 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
5185 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
5186 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
5187 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
5194 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
5198 case MEGASAS_IOC_FIRMWARE32
:
5199 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
5200 case MEGASAS_IOC_GET_AEN
:
5201 return megasas_mgmt_ioctl_aen(file
, arg
);
5209 * File operations structure for management interface
5211 static const struct file_operations megasas_mgmt_fops
= {
5212 .owner
= THIS_MODULE
,
5213 .open
= megasas_mgmt_open
,
5214 .fasync
= megasas_mgmt_fasync
,
5215 .unlocked_ioctl
= megasas_mgmt_ioctl
,
5216 .poll
= megasas_mgmt_poll
,
5217 #ifdef CONFIG_COMPAT
5218 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
5220 .llseek
= noop_llseek
,
5224 * PCI hotplug support registration structure
5226 static struct pci_driver megasas_pci_driver
= {
5228 .name
= "megaraid_sas",
5229 .id_table
= megasas_pci_table
,
5230 .probe
= megasas_probe_one
,
5231 .remove
= megasas_detach_one
,
5232 .suspend
= megasas_suspend
,
5233 .resume
= megasas_resume
,
5234 .shutdown
= megasas_shutdown
,
5238 * Sysfs driver attributes
5240 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
5242 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
5246 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
5249 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
5251 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
5255 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
5259 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
5261 return sprintf(buf
, "%u\n", support_poll_for_event
);
5264 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
5265 megasas_sysfs_show_support_poll_for_event
, NULL
);
5268 megasas_sysfs_show_support_device_change(struct device_driver
*dd
, char *buf
)
5270 return sprintf(buf
, "%u\n", support_device_change
);
5273 static DRIVER_ATTR(support_device_change
, S_IRUGO
,
5274 megasas_sysfs_show_support_device_change
, NULL
);
5277 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
5279 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
5283 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
5286 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
5287 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
5293 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
5294 megasas_sysfs_set_dbg_lvl
);
5297 megasas_aen_polling(struct work_struct
*work
)
5299 struct megasas_aen_event
*ev
=
5300 container_of(work
, struct megasas_aen_event
, hotplug_work
.work
);
5301 struct megasas_instance
*instance
= ev
->instance
;
5302 union megasas_evt_class_locale class_locale
;
5303 struct Scsi_Host
*host
;
5304 struct scsi_device
*sdev1
;
5307 int i
, j
, doscan
= 0;
5312 printk(KERN_ERR
"invalid instance!\n");
5316 instance
->ev
= NULL
;
5317 host
= instance
->host
;
5318 if (instance
->evt_detail
) {
5320 switch (instance
->evt_detail
->code
) {
5321 case MR_EVT_PD_INSERTED
:
5322 if (megasas_get_pd_list(instance
) == 0) {
5323 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5325 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5329 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5332 scsi_device_lookup(host
, i
, j
, 0);
5334 if (instance
->pd_list
[pd_index
].driveState
5335 == MR_PD_STATE_SYSTEM
) {
5337 scsi_add_device(host
, i
, j
, 0);
5341 scsi_device_put(sdev1
);
5349 case MR_EVT_PD_REMOVED
:
5350 if (megasas_get_pd_list(instance
) == 0) {
5351 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5353 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5357 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5360 scsi_device_lookup(host
, i
, j
, 0);
5362 if (instance
->pd_list
[pd_index
].driveState
5363 == MR_PD_STATE_SYSTEM
) {
5365 scsi_device_put(sdev1
);
5369 scsi_remove_device(sdev1
);
5370 scsi_device_put(sdev1
);
5379 case MR_EVT_LD_OFFLINE
:
5380 case MR_EVT_CFG_CLEARED
:
5381 case MR_EVT_LD_DELETED
:
5382 megasas_get_ld_list(instance
);
5383 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5385 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5389 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5391 sdev1
= scsi_device_lookup(host
,
5392 i
+ MEGASAS_MAX_LD_CHANNELS
,
5396 if (instance
->ld_ids
[ld_index
] != 0xff) {
5398 scsi_device_put(sdev1
);
5402 scsi_remove_device(sdev1
);
5403 scsi_device_put(sdev1
);
5410 case MR_EVT_LD_CREATED
:
5411 megasas_get_ld_list(instance
);
5412 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5414 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
5417 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5419 sdev1
= scsi_device_lookup(host
,
5420 i
+MEGASAS_MAX_LD_CHANNELS
,
5423 if (instance
->ld_ids
[ld_index
] !=
5426 scsi_add_device(host
,
5432 scsi_device_put(sdev1
);
5438 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
5439 case MR_EVT_FOREIGN_CFG_IMPORTED
:
5440 case MR_EVT_LD_STATE_CHANGE
:
5448 printk(KERN_ERR
"invalid evt_detail!\n");
5454 printk(KERN_INFO
"scanning ...\n");
5455 megasas_get_pd_list(instance
);
5456 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
5457 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5458 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
5459 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
5460 if (instance
->pd_list
[pd_index
].driveState
==
5461 MR_PD_STATE_SYSTEM
) {
5463 scsi_add_device(host
, i
, j
, 0);
5466 scsi_device_put(sdev1
);
5469 scsi_remove_device(sdev1
);
5470 scsi_device_put(sdev1
);
5476 megasas_get_ld_list(instance
);
5477 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
5478 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
5480 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
5482 sdev1
= scsi_device_lookup(host
,
5483 i
+MEGASAS_MAX_LD_CHANNELS
, j
, 0);
5484 if (instance
->ld_ids
[ld_index
] != 0xff) {
5486 scsi_add_device(host
,
5490 scsi_device_put(sdev1
);
5494 scsi_remove_device(sdev1
);
5495 scsi_device_put(sdev1
);
5502 if ( instance
->aen_cmd
!= NULL
) {
5507 seq_num
= instance
->evt_detail
->seq_num
+ 1;
5509 /* Register AEN with FW for latest sequence number plus 1 */
5510 class_locale
.members
.reserved
= 0;
5511 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
5512 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
5513 mutex_lock(&instance
->aen_mutex
);
5514 error
= megasas_register_aen(instance
, seq_num
,
5516 mutex_unlock(&instance
->aen_mutex
);
5519 printk(KERN_ERR
"register aen failed error %x\n", error
);
5525 * megasas_init - Driver load entry point
5527 static int __init
megasas_init(void)
5532 * Announce driver version and other information
5534 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
5535 MEGASAS_EXT_VERSION
);
5537 spin_lock_init(&poll_aen_lock
);
5539 support_poll_for_event
= 2;
5540 support_device_change
= 1;
5542 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
5545 * Register character device node
5547 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
5550 printk(KERN_DEBUG
"megasas: failed to open device node\n");
5554 megasas_mgmt_majorno
= rval
;
5557 * Register ourselves as PCI hotplug module
5559 rval
= pci_register_driver(&megasas_pci_driver
);
5562 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
5566 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5567 &driver_attr_version
);
5569 goto err_dcf_attr_ver
;
5570 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5571 &driver_attr_release_date
);
5573 goto err_dcf_rel_date
;
5575 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5576 &driver_attr_support_poll_for_event
);
5578 goto err_dcf_support_poll_for_event
;
5580 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5581 &driver_attr_dbg_lvl
);
5583 goto err_dcf_dbg_lvl
;
5584 rval
= driver_create_file(&megasas_pci_driver
.driver
,
5585 &driver_attr_support_device_change
);
5587 goto err_dcf_support_device_change
;
5591 err_dcf_support_device_change
:
5592 driver_remove_file(&megasas_pci_driver
.driver
,
5593 &driver_attr_dbg_lvl
);
5595 driver_remove_file(&megasas_pci_driver
.driver
,
5596 &driver_attr_support_poll_for_event
);
5598 err_dcf_support_poll_for_event
:
5599 driver_remove_file(&megasas_pci_driver
.driver
,
5600 &driver_attr_release_date
);
5603 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5605 pci_unregister_driver(&megasas_pci_driver
);
5607 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5612 * megasas_exit - Driver unload entry point
5614 static void __exit
megasas_exit(void)
5616 driver_remove_file(&megasas_pci_driver
.driver
,
5617 &driver_attr_dbg_lvl
);
5618 driver_remove_file(&megasas_pci_driver
.driver
,
5619 &driver_attr_support_poll_for_event
);
5620 driver_remove_file(&megasas_pci_driver
.driver
,
5621 &driver_attr_support_device_change
);
5622 driver_remove_file(&megasas_pci_driver
.driver
,
5623 &driver_attr_release_date
);
5624 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
5626 pci_unregister_driver(&megasas_pci_driver
);
5627 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
5630 module_init(megasas_init
);
5631 module_exit(megasas_exit
);