3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI Logic 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.03.10-rc5
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/uio.h>
37 #include <asm/uaccess.h>
39 #include <linux/compat.h>
40 #include <linux/blkdev.h>
41 #include <linux/mutex.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_host.h>
47 #include "megaraid_sas.h"
49 MODULE_LICENSE("GPL");
50 MODULE_VERSION(MEGASAS_VERSION
);
51 MODULE_AUTHOR("megaraidlinux@lsi.com");
52 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
55 * PCI ID table for all supported controllers
57 static struct pci_device_id megasas_pci_table
[] = {
59 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1064R
)},
61 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_SAS1078R
)},
63 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC
, PCI_DEVICE_ID_LSI_VERDE_ZCR
)},
64 /* xscale IOP, vega */
65 {PCI_DEVICE(PCI_VENDOR_ID_DELL
, PCI_DEVICE_ID_DELL_PERC5
)},
70 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
72 static int megasas_mgmt_majorno
;
73 static struct megasas_mgmt_info megasas_mgmt_info
;
74 static struct fasync_struct
*megasas_async_queue
;
75 static DEFINE_MUTEX(megasas_async_queue_mutex
);
77 static u32 megasas_dbg_lvl
;
80 * megasas_get_cmd - Get a command from the free pool
81 * @instance: Adapter soft state
83 * Returns a free command from the pool
85 static struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
89 struct megasas_cmd
*cmd
= NULL
;
91 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
93 if (!list_empty(&instance
->cmd_pool
)) {
94 cmd
= list_entry((&instance
->cmd_pool
)->next
,
95 struct megasas_cmd
, list
);
96 list_del_init(&cmd
->list
);
98 printk(KERN_ERR
"megasas: Command pool empty!\n");
101 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
106 * megasas_return_cmd - Return a cmd to free command pool
107 * @instance: Adapter soft state
108 * @cmd: Command packet to be returned to free command pool
111 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
115 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
118 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
120 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
125 * The following functions are defined for xscale
126 * (deviceid : 1064R, PERC5) controllers
130 * megasas_enable_intr_xscale - Enables interrupts
131 * @regs: MFI register set
134 megasas_enable_intr_xscale(struct megasas_register_set __iomem
* regs
)
136 writel(1, &(regs
)->outbound_intr_mask
);
138 /* Dummy readl to force pci flush */
139 readl(®s
->outbound_intr_mask
);
143 * megasas_disable_intr_xscale -Disables interrupt
144 * @regs: MFI register set
147 megasas_disable_intr_xscale(struct megasas_register_set __iomem
* regs
)
150 writel(mask
, ®s
->outbound_intr_mask
);
151 /* Dummy readl to force pci flush */
152 readl(®s
->outbound_intr_mask
);
156 * megasas_read_fw_status_reg_xscale - returns the current FW status value
157 * @regs: MFI register set
160 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
162 return readl(&(regs
)->outbound_msg_0
);
165 * megasas_clear_interrupt_xscale - Check & clear interrupt
166 * @regs: MFI register set
169 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
173 * Check if it is our interrupt
175 status
= readl(®s
->outbound_intr_status
);
177 if (!(status
& MFI_OB_INTR_STATUS_MASK
)) {
182 * Clear the interrupt by writing back the same value
184 writel(status
, ®s
->outbound_intr_status
);
190 * megasas_fire_cmd_xscale - Sends command to the FW
191 * @frame_phys_addr : Physical address of cmd
192 * @frame_count : Number of frames for the command
193 * @regs : MFI register set
196 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr
,u32 frame_count
, struct megasas_register_set __iomem
*regs
)
198 writel((frame_phys_addr
>> 3)|(frame_count
),
199 &(regs
)->inbound_queue_port
);
202 static struct megasas_instance_template megasas_instance_template_xscale
= {
204 .fire_cmd
= megasas_fire_cmd_xscale
,
205 .enable_intr
= megasas_enable_intr_xscale
,
206 .disable_intr
= megasas_disable_intr_xscale
,
207 .clear_intr
= megasas_clear_intr_xscale
,
208 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
212 * This is the end of set of functions & definitions specific
213 * to xscale (deviceid : 1064R, PERC5) controllers
217 * The following functions are defined for ppc (deviceid : 0x60)
222 * megasas_enable_intr_ppc - Enables interrupts
223 * @regs: MFI register set
226 megasas_enable_intr_ppc(struct megasas_register_set __iomem
* regs
)
228 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
230 writel(~0x80000004, &(regs
)->outbound_intr_mask
);
232 /* Dummy readl to force pci flush */
233 readl(®s
->outbound_intr_mask
);
237 * megasas_disable_intr_ppc - Disable interrupt
238 * @regs: MFI register set
241 megasas_disable_intr_ppc(struct megasas_register_set __iomem
* regs
)
243 u32 mask
= 0xFFFFFFFF;
244 writel(mask
, ®s
->outbound_intr_mask
);
245 /* Dummy readl to force pci flush */
246 readl(®s
->outbound_intr_mask
);
250 * megasas_read_fw_status_reg_ppc - returns the current FW status value
251 * @regs: MFI register set
254 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
256 return readl(&(regs
)->outbound_scratch_pad
);
260 * megasas_clear_interrupt_ppc - Check & clear interrupt
261 * @regs: MFI register set
264 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
268 * Check if it is our interrupt
270 status
= readl(®s
->outbound_intr_status
);
272 if (!(status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)) {
277 * Clear the interrupt by writing back the same value
279 writel(status
, ®s
->outbound_doorbell_clear
);
284 * megasas_fire_cmd_ppc - Sends command to the FW
285 * @frame_phys_addr : Physical address of cmd
286 * @frame_count : Number of frames for the command
287 * @regs : MFI register set
290 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr
, u32 frame_count
, struct megasas_register_set __iomem
*regs
)
292 writel((frame_phys_addr
| (frame_count
<<1))|1,
293 &(regs
)->inbound_queue_port
);
296 static struct megasas_instance_template megasas_instance_template_ppc
= {
298 .fire_cmd
= megasas_fire_cmd_ppc
,
299 .enable_intr
= megasas_enable_intr_ppc
,
300 .disable_intr
= megasas_disable_intr_ppc
,
301 .clear_intr
= megasas_clear_intr_ppc
,
302 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
306 * This is the end of set of functions & definitions
307 * specific to ppc (deviceid : 0x60) controllers
311 * megasas_issue_polled - Issues a polling command
312 * @instance: Adapter soft state
313 * @cmd: Command packet to be issued
315 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
318 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
321 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
323 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
325 frame_hdr
->cmd_status
= 0xFF;
326 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
329 * Issue the frame using inbound queue port
331 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
334 * Wait for cmd_status to change
336 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
++) {
341 if (frame_hdr
->cmd_status
== 0xff)
348 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
349 * @instance: Adapter soft state
350 * @cmd: Command to be issued
352 * This function waits on an event for the command to be returned from ISR.
353 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
354 * Used to issue ioctl commands.
357 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
358 struct megasas_cmd
*cmd
)
360 cmd
->cmd_status
= ENODATA
;
362 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
364 wait_event_timeout(instance
->int_cmd_wait_q
, (cmd
->cmd_status
!= ENODATA
),
365 MEGASAS_INTERNAL_CMD_WAIT_TIME
*HZ
);
371 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
372 * @instance: Adapter soft state
373 * @cmd_to_abort: Previously issued cmd to be aborted
375 * MFI firmware can abort previously issued AEN comamnd (automatic event
376 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
377 * cmd and waits for return status.
378 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
381 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
382 struct megasas_cmd
*cmd_to_abort
)
384 struct megasas_cmd
*cmd
;
385 struct megasas_abort_frame
*abort_fr
;
387 cmd
= megasas_get_cmd(instance
);
392 abort_fr
= &cmd
->frame
->abort
;
395 * Prepare and issue the abort frame
397 abort_fr
->cmd
= MFI_CMD_ABORT
;
398 abort_fr
->cmd_status
= 0xFF;
400 abort_fr
->abort_context
= cmd_to_abort
->index
;
401 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
402 abort_fr
->abort_mfi_phys_addr_hi
= 0;
405 cmd
->cmd_status
= 0xFF;
407 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
410 * Wait for this cmd to complete
412 wait_event_timeout(instance
->abort_cmd_wait_q
, (cmd
->cmd_status
!= 0xFF),
413 MEGASAS_INTERNAL_CMD_WAIT_TIME
*HZ
);
415 megasas_return_cmd(instance
, cmd
);
420 * megasas_make_sgl32 - Prepares 32-bit SGL
421 * @instance: Adapter soft state
422 * @scp: SCSI command from the mid-layer
423 * @mfi_sgl: SGL to be filled in
425 * If successful, this function returns the number of SG elements. Otherwise,
429 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
430 union megasas_sgl
*mfi_sgl
)
434 struct scatterlist
*os_sgl
;
436 sge_count
= scsi_dma_map(scp
);
437 BUG_ON(sge_count
< 0);
440 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
441 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
442 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
449 * megasas_make_sgl64 - Prepares 64-bit SGL
450 * @instance: Adapter soft state
451 * @scp: SCSI command from the mid-layer
452 * @mfi_sgl: SGL to be filled in
454 * If successful, this function returns the number of SG elements. Otherwise,
458 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
459 union megasas_sgl
*mfi_sgl
)
463 struct scatterlist
*os_sgl
;
465 sge_count
= scsi_dma_map(scp
);
466 BUG_ON(sge_count
< 0);
469 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
470 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
471 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
478 * megasas_get_frame_count - Computes the number of frames
479 * @sge_count : number of sg elements
481 * Returns the number of frames required for numnber of sge's (sge_count)
484 static u32
megasas_get_frame_count(u8 sge_count
)
491 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
492 sizeof(struct megasas_sge32
);
495 * Main frame can contain 2 SGEs for 64-bit SGLs and
496 * 3 SGEs for 32-bit SGLs
499 num_cnt
= sge_count
- 2;
501 num_cnt
= sge_count
- 3;
504 sge_bytes
= sge_sz
* num_cnt
;
506 frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
507 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) ;
518 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
519 * @instance: Adapter soft state
521 * @cmd: Command to be prepared in
523 * This function prepares CDB commands. These are typcially pass-through
524 * commands to the devices.
527 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
528 struct megasas_cmd
*cmd
)
533 struct megasas_pthru_frame
*pthru
;
535 is_logical
= MEGASAS_IS_LOGICAL(scp
);
536 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
537 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
539 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
540 flags
= MFI_FRAME_DIR_WRITE
;
541 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
542 flags
= MFI_FRAME_DIR_READ
;
543 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
544 flags
= MFI_FRAME_DIR_NONE
;
547 * Prepare the DCDB frame
549 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
550 pthru
->cmd_status
= 0x0;
551 pthru
->scsi_status
= 0x0;
552 pthru
->target_id
= device_id
;
553 pthru
->lun
= scp
->device
->lun
;
554 pthru
->cdb_len
= scp
->cmd_len
;
556 pthru
->flags
= flags
;
557 pthru
->data_xfer_len
= scsi_bufflen(scp
);
559 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
565 pthru
->flags
|= MFI_FRAME_SGL64
;
566 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
569 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
573 * Sense info specific
575 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
576 pthru
->sense_buf_phys_addr_hi
= 0;
577 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
580 * Compute the total number of frames this command consumes. FW uses
581 * this number to pull sufficient number of frames from host memory.
583 cmd
->frame_count
= megasas_get_frame_count(pthru
->sge_count
);
585 return cmd
->frame_count
;
589 * megasas_build_ldio - Prepares IOs to logical devices
590 * @instance: Adapter soft state
592 * @cmd: Command to to be prepared
594 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
597 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
598 struct megasas_cmd
*cmd
)
601 u8 sc
= scp
->cmnd
[0];
603 struct megasas_io_frame
*ldio
;
605 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
606 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
608 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
609 flags
= MFI_FRAME_DIR_WRITE
;
610 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
611 flags
= MFI_FRAME_DIR_READ
;
614 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
616 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
617 ldio
->cmd_status
= 0x0;
618 ldio
->scsi_status
= 0x0;
619 ldio
->target_id
= device_id
;
621 ldio
->reserved_0
= 0;
624 ldio
->start_lba_hi
= 0;
625 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
628 * 6-byte READ(0x08) or WRITE(0x0A) cdb
630 if (scp
->cmd_len
== 6) {
631 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
632 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
633 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
635 ldio
->start_lba_lo
&= 0x1FFFFF;
639 * 10-byte READ(0x28) or WRITE(0x2A) cdb
641 else if (scp
->cmd_len
== 10) {
642 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
643 ((u32
) scp
->cmnd
[7] << 8);
644 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
645 ((u32
) scp
->cmnd
[3] << 16) |
646 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
650 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
652 else if (scp
->cmd_len
== 12) {
653 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
654 ((u32
) scp
->cmnd
[7] << 16) |
655 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
657 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
658 ((u32
) scp
->cmnd
[3] << 16) |
659 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
663 * 16-byte READ(0x88) or WRITE(0x8A) cdb
665 else if (scp
->cmd_len
== 16) {
666 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
667 ((u32
) scp
->cmnd
[11] << 16) |
668 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
670 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
671 ((u32
) scp
->cmnd
[7] << 16) |
672 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
674 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
675 ((u32
) scp
->cmnd
[3] << 16) |
676 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
684 ldio
->flags
|= MFI_FRAME_SGL64
;
685 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
687 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
690 * Sense info specific
692 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
693 ldio
->sense_buf_phys_addr_hi
= 0;
694 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
697 * Compute the total number of frames this command consumes. FW uses
698 * this number to pull sufficient number of frames from host memory.
700 cmd
->frame_count
= megasas_get_frame_count(ldio
->sge_count
);
702 return cmd
->frame_count
;
706 * megasas_is_ldio - Checks if the cmd is for logical drive
707 * @scmd: SCSI command
709 * Called by megasas_queue_command to find out if the command to be queued
710 * is a logical drive command
712 static inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
714 if (!MEGASAS_IS_LOGICAL(cmd
))
716 switch (cmd
->cmnd
[0]) {
732 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
734 * @instance: Adapter soft state
737 megasas_dump_pending_frames(struct megasas_instance
*instance
)
739 struct megasas_cmd
*cmd
;
741 union megasas_sgl
*mfi_sgl
;
742 struct megasas_io_frame
*ldio
;
743 struct megasas_pthru_frame
*pthru
;
745 u32 max_cmd
= instance
->max_fw_cmds
;
747 printk(KERN_ERR
"\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance
->host
->host_no
);
748 printk(KERN_ERR
"megasas[%d]: Total OS Pending cmds : %d\n",instance
->host
->host_no
,atomic_read(&instance
->fw_outstanding
));
750 printk(KERN_ERR
"\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance
->host
->host_no
);
752 printk(KERN_ERR
"\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance
->host
->host_no
);
754 printk(KERN_ERR
"megasas[%d]: Pending OS cmds in FW : \n",instance
->host
->host_no
);
755 for (i
= 0; i
< max_cmd
; i
++) {
756 cmd
= instance
->cmd_list
[i
];
759 printk(KERN_ERR
"megasas[%d]: Frame addr :0x%08lx : ",instance
->host
->host_no
,(unsigned long)cmd
->frame_phys_addr
);
760 if (megasas_is_ldio(cmd
->scmd
)){
761 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
762 mfi_sgl
= &ldio
->sgl
;
763 sgcount
= ldio
->sge_count
;
764 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
);
767 pthru
= (struct megasas_pthru_frame
*) cmd
->frame
;
768 mfi_sgl
= &pthru
->sgl
;
769 sgcount
= pthru
->sge_count
;
770 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
);
772 if(megasas_dbg_lvl
& MEGASAS_DBG_LVL
){
773 for (n
= 0; n
< sgcount
; n
++){
775 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
) ;
777 printk(KERN_ERR
"megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl
->sge32
[n
].length
, mfi_sgl
->sge32
[n
].phys_addr
) ;
780 printk(KERN_ERR
"\n");
782 printk(KERN_ERR
"\nmegasas[%d]: Pending Internal cmds in FW : \n",instance
->host
->host_no
);
783 for (i
= 0; i
< max_cmd
; i
++) {
785 cmd
= instance
->cmd_list
[i
];
787 if(cmd
->sync_cmd
== 1){
788 printk(KERN_ERR
"0x%08lx : ", (unsigned long)cmd
->frame_phys_addr
);
791 printk(KERN_ERR
"megasas[%d]: Dumping Done.\n\n",instance
->host
->host_no
);
795 * megasas_queue_command - Queue entry point
796 * @scmd: SCSI command to be queued
797 * @done: Callback entry point
800 megasas_queue_command(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
803 struct megasas_cmd
*cmd
;
804 struct megasas_instance
*instance
;
806 instance
= (struct megasas_instance
*)
807 scmd
->device
->host
->hostdata
;
809 /* Don't process if we have already declared adapter dead */
810 if (instance
->hw_crit_error
)
811 return SCSI_MLQUEUE_HOST_BUSY
;
813 scmd
->scsi_done
= done
;
816 if (MEGASAS_IS_LOGICAL(scmd
) &&
817 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
818 scmd
->result
= DID_BAD_TARGET
<< 16;
822 switch (scmd
->cmnd
[0]) {
823 case SYNCHRONIZE_CACHE
:
825 * FW takes care of flush cache on its own
826 * No need to send it down
828 scmd
->result
= DID_OK
<< 16;
834 cmd
= megasas_get_cmd(instance
);
836 return SCSI_MLQUEUE_HOST_BUSY
;
839 * Logical drive command
841 if (megasas_is_ldio(scmd
))
842 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
844 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
850 scmd
->SCp
.ptr
= (char *)cmd
;
853 * Issue the command to the FW
855 atomic_inc(&instance
->fw_outstanding
);
857 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,cmd
->frame_count
-1,instance
->reg_set
);
862 megasas_return_cmd(instance
, cmd
);
868 static int megasas_slave_configure(struct scsi_device
*sdev
)
871 * Don't export physical disk devices to the disk driver.
873 * FIXME: Currently we don't export them to the midlayer at all.
874 * That will be fixed once LSI engineers have audited the
875 * firmware for possible issues.
877 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&& sdev
->type
== TYPE_DISK
)
881 * The RAID firmware may require extended timeouts.
883 if (sdev
->channel
>= MEGASAS_MAX_PD_CHANNELS
)
884 sdev
->timeout
= MEGASAS_DEFAULT_CMD_TIMEOUT
* HZ
;
889 * megasas_wait_for_outstanding - Wait for all outstanding cmds
890 * @instance: Adapter soft state
892 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
893 * complete all its outstanding commands. Returns error if one or more IOs
894 * are pending after this time period. It also marks the controller dead.
896 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
899 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
901 for (i
= 0; i
< wait_time
; i
++) {
903 int outstanding
= atomic_read(&instance
->fw_outstanding
);
908 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
909 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
910 "commands to complete\n",i
,outstanding
);
916 if (atomic_read(&instance
->fw_outstanding
)) {
918 * Send signal to FW to stop processing any pending cmds.
919 * The controller will be taken offline by the OS now.
922 &instance
->reg_set
->inbound_doorbell
);
923 megasas_dump_pending_frames(instance
);
924 instance
->hw_crit_error
= 1;
932 * megasas_generic_reset - Generic reset routine
933 * @scmd: Mid-layer SCSI command
935 * This routine implements a generic reset handler for device, bus and host
936 * reset requests. Device, bus and host specific reset handlers can use this
937 * function after they do their specific tasks.
939 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
942 struct megasas_instance
*instance
;
944 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
946 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET -%ld cmd=%x retries=%x\n",
947 scmd
->serial_number
, scmd
->cmnd
[0], scmd
->retries
);
949 if (instance
->hw_crit_error
) {
950 printk(KERN_ERR
"megasas: cannot recover from previous reset "
955 ret_val
= megasas_wait_for_outstanding(instance
);
956 if (ret_val
== SUCCESS
)
957 printk(KERN_NOTICE
"megasas: reset successful \n");
959 printk(KERN_ERR
"megasas: failed to do reset\n");
965 * megasas_reset_timer - quiesce the adapter if required
968 * Sets the FW busy flag and reduces the host->can_queue if the
969 * cmd has not been completed within the timeout period.
972 scsi_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
974 struct megasas_cmd
*cmd
= (struct megasas_cmd
*)scmd
->SCp
.ptr
;
975 struct megasas_instance
*instance
;
978 if (time_after(jiffies
, scmd
->jiffies_at_alloc
+
979 (MEGASAS_DEFAULT_CMD_TIMEOUT
* 2) * HZ
)) {
980 return EH_NOT_HANDLED
;
983 instance
= cmd
->instance
;
984 if (!(instance
->flag
& MEGASAS_FW_BUSY
)) {
985 /* FW is busy, throttle IO */
986 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
988 instance
->host
->can_queue
= 16;
989 instance
->last_time
= jiffies
;
990 instance
->flag
|= MEGASAS_FW_BUSY
;
992 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
994 return EH_RESET_TIMER
;
998 * megasas_reset_device - Device reset handler entry point
1000 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
1005 * First wait for all commands to complete
1007 ret
= megasas_generic_reset(scmd
);
1013 * megasas_reset_bus_host - Bus & host reset handler entry point
1015 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
1020 * First wait for all commands to complete
1022 ret
= megasas_generic_reset(scmd
);
1028 * megasas_bios_param - Returns disk geometry for a disk
1029 * @sdev: device handle
1030 * @bdev: block device
1031 * @capacity: drive capacity
1032 * @geom: geometry parameters
1035 megasas_bios_param(struct scsi_device
*sdev
, struct block_device
*bdev
,
1036 sector_t capacity
, int geom
[])
1042 /* Default heads (64) & sectors (32) */
1046 tmp
= heads
* sectors
;
1047 cylinders
= capacity
;
1049 sector_div(cylinders
, tmp
);
1052 * Handle extended translation size for logical drives > 1Gb
1055 if (capacity
>= 0x200000) {
1058 tmp
= heads
*sectors
;
1059 cylinders
= capacity
;
1060 sector_div(cylinders
, tmp
);
1065 geom
[2] = cylinders
;
1071 * megasas_service_aen - Processes an event notification
1072 * @instance: Adapter soft state
1073 * @cmd: AEN command completed by the ISR
1075 * For AEN, driver sends a command down to FW that is held by the FW till an
1076 * event occurs. When an event of interest occurs, FW completes the command
1077 * that it was previously holding.
1079 * This routines sends SIGIO signal to processes that have registered with the
1083 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
1086 * Don't signal app if it is just an aborted previously registered aen
1088 if (!cmd
->abort_aen
)
1089 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
1093 instance
->aen_cmd
= NULL
;
1094 megasas_return_cmd(instance
, cmd
);
1098 * Scsi host template for megaraid_sas driver
1100 static struct scsi_host_template megasas_template
= {
1102 .module
= THIS_MODULE
,
1103 .name
= "LSI Logic SAS based MegaRAID driver",
1104 .proc_name
= "megaraid_sas",
1105 .slave_configure
= megasas_slave_configure
,
1106 .queuecommand
= megasas_queue_command
,
1107 .eh_device_reset_handler
= megasas_reset_device
,
1108 .eh_bus_reset_handler
= megasas_reset_bus_host
,
1109 .eh_host_reset_handler
= megasas_reset_bus_host
,
1110 .eh_timed_out
= megasas_reset_timer
,
1111 .bios_param
= megasas_bios_param
,
1112 .use_clustering
= ENABLE_CLUSTERING
,
1113 .use_sg_chaining
= ENABLE_SG_CHAINING
,
1117 * megasas_complete_int_cmd - Completes an internal command
1118 * @instance: Adapter soft state
1119 * @cmd: Command to be completed
1121 * The megasas_issue_blocked_cmd() function waits for a command to complete
1122 * after it issues a command. This function wakes up that waiting routine by
1123 * calling wake_up() on the wait queue.
1126 megasas_complete_int_cmd(struct megasas_instance
*instance
,
1127 struct megasas_cmd
*cmd
)
1129 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
1131 if (cmd
->cmd_status
== ENODATA
) {
1132 cmd
->cmd_status
= 0;
1134 wake_up(&instance
->int_cmd_wait_q
);
1138 * megasas_complete_abort - Completes aborting a command
1139 * @instance: Adapter soft state
1140 * @cmd: Cmd that was issued to abort another cmd
1142 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1143 * after it issues an abort on a previously issued command. This function
1144 * wakes up all functions waiting on the same wait queue.
1147 megasas_complete_abort(struct megasas_instance
*instance
,
1148 struct megasas_cmd
*cmd
)
1150 if (cmd
->sync_cmd
) {
1152 cmd
->cmd_status
= 0;
1153 wake_up(&instance
->abort_cmd_wait_q
);
1160 * megasas_complete_cmd - Completes a command
1161 * @instance: Adapter soft state
1162 * @cmd: Command to be completed
1163 * @alt_status: If non-zero, use this value as status to
1164 * SCSI mid-layer instead of the value returned
1165 * by the FW. This should be used if caller wants
1166 * an alternate status (as in the case of aborted
1170 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
1174 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
1177 cmd
->scmd
->SCp
.ptr
= NULL
;
1181 case MFI_CMD_PD_SCSI_IO
:
1182 case MFI_CMD_LD_SCSI_IO
:
1185 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1186 * issued either through an IO path or an IOCTL path. If it
1187 * was via IOCTL, we will send it to internal completion.
1189 if (cmd
->sync_cmd
) {
1191 megasas_complete_int_cmd(instance
, cmd
);
1195 case MFI_CMD_LD_READ
:
1196 case MFI_CMD_LD_WRITE
:
1199 cmd
->scmd
->result
= alt_status
<< 16;
1205 atomic_dec(&instance
->fw_outstanding
);
1207 scsi_dma_unmap(cmd
->scmd
);
1208 cmd
->scmd
->scsi_done(cmd
->scmd
);
1209 megasas_return_cmd(instance
, cmd
);
1214 switch (hdr
->cmd_status
) {
1217 cmd
->scmd
->result
= DID_OK
<< 16;
1220 case MFI_STAT_SCSI_IO_FAILED
:
1221 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1223 (DID_ERROR
<< 16) | hdr
->scsi_status
;
1226 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1228 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
1230 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
1231 memset(cmd
->scmd
->sense_buffer
, 0,
1232 SCSI_SENSE_BUFFERSIZE
);
1233 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1236 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1241 case MFI_STAT_LD_OFFLINE
:
1242 case MFI_STAT_DEVICE_NOT_FOUND
:
1243 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1247 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
1249 cmd
->scmd
->result
= DID_ERROR
<< 16;
1253 atomic_dec(&instance
->fw_outstanding
);
1255 scsi_dma_unmap(cmd
->scmd
);
1256 cmd
->scmd
->scsi_done(cmd
->scmd
);
1257 megasas_return_cmd(instance
, cmd
);
1266 * See if got an event notification
1268 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
1269 megasas_service_aen(instance
, cmd
);
1271 megasas_complete_int_cmd(instance
, cmd
);
1277 * Cmd issued to abort another cmd returned
1279 megasas_complete_abort(instance
, cmd
);
1283 printk("megasas: Unknown command completed! [0x%X]\n",
1290 * megasas_deplete_reply_queue - Processes all completed commands
1291 * @instance: Adapter soft state
1292 * @alt_status: Alternate status to be returned to
1293 * SCSI mid-layer instead of the status
1294 * returned by the FW
1297 megasas_deplete_reply_queue(struct megasas_instance
*instance
, u8 alt_status
)
1300 * Check if it is our interrupt
1301 * Clear the interrupt
1303 if(instance
->instancet
->clear_intr(instance
->reg_set
))
1306 if (instance
->hw_crit_error
)
1309 * Schedule the tasklet for cmd completion
1311 tasklet_schedule(&instance
->isr_tasklet
);
1317 * megasas_isr - isr entry point
1319 static irqreturn_t
megasas_isr(int irq
, void *devp
)
1321 return megasas_deplete_reply_queue((struct megasas_instance
*)devp
,
1326 * megasas_transition_to_ready - Move the FW to READY state
1327 * @instance: Adapter soft state
1329 * During the initialization, FW passes can potentially be in any one of
1330 * several possible states. If the FW in operational, waiting-for-handshake
1331 * states, driver must take steps to bring it to ready state. Otherwise, it
1332 * has to wait for the ready state.
1335 megasas_transition_to_ready(struct megasas_instance
* instance
)
1342 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
1344 if (fw_state
!= MFI_STATE_READY
)
1345 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
1348 while (fw_state
!= MFI_STATE_READY
) {
1352 case MFI_STATE_FAULT
:
1354 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
1357 case MFI_STATE_WAIT_HANDSHAKE
:
1359 * Set the CLR bit in inbound doorbell
1361 writel(MFI_INIT_CLEAR_HANDSHAKE
|MFI_INIT_HOTPLUG
,
1362 &instance
->reg_set
->inbound_doorbell
);
1365 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
1368 case MFI_STATE_BOOT_MESSAGE_PENDING
:
1369 writel(MFI_INIT_HOTPLUG
,
1370 &instance
->reg_set
->inbound_doorbell
);
1373 cur_state
= MFI_STATE_BOOT_MESSAGE_PENDING
;
1376 case MFI_STATE_OPERATIONAL
:
1378 * Bring it to READY state; assuming max wait 10 secs
1380 instance
->instancet
->disable_intr(instance
->reg_set
);
1381 writel(MFI_RESET_FLAGS
, &instance
->reg_set
->inbound_doorbell
);
1384 cur_state
= MFI_STATE_OPERATIONAL
;
1387 case MFI_STATE_UNDEFINED
:
1389 * This state should not last for more than 2 seconds
1392 cur_state
= MFI_STATE_UNDEFINED
;
1395 case MFI_STATE_BB_INIT
:
1397 cur_state
= MFI_STATE_BB_INIT
;
1400 case MFI_STATE_FW_INIT
:
1402 cur_state
= MFI_STATE_FW_INIT
;
1405 case MFI_STATE_FW_INIT_2
:
1407 cur_state
= MFI_STATE_FW_INIT_2
;
1410 case MFI_STATE_DEVICE_SCAN
:
1412 cur_state
= MFI_STATE_DEVICE_SCAN
;
1415 case MFI_STATE_FLUSH_CACHE
:
1417 cur_state
= MFI_STATE_FLUSH_CACHE
;
1421 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
1427 * The cur_state should not last for more than max_wait secs
1429 for (i
= 0; i
< (max_wait
* 1000); i
++) {
1430 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
1433 if (fw_state
== cur_state
) {
1440 * Return error if fw_state hasn't changed after max_wait
1442 if (fw_state
== cur_state
) {
1443 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
1444 "in %d secs\n", fw_state
, max_wait
);
1448 printk(KERN_INFO
"megasas: FW now in Ready state\n");
1454 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1455 * @instance: Adapter soft state
1457 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
1460 u32 max_cmd
= instance
->max_fw_cmds
;
1461 struct megasas_cmd
*cmd
;
1463 if (!instance
->frame_dma_pool
)
1467 * Return all frames to pool
1469 for (i
= 0; i
< max_cmd
; i
++) {
1471 cmd
= instance
->cmd_list
[i
];
1474 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
1475 cmd
->frame_phys_addr
);
1478 pci_pool_free(instance
->sense_dma_pool
, cmd
->sense
,
1479 cmd
->sense_phys_addr
);
1483 * Now destroy the pool itself
1485 pci_pool_destroy(instance
->frame_dma_pool
);
1486 pci_pool_destroy(instance
->sense_dma_pool
);
1488 instance
->frame_dma_pool
= NULL
;
1489 instance
->sense_dma_pool
= NULL
;
1493 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1494 * @instance: Adapter soft state
1496 * Each command packet has an embedded DMA memory buffer that is used for
1497 * filling MFI frame and the SG list that immediately follows the frame. This
1498 * function creates those DMA memory buffers for each command packet by using
1499 * PCI pool facility.
1501 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
1509 struct megasas_cmd
*cmd
;
1511 max_cmd
= instance
->max_fw_cmds
;
1514 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1515 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1517 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1518 sizeof(struct megasas_sge32
);
1521 * Calculated the number of 64byte frames required for SGL
1523 sgl_sz
= sge_sz
* instance
->max_num_sge
;
1524 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
1527 * We need one extra frame for the MFI command
1531 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
1533 * Use DMA pool facility provided by PCI layer
1535 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
1536 instance
->pdev
, total_sz
, 64,
1539 if (!instance
->frame_dma_pool
) {
1540 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
1544 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
1545 instance
->pdev
, 128, 4, 0);
1547 if (!instance
->sense_dma_pool
) {
1548 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
1550 pci_pool_destroy(instance
->frame_dma_pool
);
1551 instance
->frame_dma_pool
= NULL
;
1557 * Allocate and attach a frame to each of the commands in cmd_list.
1558 * By making cmd->index as the context instead of the &cmd, we can
1559 * always use 32bit context regardless of the architecture
1561 for (i
= 0; i
< max_cmd
; i
++) {
1563 cmd
= instance
->cmd_list
[i
];
1565 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
1566 GFP_KERNEL
, &cmd
->frame_phys_addr
);
1568 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
1569 GFP_KERNEL
, &cmd
->sense_phys_addr
);
1572 * megasas_teardown_frame_pool() takes care of freeing
1573 * whatever has been allocated
1575 if (!cmd
->frame
|| !cmd
->sense
) {
1576 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
1577 megasas_teardown_frame_pool(instance
);
1581 cmd
->frame
->io
.context
= cmd
->index
;
1588 * megasas_free_cmds - Free all the cmds in the free cmd pool
1589 * @instance: Adapter soft state
1591 static void megasas_free_cmds(struct megasas_instance
*instance
)
1594 /* First free the MFI frame pool */
1595 megasas_teardown_frame_pool(instance
);
1597 /* Free all the commands in the cmd_list */
1598 for (i
= 0; i
< instance
->max_fw_cmds
; i
++)
1599 kfree(instance
->cmd_list
[i
]);
1601 /* Free the cmd_list buffer itself */
1602 kfree(instance
->cmd_list
);
1603 instance
->cmd_list
= NULL
;
1605 INIT_LIST_HEAD(&instance
->cmd_pool
);
1609 * megasas_alloc_cmds - Allocates the command packets
1610 * @instance: Adapter soft state
1612 * Each command that is issued to the FW, whether IO commands from the OS or
1613 * internal commands like IOCTLs, are wrapped in local data structure called
1614 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1617 * Each frame has a 32-bit field called context (tag). This context is used
1618 * to get back the megasas_cmd from the frame when a frame gets completed in
1619 * the ISR. Typically the address of the megasas_cmd itself would be used as
1620 * the context. But we wanted to keep the differences between 32 and 64 bit
1621 * systems to the mininum. We always use 32 bit integers for the context. In
1622 * this driver, the 32 bit values are the indices into an array cmd_list.
1623 * This array is used only to look up the megasas_cmd given the context. The
1624 * free commands themselves are maintained in a linked list called cmd_pool.
1626 static int megasas_alloc_cmds(struct megasas_instance
*instance
)
1631 struct megasas_cmd
*cmd
;
1633 max_cmd
= instance
->max_fw_cmds
;
1636 * instance->cmd_list is an array of struct megasas_cmd pointers.
1637 * Allocate the dynamic array first and then allocate individual
1640 instance
->cmd_list
= kcalloc(max_cmd
, sizeof(struct megasas_cmd
*), GFP_KERNEL
);
1642 if (!instance
->cmd_list
) {
1643 printk(KERN_DEBUG
"megasas: out of memory\n");
1648 for (i
= 0; i
< max_cmd
; i
++) {
1649 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
1652 if (!instance
->cmd_list
[i
]) {
1654 for (j
= 0; j
< i
; j
++)
1655 kfree(instance
->cmd_list
[j
]);
1657 kfree(instance
->cmd_list
);
1658 instance
->cmd_list
= NULL
;
1665 * Add all the commands to command pool (instance->cmd_pool)
1667 for (i
= 0; i
< max_cmd
; i
++) {
1668 cmd
= instance
->cmd_list
[i
];
1669 memset(cmd
, 0, sizeof(struct megasas_cmd
));
1671 cmd
->instance
= instance
;
1673 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
1677 * Create a frame pool and assign one frame to each cmd
1679 if (megasas_create_frame_pool(instance
)) {
1680 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
1681 megasas_free_cmds(instance
);
1688 * megasas_get_controller_info - Returns FW's controller structure
1689 * @instance: Adapter soft state
1690 * @ctrl_info: Controller information structure
1692 * Issues an internal command (DCMD) to get the FW's controller structure.
1693 * This information is mainly used to find out the maximum IO transfer per
1694 * command supported by the FW.
1697 megasas_get_ctrl_info(struct megasas_instance
*instance
,
1698 struct megasas_ctrl_info
*ctrl_info
)
1701 struct megasas_cmd
*cmd
;
1702 struct megasas_dcmd_frame
*dcmd
;
1703 struct megasas_ctrl_info
*ci
;
1704 dma_addr_t ci_h
= 0;
1706 cmd
= megasas_get_cmd(instance
);
1709 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
1713 dcmd
= &cmd
->frame
->dcmd
;
1715 ci
= pci_alloc_consistent(instance
->pdev
,
1716 sizeof(struct megasas_ctrl_info
), &ci_h
);
1719 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
1720 megasas_return_cmd(instance
, cmd
);
1724 memset(ci
, 0, sizeof(*ci
));
1725 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1727 dcmd
->cmd
= MFI_CMD_DCMD
;
1728 dcmd
->cmd_status
= 0xFF;
1729 dcmd
->sge_count
= 1;
1730 dcmd
->flags
= MFI_FRAME_DIR_READ
;
1732 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
1733 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
1734 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
1735 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
1737 if (!megasas_issue_polled(instance
, cmd
)) {
1739 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
1744 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
1747 megasas_return_cmd(instance
, cmd
);
1752 * megasas_complete_cmd_dpc - Returns FW's controller structure
1753 * @instance_addr: Address of adapter soft state
1755 * Tasklet to complete cmds
1757 static void megasas_complete_cmd_dpc(unsigned long instance_addr
)
1762 struct megasas_cmd
*cmd
;
1763 struct megasas_instance
*instance
= (struct megasas_instance
*)instance_addr
;
1764 unsigned long flags
;
1766 /* If we have already declared adapter dead, donot complete cmds */
1767 if (instance
->hw_crit_error
)
1770 producer
= *instance
->producer
;
1771 consumer
= *instance
->consumer
;
1773 while (consumer
!= producer
) {
1774 context
= instance
->reply_queue
[consumer
];
1776 cmd
= instance
->cmd_list
[context
];
1778 megasas_complete_cmd(instance
, cmd
, DID_OK
);
1781 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1786 *instance
->consumer
= producer
;
1789 * Check if we can restore can_queue
1791 if (instance
->flag
& MEGASAS_FW_BUSY
1792 && time_after(jiffies
, instance
->last_time
+ 5 * HZ
)
1793 && atomic_read(&instance
->fw_outstanding
) < 17) {
1795 spin_lock_irqsave(instance
->host
->host_lock
, flags
);
1796 instance
->flag
&= ~MEGASAS_FW_BUSY
;
1797 instance
->host
->can_queue
=
1798 instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
1800 spin_unlock_irqrestore(instance
->host
->host_lock
, flags
);
1806 * megasas_init_mfi - Initializes the FW
1807 * @instance: Adapter soft state
1809 * This is the main function for initializing MFI firmware.
1811 static int megasas_init_mfi(struct megasas_instance
*instance
)
1817 struct megasas_register_set __iomem
*reg_set
;
1819 struct megasas_cmd
*cmd
;
1820 struct megasas_ctrl_info
*ctrl_info
;
1822 struct megasas_init_frame
*init_frame
;
1823 struct megasas_init_queue_info
*initq_info
;
1824 dma_addr_t init_frame_h
;
1825 dma_addr_t initq_info_h
;
1828 * Map the message registers
1830 instance
->base_addr
= pci_resource_start(instance
->pdev
, 0);
1832 if (pci_request_regions(instance
->pdev
, "megasas: LSI Logic")) {
1833 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
1837 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
1839 if (!instance
->reg_set
) {
1840 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
1844 reg_set
= instance
->reg_set
;
1846 switch(instance
->pdev
->device
)
1848 case PCI_DEVICE_ID_LSI_SAS1078R
:
1849 instance
->instancet
= &megasas_instance_template_ppc
;
1851 case PCI_DEVICE_ID_LSI_SAS1064R
:
1852 case PCI_DEVICE_ID_DELL_PERC5
:
1854 instance
->instancet
= &megasas_instance_template_xscale
;
1859 * We expect the FW state to be READY
1861 if (megasas_transition_to_ready(instance
))
1862 goto fail_ready_state
;
1865 * Get various operational parameters from status register
1867 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
1869 * Reduce the max supported cmds by 1. This is to ensure that the
1870 * reply_q_sz (1 more than the max cmd that driver may send)
1871 * does not exceed max cmds that the FW can support
1873 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
1874 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
1877 * Create a pool of commands
1879 if (megasas_alloc_cmds(instance
))
1880 goto fail_alloc_cmds
;
1883 * Allocate memory for reply queue. Length of reply queue should
1884 * be _one_ more than the maximum commands handled by the firmware.
1886 * Note: When FW completes commands, it places corresponding contex
1887 * values in this circular reply queue. This circular queue is a fairly
1888 * typical producer-consumer queue. FW is the producer (of completed
1889 * commands) and the driver is the consumer.
1891 context_sz
= sizeof(u32
);
1892 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
1894 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
1896 &instance
->reply_queue_h
);
1898 if (!instance
->reply_queue
) {
1899 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
1900 goto fail_reply_queue
;
1904 * Prepare a init frame. Note the init frame points to queue info
1905 * structure. Each frame has SGL allocated after first 64 bytes. For
1906 * this frame - since we don't need any SGL - we use SGL's space as
1907 * queue info structure
1909 * We will not get a NULL command below. We just created the pool.
1911 cmd
= megasas_get_cmd(instance
);
1913 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
1914 initq_info
= (struct megasas_init_queue_info
*)
1915 ((unsigned long)init_frame
+ 64);
1917 init_frame_h
= cmd
->frame_phys_addr
;
1918 initq_info_h
= init_frame_h
+ 64;
1920 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
1921 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
1923 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
1924 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
1926 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
1927 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
1929 init_frame
->cmd
= MFI_CMD_INIT
;
1930 init_frame
->cmd_status
= 0xFF;
1931 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
1933 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
1936 * disable the intr before firing the init frame to FW
1938 instance
->instancet
->disable_intr(instance
->reg_set
);
1941 * Issue the init frame in polled mode
1943 if (megasas_issue_polled(instance
, cmd
)) {
1944 printk(KERN_DEBUG
"megasas: Failed to init firmware\n");
1948 megasas_return_cmd(instance
, cmd
);
1950 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
1953 * Compute the max allowed sectors per IO: The controller info has two
1954 * limits on max sectors. Driver should use the minimum of these two.
1956 * 1 << stripe_sz_ops.min = max sectors per strip
1958 * Note that older firmwares ( < FW ver 30) didn't report information
1959 * to calculate max_sectors_1. So the number ended up as zero always.
1961 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
1963 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
1964 ctrl_info
->max_strips_per_io
;
1965 max_sectors_2
= ctrl_info
->max_request_size
;
1967 instance
->max_sectors_per_req
= (max_sectors_1
< max_sectors_2
)
1968 ? max_sectors_1
: max_sectors_2
;
1970 instance
->max_sectors_per_req
= instance
->max_num_sge
*
1976 * Setup tasklet for cmd completion
1979 tasklet_init(&instance
->isr_tasklet
, megasas_complete_cmd_dpc
,
1980 (unsigned long)instance
);
1984 megasas_return_cmd(instance
, cmd
);
1986 pci_free_consistent(instance
->pdev
, reply_q_sz
,
1987 instance
->reply_queue
, instance
->reply_queue_h
);
1989 megasas_free_cmds(instance
);
1993 iounmap(instance
->reg_set
);
1996 pci_release_regions(instance
->pdev
);
2002 * megasas_release_mfi - Reverses the FW initialization
2003 * @intance: Adapter soft state
2005 static void megasas_release_mfi(struct megasas_instance
*instance
)
2007 u32 reply_q_sz
= sizeof(u32
) * (instance
->max_fw_cmds
+ 1);
2009 pci_free_consistent(instance
->pdev
, reply_q_sz
,
2010 instance
->reply_queue
, instance
->reply_queue_h
);
2012 megasas_free_cmds(instance
);
2014 iounmap(instance
->reg_set
);
2016 pci_release_regions(instance
->pdev
);
2020 * megasas_get_seq_num - Gets latest event sequence numbers
2021 * @instance: Adapter soft state
2022 * @eli: FW event log sequence numbers information
2024 * FW maintains a log of all events in a non-volatile area. Upper layers would
2025 * usually find out the latest sequence number of the events, the seq number at
2026 * the boot etc. They would "read" all the events below the latest seq number
2027 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2028 * number), they would subsribe to AEN (asynchronous event notification) and
2029 * wait for the events to happen.
2032 megasas_get_seq_num(struct megasas_instance
*instance
,
2033 struct megasas_evt_log_info
*eli
)
2035 struct megasas_cmd
*cmd
;
2036 struct megasas_dcmd_frame
*dcmd
;
2037 struct megasas_evt_log_info
*el_info
;
2038 dma_addr_t el_info_h
= 0;
2040 cmd
= megasas_get_cmd(instance
);
2046 dcmd
= &cmd
->frame
->dcmd
;
2047 el_info
= pci_alloc_consistent(instance
->pdev
,
2048 sizeof(struct megasas_evt_log_info
),
2052 megasas_return_cmd(instance
, cmd
);
2056 memset(el_info
, 0, sizeof(*el_info
));
2057 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2059 dcmd
->cmd
= MFI_CMD_DCMD
;
2060 dcmd
->cmd_status
= 0x0;
2061 dcmd
->sge_count
= 1;
2062 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2064 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
2065 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
2066 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
2067 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
2069 megasas_issue_blocked_cmd(instance
, cmd
);
2072 * Copy the data back into callers buffer
2074 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
2076 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
2077 el_info
, el_info_h
);
2079 megasas_return_cmd(instance
, cmd
);
2085 * megasas_register_aen - Registers for asynchronous event notification
2086 * @instance: Adapter soft state
2087 * @seq_num: The starting sequence number
2088 * @class_locale: Class of the event
2090 * This function subscribes for AEN for events beyond the @seq_num. It requests
2091 * to be notified if and only if the event is of type @class_locale
2094 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
2095 u32 class_locale_word
)
2098 struct megasas_cmd
*cmd
;
2099 struct megasas_dcmd_frame
*dcmd
;
2100 union megasas_evt_class_locale curr_aen
;
2101 union megasas_evt_class_locale prev_aen
;
2104 * If there an AEN pending already (aen_cmd), check if the
2105 * class_locale of that pending AEN is inclusive of the new
2106 * AEN request we currently have. If it is, then we don't have
2107 * to do anything. In other words, whichever events the current
2108 * AEN request is subscribing to, have already been subscribed
2111 * If the old_cmd is _not_ inclusive, then we have to abort
2112 * that command, form a class_locale that is superset of both
2113 * old and current and re-issue to the FW
2116 curr_aen
.word
= class_locale_word
;
2118 if (instance
->aen_cmd
) {
2120 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
2123 * A class whose enum value is smaller is inclusive of all
2124 * higher values. If a PROGRESS (= -1) was previously
2125 * registered, then a new registration requests for higher
2126 * classes need not be sent to FW. They are automatically
2129 * Locale numbers don't have such hierarchy. They are bitmap
2132 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
2133 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
2134 curr_aen
.members
.locale
)) {
2136 * Previously issued event registration includes
2137 * current request. Nothing to do.
2141 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
2143 if (prev_aen
.members
.class < curr_aen
.members
.class)
2144 curr_aen
.members
.class = prev_aen
.members
.class;
2146 instance
->aen_cmd
->abort_aen
= 1;
2147 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
2152 printk(KERN_DEBUG
"megasas: Failed to abort "
2153 "previous AEN command\n");
2159 cmd
= megasas_get_cmd(instance
);
2164 dcmd
= &cmd
->frame
->dcmd
;
2166 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
2169 * Prepare DCMD for aen registration
2171 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2173 dcmd
->cmd
= MFI_CMD_DCMD
;
2174 dcmd
->cmd_status
= 0x0;
2175 dcmd
->sge_count
= 1;
2176 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2178 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
2179 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
2180 dcmd
->mbox
.w
[0] = seq_num
;
2181 dcmd
->mbox
.w
[1] = curr_aen
.word
;
2182 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
2183 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
2186 * Store reference to the cmd used to register for AEN. When an
2187 * application wants us to register for AEN, we have to abort this
2188 * cmd and re-register with a new EVENT LOCALE supplied by that app
2190 instance
->aen_cmd
= cmd
;
2193 * Issue the aen registration frame
2195 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
2201 * megasas_start_aen - Subscribes to AEN during driver load time
2202 * @instance: Adapter soft state
2204 static int megasas_start_aen(struct megasas_instance
*instance
)
2206 struct megasas_evt_log_info eli
;
2207 union megasas_evt_class_locale class_locale
;
2210 * Get the latest sequence number from FW
2212 memset(&eli
, 0, sizeof(eli
));
2214 if (megasas_get_seq_num(instance
, &eli
))
2218 * Register AEN with FW for latest sequence number plus 1
2220 class_locale
.members
.reserved
= 0;
2221 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2222 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2224 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
2229 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2230 * @instance: Adapter soft state
2232 static int megasas_io_attach(struct megasas_instance
*instance
)
2234 struct Scsi_Host
*host
= instance
->host
;
2237 * Export parameters required by SCSI mid-layer
2239 host
->irq
= instance
->pdev
->irq
;
2240 host
->unique_id
= instance
->unique_id
;
2241 host
->can_queue
= instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
2242 host
->this_id
= instance
->init_id
;
2243 host
->sg_tablesize
= instance
->max_num_sge
;
2244 host
->max_sectors
= instance
->max_sectors_per_req
;
2245 host
->cmd_per_lun
= 128;
2246 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
2247 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
2248 host
->max_lun
= MEGASAS_MAX_LUN
;
2249 host
->max_cmd_len
= 16;
2252 * Notify the mid-layer about the new controller
2254 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
2255 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
2260 * Trigger SCSI to scan our drives
2262 scsi_scan_host(host
);
2267 * megasas_probe_one - PCI hotplug entry point
2268 * @pdev: PCI device structure
2269 * @id: PCI ids of supported hotplugged adapter
2271 static int __devinit
2272 megasas_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2275 struct Scsi_Host
*host
;
2276 struct megasas_instance
*instance
;
2279 * Announce PCI information
2281 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2282 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
2283 pdev
->subsystem_device
);
2285 printk("bus %d:slot %d:func %d\n",
2286 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
2289 * PCI prepping: enable device set bus mastering and dma mask
2291 rval
= pci_enable_device(pdev
);
2297 pci_set_master(pdev
);
2300 * All our contollers are capable of performing 64-bit DMA
2303 if (pci_set_dma_mask(pdev
, DMA_64BIT_MASK
) != 0) {
2305 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) != 0)
2306 goto fail_set_dma_mask
;
2309 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) != 0)
2310 goto fail_set_dma_mask
;
2313 host
= scsi_host_alloc(&megasas_template
,
2314 sizeof(struct megasas_instance
));
2317 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
2318 goto fail_alloc_instance
;
2321 instance
= (struct megasas_instance
*)host
->hostdata
;
2322 memset(instance
, 0, sizeof(*instance
));
2324 instance
->producer
= pci_alloc_consistent(pdev
, sizeof(u32
),
2325 &instance
->producer_h
);
2326 instance
->consumer
= pci_alloc_consistent(pdev
, sizeof(u32
),
2327 &instance
->consumer_h
);
2329 if (!instance
->producer
|| !instance
->consumer
) {
2330 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
2331 "producer, consumer\n");
2332 goto fail_alloc_dma_buf
;
2335 *instance
->producer
= 0;
2336 *instance
->consumer
= 0;
2338 instance
->evt_detail
= pci_alloc_consistent(pdev
,
2340 megasas_evt_detail
),
2341 &instance
->evt_detail_h
);
2343 if (!instance
->evt_detail
) {
2344 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
2345 "event detail structure\n");
2346 goto fail_alloc_dma_buf
;
2350 * Initialize locks and queues
2352 INIT_LIST_HEAD(&instance
->cmd_pool
);
2354 atomic_set(&instance
->fw_outstanding
,0);
2356 init_waitqueue_head(&instance
->int_cmd_wait_q
);
2357 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
2359 spin_lock_init(&instance
->cmd_pool_lock
);
2361 sema_init(&instance
->aen_mutex
, 1);
2362 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
2365 * Initialize PCI related and misc parameters
2367 instance
->pdev
= pdev
;
2368 instance
->host
= host
;
2369 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
2370 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
2372 megasas_dbg_lvl
= 0;
2374 instance
->last_time
= 0;
2377 * Initialize MFI Firmware
2379 if (megasas_init_mfi(instance
))
2385 if (request_irq(pdev
->irq
, megasas_isr
, IRQF_SHARED
, "megasas", instance
)) {
2386 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
2390 instance
->instancet
->enable_intr(instance
->reg_set
);
2393 * Store instance in PCI softstate
2395 pci_set_drvdata(pdev
, instance
);
2398 * Add this controller to megasas_mgmt_info structure so that it
2399 * can be exported to management applications
2401 megasas_mgmt_info
.count
++;
2402 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
2403 megasas_mgmt_info
.max_index
++;
2406 * Initiate AEN (Asynchronous Event Notification)
2408 if (megasas_start_aen(instance
)) {
2409 printk(KERN_DEBUG
"megasas: start aen failed\n");
2410 goto fail_start_aen
;
2414 * Register with SCSI mid-layer
2416 if (megasas_io_attach(instance
))
2417 goto fail_io_attach
;
2423 megasas_mgmt_info
.count
--;
2424 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
2425 megasas_mgmt_info
.max_index
--;
2427 pci_set_drvdata(pdev
, NULL
);
2428 instance
->instancet
->disable_intr(instance
->reg_set
);
2429 free_irq(instance
->pdev
->irq
, instance
);
2431 megasas_release_mfi(instance
);
2436 if (instance
->evt_detail
)
2437 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
2438 instance
->evt_detail
,
2439 instance
->evt_detail_h
);
2441 if (instance
->producer
)
2442 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
2443 instance
->producer_h
);
2444 if (instance
->consumer
)
2445 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
2446 instance
->consumer_h
);
2447 scsi_host_put(host
);
2449 fail_alloc_instance
:
2451 pci_disable_device(pdev
);
2457 * megasas_flush_cache - Requests FW to flush all its caches
2458 * @instance: Adapter soft state
2460 static void megasas_flush_cache(struct megasas_instance
*instance
)
2462 struct megasas_cmd
*cmd
;
2463 struct megasas_dcmd_frame
*dcmd
;
2465 cmd
= megasas_get_cmd(instance
);
2470 dcmd
= &cmd
->frame
->dcmd
;
2472 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2474 dcmd
->cmd
= MFI_CMD_DCMD
;
2475 dcmd
->cmd_status
= 0x0;
2476 dcmd
->sge_count
= 0;
2477 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
2479 dcmd
->data_xfer_len
= 0;
2480 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
2481 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
2483 megasas_issue_blocked_cmd(instance
, cmd
);
2485 megasas_return_cmd(instance
, cmd
);
2491 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2492 * @instance: Adapter soft state
2494 static void megasas_shutdown_controller(struct megasas_instance
*instance
)
2496 struct megasas_cmd
*cmd
;
2497 struct megasas_dcmd_frame
*dcmd
;
2499 cmd
= megasas_get_cmd(instance
);
2504 if (instance
->aen_cmd
)
2505 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
2507 dcmd
= &cmd
->frame
->dcmd
;
2509 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2511 dcmd
->cmd
= MFI_CMD_DCMD
;
2512 dcmd
->cmd_status
= 0x0;
2513 dcmd
->sge_count
= 0;
2514 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
2516 dcmd
->data_xfer_len
= 0;
2517 dcmd
->opcode
= MR_DCMD_CTRL_SHUTDOWN
;
2519 megasas_issue_blocked_cmd(instance
, cmd
);
2521 megasas_return_cmd(instance
, cmd
);
2527 * megasas_detach_one - PCI hot"un"plug entry point
2528 * @pdev: PCI device structure
2530 static void megasas_detach_one(struct pci_dev
*pdev
)
2533 struct Scsi_Host
*host
;
2534 struct megasas_instance
*instance
;
2536 instance
= pci_get_drvdata(pdev
);
2537 host
= instance
->host
;
2539 scsi_remove_host(instance
->host
);
2540 megasas_flush_cache(instance
);
2541 megasas_shutdown_controller(instance
);
2542 tasklet_kill(&instance
->isr_tasklet
);
2545 * Take the instance off the instance array. Note that we will not
2546 * decrement the max_index. We let this array be sparse array
2548 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
2549 if (megasas_mgmt_info
.instance
[i
] == instance
) {
2550 megasas_mgmt_info
.count
--;
2551 megasas_mgmt_info
.instance
[i
] = NULL
;
2557 pci_set_drvdata(instance
->pdev
, NULL
);
2559 instance
->instancet
->disable_intr(instance
->reg_set
);
2561 free_irq(instance
->pdev
->irq
, instance
);
2563 megasas_release_mfi(instance
);
2565 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
2566 instance
->evt_detail
, instance
->evt_detail_h
);
2568 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
2569 instance
->producer_h
);
2571 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
2572 instance
->consumer_h
);
2574 scsi_host_put(host
);
2576 pci_set_drvdata(pdev
, NULL
);
2578 pci_disable_device(pdev
);
2584 * megasas_shutdown - Shutdown entry point
2585 * @device: Generic device structure
2587 static void megasas_shutdown(struct pci_dev
*pdev
)
2589 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
2590 megasas_flush_cache(instance
);
2594 * megasas_mgmt_open - char node "open" entry point
2596 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
2599 * Allow only those users with admin rights
2601 if (!capable(CAP_SYS_ADMIN
))
2608 * megasas_mgmt_release - char node "release" entry point
2610 static int megasas_mgmt_release(struct inode
*inode
, struct file
*filep
)
2612 filep
->private_data
= NULL
;
2613 fasync_helper(-1, filep
, 0, &megasas_async_queue
);
2619 * megasas_mgmt_fasync - Async notifier registration from applications
2621 * This function adds the calling process to a driver global queue. When an
2622 * event occurs, SIGIO will be sent to all processes in this queue.
2624 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
2628 mutex_lock(&megasas_async_queue_mutex
);
2630 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
2632 mutex_unlock(&megasas_async_queue_mutex
);
2635 /* For sanity check when we get ioctl */
2636 filep
->private_data
= filep
;
2640 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
2646 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2647 * @instance: Adapter soft state
2648 * @argp: User's ioctl packet
2651 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
2652 struct megasas_iocpacket __user
* user_ioc
,
2653 struct megasas_iocpacket
*ioc
)
2655 struct megasas_sge32
*kern_sge32
;
2656 struct megasas_cmd
*cmd
;
2657 void *kbuff_arr
[MAX_IOCTL_SGE
];
2658 dma_addr_t buf_handle
= 0;
2661 dma_addr_t sense_handle
;
2664 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
2666 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
2667 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
2668 ioc
->sge_count
, MAX_IOCTL_SGE
);
2672 cmd
= megasas_get_cmd(instance
);
2674 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
2679 * User's IOCTL packet has 2 frames (maximum). Copy those two
2680 * frames into our cmd's frames. cmd->frame's context will get
2681 * overwritten when we copy from user's frames. So set that value
2684 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
2685 cmd
->frame
->hdr
.context
= cmd
->index
;
2688 * The management interface between applications and the fw uses
2689 * MFI frames. E.g, RAID configuration changes, LD property changes
2690 * etc are accomplishes through different kinds of MFI frames. The
2691 * driver needs to care only about substituting user buffers with
2692 * kernel buffers in SGLs. The location of SGL is embedded in the
2693 * struct iocpacket itself.
2695 kern_sge32
= (struct megasas_sge32
*)
2696 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
2699 * For each user buffer, create a mirror buffer and copy in
2701 for (i
= 0; i
< ioc
->sge_count
; i
++) {
2702 kbuff_arr
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
2703 ioc
->sgl
[i
].iov_len
,
2704 &buf_handle
, GFP_KERNEL
);
2705 if (!kbuff_arr
[i
]) {
2706 printk(KERN_DEBUG
"megasas: Failed to alloc "
2707 "kernel SGL buffer for IOCTL \n");
2713 * We don't change the dma_coherent_mask, so
2714 * pci_alloc_consistent only returns 32bit addresses
2716 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
2717 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
2720 * We created a kernel buffer corresponding to the
2721 * user buffer. Now copy in from the user buffer
2723 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
2724 (u32
) (ioc
->sgl
[i
].iov_len
))) {
2730 if (ioc
->sense_len
) {
2731 sense
= dma_alloc_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
2732 &sense_handle
, GFP_KERNEL
);
2739 (u32
*) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
2740 *sense_ptr
= sense_handle
;
2744 * Set the sync_cmd flag so that the ISR knows not to complete this
2745 * cmd to the SCSI mid-layer
2748 megasas_issue_blocked_cmd(instance
, cmd
);
2752 * copy out the kernel buffers to user buffers
2754 for (i
= 0; i
< ioc
->sge_count
; i
++) {
2755 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
2756 ioc
->sgl
[i
].iov_len
)) {
2763 * copy out the sense
2765 if (ioc
->sense_len
) {
2767 * sense_ptr points to the location that has the user
2768 * sense buffer address
2770 sense_ptr
= (u32
*) ((unsigned long)ioc
->frame
.raw
+
2773 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
2774 sense
, ioc
->sense_len
)) {
2781 * copy the status codes returned by the fw
2783 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
2784 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
2785 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
2791 dma_free_coherent(&instance
->pdev
->dev
, ioc
->sense_len
,
2792 sense
, sense_handle
);
2795 for (i
= 0; i
< ioc
->sge_count
&& kbuff_arr
[i
]; i
++) {
2796 dma_free_coherent(&instance
->pdev
->dev
,
2797 kern_sge32
[i
].length
,
2798 kbuff_arr
[i
], kern_sge32
[i
].phys_addr
);
2801 megasas_return_cmd(instance
, cmd
);
2805 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
2809 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
2811 if ((megasas_mgmt_info
.instance
[i
]) &&
2812 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
2813 return megasas_mgmt_info
.instance
[i
];
2819 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
2821 struct megasas_iocpacket __user
*user_ioc
=
2822 (struct megasas_iocpacket __user
*)arg
;
2823 struct megasas_iocpacket
*ioc
;
2824 struct megasas_instance
*instance
;
2827 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
2831 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
2836 instance
= megasas_lookup_instance(ioc
->host_no
);
2843 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2845 if (down_interruptible(&instance
->ioctl_sem
)) {
2846 error
= -ERESTARTSYS
;
2849 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
2850 up(&instance
->ioctl_sem
);
2857 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
2859 struct megasas_instance
*instance
;
2860 struct megasas_aen aen
;
2863 if (file
->private_data
!= file
) {
2864 printk(KERN_DEBUG
"megasas: fasync_helper was not "
2869 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
2872 instance
= megasas_lookup_instance(aen
.host_no
);
2877 down(&instance
->aen_mutex
);
2878 error
= megasas_register_aen(instance
, aen
.seq_num
,
2879 aen
.class_locale_word
);
2880 up(&instance
->aen_mutex
);
2885 * megasas_mgmt_ioctl - char node ioctl entry point
2888 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2891 case MEGASAS_IOC_FIRMWARE
:
2892 return megasas_mgmt_ioctl_fw(file
, arg
);
2894 case MEGASAS_IOC_GET_AEN
:
2895 return megasas_mgmt_ioctl_aen(file
, arg
);
2901 #ifdef CONFIG_COMPAT
2902 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
2904 struct compat_megasas_iocpacket __user
*cioc
=
2905 (struct compat_megasas_iocpacket __user
*)arg
;
2906 struct megasas_iocpacket __user
*ioc
=
2907 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
2911 if (clear_user(ioc
, sizeof(*ioc
)))
2914 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
2915 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
2916 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
2917 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
2918 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
2919 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
2922 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
2925 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
2926 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
2927 copy_in_user(&ioc
->sgl
[i
].iov_len
,
2928 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
2932 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
2934 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
2935 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
2936 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
2943 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
2947 case MEGASAS_IOC_FIRMWARE32
:
2948 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
2949 case MEGASAS_IOC_GET_AEN
:
2950 return megasas_mgmt_ioctl_aen(file
, arg
);
2958 * File operations structure for management interface
2960 static const struct file_operations megasas_mgmt_fops
= {
2961 .owner
= THIS_MODULE
,
2962 .open
= megasas_mgmt_open
,
2963 .release
= megasas_mgmt_release
,
2964 .fasync
= megasas_mgmt_fasync
,
2965 .unlocked_ioctl
= megasas_mgmt_ioctl
,
2966 #ifdef CONFIG_COMPAT
2967 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
2972 * PCI hotplug support registration structure
2974 static struct pci_driver megasas_pci_driver
= {
2976 .name
= "megaraid_sas",
2977 .id_table
= megasas_pci_table
,
2978 .probe
= megasas_probe_one
,
2979 .remove
= __devexit_p(megasas_detach_one
),
2980 .shutdown
= megasas_shutdown
,
2984 * Sysfs driver attributes
2986 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
2988 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
2992 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
2995 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
2997 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
3001 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
3005 megasas_sysfs_show_dbg_lvl(struct device_driver
*dd
, char *buf
)
3007 return sprintf(buf
,"%u",megasas_dbg_lvl
);
3011 megasas_sysfs_set_dbg_lvl(struct device_driver
*dd
, const char *buf
, size_t count
)
3014 if(sscanf(buf
,"%u",&megasas_dbg_lvl
)<1){
3015 printk(KERN_ERR
"megasas: could not set dbg_lvl\n");
3021 static DRIVER_ATTR(dbg_lvl
, S_IRUGO
|S_IWUGO
, megasas_sysfs_show_dbg_lvl
,
3022 megasas_sysfs_set_dbg_lvl
);
3025 * megasas_init - Driver load entry point
3027 static int __init
megasas_init(void)
3032 * Announce driver version and other information
3034 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
3035 MEGASAS_EXT_VERSION
);
3037 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
3040 * Register character device node
3042 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
3045 printk(KERN_DEBUG
"megasas: failed to open device node\n");
3049 megasas_mgmt_majorno
= rval
;
3052 * Register ourselves as PCI hotplug module
3054 rval
= pci_register_driver(&megasas_pci_driver
);
3057 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
3061 rval
= driver_create_file(&megasas_pci_driver
.driver
,
3062 &driver_attr_version
);
3064 goto err_dcf_attr_ver
;
3065 rval
= driver_create_file(&megasas_pci_driver
.driver
,
3066 &driver_attr_release_date
);
3068 goto err_dcf_rel_date
;
3069 rval
= driver_create_file(&megasas_pci_driver
.driver
,
3070 &driver_attr_dbg_lvl
);
3072 goto err_dcf_dbg_lvl
;
3076 driver_remove_file(&megasas_pci_driver
.driver
,
3077 &driver_attr_release_date
);
3079 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
3081 pci_unregister_driver(&megasas_pci_driver
);
3083 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
3088 * megasas_exit - Driver unload entry point
3090 static void __exit
megasas_exit(void)
3092 driver_remove_file(&megasas_pci_driver
.driver
,
3093 &driver_attr_dbg_lvl
);
3094 driver_remove_file(&megasas_pci_driver
.driver
,
3095 &driver_attr_release_date
);
3096 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
3098 pci_unregister_driver(&megasas_pci_driver
);
3099 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
3102 module_init(megasas_init
);
3103 module_exit(megasas_exit
);