3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_sas.c
13 * Version : v00.00.04.17.1-rc1
16 * (email-id : megaraidlinux@lsi.com)
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/list.h>
31 #include <linux/moduleparam.h>
32 #include <linux/module.h>
33 #include <linux/spinlock.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/smp_lock.h>
37 #include <linux/uio.h>
38 #include <linux/slab.h>
39 #include <asm/uaccess.h>
41 #include <linux/compat.h>
42 #include <linux/blkdev.h>
43 #include <linux/mutex.h>
44 #include <linux/poll.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_cmnd.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_host.h>
50 #include "megaraid_sas.h"
53 * poll_mode_io:1- schedule complete completion from q cmd
55 static unsigned int poll_mode_io
;
56 module_param_named(poll_mode_io
, poll_mode_io
, int, 0);
57 MODULE_PARM_DESC(poll_mode_io
,
58 "Complete cmds from IO path, (default=0)");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MEGASAS_VERSION
);
62 MODULE_AUTHOR("megaraidlinux@lsi.com");
63 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
66 * PCI ID table for all supported controllers
68 static struct pci_device_id megasas_pci_table
[] = {
70 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1064R
)},
72 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078R
)},
74 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078DE
)},
76 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078GEN2
)},
78 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0079GEN2
)},
80 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0073SKINNY
)},
82 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS0071SKINNY
)},
84 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_VERDE_ZCR
)},
85 /* xscale IOP, vega */
86 {PCI_DEVICE(PCI_VENDOR_ID_DELL
, PCI_DEVICE_ID_DELL_PERC5
)},
91 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
93 static int megasas_mgmt_majorno
;
94 static struct megasas_mgmt_info megasas_mgmt_info
;
95 static struct fasync_struct
*megasas_async_queue
;
96 static DEFINE_MUTEX(megasas_async_queue_mutex
);
98 static int megasas_poll_wait_aen
;
99 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait
);
100 static u32 support_poll_for_event
;
101 static u32 megasas_dbg_lvl
;
103 /* define lock for aen poll */
104 spinlock_t poll_aen_lock
;
107 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
111 * megasas_get_cmd - Get a command from the free pool
112 * @instance: Adapter soft state
114 * Returns a free command from the pool
116 static struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
120 struct megasas_cmd
*cmd
= NULL
;
122 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
124 if (!list_empty(&instance
->cmd_pool
)) {
125 cmd
= list_entry((&instance
->cmd_pool
)->next
,
126 struct megasas_cmd
, list
);
127 list_del_init(&cmd
->list
);
129 printk(KERN_ERR
"megasas: Command pool empty!\n");
132 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
137 * megasas_return_cmd - Return a cmd to free command pool
138 * @instance: Adapter soft state
139 * @cmd: Command packet to be returned to free command pool
142 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
146 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
149 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
151 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
156 * The following functions are defined for xscale
157 * (deviceid : 1064R, PERC5) controllers
161 * megasas_enable_intr_xscale - Enables interrupts
162 * @regs: MFI register set
165 megasas_enable_intr_xscale(struct megasas_register_set __iomem
* regs
)
167 writel(1, &(regs
)->outbound_intr_mask
);
169 /* Dummy readl to force pci flush */
170 readl(®s
->outbound_intr_mask
);
174 * megasas_disable_intr_xscale -Disables interrupt
175 * @regs: MFI register set
178 megasas_disable_intr_xscale(struct megasas_register_set __iomem
* regs
)
181 writel(mask
, ®s
->outbound_intr_mask
);
182 /* Dummy readl to force pci flush */
183 readl(®s
->outbound_intr_mask
);
187 * megasas_read_fw_status_reg_xscale - returns the current FW status value
188 * @regs: MFI register set
191 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
193 return readl(&(regs
)->outbound_msg_0
);
196 * megasas_clear_interrupt_xscale - Check & clear interrupt
197 * @regs: MFI register set
200 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
204 * Check if it is our interrupt
206 status
= readl(®s
->outbound_intr_status
);
208 if (!(status
& MFI_OB_INTR_STATUS_MASK
)) {
213 * Clear the interrupt by writing back the same value
215 writel(status
, ®s
->outbound_intr_status
);
217 /* Dummy readl to force pci flush */
218 readl(®s
->outbound_intr_status
);
224 * megasas_fire_cmd_xscale - Sends command to the FW
225 * @frame_phys_addr : Physical address of cmd
226 * @frame_count : Number of frames for the command
227 * @regs : MFI register set
230 megasas_fire_cmd_xscale(struct megasas_instance
*instance
,
231 dma_addr_t frame_phys_addr
,
233 struct megasas_register_set __iomem
*regs
)
235 writel((frame_phys_addr
>> 3)|(frame_count
),
236 &(regs
)->inbound_queue_port
);
239 static struct megasas_instance_template megasas_instance_template_xscale
= {
241 .fire_cmd
= megasas_fire_cmd_xscale
,
242 .enable_intr
= megasas_enable_intr_xscale
,
243 .disable_intr
= megasas_disable_intr_xscale
,
244 .clear_intr
= megasas_clear_intr_xscale
,
245 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
249 * This is the end of set of functions & definitions specific
250 * to xscale (deviceid : 1064R, PERC5) controllers
254 * The following functions are defined for ppc (deviceid : 0x60)
259 * megasas_enable_intr_ppc - Enables interrupts
260 * @regs: MFI register set
263 megasas_enable_intr_ppc(struct megasas_register_set __iomem
* regs
)
265 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
267 writel(~0x80000004, &(regs
)->outbound_intr_mask
);
269 /* Dummy readl to force pci flush */
270 readl(®s
->outbound_intr_mask
);
274 * megasas_disable_intr_ppc - Disable interrupt
275 * @regs: MFI register set
278 megasas_disable_intr_ppc(struct megasas_register_set __iomem
* regs
)
280 u32 mask
= 0xFFFFFFFF;
281 writel(mask
, ®s
->outbound_intr_mask
);
282 /* Dummy readl to force pci flush */
283 readl(®s
->outbound_intr_mask
);
287 * megasas_read_fw_status_reg_ppc - returns the current FW status value
288 * @regs: MFI register set
291 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
293 return readl(&(regs
)->outbound_scratch_pad
);
297 * megasas_clear_interrupt_ppc - Check & clear interrupt
298 * @regs: MFI register set
301 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
305 * Check if it is our interrupt
307 status
= readl(®s
->outbound_intr_status
);
309 if (!(status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)) {
314 * Clear the interrupt by writing back the same value
316 writel(status
, ®s
->outbound_doorbell_clear
);
318 /* Dummy readl to force pci flush */
319 readl(®s
->outbound_doorbell_clear
);
324 * megasas_fire_cmd_ppc - Sends command to the FW
325 * @frame_phys_addr : Physical address of cmd
326 * @frame_count : Number of frames for the command
327 * @regs : MFI register set
330 megasas_fire_cmd_ppc(struct megasas_instance
*instance
,
331 dma_addr_t frame_phys_addr
,
333 struct megasas_register_set __iomem
*regs
)
335 writel((frame_phys_addr
| (frame_count
<<1))|1,
336 &(regs
)->inbound_queue_port
);
339 static struct megasas_instance_template megasas_instance_template_ppc
= {
341 .fire_cmd
= megasas_fire_cmd_ppc
,
342 .enable_intr
= megasas_enable_intr_ppc
,
343 .disable_intr
= megasas_disable_intr_ppc
,
344 .clear_intr
= megasas_clear_intr_ppc
,
345 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
349 * megasas_enable_intr_skinny - Enables interrupts
350 * @regs: MFI register set
353 megasas_enable_intr_skinny(struct megasas_register_set __iomem
*regs
)
355 writel(0xFFFFFFFF, &(regs
)->outbound_intr_mask
);
357 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
359 /* Dummy readl to force pci flush */
360 readl(®s
->outbound_intr_mask
);
364 * megasas_disable_intr_skinny - Disables interrupt
365 * @regs: MFI register set
368 megasas_disable_intr_skinny(struct megasas_register_set __iomem
*regs
)
370 u32 mask
= 0xFFFFFFFF;
371 writel(mask
, ®s
->outbound_intr_mask
);
372 /* Dummy readl to force pci flush */
373 readl(®s
->outbound_intr_mask
);
377 * megasas_read_fw_status_reg_skinny - returns the current FW status value
378 * @regs: MFI register set
381 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem
*regs
)
383 return readl(&(regs
)->outbound_scratch_pad
);
387 * megasas_clear_interrupt_skinny - Check & clear interrupt
388 * @regs: MFI register set
391 megasas_clear_intr_skinny(struct megasas_register_set __iomem
*regs
)
395 * Check if it is our interrupt
397 status
= readl(®s
->outbound_intr_status
);
399 if (!(status
& MFI_SKINNY_ENABLE_INTERRUPT_MASK
)) {
404 * Clear the interrupt by writing back the same value
406 writel(status
, ®s
->outbound_intr_status
);
409 * dummy read to flush PCI
411 readl(®s
->outbound_intr_status
);
417 * megasas_fire_cmd_skinny - Sends command to the FW
418 * @frame_phys_addr : Physical address of cmd
419 * @frame_count : Number of frames for the command
420 * @regs : MFI register set
423 megasas_fire_cmd_skinny(struct megasas_instance
*instance
,
424 dma_addr_t frame_phys_addr
,
426 struct megasas_register_set __iomem
*regs
)
429 spin_lock_irqsave(&instance
->fire_lock
, flags
);
430 writel(0, &(regs
)->inbound_high_queue_port
);
431 writel((frame_phys_addr
| (frame_count
<<1))|1,
432 &(regs
)->inbound_low_queue_port
);
433 spin_unlock_irqrestore(&instance
->fire_lock
, flags
);
436 static struct megasas_instance_template megasas_instance_template_skinny
= {
438 .fire_cmd
= megasas_fire_cmd_skinny
,
439 .enable_intr
= megasas_enable_intr_skinny
,
440 .disable_intr
= megasas_disable_intr_skinny
,
441 .clear_intr
= megasas_clear_intr_skinny
,
442 .read_fw_status_reg
= megasas_read_fw_status_reg_skinny
,
447 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
452 * megasas_enable_intr_gen2 - Enables interrupts
453 * @regs: MFI register set
456 megasas_enable_intr_gen2(struct megasas_register_set __iomem
*regs
)
458 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
460 /* write ~0x00000005 (4 & 1) to the intr mask*/
461 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
463 /* Dummy readl to force pci flush */
464 readl(®s
->outbound_intr_mask
);
468 * megasas_disable_intr_gen2 - Disables interrupt
469 * @regs: MFI register set
472 megasas_disable_intr_gen2(struct megasas_register_set __iomem
*regs
)
474 u32 mask
= 0xFFFFFFFF;
475 writel(mask
, ®s
->outbound_intr_mask
);
476 /* Dummy readl to force pci flush */
477 readl(®s
->outbound_intr_mask
);
481 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
482 * @regs: MFI register set
485 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem
*regs
)
487 return readl(&(regs
)->outbound_scratch_pad
);
491 * megasas_clear_interrupt_gen2 - Check & clear interrupt
492 * @regs: MFI register set
495 megasas_clear_intr_gen2(struct megasas_register_set __iomem
*regs
)
499 * Check if it is our interrupt
501 status
= readl(®s
->outbound_intr_status
);
503 if (!(status
& MFI_GEN2_ENABLE_INTERRUPT_MASK
))
507 * Clear the interrupt by writing back the same value
509 writel(status
, ®s
->outbound_doorbell_clear
);
511 /* Dummy readl to force pci flush */
512 readl(®s
->outbound_intr_status
);
517 * megasas_fire_cmd_gen2 - Sends command to the FW
518 * @frame_phys_addr : Physical address of cmd
519 * @frame_count : Number of frames for the command
520 * @regs : MFI register set
523 megasas_fire_cmd_gen2(struct megasas_instance
*instance
,
524 dma_addr_t frame_phys_addr
,
526 struct megasas_register_set __iomem
*regs
)
528 writel((frame_phys_addr
| (frame_count
<<1))|1,
529 &(regs
)->inbound_queue_port
);
532 static struct megasas_instance_template megasas_instance_template_gen2
= {
534 .fire_cmd
= megasas_fire_cmd_gen2
,
535 .enable_intr
= megasas_enable_intr_gen2
,
536 .disable_intr
= megasas_disable_intr_gen2
,
537 .clear_intr
= megasas_clear_intr_gen2
,
538 .read_fw_status_reg
= megasas_read_fw_status_reg_gen2
,
542 * This is the end of set of functions & definitions
543 * specific to ppc (deviceid : 0x60) controllers
547 * megasas_issue_polled - Issues a polling command
548 * @instance: Adapter soft state
549 * @cmd: Command packet to be issued
551 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
554 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
557 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
559 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
561 frame_hdr
->cmd_status
= 0xFF;
562 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
565 * Issue the frame using inbound queue port
567 instance
->instancet
->fire_cmd(instance
,
568 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
571 * Wait for cmd_status to change
573 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
++) {
578 if (frame_hdr
->cmd_status
== 0xff)
585 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
586 * @instance: Adapter soft state
587 * @cmd: Command to be issued
589 * This function waits on an event for the command to be returned from ISR.
590 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
591 * Used to issue ioctl commands.
594 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
595 struct megasas_cmd
*cmd
)
597 cmd
->cmd_status
= ENODATA
;
599 instance
->instancet
->fire_cmd(instance
,
600 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
602 wait_event_timeout(instance
->int_cmd_wait_q
, (cmd
->cmd_status
!= ENODATA
),
603 MEGASAS_INTERNAL_CMD_WAIT_TIME
*HZ
);
609 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
610 * @instance: Adapter soft state
611 * @cmd_to_abort: Previously issued cmd to be aborted
613 * MFI firmware can abort previously issued AEN comamnd (automatic event
614 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
615 * cmd and waits for return status.
616 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
619 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
620 struct megasas_cmd
*cmd_to_abort
)
622 struct megasas_cmd
*cmd
;
623 struct megasas_abort_frame
*abort_fr
;
625 cmd
= megasas_get_cmd(instance
);
630 abort_fr
= &cmd
->frame
->abort
;
633 * Prepare and issue the abort frame
635 abort_fr
->cmd
= MFI_CMD_ABORT
;
636 abort_fr
->cmd_status
= 0xFF;
638 abort_fr
->abort_context
= cmd_to_abort
->index
;
639 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
640 abort_fr
->abort_mfi_phys_addr_hi
= 0;
643 cmd
->cmd_status
= 0xFF;
645 instance
->instancet
->fire_cmd(instance
,
646 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
649 * Wait for this cmd to complete
651 wait_event_timeout(instance
->abort_cmd_wait_q
, (cmd
->cmd_status
!= 0xFF),
652 MEGASAS_INTERNAL_CMD_WAIT_TIME
*HZ
);
654 megasas_return_cmd(instance
, cmd
);
659 * megasas_make_sgl32 - Prepares 32-bit SGL
660 * @instance: Adapter soft state
661 * @scp: SCSI command from the mid-layer
662 * @mfi_sgl: SGL to be filled in
664 * If successful, this function returns the number of SG elements. Otherwise,
668 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
669 union megasas_sgl
*mfi_sgl
)
673 struct scatterlist
*os_sgl
;
675 sge_count
= scsi_dma_map(scp
);
676 BUG_ON(sge_count
< 0);
679 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
680 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
681 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
688 * megasas_make_sgl64 - Prepares 64-bit SGL
689 * @instance: Adapter soft state
690 * @scp: SCSI command from the mid-layer
691 * @mfi_sgl: SGL to be filled in
693 * If successful, this function returns the number of SG elements. Otherwise,
697 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
698 union megasas_sgl
*mfi_sgl
)
702 struct scatterlist
*os_sgl
;
704 sge_count
= scsi_dma_map(scp
);
705 BUG_ON(sge_count
< 0);
708 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
709 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
710 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
717 * megasas_make_sgl_skinny - Prepares IEEE SGL
718 * @instance: Adapter soft state
719 * @scp: SCSI command from the mid-layer
720 * @mfi_sgl: SGL to be filled in
722 * If successful, this function returns the number of SG elements. Otherwise,
726 megasas_make_sgl_skinny(struct megasas_instance
*instance
,
727 struct scsi_cmnd
*scp
, union megasas_sgl
*mfi_sgl
)
731 struct scatterlist
*os_sgl
;
733 sge_count
= scsi_dma_map(scp
);
736 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
737 mfi_sgl
->sge_skinny
[i
].length
= sg_dma_len(os_sgl
);
738 mfi_sgl
->sge_skinny
[i
].phys_addr
=
739 sg_dma_address(os_sgl
);
746 * megasas_get_frame_count - Computes the number of frames
747 * @frame_type : type of frame- io or pthru frame
748 * @sge_count : number of sg elements
750 * Returns the number of frames required for numnber of sge's (sge_count)
753 static u32
megasas_get_frame_count(struct megasas_instance
*instance
,
754 u8 sge_count
, u8 frame_type
)
761 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
762 sizeof(struct megasas_sge32
);
764 if (instance
->flag_ieee
) {
765 sge_sz
= sizeof(struct megasas_sge_skinny
);
769 * Main frame can contain 2 SGEs for 64-bit SGLs and
770 * 3 SGEs for 32-bit SGLs for ldio &
771 * 1 SGEs for 64-bit SGLs and
772 * 2 SGEs for 32-bit SGLs for pthru frame
774 if (unlikely(frame_type
== PTHRU_FRAME
)) {
775 if (instance
->flag_ieee
== 1) {
776 num_cnt
= sge_count
- 1;
778 num_cnt
= sge_count
- 1;
780 num_cnt
= sge_count
- 2;
782 if (instance
->flag_ieee
== 1) {
783 num_cnt
= sge_count
- 1;
785 num_cnt
= sge_count
- 2;
787 num_cnt
= sge_count
- 3;
791 sge_bytes
= sge_sz
* num_cnt
;
793 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
794 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
805 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
806 * @instance: Adapter soft state
808 * @cmd: Command to be prepared in
810 * This function prepares CDB commands. These are typcially pass-through
811 * commands to the devices.
814 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
815 struct megasas_cmd
*cmd
)
820 struct megasas_pthru_frame
*pthru
;
822 is_logical
= MEGASAS_IS_LOGICAL(scp
);
823 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
824 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
826 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
827 flags
= MFI_FRAME_DIR_WRITE
;
828 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
829 flags
= MFI_FRAME_DIR_READ
;
830 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
831 flags
= MFI_FRAME_DIR_NONE
;
833 if (instance
->flag_ieee
== 1) {
834 flags
|= MFI_FRAME_IEEE
;
838 * Prepare the DCDB frame
840 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
841 pthru
->cmd_status
= 0x0;
842 pthru
->scsi_status
= 0x0;
843 pthru
->target_id
= device_id
;
844 pthru
->lun
= scp
->device
->lun
;
845 pthru
->cdb_len
= scp
->cmd_len
;
848 pthru
->flags
= flags
;
849 pthru
->data_xfer_len
= scsi_bufflen(scp
);
851 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
854 * If the command is for the tape device, set the
855 * pthru timeout to the os layer timeout value.
857 if (scp
->device
->type
== TYPE_TAPE
) {
858 if ((scp
->request
->timeout
/ HZ
) > 0xFFFF)
859 pthru
->timeout
= 0xFFFF;
861 pthru
->timeout
= scp
->request
->timeout
/ HZ
;
867 if (instance
->flag_ieee
== 1) {
868 pthru
->flags
|= MFI_FRAME_SGL64
;
869 pthru
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
871 } else if (IS_DMA64
) {
872 pthru
->flags
|= MFI_FRAME_SGL64
;
873 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
876 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
879 if (pthru
->sge_count
> instance
->max_num_sge
) {
880 printk(KERN_ERR
"megasas: DCDB two many SGE NUM=%x\n",
886 * Sense info specific
888 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
889 pthru
->sense_buf_phys_addr_hi
= 0;
890 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
893 * Compute the total number of frames this command consumes. FW uses
894 * this number to pull sufficient number of frames from host memory.
896 cmd
->frame_count
= megasas_get_frame_count(instance
, pthru
->sge_count
,
899 return cmd
->frame_count
;
903 * megasas_build_ldio - Prepares IOs to logical devices
904 * @instance: Adapter soft state
906 * @cmd: Command to be prepared
908 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
911 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
912 struct megasas_cmd
*cmd
)
915 u8 sc
= scp
->cmnd
[0];
917 struct megasas_io_frame
*ldio
;
919 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
920 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
922 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
923 flags
= MFI_FRAME_DIR_WRITE
;
924 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
925 flags
= MFI_FRAME_DIR_READ
;
927 if (instance
->flag_ieee
== 1) {
928 flags
|= MFI_FRAME_IEEE
;
932 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
934 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
935 ldio
->cmd_status
= 0x0;
936 ldio
->scsi_status
= 0x0;
937 ldio
->target_id
= device_id
;
939 ldio
->reserved_0
= 0;
942 ldio
->start_lba_hi
= 0;
943 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
946 * 6-byte READ(0x08) or WRITE(0x0A) cdb
948 if (scp
->cmd_len
== 6) {
949 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
950 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
951 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
953 ldio
->start_lba_lo
&= 0x1FFFFF;
957 * 10-byte READ(0x28) or WRITE(0x2A) cdb
959 else if (scp
->cmd_len
== 10) {
960 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
961 ((u32
) scp
->cmnd
[7] << 8);
962 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
963 ((u32
) scp
->cmnd
[3] << 16) |
964 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
968 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
970 else if (scp
->cmd_len
== 12) {
971 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
972 ((u32
) scp
->cmnd
[7] << 16) |
973 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
975 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
976 ((u32
) scp
->cmnd
[3] << 16) |
977 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
981 * 16-byte READ(0x88) or WRITE(0x8A) cdb
983 else if (scp
->cmd_len
== 16) {
984 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
985 ((u32
) scp
->cmnd
[11] << 16) |
986 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
988 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
989 ((u32
) scp
->cmnd
[7] << 16) |
990 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
992 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
993 ((u32
) scp
->cmnd
[3] << 16) |
994 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1001 if (instance
->flag_ieee
) {
1002 ldio
->flags
|= MFI_FRAME_SGL64
;
1003 ldio
->sge_count
= megasas_make_sgl_skinny(instance
, scp
,
1005 } else if (IS_DMA64
) {
1006 ldio
->flags
|= MFI_FRAME_SGL64
;
1007 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
1009 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
1011 if (ldio
->sge_count
> instance
->max_num_sge
) {
1012 printk(KERN_ERR
"megasas: build_ld_io: sge_count = %x\n",
1018 * Sense info specific
1020 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
1021 ldio
->sense_buf_phys_addr_hi
= 0;
1022 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
1025 * Compute the total number of frames this command consumes. FW uses
1026 * this number to pull sufficient number of frames from host memory.
1028 cmd
->frame_count
= megasas_get_frame_count(instance
,
1029 ldio
->sge_count
, IO_FRAME
);
1031 return cmd
->frame_count
;
1035 * megasas_is_ldio - Checks if the cmd is for logical drive
1036 * @scmd: SCSI command
1038 * Called by megasas_queue_command to find out if the command to be queued
1039 * is a logical drive command
1041 static inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
1043 if (!MEGASAS_IS_LOGICAL(cmd
))
1045 switch (cmd
->cmnd
[0]) {
1061 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1063 * @instance: Adapter soft state
1066 megasas_dump_pending_frames(struct megasas_instance
*instance
)
1068 struct megasas_cmd
*cmd
;
1070 union megasas_sgl
*mfi_sgl
;
1071 struct megasas_io_frame
*ldio
;
1072 struct megasas_pthru_frame
*pthru
;
1074 u32 max_cmd
= instance
->max_fw_cmds
;
1076 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
1077 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
1079 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1081 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
1083 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
1084 for (i
= 0; i
< max_cmd
; i
++) {
1085 cmd
= instance
->cmd_list
[i
];
1088 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
1089 if (megasas_is_ldio(cmd
->scmd
)){
1090 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
1091 mfi_sgl
= &ldio
->sgl
;
1092 sgcount
= ldio
->sge_count
;
1093 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
);
1096 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
1097 mfi_sgl
= &pthru
->sgl
;
1098 sgcount
= pthru
->sge_count
;
1099 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
);
1101 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
1102 for (n
= 0; n
< sgcount
; n
++){
1104 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
) ;
1106 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl
->sge32
[n
].length
, mfi_sgl
->sge32
[n
].phys_addr
) ;
1109 printk(KERN_ERR
"\n");
1111 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
1112 for (i
= 0; i
< max_cmd
; i
++) {
1114 cmd
= instance
->cmd_list
[i
];
1116 if(cmd
->sync_cmd
== 1){
1117 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
1120 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
1124 * megasas_queue_command - Queue entry point
1125 * @scmd: SCSI command to be queued
1126 * @done: Callback entry point
1129 megasas_queue_command(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
1132 struct megasas_cmd
*cmd
;
1133 struct megasas_instance
*instance
;
1135 instance
= (struct megasas_instance
*)
1136 scmd
->device
->host
->hostdata
;
1138 /* Don't process if we have already declared adapter dead */
1139 if (instance
->hw_crit_error
)
1140 return SCSI_MLQUEUE_HOST_BUSY
;
1142 scmd
->scsi_done
= done
;
1145 if (MEGASAS_IS_LOGICAL(scmd
) &&
1146 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
1147 scmd
->result
= DID_BAD_TARGET
<< 16;
1151 switch (scmd
->cmnd
[0]) {
1152 case SYNCHRONIZE_CACHE
:
1154 * FW takes care of flush cache on its own
1155 * No need to send it down
1157 scmd
->result
= DID_OK
<< 16;
1163 cmd
= megasas_get_cmd(instance
);
1165 return SCSI_MLQUEUE_HOST_BUSY
;
1168 * Logical drive command
1170 if (megasas_is_ldio(scmd
))
1171 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
1173 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
1176 goto out_return_cmd
;
1179 scmd
->SCp
.ptr
= (char *)cmd
;
1182 * Issue the command to the FW
1184 atomic_inc(&instance
->fw_outstanding
);
1186 instance
->instancet
->fire_cmd(instance
, cmd
->frame_phys_addr
,
1187 cmd
->frame_count
-1, instance
->reg_set
);
1189 * Check if we have pend cmds to be completed
1191 if (poll_mode_io
&& atomic_read(&instance
->fw_outstanding
))
1192 tasklet_schedule(&instance
->isr_tasklet
);
1198 megasas_return_cmd(instance
, cmd
);
1204 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
1208 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
1210 if ((megasas_mgmt_info
.instance
[i
]) &&
1211 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
1212 return megasas_mgmt_info
.instance
[i
];
1218 static int megasas_slave_configure(struct scsi_device
*sdev
)
1221 struct megasas_instance
*instance
;
1223 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1226 * Don't export physical disk devices to the disk driver.
1228 * FIXME: Currently we don't export them to the midlayer at all.
1229 * That will be fixed once LSI engineers have audited the
1230 * firmware for possible issues.
1232 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1233 sdev
->type
== TYPE_DISK
) {
1234 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1236 if (instance
->pd_list
[pd_index
].driveState
==
1237 MR_PD_STATE_SYSTEM
) {
1238 blk_queue_rq_timeout(sdev
->request_queue
,
1239 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1246 * The RAID firmware may require extended timeouts.
1248 blk_queue_rq_timeout(sdev
->request_queue
,
1249 MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
);
1253 static int megasas_slave_alloc(struct scsi_device
*sdev
)
1256 struct megasas_instance
*instance
;
1257 instance
= megasas_lookup_instance(sdev
->host
->host_no
);
1258 if ((sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
) &&
1259 (sdev
->type
== TYPE_DISK
)) {
1261 * Open the OS scan to the SYSTEM PD
1264 (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
) +
1266 if ((instance
->pd_list
[pd_index
].driveState
==
1267 MR_PD_STATE_SYSTEM
) &&
1268 (instance
->pd_list
[pd_index
].driveType
==
1278 * megasas_complete_cmd_dpc - Returns FW's controller structure
1279 * @instance_addr: Address of adapter soft state
1281 * Tasklet to complete cmds
1283 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1288 struct megasas_cmd
*cmd
;
1289 struct megasas_instance
*instance
=
1290 (struct megasas_instance
*)instance_addr
;
1291 unsigned long flags
;
1293 /* If we have already declared adapter dead, donot complete cmds */
1294 if (instance
->hw_crit_error
)
1297 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1299 producer
= *instance
->producer
;
1300 consumer
= *instance
->consumer
;
1302 while (consumer
!= producer
) {
1303 context
= instance
->reply_queue
[consumer
];
1305 cmd
= instance
->cmd_list
[context
];
1307 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1310 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1315 *instance
->consumer
= producer
;
1317 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1320 * Check if we can restore can_queue
1322 if (instance
->flag
& MEGASAS_FW_BUSY
1323 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1324 && atomic_read(&instance
->fw_outstanding
) < 17) {
1326 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1327 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1328 if ((instance
->pdev
->device
==
1329 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1330 (instance
->pdev
->device
==
1331 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1332 instance
->host
->can_queue
=
1333 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
1335 instance
->host
->can_queue
=
1336 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1338 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1343 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1344 * @instance: Adapter soft state
1346 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1347 * complete all its outstanding commands. Returns error if one or more IOs
1348 * are pending after this time period. It also marks the controller dead.
1350 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
1353 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
1355 for (i
= 0; i
< wait_time
; i
++) {
1357 int outstanding
= atomic_read(&instance
->fw_outstanding
);
1362 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
1363 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
1364 "commands to complete\n",i
,outstanding
);
1366 * Call cmd completion routine. Cmd to be
1367 * be completed directly without depending on isr.
1369 megasas_complete_cmd_dpc((unsigned long)instance
);
1375 if (atomic_read(&instance
->fw_outstanding
)) {
1377 * Send signal to FW to stop processing any pending cmds.
1378 * The controller will be taken offline by the OS now.
1380 if ((instance
->pdev
->device
==
1381 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1382 (instance
->pdev
->device
==
1383 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1384 writel(MFI_STOP_ADP
,
1385 &instance
->reg_set
->reserved_0
[0]);
1387 writel(MFI_STOP_ADP
,
1388 &instance
->reg_set
->inbound_doorbell
);
1390 megasas_dump_pending_frames(instance
);
1391 instance
->hw_crit_error
= 1;
1399 * megasas_generic_reset - Generic reset routine
1400 * @scmd: Mid-layer SCSI command
1402 * This routine implements a generic reset handler for device, bus and host
1403 * reset requests. Device, bus and host specific reset handlers can use this
1404 * function after they do their specific tasks.
1406 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
1409 struct megasas_instance
*instance
;
1411 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
1413 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET -%ld cmd=%x retries=%x\n",
1414 scmd
->serial_number
, scmd
->cmnd
[0], scmd
->retries
);
1416 if (instance
->hw_crit_error
) {
1417 printk(KERN_ERR
"megasas: cannot recover from previous reset "
1422 ret_val
= megasas_wait_for_outstanding(instance
);
1423 if (ret_val
== SUCCESS
)
1424 printk(KERN_NOTICE
"megasas: reset successful \n");
1426 printk(KERN_ERR
"megasas: failed to do reset\n");
1432 * megasas_reset_timer - quiesce the adapter if required
1435 * Sets the FW busy flag and reduces the host->can_queue if the
1436 * cmd has not been completed within the timeout period.
1439 blk_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
1441 struct megasas_cmd
*cmd
= (struct megasas_cmd
*)scmd
->SCp
.ptr
;
1442 struct megasas_instance
*instance
;
1443 unsigned long flags
;
1445 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
1446 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
1447 return BLK_EH_NOT_HANDLED
;
1450 instance
= cmd
->instance
;
1451 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
1452 /* FW is busy, throttle IO */
1453 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1455 instance
->host
->can_queue
= 16;
1456 instance
->last_time
= jiffies
;
1457 instance
->flag
|= MEGASAS_FW_BUSY
;
1459 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1461 return BLK_EH_RESET_TIMER
;
1465 * megasas_reset_device - Device reset handler entry point
1467 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
1472 * First wait for all commands to complete
1474 ret
= megasas_generic_reset(scmd
);
1480 * megasas_reset_bus_host - Bus & host reset handler entry point
1482 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
1487 * First wait for all commands to complete
1489 ret
= megasas_generic_reset(scmd
);
1495 * megasas_bios_param - Returns disk geometry for a disk
1496 * @sdev: device handle
1497 * @bdev: block device
1498 * @capacity: drive capacity
1499 * @geom: geometry parameters
1502 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
1503 sector_t capacity
, int geom
[])
1509 /* Default heads (64) & sectors (32) */
1513 tmp
= heads
* sectors
;
1514 cylinders
= capacity
;
1516 sector_div(cylinders
, tmp
);
1519 * Handle extended translation size for logical drives > 1Gb
1522 if (capacity
>= 0x200000) {
1525 tmp
= heads
*sectors
;
1526 cylinders
= capacity
;
1527 sector_div(cylinders
, tmp
);
1532 geom
[2] = cylinders
;
1537 static void megasas_aen_polling(struct work_struct
*work
);
1540 * megasas_service_aen - Processes an event notification
1541 * @instance: Adapter soft state
1542 * @cmd: AEN command completed by the ISR
1544 * For AEN, driver sends a command down to FW that is held by the FW till an
1545 * event occurs. When an event of interest occurs, FW completes the command
1546 * that it was previously holding.
1548 * This routines sends SIGIO signal to processes that have registered with the
1552 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
1554 unsigned long flags
;
1556 * Don't signal app if it is just an aborted previously registered aen
1558 if ((!cmd
->abort_aen
) && (instance
->unload
== 0)) {
1559 spin_lock_irqsave(&poll_aen_lock
, flags
);
1560 megasas_poll_wait_aen
= 1;
1561 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
1562 wake_up(&megasas_poll_wait
);
1563 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
1568 instance
->aen_cmd
= NULL
;
1569 megasas_return_cmd(instance
, cmd
);
1571 if (instance
->unload
== 0) {
1572 struct megasas_aen_event
*ev
;
1573 ev
= kzalloc(sizeof(*ev
), GFP_ATOMIC
);
1575 printk(KERN_ERR
"megasas_service_aen: out of memory\n");
1577 ev
->instance
= instance
;
1579 INIT_WORK(&ev
->hotplug_work
, megasas_aen_polling
);
1580 schedule_delayed_work(
1581 (struct delayed_work
*)&ev
->hotplug_work
, 0);
1587 * Scsi host template for megaraid_sas driver
1589 static struct scsi_host_template megasas_template
= {
1591 .module
= THIS_MODULE
,
1592 .name
= "LSI SAS based MegaRAID driver",
1593 .proc_name
= "megaraid_sas",
1594 .slave_configure
= megasas_slave_configure
,
1595 .slave_alloc
= megasas_slave_alloc
,
1596 .queuecommand
= megasas_queue_command
,
1597 .eh_device_reset_handler
= megasas_reset_device
,
1598 .eh_bus_reset_handler
= megasas_reset_bus_host
,
1599 .eh_host_reset_handler
= megasas_reset_bus_host
,
1600 .eh_timed_out
= megasas_reset_timer
,
1601 .bios_param
= megasas_bios_param
,
1602 .use_clustering
= ENABLE_CLUSTERING
,
1606 * megasas_complete_int_cmd - Completes an internal command
1607 * @instance: Adapter soft state
1608 * @cmd: Command to be completed
1610 * The megasas_issue_blocked_cmd() function waits for a command to complete
1611 * after it issues a command. This function wakes up that waiting routine by
1612 * calling wake_up() on the wait queue.
1615 megasas_complete_int_cmd(struct megasas_instance
*instance
,
1616 struct megasas_cmd
*cmd
)
1618 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
1620 if (cmd
->cmd_status
== ENODATA
) {
1621 cmd
->cmd_status
= 0;
1623 wake_up(&instance
->int_cmd_wait_q
);
1627 * megasas_complete_abort - Completes aborting a command
1628 * @instance: Adapter soft state
1629 * @cmd: Cmd that was issued to abort another cmd
1631 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1632 * after it issues an abort on a previously issued command. This function
1633 * wakes up all functions waiting on the same wait queue.
1636 megasas_complete_abort(struct megasas_instance
*instance
,
1637 struct megasas_cmd
*cmd
)
1639 if (cmd
->sync_cmd
) {
1641 cmd
->cmd_status
= 0;
1642 wake_up(&instance
->abort_cmd_wait_q
);
1649 * megasas_complete_cmd - Completes a command
1650 * @instance: Adapter soft state
1651 * @cmd: Command to be completed
1652 * @alt_status: If non-zero, use this value as status to
1653 * SCSI mid-layer instead of the value returned
1654 * by the FW. This should be used if caller wants
1655 * an alternate status (as in the case of aborted
1659 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
1663 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
1664 unsigned long flags
;
1667 cmd
->scmd
->SCp
.ptr
= NULL
;
1671 case MFI_CMD_PD_SCSI_IO
:
1672 case MFI_CMD_LD_SCSI_IO
:
1675 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1676 * issued either through an IO path or an IOCTL path. If it
1677 * was via IOCTL, we will send it to internal completion.
1679 if (cmd
->sync_cmd
) {
1681 megasas_complete_int_cmd(instance
, cmd
);
1685 case MFI_CMD_LD_READ
:
1686 case MFI_CMD_LD_WRITE
:
1689 cmd
->scmd
->result
= alt_status
<< 16;
1695 atomic_dec(&instance
->fw_outstanding
);
1697 scsi_dma_unmap(cmd
->scmd
);
1698 cmd
->scmd
->scsi_done(cmd
->scmd
);
1699 megasas_return_cmd(instance
, cmd
);
1704 switch (hdr
->cmd_status
) {
1707 cmd
->scmd
->result
= DID_OK
<< 16;
1710 case MFI_STAT_SCSI_IO_FAILED
:
1711 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1713 (DID_ERROR
<< 16) | hdr
->scsi_status
;
1716 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1718 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
1720 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
1721 memset(cmd
->scmd
->sense_buffer
, 0,
1722 SCSI_SENSE_BUFFERSIZE
);
1723 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1726 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1731 case MFI_STAT_LD_OFFLINE
:
1732 case MFI_STAT_DEVICE_NOT_FOUND
:
1733 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1737 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
1739 cmd
->scmd
->result
= DID_ERROR
<< 16;
1743 atomic_dec(&instance
->fw_outstanding
);
1745 scsi_dma_unmap(cmd
->scmd
);
1746 cmd
->scmd
->scsi_done(cmd
->scmd
);
1747 megasas_return_cmd(instance
, cmd
);
1754 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET_INFO
||
1755 cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_GET
) {
1756 spin_lock_irqsave(&poll_aen_lock
, flags
);
1757 megasas_poll_wait_aen
= 0;
1758 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
1762 * See if got an event notification
1764 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
1765 megasas_service_aen(instance
, cmd
);
1767 megasas_complete_int_cmd(instance
, cmd
);
1773 * Cmd issued to abort another cmd returned
1775 megasas_complete_abort(instance
, cmd
);
1779 printk("megasas: Unknown command completed! [0x%X]\n",
1786 * megasas_deplete_reply_queue - Processes all completed commands
1787 * @instance: Adapter soft state
1788 * @alt_status: Alternate status to be returned to
1789 * SCSI mid-layer instead of the status
1790 * returned by the FW
1793 megasas_deplete_reply_queue(struct megasas_instance
*instance
, u8 alt_status
)
1796 * Check if it is our interrupt
1797 * Clear the interrupt
1799 if(instance
->instancet
->clear_intr(instance
->reg_set
))
1802 if (instance
->hw_crit_error
)
1805 * Schedule the tasklet for cmd completion
1807 tasklet_schedule(&instance
->isr_tasklet
);
1813 * megasas_isr - isr entry point
1815 static irqreturn_t
megasas_isr(int irq
, void *devp
)
1817 return megasas_deplete_reply_queue((struct megasas_instance
*)devp
,
1822 * megasas_transition_to_ready - Move the FW to READY state
1823 * @instance: Adapter soft state
1825 * During the initialization, FW passes can potentially be in any one of
1826 * several possible states. If the FW in operational, waiting-for-handshake
1827 * states, driver must take steps to bring it to ready state. Otherwise, it
1828 * has to wait for the ready state.
1831 megasas_transition_to_ready(struct megasas_instance
* instance
)
1837 u32 abs_state
, curr_abs_state
;
1839 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
1841 if (fw_state
!= MFI_STATE_READY
)
1842 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
1845 while (fw_state
!= MFI_STATE_READY
) {
1848 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
1852 case MFI_STATE_FAULT
:
1854 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
1857 case MFI_STATE_WAIT_HANDSHAKE
:
1859 * Set the CLR bit in inbound doorbell
1861 if ((instance
->pdev
->device
==
1862 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1863 (instance
->pdev
->device
==
1864 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1867 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
1868 &instance
->reg_set
->reserved_0
[0]);
1871 MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
1872 &instance
->reg_set
->inbound_doorbell
);
1875 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1876 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
1879 case MFI_STATE_BOOT_MESSAGE_PENDING
:
1880 if ((instance
->pdev
->device
==
1881 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1882 (instance
->pdev
->device
==
1883 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1884 writel(MFI_INIT_HOTPLUG
,
1885 &instance
->reg_set
->reserved_0
[0]);
1887 writel(MFI_INIT_HOTPLUG
,
1888 &instance
->reg_set
->inbound_doorbell
);
1890 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1891 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
1894 case MFI_STATE_OPERATIONAL
:
1896 * Bring it to READY state; assuming max wait 10 secs
1898 instance
->instancet
->disable_intr(instance
->reg_set
);
1899 if ((instance
->pdev
->device
==
1900 PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
1901 (instance
->pdev
->device
==
1902 PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
1903 writel(MFI_RESET_FLAGS
,
1904 &instance
->reg_set
->reserved_0
[0]);
1906 writel(MFI_RESET_FLAGS
,
1907 &instance
->reg_set
->inbound_doorbell
);
1909 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1910 cur_state
= MFI_STATE_OPERATIONAL
;
1913 case MFI_STATE_UNDEFINED
:
1915 * This state should not last for more than 2 seconds
1917 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1918 cur_state
= MFI_STATE_UNDEFINED
;
1921 case MFI_STATE_BB_INIT
:
1922 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1923 cur_state
= MFI_STATE_BB_INIT
;
1926 case MFI_STATE_FW_INIT
:
1927 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1928 cur_state
= MFI_STATE_FW_INIT
;
1931 case MFI_STATE_FW_INIT_2
:
1932 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1933 cur_state
= MFI_STATE_FW_INIT_2
;
1936 case MFI_STATE_DEVICE_SCAN
:
1937 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1938 cur_state
= MFI_STATE_DEVICE_SCAN
;
1941 case MFI_STATE_FLUSH_CACHE
:
1942 max_wait
= MEGASAS_RESET_WAIT_TIME
;
1943 cur_state
= MFI_STATE_FLUSH_CACHE
;
1947 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
1953 * The cur_state should not last for more than max_wait secs
1955 for (i
= 0; i
< (max_wait
* 1000); i
++) {
1956 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
1959 instance
->instancet
->read_fw_status_reg(instance
->reg_set
);
1961 if (abs_state
== curr_abs_state
) {
1968 * Return error if fw_state hasn't changed after max_wait
1970 if (curr_abs_state
== abs_state
) {
1971 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
1972 "in %d secs\n", fw_state
, max_wait
);
1976 printk(KERN_INFO
"megasas: FW now in Ready state\n");
1982 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1983 * @instance: Adapter soft state
1985 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
1988 u32 max_cmd
= instance
->max_fw_cmds
;
1989 struct megasas_cmd
*cmd
;
1991 if (!instance
->frame_dma_pool
)
1995 * Return all frames to pool
1997 for (i
= 0; i
< max_cmd
; i
++) {
1999 cmd
= instance
->cmd_list
[i
];
2002 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
2003 cmd
->frame_phys_addr
);
2006 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
2007 cmd
->sense_phys_addr
);
2011 * Now destroy the pool itself
2013 pci_pool_destroy(instance
->frame_dma_pool
);
2014 pci_pool_destroy(instance
->sense_dma_pool
);
2016 instance
->frame_dma_pool
= NULL
;
2017 instance
->sense_dma_pool
= NULL
;
2021 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2022 * @instance: Adapter soft state
2024 * Each command packet has an embedded DMA memory buffer that is used for
2025 * filling MFI frame and the SG list that immediately follows the frame. This
2026 * function creates those DMA memory buffers for each command packet by using
2027 * PCI pool facility.
2029 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
2037 struct megasas_cmd
*cmd
;
2039 max_cmd
= instance
->max_fw_cmds
;
2042 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2043 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2045 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
2046 sizeof(struct megasas_sge32
);
2048 if (instance
->flag_ieee
) {
2049 sge_sz
= sizeof(struct megasas_sge_skinny
);
2053 * Calculated the number of 64byte frames required for SGL
2055 sgl_sz
= sge_sz
* instance
->max_num_sge
;
2056 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
2059 * We need one extra frame for the MFI command
2063 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
2065 * Use DMA pool facility provided by PCI layer
2067 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
2068 instance
->pdev
, total_sz
, 64,
2071 if (!instance
->frame_dma_pool
) {
2072 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
2076 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
2077 instance
->pdev
, 128, 4, 0);
2079 if (!instance
->sense_dma_pool
) {
2080 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
2082 pci_pool_destroy(instance
->frame_dma_pool
);
2083 instance
->frame_dma_pool
= NULL
;
2089 * Allocate and attach a frame to each of the commands in cmd_list.
2090 * By making cmd->index as the context instead of the &cmd, we can
2091 * always use 32bit context regardless of the architecture
2093 for (i
= 0; i
< max_cmd
; i
++) {
2095 cmd
= instance
->cmd_list
[i
];
2097 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
2098 GFP_KERNEL
, &cmd
->frame_phys_addr
);
2100 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
2101 GFP_KERNEL
, &cmd
->sense_phys_addr
);
2104 * megasas_teardown_frame_pool() takes care of freeing
2105 * whatever has been allocated
2107 if (!cmd
->frame
|| !cmd
->sense
) {
2108 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
2109 megasas_teardown_frame_pool(instance
);
2113 cmd
->frame
->io
.context
= cmd
->index
;
2114 cmd
->frame
->io
.pad_0
= 0;
2121 * megasas_free_cmds - Free all the cmds in the free cmd pool
2122 * @instance: Adapter soft state
2124 static void megasas_free_cmds(struct megasas_instance
*instance
)
2127 /* First free the MFI frame pool */
2128 megasas_teardown_frame_pool(instance
);
2130 /* Free all the commands in the cmd_list */
2131 for (i
= 0; i
< instance
->max_fw_cmds
; i
++)
2132 kfree(instance
->cmd_list
[i
]);
2134 /* Free the cmd_list buffer itself */
2135 kfree(instance
->cmd_list
);
2136 instance
->cmd_list
= NULL
;
2138 INIT_LIST_HEAD(&instance
->cmd_pool
);
2142 * megasas_alloc_cmds - Allocates the command packets
2143 * @instance: Adapter soft state
2145 * Each command that is issued to the FW, whether IO commands from the OS or
2146 * internal commands like IOCTLs, are wrapped in local data structure called
2147 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2150 * Each frame has a 32-bit field called context (tag). This context is used
2151 * to get back the megasas_cmd from the frame when a frame gets completed in
2152 * the ISR. Typically the address of the megasas_cmd itself would be used as
2153 * the context. But we wanted to keep the differences between 32 and 64 bit
2154 * systems to the mininum. We always use 32 bit integers for the context. In
2155 * this driver, the 32 bit values are the indices into an array cmd_list.
2156 * This array is used only to look up the megasas_cmd given the context. The
2157 * free commands themselves are maintained in a linked list called cmd_pool.
2159 static int megasas_alloc_cmds(struct megasas_instance
*instance
)
2164 struct megasas_cmd
*cmd
;
2166 max_cmd
= instance
->max_fw_cmds
;
2169 * instance->cmd_list is an array of struct megasas_cmd pointers.
2170 * Allocate the dynamic array first and then allocate individual
2173 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
2175 if (!instance
->cmd_list
) {
2176 printk(KERN_DEBUG
"megasas: out of memory\n");
2181 for (i
= 0; i
< max_cmd
; i
++) {
2182 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
2185 if (!instance
->cmd_list
[i
]) {
2187 for (j
= 0; j
< i
; j
++)
2188 kfree(instance
->cmd_list
[j
]);
2190 kfree(instance
->cmd_list
);
2191 instance
->cmd_list
= NULL
;
2198 * Add all the commands to command pool (instance->cmd_pool)
2200 for (i
= 0; i
< max_cmd
; i
++) {
2201 cmd
= instance
->cmd_list
[i
];
2202 memset(cmd
, 0, sizeof(struct megasas_cmd
));
2204 cmd
->instance
= instance
;
2206 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
2210 * Create a frame pool and assign one frame to each cmd
2212 if (megasas_create_frame_pool(instance
)) {
2213 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
2214 megasas_free_cmds(instance
);
2221 * megasas_get_pd_list_info - Returns FW's pd_list structure
2222 * @instance: Adapter soft state
2223 * @pd_list: pd_list structure
2225 * Issues an internal command (DCMD) to get the FW's controller PD
2226 * list structure. This information is mainly used to find out SYSTEM
2227 * supported by the FW.
2230 megasas_get_pd_list(struct megasas_instance
*instance
)
2232 int ret
= 0, pd_index
= 0;
2233 struct megasas_cmd
*cmd
;
2234 struct megasas_dcmd_frame
*dcmd
;
2235 struct MR_PD_LIST
*ci
;
2236 struct MR_PD_ADDRESS
*pd_addr
;
2237 dma_addr_t ci_h
= 0;
2239 cmd
= megasas_get_cmd(instance
);
2242 printk(KERN_DEBUG
"megasas (get_pd_list): Failed to get cmd\n");
2246 dcmd
= &cmd
->frame
->dcmd
;
2248 ci
= pci_alloc_consistent(instance
->pdev
,
2249 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
), &ci_h
);
2252 printk(KERN_DEBUG
"Failed to alloc mem for pd_list\n");
2253 megasas_return_cmd(instance
, cmd
);
2257 memset(ci
, 0, sizeof(*ci
));
2258 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2260 dcmd
->mbox
.b
[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST
;
2261 dcmd
->mbox
.b
[1] = 0;
2262 dcmd
->cmd
= MFI_CMD_DCMD
;
2263 dcmd
->cmd_status
= 0xFF;
2264 dcmd
->sge_count
= 1;
2265 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2268 dcmd
->data_xfer_len
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
2269 dcmd
->opcode
= MR_DCMD_PD_LIST_QUERY
;
2270 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
2271 dcmd
->sgl
.sge32
[0].length
= MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
);
2273 if (!megasas_issue_polled(instance
, cmd
)) {
2280 * the following function will get the instance PD LIST.
2287 (MEGASAS_MAX_PD_CHANNELS
* MEGASAS_MAX_DEV_PER_CHANNEL
))) {
2289 memset(instance
->pd_list
, 0,
2290 MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
));
2292 for (pd_index
= 0; pd_index
< ci
->count
; pd_index
++) {
2294 instance
->pd_list
[pd_addr
->deviceId
].tid
=
2296 instance
->pd_list
[pd_addr
->deviceId
].driveType
=
2297 pd_addr
->scsiDevType
;
2298 instance
->pd_list
[pd_addr
->deviceId
].driveState
=
2304 pci_free_consistent(instance
->pdev
,
2305 MEGASAS_MAX_PD
* sizeof(struct MR_PD_LIST
),
2307 megasas_return_cmd(instance
, cmd
);
2313 * megasas_get_ld_list_info - Returns FW's ld_list structure
2314 * @instance: Adapter soft state
2315 * @ld_list: ld_list structure
2317 * Issues an internal command (DCMD) to get the FW's controller PD
2318 * list structure. This information is mainly used to find out SYSTEM
2319 * supported by the FW.
2322 megasas_get_ld_list(struct megasas_instance
*instance
)
2324 int ret
= 0, ld_index
= 0, ids
= 0;
2325 struct megasas_cmd
*cmd
;
2326 struct megasas_dcmd_frame
*dcmd
;
2327 struct MR_LD_LIST
*ci
;
2328 dma_addr_t ci_h
= 0;
2330 cmd
= megasas_get_cmd(instance
);
2333 printk(KERN_DEBUG
"megasas_get_ld_list: Failed to get cmd\n");
2337 dcmd
= &cmd
->frame
->dcmd
;
2339 ci
= pci_alloc_consistent(instance
->pdev
,
2340 sizeof(struct MR_LD_LIST
),
2344 printk(KERN_DEBUG
"Failed to alloc mem in get_ld_list\n");
2345 megasas_return_cmd(instance
, cmd
);
2349 memset(ci
, 0, sizeof(*ci
));
2350 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2352 dcmd
->cmd
= MFI_CMD_DCMD
;
2353 dcmd
->cmd_status
= 0xFF;
2354 dcmd
->sge_count
= 1;
2355 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2357 dcmd
->data_xfer_len
= sizeof(struct MR_LD_LIST
);
2358 dcmd
->opcode
= MR_DCMD_LD_GET_LIST
;
2359 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
2360 dcmd
->sgl
.sge32
[0].length
= sizeof(struct MR_LD_LIST
);
2363 if (!megasas_issue_polled(instance
, cmd
)) {
2369 /* the following function will get the instance PD LIST */
2371 if ((ret
== 0) && (ci
->ldCount
< MAX_LOGICAL_DRIVES
)) {
2372 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
2374 for (ld_index
= 0; ld_index
< ci
->ldCount
; ld_index
++) {
2375 if (ci
->ldList
[ld_index
].state
!= 0) {
2376 ids
= ci
->ldList
[ld_index
].ref
.targetId
;
2377 instance
->ld_ids
[ids
] =
2378 ci
->ldList
[ld_index
].ref
.targetId
;
2383 pci_free_consistent(instance
->pdev
,
2384 sizeof(struct MR_LD_LIST
),
2388 megasas_return_cmd(instance
, cmd
);
2393 * megasas_get_controller_info - Returns FW's controller structure
2394 * @instance: Adapter soft state
2395 * @ctrl_info: Controller information structure
2397 * Issues an internal command (DCMD) to get the FW's controller structure.
2398 * This information is mainly used to find out the maximum IO transfer per
2399 * command supported by the FW.
2402 megasas_get_ctrl_info(struct megasas_instance
*instance
,
2403 struct megasas_ctrl_info
*ctrl_info
)
2406 struct megasas_cmd
*cmd
;
2407 struct megasas_dcmd_frame
*dcmd
;
2408 struct megasas_ctrl_info
*ci
;
2409 dma_addr_t ci_h
= 0;
2411 cmd
= megasas_get_cmd(instance
);
2414 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
2418 dcmd
= &cmd
->frame
->dcmd
;
2420 ci
= pci_alloc_consistent(instance
->pdev
,
2421 sizeof(struct megasas_ctrl_info
), &ci_h
);
2424 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
2425 megasas_return_cmd(instance
, cmd
);
2429 memset(ci
, 0, sizeof(*ci
));
2430 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2432 dcmd
->cmd
= MFI_CMD_DCMD
;
2433 dcmd
->cmd_status
= 0xFF;
2434 dcmd
->sge_count
= 1;
2435 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2438 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
2439 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
2440 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
2441 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
2443 if (!megasas_issue_polled(instance
, cmd
)) {
2445 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
2450 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
2453 megasas_return_cmd(instance
, cmd
);
2458 * megasas_issue_init_mfi - Initializes the FW
2459 * @instance: Adapter soft state
2461 * Issues the INIT MFI cmd
2464 megasas_issue_init_mfi(struct megasas_instance
*instance
)
2468 struct megasas_cmd
*cmd
;
2470 struct megasas_init_frame
*init_frame
;
2471 struct megasas_init_queue_info
*initq_info
;
2472 dma_addr_t init_frame_h
;
2473 dma_addr_t initq_info_h
;
2476 * Prepare a init frame. Note the init frame points to queue info
2477 * structure. Each frame has SGL allocated after first 64 bytes. For
2478 * this frame - since we don't need any SGL - we use SGL's space as
2479 * queue info structure
2481 * We will not get a NULL command below. We just created the pool.
2483 cmd
= megasas_get_cmd(instance
);
2485 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
2486 initq_info
= (struct megasas_init_queue_info
*)
2487 ((unsigned long)init_frame
+ 64);
2489 init_frame_h
= cmd
->frame_phys_addr
;
2490 initq_info_h
= init_frame_h
+ 64;
2492 context
= init_frame
->context
;
2493 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
2494 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
2495 init_frame
->context
= context
;
2497 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
2498 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
2500 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
2501 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
2503 init_frame
->cmd
= MFI_CMD_INIT
;
2504 init_frame
->cmd_status
= 0xFF;
2505 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
2507 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
2510 * disable the intr before firing the init frame to FW
2512 instance
->instancet
->disable_intr(instance
->reg_set
);
2515 * Issue the init frame in polled mode
2518 if (megasas_issue_polled(instance
, cmd
)) {
2519 printk(KERN_ERR
"megasas: Failed to init firmware\n");
2520 megasas_return_cmd(instance
, cmd
);
2524 megasas_return_cmd(instance
, cmd
);
2533 * megasas_start_timer - Initializes a timer object
2534 * @instance: Adapter soft state
2535 * @timer: timer object to be initialized
2536 * @fn: timer function
2537 * @interval: time interval between timer function call
2540 megasas_start_timer(struct megasas_instance
*instance
,
2541 struct timer_list
*timer
,
2542 void *fn
, unsigned long interval
)
2545 timer
->expires
= jiffies
+ interval
;
2546 timer
->data
= (unsigned long)instance
;
2547 timer
->function
= fn
;
2552 * megasas_io_completion_timer - Timer fn
2553 * @instance_addr: Address of adapter soft state
2555 * Schedules tasklet for cmd completion
2556 * if poll_mode_io is set
2559 megasas_io_completion_timer(unsigned long instance_addr
)
2561 struct megasas_instance
*instance
=
2562 (struct megasas_instance
*)instance_addr
;
2564 if (atomic_read(&instance
->fw_outstanding
))
2565 tasklet_schedule(&instance
->isr_tasklet
);
2569 mod_timer(&instance
->io_completion_timer
,
2570 jiffies
+ MEGASAS_COMPLETION_TIMER_INTERVAL
);
2574 * megasas_init_mfi - Initializes the FW
2575 * @instance: Adapter soft state
2577 * This is the main function for initializing MFI firmware.
2579 static int megasas_init_mfi(struct megasas_instance
*instance
)
2586 struct megasas_register_set __iomem
*reg_set
;
2587 struct megasas_ctrl_info
*ctrl_info
;
2589 * Map the message registers
2591 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS1078GEN2
) ||
2592 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
) ||
2593 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
2594 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0079GEN2
)) {
2595 instance
->base_addr
= pci_resource_start(instance
->pdev
, 1);
2597 instance
->base_addr
= pci_resource_start(instance
->pdev
, 0);
2600 if (pci_request_selected_regions(instance
->pdev
,
2601 pci_select_bars(instance
->pdev
, IORESOURCE_MEM
),
2603 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
2607 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
2609 if (!instance
->reg_set
) {
2610 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
2614 reg_set
= instance
->reg_set
;
2616 switch(instance
->pdev
->device
)
2618 case PCI_DEVICE_ID_LSI_SAS1078R
:
2619 case PCI_DEVICE_ID_LSI_SAS1078DE
:
2620 instance
->instancet
= &megasas_instance_template_ppc
;
2622 case PCI_DEVICE_ID_LSI_SAS1078GEN2
:
2623 case PCI_DEVICE_ID_LSI_SAS0079GEN2
:
2624 instance
->instancet
= &megasas_instance_template_gen2
;
2626 case PCI_DEVICE_ID_LSI_SAS0073SKINNY
:
2627 case PCI_DEVICE_ID_LSI_SAS0071SKINNY
:
2628 instance
->instancet
= &megasas_instance_template_skinny
;
2630 case PCI_DEVICE_ID_LSI_SAS1064R
:
2631 case PCI_DEVICE_ID_DELL_PERC5
:
2633 instance
->instancet
= &megasas_instance_template_xscale
;
2638 * We expect the FW state to be READY
2640 if (megasas_transition_to_ready(instance
))
2641 goto fail_ready_state
;
2644 * Get various operational parameters from status register
2646 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
2648 * Reduce the max supported cmds by 1. This is to ensure that the
2649 * reply_q_sz (1 more than the max cmd that driver may send)
2650 * does not exceed max cmds that the FW can support
2652 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
2653 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
2656 * Create a pool of commands
2658 if (megasas_alloc_cmds(instance
))
2659 goto fail_alloc_cmds
;
2662 * Allocate memory for reply queue. Length of reply queue should
2663 * be _one_ more than the maximum commands handled by the firmware.
2665 * Note: When FW completes commands, it places corresponding contex
2666 * values in this circular reply queue. This circular queue is a fairly
2667 * typical producer-consumer queue. FW is the producer (of completed
2668 * commands) and the driver is the consumer.
2670 context_sz
= sizeof(u32
);
2671 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
2673 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
2675 &instance
->reply_queue_h
);
2677 if (!instance
->reply_queue
) {
2678 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
2679 goto fail_reply_queue
;
2682 if (megasas_issue_init_mfi(instance
))
2685 memset(instance
->pd_list
, 0 ,
2686 (MEGASAS_MAX_PD
* sizeof(struct megasas_pd_list
)));
2687 megasas_get_pd_list(instance
);
2689 memset(instance
->ld_ids
, 0xff, MEGASAS_MAX_LD_IDS
);
2690 megasas_get_ld_list(instance
);
2692 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
2695 * Compute the max allowed sectors per IO: The controller info has two
2696 * limits on max sectors. Driver should use the minimum of these two.
2698 * 1 << stripe_sz_ops.min = max sectors per strip
2700 * Note that older firmwares ( < FW ver 30) didn't report information
2701 * to calculate max_sectors_1. So the number ended up as zero always.
2704 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
2706 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
2707 ctrl_info
->max_strips_per_io
;
2708 max_sectors_2
= ctrl_info
->max_request_size
;
2710 tmp_sectors
= min_t(u32
, max_sectors_1
, max_sectors_2
);
2713 instance
->max_sectors_per_req
= instance
->max_num_sge
*
2715 if (tmp_sectors
&& (instance
->max_sectors_per_req
> tmp_sectors
))
2716 instance
->max_sectors_per_req
= tmp_sectors
;
2721 * Setup tasklet for cmd completion
2724 tasklet_init(&instance
->isr_tasklet
, megasas_complete_cmd_dpc
,
2725 (unsigned long)instance
);
2727 /* Initialize the cmd completion timer */
2729 megasas_start_timer(instance
, &instance
->io_completion_timer
,
2730 megasas_io_completion_timer
,
2731 MEGASAS_COMPLETION_TIMER_INTERVAL
);
2736 pci_free_consistent(instance
->pdev
, reply_q_sz
,
2737 instance
->reply_queue
, instance
->reply_queue_h
);
2739 megasas_free_cmds(instance
);
2743 iounmap(instance
->reg_set
);
2746 pci_release_selected_regions(instance
->pdev
,
2747 pci_select_bars(instance
->pdev
, IORESOURCE_MEM
));
2753 * megasas_release_mfi - Reverses the FW initialization
2754 * @intance: Adapter soft state
2756 static void megasas_release_mfi(struct megasas_instance
*instance
)
2758 u32 reply_q_sz
= sizeof(u32
) * (instance
->max_fw_cmds
+ 1);
2760 pci_free_consistent(instance
->pdev
, reply_q_sz
,
2761 instance
->reply_queue
, instance
->reply_queue_h
);
2763 megasas_free_cmds(instance
);
2765 iounmap(instance
->reg_set
);
2767 pci_release_selected_regions(instance
->pdev
,
2768 pci_select_bars(instance
->pdev
, IORESOURCE_MEM
));
2772 * megasas_get_seq_num - Gets latest event sequence numbers
2773 * @instance: Adapter soft state
2774 * @eli: FW event log sequence numbers information
2776 * FW maintains a log of all events in a non-volatile area. Upper layers would
2777 * usually find out the latest sequence number of the events, the seq number at
2778 * the boot etc. They would "read" all the events below the latest seq number
2779 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2780 * number), they would subsribe to AEN (asynchronous event notification) and
2781 * wait for the events to happen.
2784 megasas_get_seq_num(struct megasas_instance
*instance
,
2785 struct megasas_evt_log_info
*eli
)
2787 struct megasas_cmd
*cmd
;
2788 struct megasas_dcmd_frame
*dcmd
;
2789 struct megasas_evt_log_info
*el_info
;
2790 dma_addr_t el_info_h
= 0;
2792 cmd
= megasas_get_cmd(instance
);
2798 dcmd
= &cmd
->frame
->dcmd
;
2799 el_info
= pci_alloc_consistent(instance
->pdev
,
2800 sizeof(struct megasas_evt_log_info
),
2804 megasas_return_cmd(instance
, cmd
);
2808 memset(el_info
, 0, sizeof(*el_info
));
2809 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2811 dcmd
->cmd
= MFI_CMD_DCMD
;
2812 dcmd
->cmd_status
= 0x0;
2813 dcmd
->sge_count
= 1;
2814 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2817 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
2818 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
2819 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
2820 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
2822 megasas_issue_blocked_cmd(instance
, cmd
);
2825 * Copy the data back into callers buffer
2827 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
2829 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
2830 el_info
, el_info_h
);
2832 megasas_return_cmd(instance
, cmd
);
2838 * megasas_register_aen - Registers for asynchronous event notification
2839 * @instance: Adapter soft state
2840 * @seq_num: The starting sequence number
2841 * @class_locale: Class of the event
2843 * This function subscribes for AEN for events beyond the @seq_num. It requests
2844 * to be notified if and only if the event is of type @class_locale
2847 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
2848 u32 class_locale_word
)
2851 struct megasas_cmd
*cmd
;
2852 struct megasas_dcmd_frame
*dcmd
;
2853 union megasas_evt_class_locale curr_aen
;
2854 union megasas_evt_class_locale prev_aen
;
2857 * If there an AEN pending already (aen_cmd), check if the
2858 * class_locale of that pending AEN is inclusive of the new
2859 * AEN request we currently have. If it is, then we don't have
2860 * to do anything. In other words, whichever events the current
2861 * AEN request is subscribing to, have already been subscribed
2864 * If the old_cmd is _not_ inclusive, then we have to abort
2865 * that command, form a class_locale that is superset of both
2866 * old and current and re-issue to the FW
2869 curr_aen
.word
= class_locale_word
;
2871 if (instance
->aen_cmd
) {
2873 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
2876 * A class whose enum value is smaller is inclusive of all
2877 * higher values. If a PROGRESS (= -1) was previously
2878 * registered, then a new registration requests for higher
2879 * classes need not be sent to FW. They are automatically
2882 * Locale numbers don't have such hierarchy. They are bitmap
2885 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
2886 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
2887 curr_aen
.members
.locale
)) {
2889 * Previously issued event registration includes
2890 * current request. Nothing to do.
2894 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
2896 if (prev_aen
.members
.class < curr_aen
.members
.class)
2897 curr_aen
.members
.class = prev_aen
.members
.class;
2899 instance
->aen_cmd
->abort_aen
= 1;
2900 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
2905 printk(KERN_DEBUG
"megasas: Failed to abort "
2906 "previous AEN command\n");
2912 cmd
= megasas_get_cmd(instance
);
2917 dcmd
= &cmd
->frame
->dcmd
;
2919 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
2922 * Prepare DCMD for aen registration
2924 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2926 dcmd
->cmd
= MFI_CMD_DCMD
;
2927 dcmd
->cmd_status
= 0x0;
2928 dcmd
->sge_count
= 1;
2929 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2932 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
2933 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
2934 dcmd
->mbox
.w
[0] = seq_num
;
2935 dcmd
->mbox
.w
[1] = curr_aen
.word
;
2936 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
2937 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
2939 if (instance
->aen_cmd
!= NULL
) {
2940 megasas_return_cmd(instance
, cmd
);
2945 * Store reference to the cmd used to register for AEN. When an
2946 * application wants us to register for AEN, we have to abort this
2947 * cmd and re-register with a new EVENT LOCALE supplied by that app
2949 instance
->aen_cmd
= cmd
;
2952 * Issue the aen registration frame
2954 instance
->instancet
->fire_cmd(instance
,
2955 cmd
->frame_phys_addr
, 0, instance
->reg_set
);
2961 * megasas_start_aen - Subscribes to AEN during driver load time
2962 * @instance: Adapter soft state
2964 static int megasas_start_aen(struct megasas_instance
*instance
)
2966 struct megasas_evt_log_info eli
;
2967 union megasas_evt_class_locale class_locale
;
2970 * Get the latest sequence number from FW
2972 memset(&eli
, 0, sizeof(eli
));
2974 if (megasas_get_seq_num(instance
, &eli
))
2978 * Register AEN with FW for latest sequence number plus 1
2980 class_locale
.members
.reserved
= 0;
2981 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2982 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2984 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
2989 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2990 * @instance: Adapter soft state
2992 static int megasas_io_attach(struct megasas_instance
*instance
)
2994 struct Scsi_Host
*host
= instance
->host
;
2997 * Export parameters required by SCSI mid-layer
2999 host
->irq
= instance
->pdev
->irq
;
3000 host
->unique_id
= instance
->unique_id
;
3001 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3002 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
3004 instance
->max_fw_cmds
- MEGASAS_SKINNY_INT_CMDS
;
3007 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
3008 host
->this_id
= instance
->init_id
;
3009 host
->sg_tablesize
= instance
->max_num_sge
;
3010 host
->max_sectors
= instance
->max_sectors_per_req
;
3011 host
->cmd_per_lun
= 128;
3012 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
3013 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
3014 host
->max_lun
= MEGASAS_MAX_LUN
;
3015 host
->max_cmd_len
= 16;
3018 * Notify the mid-layer about the new controller
3020 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
3021 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
3026 * Trigger SCSI to scan our drives
3028 scsi_scan_host(host
);
3033 megasas_set_dma_mask(struct pci_dev
*pdev
)
3036 * All our contollers are capable of performing 64-bit DMA
3039 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64)) != 0) {
3041 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3042 goto fail_set_dma_mask
;
3045 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)) != 0)
3046 goto fail_set_dma_mask
;
3055 * megasas_probe_one - PCI hotplug entry point
3056 * @pdev: PCI device structure
3057 * @id: PCI ids of supported hotplugged adapter
3059 static int __devinit
3060 megasas_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
3063 struct Scsi_Host
*host
;
3064 struct megasas_instance
*instance
;
3067 * Announce PCI information
3069 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3070 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
3071 pdev
->subsystem_device
);
3073 printk("bus %d:slot %d:func %d\n",
3074 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
3077 * PCI prepping: enable device set bus mastering and dma mask
3079 rval
= pci_enable_device_mem(pdev
);
3085 pci_set_master(pdev
);
3087 if (megasas_set_dma_mask(pdev
))
3088 goto fail_set_dma_mask
;
3090 host
= scsi_host_alloc(&megasas_template
,
3091 sizeof(struct megasas_instance
));
3094 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
3095 goto fail_alloc_instance
;
3098 instance
= (struct megasas_instance
*)host
->hostdata
;
3099 memset(instance
, 0, sizeof(*instance
));
3101 instance
->producer
= pci_alloc_consistent(pdev
, sizeof(u32
),
3102 &instance
->producer_h
);
3103 instance
->consumer
= pci_alloc_consistent(pdev
, sizeof(u32
),
3104 &instance
->consumer_h
);
3106 if (!instance
->producer
|| !instance
->consumer
) {
3107 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
3108 "producer, consumer\n");
3109 goto fail_alloc_dma_buf
;
3112 *instance
->producer
= 0;
3113 *instance
->consumer
= 0;
3114 megasas_poll_wait_aen
= 0;
3115 instance
->flag_ieee
= 0;
3116 instance
->ev
= NULL
;
3118 instance
->evt_detail
= pci_alloc_consistent(pdev
,
3120 megasas_evt_detail
),
3121 &instance
->evt_detail_h
);
3123 if (!instance
->evt_detail
) {
3124 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
3125 "event detail structure\n");
3126 goto fail_alloc_dma_buf
;
3130 * Initialize locks and queues
3132 INIT_LIST_HEAD(&instance
->cmd_pool
);
3134 atomic_set(&instance
->fw_outstanding
,0);
3136 init_waitqueue_head(&instance
->int_cmd_wait_q
);
3137 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
3139 spin_lock_init(&instance
->cmd_pool_lock
);
3140 spin_lock_init(&instance
->fire_lock
);
3141 spin_lock_init(&instance
->completion_lock
);
3142 spin_lock_init(&poll_aen_lock
);
3144 mutex_init(&instance
->aen_mutex
);
3147 * Initialize PCI related and misc parameters
3149 instance
->pdev
= pdev
;
3150 instance
->host
= host
;
3151 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
3152 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
3154 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0073SKINNY
) ||
3155 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_SAS0071SKINNY
)) {
3156 instance
->flag_ieee
= 1;
3157 sema_init(&instance
->ioctl_sem
, MEGASAS_SKINNY_INT_CMDS
);
3159 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
3161 megasas_dbg_lvl
= 0;
3163 instance
->unload
= 1;
3164 instance
->last_time
= 0;
3167 * Initialize MFI Firmware
3169 if (megasas_init_mfi(instance
))
3175 if (request_irq(pdev
->irq
, megasas_isr
, IRQF_SHARED
, "megasas", instance
)) {
3176 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
3180 instance
->instancet
->enable_intr(instance
->reg_set
);
3183 * Store instance in PCI softstate
3185 pci_set_drvdata(pdev
, instance
);
3188 * Add this controller to megasas_mgmt_info structure so that it
3189 * can be exported to management applications
3191 megasas_mgmt_info
.count
++;
3192 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
3193 megasas_mgmt_info
.max_index
++;
3196 * Initiate AEN (Asynchronous Event Notification)
3198 if (megasas_start_aen(instance
)) {
3199 printk(KERN_DEBUG
"megasas: start aen failed\n");
3200 goto fail_start_aen
;
3204 * Register with SCSI mid-layer
3206 if (megasas_io_attach(instance
))
3207 goto fail_io_attach
;
3209 instance
->unload
= 0;
3214 megasas_mgmt_info
.count
--;
3215 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
3216 megasas_mgmt_info
.max_index
--;
3218 pci_set_drvdata(pdev
, NULL
);
3219 instance
->instancet
->disable_intr(instance
->reg_set
);
3220 free_irq(instance
->pdev
->irq
, instance
);
3222 megasas_release_mfi(instance
);
3227 if (instance
->evt_detail
)
3228 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
3229 instance
->evt_detail
,
3230 instance
->evt_detail_h
);
3232 if (instance
->producer
)
3233 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
3234 instance
->producer_h
);
3235 if (instance
->consumer
)
3236 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
3237 instance
->consumer_h
);
3238 scsi_host_put(host
);
3240 fail_alloc_instance
:
3242 pci_disable_device(pdev
);
3248 * megasas_flush_cache - Requests FW to flush all its caches
3249 * @instance: Adapter soft state
3251 static void megasas_flush_cache(struct megasas_instance
*instance
)
3253 struct megasas_cmd
*cmd
;
3254 struct megasas_dcmd_frame
*dcmd
;
3256 cmd
= megasas_get_cmd(instance
);
3261 dcmd
= &cmd
->frame
->dcmd
;
3263 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3265 dcmd
->cmd
= MFI_CMD_DCMD
;
3266 dcmd
->cmd_status
= 0x0;
3267 dcmd
->sge_count
= 0;
3268 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
3271 dcmd
->data_xfer_len
= 0;
3272 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
3273 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
3275 megasas_issue_blocked_cmd(instance
, cmd
);
3277 megasas_return_cmd(instance
, cmd
);
3283 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3284 * @instance: Adapter soft state
3285 * @opcode: Shutdown/Hibernate
3287 static void megasas_shutdown_controller(struct megasas_instance
*instance
,
3290 struct megasas_cmd
*cmd
;
3291 struct megasas_dcmd_frame
*dcmd
;
3293 cmd
= megasas_get_cmd(instance
);
3298 if (instance
->aen_cmd
)
3299 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
3301 dcmd
= &cmd
->frame
->dcmd
;
3303 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
3305 dcmd
->cmd
= MFI_CMD_DCMD
;
3306 dcmd
->cmd_status
= 0x0;
3307 dcmd
->sge_count
= 0;
3308 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
3311 dcmd
->data_xfer_len
= 0;
3312 dcmd
->opcode
= opcode
;
3314 megasas_issue_blocked_cmd(instance
, cmd
);
3316 megasas_return_cmd(instance
, cmd
);
3323 * megasas_suspend - driver suspend entry point
3324 * @pdev: PCI device structure
3325 * @state: PCI power state to suspend routine
3328 megasas_suspend(struct pci_dev
*pdev
, pm_message_t state
)
3330 struct Scsi_Host
*host
;
3331 struct megasas_instance
*instance
;
3333 instance
= pci_get_drvdata(pdev
);
3334 host
= instance
->host
;
3335 instance
->unload
= 1;
3338 del_timer_sync(&instance
->io_completion_timer
);
3340 megasas_flush_cache(instance
);
3341 megasas_shutdown_controller(instance
, MR_DCMD_HIBERNATE_SHUTDOWN
);
3343 /* cancel the delayed work if this work still in queue */
3344 if (instance
->ev
!= NULL
) {
3345 struct megasas_aen_event
*ev
= instance
->ev
;
3346 cancel_delayed_work(
3347 (struct delayed_work
*)&ev
->hotplug_work
);
3348 flush_scheduled_work();
3349 instance
->ev
= NULL
;
3352 tasklet_kill(&instance
->isr_tasklet
);
3354 pci_set_drvdata(instance
->pdev
, instance
);
3355 instance
->instancet
->disable_intr(instance
->reg_set
);
3356 free_irq(instance
->pdev
->irq
, instance
);
3358 pci_save_state(pdev
);
3359 pci_disable_device(pdev
);
3361 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
3367 * megasas_resume- driver resume entry point
3368 * @pdev: PCI device structure
3371 megasas_resume(struct pci_dev
*pdev
)
3374 struct Scsi_Host
*host
;
3375 struct megasas_instance
*instance
;
3377 instance
= pci_get_drvdata(pdev
);
3378 host
= instance
->host
;
3379 pci_set_power_state(pdev
, PCI_D0
);
3380 pci_enable_wake(pdev
, PCI_D0
, 0);
3381 pci_restore_state(pdev
);
3384 * PCI prepping: enable device set bus mastering and dma mask
3386 rval
= pci_enable_device_mem(pdev
);
3389 printk(KERN_ERR
"megasas: Enable device failed\n");
3393 pci_set_master(pdev
);
3395 if (megasas_set_dma_mask(pdev
))
3396 goto fail_set_dma_mask
;
3399 * Initialize MFI Firmware
3402 *instance
->producer
= 0;
3403 *instance
->consumer
= 0;
3405 atomic_set(&instance
->fw_outstanding
, 0);
3408 * We expect the FW state to be READY
3410 if (megasas_transition_to_ready(instance
))
3411 goto fail_ready_state
;
3413 if (megasas_issue_init_mfi(instance
))
3416 tasklet_init(&instance
->isr_tasklet
, megasas_complete_cmd_dpc
,
3417 (unsigned long)instance
);
3422 if (request_irq(pdev
->irq
, megasas_isr
, IRQF_SHARED
,
3423 "megasas", instance
)) {
3424 printk(KERN_ERR
"megasas: Failed to register IRQ\n");
3428 instance
->instancet
->enable_intr(instance
->reg_set
);
3431 * Initiate AEN (Asynchronous Event Notification)
3433 if (megasas_start_aen(instance
))
3434 printk(KERN_ERR
"megasas: Start AEN failed\n");
3436 /* Initialize the cmd completion timer */
3438 megasas_start_timer(instance
, &instance
->io_completion_timer
,
3439 megasas_io_completion_timer
,
3440 MEGASAS_COMPLETION_TIMER_INTERVAL
);
3441 instance
->unload
= 0;
3447 if (instance
->evt_detail
)
3448 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
3449 instance
->evt_detail
,
3450 instance
->evt_detail_h
);
3452 if (instance
->producer
)
3453 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
3454 instance
->producer_h
);
3455 if (instance
->consumer
)
3456 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
3457 instance
->consumer_h
);
3458 scsi_host_put(host
);
3463 pci_disable_device(pdev
);
3468 #define megasas_suspend NULL
3469 #define megasas_resume NULL
3473 * megasas_detach_one - PCI hot"un"plug entry point
3474 * @pdev: PCI device structure
3476 static void __devexit
megasas_detach_one(struct pci_dev
*pdev
)
3479 struct Scsi_Host
*host
;
3480 struct megasas_instance
*instance
;
3482 instance
= pci_get_drvdata(pdev
);
3483 instance
->unload
= 1;
3484 host
= instance
->host
;
3487 del_timer_sync(&instance
->io_completion_timer
);
3489 scsi_remove_host(instance
->host
);
3490 megasas_flush_cache(instance
);
3491 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
3493 /* cancel the delayed work if this work still in queue*/
3494 if (instance
->ev
!= NULL
) {
3495 struct megasas_aen_event
*ev
= instance
->ev
;
3496 cancel_delayed_work(
3497 (struct delayed_work
*)&ev
->hotplug_work
);
3498 flush_scheduled_work();
3499 instance
->ev
= NULL
;
3502 tasklet_kill(&instance
->isr_tasklet
);
3505 * Take the instance off the instance array. Note that we will not
3506 * decrement the max_index. We let this array be sparse array
3508 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
3509 if (megasas_mgmt_info
.instance
[i
] == instance
) {
3510 megasas_mgmt_info
.count
--;
3511 megasas_mgmt_info
.instance
[i
] = NULL
;
3517 pci_set_drvdata(instance
->pdev
, NULL
);
3519 instance
->instancet
->disable_intr(instance
->reg_set
);
3521 free_irq(instance
->pdev
->irq
, instance
);
3523 megasas_release_mfi(instance
);
3525 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
3526 instance
->evt_detail
, instance
->evt_detail_h
);
3528 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
3529 instance
->producer_h
);
3531 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
3532 instance
->consumer_h
);
3534 scsi_host_put(host
);
3536 pci_set_drvdata(pdev
, NULL
);
3538 pci_disable_device(pdev
);
3544 * megasas_shutdown - Shutdown entry point
3545 * @device: Generic device structure
3547 static void megasas_shutdown(struct pci_dev
*pdev
)
3549 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
3550 instance
->unload
= 1;
3551 megasas_flush_cache(instance
);
3552 megasas_shutdown_controller(instance
, MR_DCMD_CTRL_SHUTDOWN
);
3556 * megasas_mgmt_open - char node "open" entry point
3558 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
3560 cycle_kernel_lock();
3562 * Allow only those users with admin rights
3564 if (!capable(CAP_SYS_ADMIN
))
3571 * megasas_mgmt_fasync - Async notifier registration from applications
3573 * This function adds the calling process to a driver global queue. When an
3574 * event occurs, SIGIO will be sent to all processes in this queue.
3576 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
3580 mutex_lock(&megasas_async_queue_mutex
);
3582 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
3584 mutex_unlock(&megasas_async_queue_mutex
);
3587 /* For sanity check when we get ioctl */
3588 filep
->private_data
= filep
;
3592 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
3598 * megasas_mgmt_poll - char node "poll" entry point
3600 static unsigned int megasas_mgmt_poll(struct file
*file
, poll_table
*wait
)
3603 unsigned long flags
;
3604 poll_wait(file
, &megasas_poll_wait
, wait
);
3605 spin_lock_irqsave(&poll_aen_lock
, flags
);
3606 if (megasas_poll_wait_aen
)
3607 mask
= (POLLIN
| POLLRDNORM
);
3610 spin_unlock_irqrestore(&poll_aen_lock
, flags
);
3615 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3616 * @instance: Adapter soft state
3617 * @argp: User's ioctl packet
3620 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
3621 struct megasas_iocpacket __user
* user_ioc
,
3622 struct megasas_iocpacket
*ioc
)
3624 struct megasas_sge32
*kern_sge32
;
3625 struct megasas_cmd
*cmd
;
3626 void *kbuff_arr
[MAX_IOCTL_SGE
];
3627 dma_addr_t buf_handle
= 0;
3630 dma_addr_t sense_handle
;
3631 unsigned long *sense_ptr
;
3633 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
3635 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
3636 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
3637 ioc
->sge_count
, MAX_IOCTL_SGE
);
3641 cmd
= megasas_get_cmd(instance
);
3643 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
3648 * User's IOCTL packet has 2 frames (maximum). Copy those two
3649 * frames into our cmd's frames. cmd->frame's context will get
3650 * overwritten when we copy from user's frames. So set that value
3653 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
3654 cmd
->frame
->hdr
.context
= cmd
->index
;
3655 cmd
->frame
->hdr
.pad_0
= 0;
3658 * The management interface between applications and the fw uses
3659 * MFI frames. E.g, RAID configuration changes, LD property changes
3660 * etc are accomplishes through different kinds of MFI frames. The
3661 * driver needs to care only about substituting user buffers with
3662 * kernel buffers in SGLs. The location of SGL is embedded in the
3663 * struct iocpacket itself.
3665 kern_sge32
= (struct megasas_sge32
*)
3666 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
3669 * For each user buffer, create a mirror buffer and copy in
3671 for (i
= 0; i
< ioc
->sge_count
; i
++) {
3672 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
3673 ioc
->sgl
[i
].iov_len
,
3674 &buf_handle
, GFP_KERNEL
);
3675 if (!kbuff_arr
[i
]) {
3676 printk(KERN_DEBUG
"megasas: Failed to alloc "
3677 "kernel SGL buffer for IOCTL \n");
3683 * We don't change the dma_coherent_mask, so
3684 * pci_alloc_consistent only returns 32bit addresses
3686 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
3687 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
3690 * We created a kernel buffer corresponding to the
3691 * user buffer. Now copy in from the user buffer
3693 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
3694 (u32
) (ioc
->sgl
[i
].iov_len
))) {
3700 if (ioc
->sense_len
) {
3701 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
3702 &sense_handle
, GFP_KERNEL
);
3709 (unsigned long *) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
3710 *sense_ptr
= sense_handle
;
3714 * Set the sync_cmd flag so that the ISR knows not to complete this
3715 * cmd to the SCSI mid-layer
3718 megasas_issue_blocked_cmd(instance
, cmd
);
3722 * copy out the kernel buffers to user buffers
3724 for (i
= 0; i
< ioc
->sge_count
; i
++) {
3725 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
3726 ioc
->sgl
[i
].iov_len
)) {
3733 * copy out the sense
3735 if (ioc
->sense_len
) {
3737 * sense_ptr points to the location that has the user
3738 * sense buffer address
3740 sense_ptr
= (unsigned long *) ((unsigned long)ioc
->frame
.raw
+
3743 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
3744 sense
, ioc
->sense_len
)) {
3745 printk(KERN_ERR
"megasas: Failed to copy out to user "
3753 * copy the status codes returned by the fw
3755 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
3756 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
3757 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
3763 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
3764 sense
, sense_handle
);
3767 for (i
= 0; i
< ioc
->sge_count
&& kbuff_arr
[i
]; i
++) {
3768 dma_free_coherent(&instance
->pdev
->dev
,
3769 kern_sge32
[i
].length
,
3770 kbuff_arr
[i
], kern_sge32
[i
].phys_addr
);
3773 megasas_return_cmd(instance
, cmd
);
3777 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
3779 struct megasas_iocpacket __user
*user_ioc
=
3780 (struct megasas_iocpacket __user
*)arg
;
3781 struct megasas_iocpacket
*ioc
;
3782 struct megasas_instance
*instance
;
3785 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
3789 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
3794 instance
= megasas_lookup_instance(ioc
->host_no
);
3800 if (instance
->hw_crit_error
== 1) {
3801 printk(KERN_DEBUG
"Controller in Crit ERROR\n");
3806 if (instance
->unload
== 1) {
3812 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3814 if (down_interruptible(&instance
->ioctl_sem
)) {
3815 error
= -ERESTARTSYS
;
3818 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
3819 up(&instance
->ioctl_sem
);
3826 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
3828 struct megasas_instance
*instance
;
3829 struct megasas_aen aen
;
3832 if (file
->private_data
!= file
) {
3833 printk(KERN_DEBUG
"megasas: fasync_helper was not "
3838 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
3841 instance
= megasas_lookup_instance(aen
.host_no
);
3846 if (instance
->hw_crit_error
== 1) {
3850 if (instance
->unload
== 1) {
3854 mutex_lock(&instance
->aen_mutex
);
3855 error
= megasas_register_aen(instance
, aen
.seq_num
,
3856 aen
.class_locale_word
);
3857 mutex_unlock(&instance
->aen_mutex
);
3862 * megasas_mgmt_ioctl - char node ioctl entry point
3865 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
3868 case MEGASAS_IOC_FIRMWARE
:
3869 return megasas_mgmt_ioctl_fw(file
, arg
);
3871 case MEGASAS_IOC_GET_AEN
:
3872 return megasas_mgmt_ioctl_aen(file
, arg
);
3878 #ifdef CONFIG_COMPAT
3879 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
3881 struct compat_megasas_iocpacket __user
*cioc
=
3882 (struct compat_megasas_iocpacket __user
*)arg
;
3883 struct megasas_iocpacket __user
*ioc
=
3884 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
3889 if (clear_user(ioc
, sizeof(*ioc
)))
3892 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
3893 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
3894 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
3895 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
3896 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
3897 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
3901 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
3902 * sense_len is not null, so prepare the 64bit value under
3903 * the same condition.
3905 if (ioc
->sense_len
) {
3906 void __user
**sense_ioc_ptr
=
3907 (void __user
**)(ioc
->frame
.raw
+ ioc
->sense_off
);
3908 compat_uptr_t
*sense_cioc_ptr
=
3909 (compat_uptr_t
*)(cioc
->frame
.raw
+ cioc
->sense_off
);
3910 if (get_user(ptr
, sense_cioc_ptr
) ||
3911 put_user(compat_ptr(ptr
), sense_ioc_ptr
))
3915 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
3916 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
3917 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
3918 copy_in_user(&ioc
->sgl
[i
].iov_len
,
3919 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
3923 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
3925 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
3926 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
3927 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
3934 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
3938 case MEGASAS_IOC_FIRMWARE32
:
3939 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
3940 case MEGASAS_IOC_GET_AEN
:
3941 return megasas_mgmt_ioctl_aen(file
, arg
);
3949 * File operations structure for management interface
3951 static const struct file_operations megasas_mgmt_fops
= {
3952 .owner
= THIS_MODULE
,
3953 .open
= megasas_mgmt_open
,
3954 .fasync
= megasas_mgmt_fasync
,
3955 .unlocked_ioctl
= megasas_mgmt_ioctl
,
3956 .poll
= megasas_mgmt_poll
,
3957 #ifdef CONFIG_COMPAT
3958 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
3963 * PCI hotplug support registration structure
3965 static struct pci_driver megasas_pci_driver
= {
3967 .name
= "megaraid_sas",
3968 .id_table
= megasas_pci_table
,
3969 .probe
= megasas_probe_one
,
3970 .remove
= __devexit_p(megasas_detach_one
),
3971 .suspend
= megasas_suspend
,
3972 .resume
= megasas_resume
,
3973 .shutdown
= megasas_shutdown
,
3977 * Sysfs driver attributes
3979 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
3981 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
3985 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
3988 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
3990 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
3994 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
3998 megasas_sysfs_show_support_poll_for_event(struct device_driver
*dd
, char *buf
)
4000 return sprintf(buf
, "%u\n", support_poll_for_event
);
4003 static DRIVER_ATTR(support_poll_for_event
, S_IRUGO
,
4004 megasas_sysfs_show_support_poll_for_event
, NULL
);
4007 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
4009 return sprintf(buf
, "%u\n", megasas_dbg_lvl
);
4013 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
4016 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
4017 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
4023 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUSR
, megasas_sysfs_show_dbg_lvl
,
4024 megasas_sysfs_set_dbg_lvl
);
4027 megasas_sysfs_show_poll_mode_io(struct device_driver
*dd
, char *buf
)
4029 return sprintf(buf
, "%u\n", poll_mode_io
);
4033 megasas_sysfs_set_poll_mode_io(struct device_driver
*dd
,
4034 const char *buf
, size_t count
)
4037 int tmp
= poll_mode_io
;
4039 struct megasas_instance
*instance
;
4041 if (sscanf(buf
, "%u", &poll_mode_io
) < 1) {
4042 printk(KERN_ERR
"megasas: could not set poll_mode_io\n");
4047 * Check if poll_mode_io is already set or is same as previous value
4049 if ((tmp
&& poll_mode_io
) || (tmp
== poll_mode_io
))
4054 * Start timers for all adapters
4056 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
4057 instance
= megasas_mgmt_info
.instance
[i
];
4059 megasas_start_timer(instance
,
4060 &instance
->io_completion_timer
,
4061 megasas_io_completion_timer
,
4062 MEGASAS_COMPLETION_TIMER_INTERVAL
);
4067 * Delete timers for all adapters
4069 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
4070 instance
= megasas_mgmt_info
.instance
[i
];
4072 del_timer_sync(&instance
->io_completion_timer
);
4081 megasas_aen_polling(struct work_struct
*work
)
4083 struct megasas_aen_event
*ev
=
4084 container_of(work
, struct megasas_aen_event
, hotplug_work
);
4085 struct megasas_instance
*instance
= ev
->instance
;
4086 union megasas_evt_class_locale class_locale
;
4087 struct Scsi_Host
*host
;
4088 struct scsi_device
*sdev1
;
4091 int i
, j
, doscan
= 0;
4096 printk(KERN_ERR
"invalid instance!\n");
4100 instance
->ev
= NULL
;
4101 host
= instance
->host
;
4102 if (instance
->evt_detail
) {
4104 switch (instance
->evt_detail
->code
) {
4105 case MR_EVT_PD_INSERTED
:
4106 if (megasas_get_pd_list(instance
) == 0) {
4107 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
4109 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
4113 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
4116 scsi_device_lookup(host
, i
, j
, 0);
4118 if (instance
->pd_list
[pd_index
].driveState
4119 == MR_PD_STATE_SYSTEM
) {
4121 scsi_add_device(host
, i
, j
, 0);
4125 scsi_device_put(sdev1
);
4133 case MR_EVT_PD_REMOVED
:
4134 if (megasas_get_pd_list(instance
) == 0) {
4135 megasas_get_pd_list(instance
);
4136 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
4138 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
4142 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
4145 scsi_device_lookup(host
, i
, j
, 0);
4147 if (instance
->pd_list
[pd_index
].driveState
4148 == MR_PD_STATE_SYSTEM
) {
4150 scsi_device_put(sdev1
);
4154 scsi_remove_device(sdev1
);
4155 scsi_device_put(sdev1
);
4164 case MR_EVT_LD_OFFLINE
:
4165 case MR_EVT_LD_DELETED
:
4166 megasas_get_ld_list(instance
);
4167 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
4169 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
4173 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
4175 sdev1
= scsi_device_lookup(host
,
4176 i
+ MEGASAS_MAX_LD_CHANNELS
,
4180 if (instance
->ld_ids
[ld_index
] != 0xff) {
4182 scsi_device_put(sdev1
);
4186 scsi_remove_device(sdev1
);
4187 scsi_device_put(sdev1
);
4194 case MR_EVT_LD_CREATED
:
4195 megasas_get_ld_list(instance
);
4196 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
4198 j
< MEGASAS_MAX_DEV_PER_CHANNEL
;
4201 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
4203 sdev1
= scsi_device_lookup(host
,
4204 i
+MEGASAS_MAX_LD_CHANNELS
,
4207 if (instance
->ld_ids
[ld_index
] !=
4210 scsi_add_device(host
,
4216 scsi_device_put(sdev1
);
4222 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED
:
4223 case MR_EVT_FOREIGN_CFG_IMPORTED
:
4231 printk(KERN_ERR
"invalid evt_detail!\n");
4237 printk(KERN_INFO
"scanning ...\n");
4238 megasas_get_pd_list(instance
);
4239 for (i
= 0; i
< MEGASAS_MAX_PD_CHANNELS
; i
++) {
4240 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
4241 pd_index
= i
*MEGASAS_MAX_DEV_PER_CHANNEL
+ j
;
4242 sdev1
= scsi_device_lookup(host
, i
, j
, 0);
4243 if (instance
->pd_list
[pd_index
].driveState
==
4244 MR_PD_STATE_SYSTEM
) {
4246 scsi_add_device(host
, i
, j
, 0);
4249 scsi_device_put(sdev1
);
4252 scsi_remove_device(sdev1
);
4253 scsi_device_put(sdev1
);
4259 megasas_get_ld_list(instance
);
4260 for (i
= 0; i
< MEGASAS_MAX_LD_CHANNELS
; i
++) {
4261 for (j
= 0; j
< MEGASAS_MAX_DEV_PER_CHANNEL
; j
++) {
4263 (i
* MEGASAS_MAX_DEV_PER_CHANNEL
) + j
;
4265 sdev1
= scsi_device_lookup(host
,
4266 i
+MEGASAS_MAX_LD_CHANNELS
, j
, 0);
4267 if (instance
->ld_ids
[ld_index
] != 0xff) {
4269 scsi_add_device(host
,
4273 scsi_device_put(sdev1
);
4277 scsi_remove_device(sdev1
);
4278 scsi_device_put(sdev1
);
4285 if ( instance
->aen_cmd
!= NULL
) {
4290 seq_num
= instance
->evt_detail
->seq_num
+ 1;
4292 /* Register AEN with FW for latest sequence number plus 1 */
4293 class_locale
.members
.reserved
= 0;
4294 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
4295 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
4296 mutex_lock(&instance
->aen_mutex
);
4297 error
= megasas_register_aen(instance
, seq_num
,
4299 mutex_unlock(&instance
->aen_mutex
);
4302 printk(KERN_ERR
"register aen failed error %x\n", error
);
4308 static DRIVER_ATTR(poll_mode_io
, S_IRUGO
|S_IWUSR
,
4309 megasas_sysfs_show_poll_mode_io
,
4310 megasas_sysfs_set_poll_mode_io
);
4313 * megasas_init - Driver load entry point
4315 static int __init
megasas_init(void)
4320 * Announce driver version and other information
4322 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
4323 MEGASAS_EXT_VERSION
);
4325 support_poll_for_event
= 2;
4327 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
4330 * Register character device node
4332 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
4335 printk(KERN_DEBUG
"megasas: failed to open device node\n");
4339 megasas_mgmt_majorno
= rval
;
4342 * Register ourselves as PCI hotplug module
4344 rval
= pci_register_driver(&megasas_pci_driver
);
4347 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
4351 rval
= driver_create_file(&megasas_pci_driver
.driver
,
4352 &driver_attr_version
);
4354 goto err_dcf_attr_ver
;
4355 rval
= driver_create_file(&megasas_pci_driver
.driver
,
4356 &driver_attr_release_date
);
4358 goto err_dcf_rel_date
;
4360 rval
= driver_create_file(&megasas_pci_driver
.driver
,
4361 &driver_attr_support_poll_for_event
);
4363 goto err_dcf_support_poll_for_event
;
4365 rval
= driver_create_file(&megasas_pci_driver
.driver
,
4366 &driver_attr_dbg_lvl
);
4368 goto err_dcf_dbg_lvl
;
4369 rval
= driver_create_file(&megasas_pci_driver
.driver
,
4370 &driver_attr_poll_mode_io
);
4372 goto err_dcf_poll_mode_io
;
4376 err_dcf_poll_mode_io
:
4377 driver_remove_file(&megasas_pci_driver
.driver
,
4378 &driver_attr_dbg_lvl
);
4380 driver_remove_file(&megasas_pci_driver
.driver
,
4381 &driver_attr_support_poll_for_event
);
4383 err_dcf_support_poll_for_event
:
4384 driver_remove_file(&megasas_pci_driver
.driver
,
4385 &driver_attr_release_date
);
4388 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
4390 pci_unregister_driver(&megasas_pci_driver
);
4392 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
4397 * megasas_exit - Driver unload entry point
4399 static void __exit
megasas_exit(void)
4401 driver_remove_file(&megasas_pci_driver
.driver
,
4402 &driver_attr_poll_mode_io
);
4403 driver_remove_file(&megasas_pci_driver
.driver
,
4404 &driver_attr_dbg_lvl
);
4405 driver_remove_file(&megasas_pci_driver
.driver
,
4406 &driver_attr_release_date
);
4407 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
4409 pci_unregister_driver(&megasas_pci_driver
);
4410 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
4413 module_init(megasas_init
);
4414 module_exit(megasas_exit
);