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.02.04
16 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
17 * Sumant Patro <Sumant.Patro@lsil.com>
19 * List of supported controllers
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/list.h>
29 #include <linux/moduleparam.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
34 #include <linux/uio.h>
35 #include <asm/uaccess.h>
37 #include <linux/compat.h>
38 #include <linux/mutex.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include "megaraid_sas.h"
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(MEGASAS_VERSION
);
48 MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
52 * PCI ID table for all supported controllers
54 static struct pci_device_id megasas_pci_table
[] = {
57 PCI_VENDOR_ID_LSI_LOGIC
,
58 PCI_DEVICE_ID_LSI_SAS1064R
, // xscale IOP
63 PCI_VENDOR_ID_LSI_LOGIC
,
64 PCI_DEVICE_ID_LSI_SAS1078R
, // ppc IOP
70 PCI_DEVICE_ID_DELL_PERC5
, // xscale IOP
74 {0} /* Terminating entry */
77 MODULE_DEVICE_TABLE(pci
, megasas_pci_table
);
79 static int megasas_mgmt_majorno
;
80 static struct megasas_mgmt_info megasas_mgmt_info
;
81 static struct fasync_struct
*megasas_async_queue
;
82 static DEFINE_MUTEX(megasas_async_queue_mutex
);
85 * megasas_get_cmd - Get a command from the free pool
86 * @instance: Adapter soft state
88 * Returns a free command from the pool
90 static struct megasas_cmd
*megasas_get_cmd(struct megasas_instance
94 struct megasas_cmd
*cmd
= NULL
;
96 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
98 if (!list_empty(&instance
->cmd_pool
)) {
99 cmd
= list_entry((&instance
->cmd_pool
)->next
,
100 struct megasas_cmd
, list
);
101 list_del_init(&cmd
->list
);
103 printk(KERN_ERR
"megasas: Command pool empty!\n");
106 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
111 * megasas_return_cmd - Return a cmd to free command pool
112 * @instance: Adapter soft state
113 * @cmd: Command packet to be returned to free command pool
116 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
120 spin_lock_irqsave(&instance
->cmd_pool_lock
, flags
);
123 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
125 spin_unlock_irqrestore(&instance
->cmd_pool_lock
, flags
);
130 * The following functions are defined for xscale
131 * (deviceid : 1064R, PERC5) controllers
135 * megasas_enable_intr_xscale - Enables interrupts
136 * @regs: MFI register set
139 megasas_enable_intr_xscale(struct megasas_register_set __iomem
* regs
)
141 writel(1, &(regs
)->outbound_intr_mask
);
143 /* Dummy readl to force pci flush */
144 readl(®s
->outbound_intr_mask
);
148 * megasas_read_fw_status_reg_xscale - returns the current FW status value
149 * @regs: MFI register set
152 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem
* regs
)
154 return readl(&(regs
)->outbound_msg_0
);
157 * megasas_clear_interrupt_xscale - Check & clear interrupt
158 * @regs: MFI register set
161 megasas_clear_intr_xscale(struct megasas_register_set __iomem
* regs
)
165 * Check if it is our interrupt
167 status
= readl(®s
->outbound_intr_status
);
169 if (!(status
& MFI_OB_INTR_STATUS_MASK
)) {
174 * Clear the interrupt by writing back the same value
176 writel(status
, ®s
->outbound_intr_status
);
182 * megasas_fire_cmd_xscale - Sends command to the FW
183 * @frame_phys_addr : Physical address of cmd
184 * @frame_count : Number of frames for the command
185 * @regs : MFI register set
188 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr
,u32 frame_count
, struct megasas_register_set __iomem
*regs
)
190 writel((frame_phys_addr
>> 3)|(frame_count
),
191 &(regs
)->inbound_queue_port
);
194 static struct megasas_instance_template megasas_instance_template_xscale
= {
196 .fire_cmd
= megasas_fire_cmd_xscale
,
197 .enable_intr
= megasas_enable_intr_xscale
,
198 .clear_intr
= megasas_clear_intr_xscale
,
199 .read_fw_status_reg
= megasas_read_fw_status_reg_xscale
,
203 * This is the end of set of functions & definitions specific
204 * to xscale (deviceid : 1064R, PERC5) controllers
208 * The following functions are defined for ppc (deviceid : 0x60)
213 * megasas_enable_intr_ppc - Enables interrupts
214 * @regs: MFI register set
217 megasas_enable_intr_ppc(struct megasas_register_set __iomem
* regs
)
219 writel(0xFFFFFFFF, &(regs
)->outbound_doorbell_clear
);
221 writel(~0x80000004, &(regs
)->outbound_intr_mask
);
223 /* Dummy readl to force pci flush */
224 readl(®s
->outbound_intr_mask
);
228 * megasas_read_fw_status_reg_ppc - returns the current FW status value
229 * @regs: MFI register set
232 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem
* regs
)
234 return readl(&(regs
)->outbound_scratch_pad
);
238 * megasas_clear_interrupt_ppc - Check & clear interrupt
239 * @regs: MFI register set
242 megasas_clear_intr_ppc(struct megasas_register_set __iomem
* regs
)
246 * Check if it is our interrupt
248 status
= readl(®s
->outbound_intr_status
);
250 if (!(status
& MFI_REPLY_1078_MESSAGE_INTERRUPT
)) {
255 * Clear the interrupt by writing back the same value
257 writel(status
, ®s
->outbound_doorbell_clear
);
262 * megasas_fire_cmd_ppc - Sends command to the FW
263 * @frame_phys_addr : Physical address of cmd
264 * @frame_count : Number of frames for the command
265 * @regs : MFI register set
268 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr
, u32 frame_count
, struct megasas_register_set __iomem
*regs
)
270 writel((frame_phys_addr
| (frame_count
<<1))|1,
271 &(regs
)->inbound_queue_port
);
274 static struct megasas_instance_template megasas_instance_template_ppc
= {
276 .fire_cmd
= megasas_fire_cmd_ppc
,
277 .enable_intr
= megasas_enable_intr_ppc
,
278 .clear_intr
= megasas_clear_intr_ppc
,
279 .read_fw_status_reg
= megasas_read_fw_status_reg_ppc
,
283 * This is the end of set of functions & definitions
284 * specific to ppc (deviceid : 0x60) controllers
288 * megasas_disable_intr - Disables interrupts
289 * @regs: MFI register set
292 megasas_disable_intr(struct megasas_register_set __iomem
* regs
)
295 writel(mask
, ®s
->outbound_intr_mask
);
297 /* Dummy readl to force pci flush */
298 readl(®s
->outbound_intr_mask
);
302 * megasas_issue_polled - Issues a polling command
303 * @instance: Adapter soft state
304 * @cmd: Command packet to be issued
306 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
309 megasas_issue_polled(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
312 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
314 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
316 frame_hdr
->cmd_status
= 0xFF;
317 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
320 * Issue the frame using inbound queue port
322 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
325 * Wait for cmd_status to change
327 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
++) {
332 if (frame_hdr
->cmd_status
== 0xff)
339 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
340 * @instance: Adapter soft state
341 * @cmd: Command to be issued
343 * This function waits on an event for the command to be returned from ISR.
344 * Used to issue ioctl commands.
347 megasas_issue_blocked_cmd(struct megasas_instance
*instance
,
348 struct megasas_cmd
*cmd
)
350 cmd
->cmd_status
= ENODATA
;
352 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
354 wait_event(instance
->int_cmd_wait_q
, (cmd
->cmd_status
!= ENODATA
));
360 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
361 * @instance: Adapter soft state
362 * @cmd_to_abort: Previously issued cmd to be aborted
364 * MFI firmware can abort previously issued AEN comamnd (automatic event
365 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
366 * cmd and blocks till it is completed.
369 megasas_issue_blocked_abort_cmd(struct megasas_instance
*instance
,
370 struct megasas_cmd
*cmd_to_abort
)
372 struct megasas_cmd
*cmd
;
373 struct megasas_abort_frame
*abort_fr
;
375 cmd
= megasas_get_cmd(instance
);
380 abort_fr
= &cmd
->frame
->abort
;
383 * Prepare and issue the abort frame
385 abort_fr
->cmd
= MFI_CMD_ABORT
;
386 abort_fr
->cmd_status
= 0xFF;
388 abort_fr
->abort_context
= cmd_to_abort
->index
;
389 abort_fr
->abort_mfi_phys_addr_lo
= cmd_to_abort
->frame_phys_addr
;
390 abort_fr
->abort_mfi_phys_addr_hi
= 0;
393 cmd
->cmd_status
= 0xFF;
395 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
398 * Wait for this cmd to complete
400 wait_event(instance
->abort_cmd_wait_q
, (cmd
->cmd_status
!= 0xFF));
402 megasas_return_cmd(instance
, cmd
);
407 * megasas_make_sgl32 - Prepares 32-bit SGL
408 * @instance: Adapter soft state
409 * @scp: SCSI command from the mid-layer
410 * @mfi_sgl: SGL to be filled in
412 * If successful, this function returns the number of SG elements. Otherwise,
416 megasas_make_sgl32(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
417 union megasas_sgl
*mfi_sgl
)
421 struct scatterlist
*os_sgl
;
424 * Return 0 if there is no data transfer
426 if (!scp
->request_buffer
|| !scp
->request_bufflen
)
430 mfi_sgl
->sge32
[0].phys_addr
= pci_map_single(instance
->pdev
,
437 mfi_sgl
->sge32
[0].length
= scp
->request_bufflen
;
442 os_sgl
= (struct scatterlist
*)scp
->request_buffer
;
443 sge_count
= pci_map_sg(instance
->pdev
, os_sgl
, scp
->use_sg
,
444 scp
->sc_data_direction
);
446 for (i
= 0; i
< sge_count
; i
++, os_sgl
++) {
447 mfi_sgl
->sge32
[i
].length
= sg_dma_len(os_sgl
);
448 mfi_sgl
->sge32
[i
].phys_addr
= sg_dma_address(os_sgl
);
455 * megasas_make_sgl64 - Prepares 64-bit SGL
456 * @instance: Adapter soft state
457 * @scp: SCSI command from the mid-layer
458 * @mfi_sgl: SGL to be filled in
460 * If successful, this function returns the number of SG elements. Otherwise,
464 megasas_make_sgl64(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
465 union megasas_sgl
*mfi_sgl
)
469 struct scatterlist
*os_sgl
;
472 * Return 0 if there is no data transfer
474 if (!scp
->request_buffer
|| !scp
->request_bufflen
)
478 mfi_sgl
->sge64
[0].phys_addr
= pci_map_single(instance
->pdev
,
486 mfi_sgl
->sge64
[0].length
= scp
->request_bufflen
;
491 os_sgl
= (struct scatterlist
*)scp
->request_buffer
;
492 sge_count
= pci_map_sg(instance
->pdev
, os_sgl
, scp
->use_sg
,
493 scp
->sc_data_direction
);
495 for (i
= 0; i
< sge_count
; i
++, os_sgl
++) {
496 mfi_sgl
->sge64
[i
].length
= sg_dma_len(os_sgl
);
497 mfi_sgl
->sge64
[i
].phys_addr
= sg_dma_address(os_sgl
);
504 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
505 * @instance: Adapter soft state
507 * @cmd: Command to be prepared in
509 * This function prepares CDB commands. These are typcially pass-through
510 * commands to the devices.
513 megasas_build_dcdb(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
514 struct megasas_cmd
*cmd
)
521 struct megasas_pthru_frame
*pthru
;
523 is_logical
= MEGASAS_IS_LOGICAL(scp
);
524 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
525 pthru
= (struct megasas_pthru_frame
*)cmd
->frame
;
527 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
528 flags
= MFI_FRAME_DIR_WRITE
;
529 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
530 flags
= MFI_FRAME_DIR_READ
;
531 else if (scp
->sc_data_direction
== PCI_DMA_NONE
)
532 flags
= MFI_FRAME_DIR_NONE
;
535 * Prepare the DCDB frame
537 pthru
->cmd
= (is_logical
) ? MFI_CMD_LD_SCSI_IO
: MFI_CMD_PD_SCSI_IO
;
538 pthru
->cmd_status
= 0x0;
539 pthru
->scsi_status
= 0x0;
540 pthru
->target_id
= device_id
;
541 pthru
->lun
= scp
->device
->lun
;
542 pthru
->cdb_len
= scp
->cmd_len
;
544 pthru
->flags
= flags
;
545 pthru
->data_xfer_len
= scp
->request_bufflen
;
547 memcpy(pthru
->cdb
, scp
->cmnd
, scp
->cmd_len
);
552 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
553 sizeof(struct megasas_sge32
);
556 pthru
->flags
|= MFI_FRAME_SGL64
;
557 pthru
->sge_count
= megasas_make_sgl64(instance
, scp
,
560 pthru
->sge_count
= megasas_make_sgl32(instance
, scp
,
564 * Sense info specific
566 pthru
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
567 pthru
->sense_buf_phys_addr_hi
= 0;
568 pthru
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
570 sge_bytes
= sge_sz
* pthru
->sge_count
;
573 * Compute the total number of frames this command consumes. FW uses
574 * this number to pull sufficient number of frames from host memory.
576 cmd
->frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
577 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) + 1;
579 if (cmd
->frame_count
> 7)
580 cmd
->frame_count
= 8;
582 return cmd
->frame_count
;
586 * megasas_build_ldio - Prepares IOs to logical devices
587 * @instance: Adapter soft state
589 * @cmd: Command to to be prepared
591 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
594 megasas_build_ldio(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
595 struct megasas_cmd
*cmd
)
600 u8 sc
= scp
->cmnd
[0];
602 struct megasas_io_frame
*ldio
;
604 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
605 ldio
= (struct megasas_io_frame
*)cmd
->frame
;
607 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
608 flags
= MFI_FRAME_DIR_WRITE
;
609 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
610 flags
= MFI_FRAME_DIR_READ
;
613 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
615 ldio
->cmd
= (sc
& 0x02) ? MFI_CMD_LD_WRITE
: MFI_CMD_LD_READ
;
616 ldio
->cmd_status
= 0x0;
617 ldio
->scsi_status
= 0x0;
618 ldio
->target_id
= device_id
;
620 ldio
->reserved_0
= 0;
623 ldio
->start_lba_hi
= 0;
624 ldio
->access_byte
= (scp
->cmd_len
!= 6) ? scp
->cmnd
[1] : 0;
627 * 6-byte READ(0x08) or WRITE(0x0A) cdb
629 if (scp
->cmd_len
== 6) {
630 ldio
->lba_count
= (u32
) scp
->cmnd
[4];
631 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
632 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
634 ldio
->start_lba_lo
&= 0x1FFFFF;
638 * 10-byte READ(0x28) or WRITE(0x2A) cdb
640 else if (scp
->cmd_len
== 10) {
641 ldio
->lba_count
= (u32
) scp
->cmnd
[8] |
642 ((u32
) scp
->cmnd
[7] << 8);
643 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
644 ((u32
) scp
->cmnd
[3] << 16) |
645 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
649 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
651 else if (scp
->cmd_len
== 12) {
652 ldio
->lba_count
= ((u32
) scp
->cmnd
[6] << 24) |
653 ((u32
) scp
->cmnd
[7] << 16) |
654 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
656 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
657 ((u32
) scp
->cmnd
[3] << 16) |
658 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
662 * 16-byte READ(0x88) or WRITE(0x8A) cdb
664 else if (scp
->cmd_len
== 16) {
665 ldio
->lba_count
= ((u32
) scp
->cmnd
[10] << 24) |
666 ((u32
) scp
->cmnd
[11] << 16) |
667 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
669 ldio
->start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
670 ((u32
) scp
->cmnd
[7] << 16) |
671 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
673 ldio
->start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
674 ((u32
) scp
->cmnd
[3] << 16) |
675 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
682 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
683 sizeof(struct megasas_sge32
);
686 ldio
->flags
|= MFI_FRAME_SGL64
;
687 ldio
->sge_count
= megasas_make_sgl64(instance
, scp
, &ldio
->sgl
);
689 ldio
->sge_count
= megasas_make_sgl32(instance
, scp
, &ldio
->sgl
);
692 * Sense info specific
694 ldio
->sense_len
= SCSI_SENSE_BUFFERSIZE
;
695 ldio
->sense_buf_phys_addr_hi
= 0;
696 ldio
->sense_buf_phys_addr_lo
= cmd
->sense_phys_addr
;
698 sge_bytes
= sge_sz
* ldio
->sge_count
;
700 cmd
->frame_count
= (sge_bytes
/ MEGAMFI_FRAME_SIZE
) +
701 ((sge_bytes
% MEGAMFI_FRAME_SIZE
) ? 1 : 0) + 1;
703 if (cmd
->frame_count
> 7)
704 cmd
->frame_count
= 8;
706 return cmd
->frame_count
;
710 * megasas_is_ldio - Checks if the cmd is for logical drive
711 * @scmd: SCSI command
713 * Called by megasas_queue_command to find out if the command to be queued
714 * is a logical drive command
716 static inline int megasas_is_ldio(struct scsi_cmnd
*cmd
)
718 if (!MEGASAS_IS_LOGICAL(cmd
))
720 switch (cmd
->cmnd
[0]) {
736 * megasas_queue_command - Queue entry point
737 * @scmd: SCSI command to be queued
738 * @done: Callback entry point
741 megasas_queue_command(struct scsi_cmnd
*scmd
, void (*done
) (struct scsi_cmnd
*))
745 struct megasas_cmd
*cmd
;
746 struct megasas_instance
*instance
;
748 instance
= (struct megasas_instance
*)
749 scmd
->device
->host
->hostdata
;
750 scmd
->scsi_done
= done
;
753 if (MEGASAS_IS_LOGICAL(scmd
) &&
754 (scmd
->device
->id
>= MEGASAS_MAX_LD
|| scmd
->device
->lun
)) {
755 scmd
->result
= DID_BAD_TARGET
<< 16;
759 cmd
= megasas_get_cmd(instance
);
761 return SCSI_MLQUEUE_HOST_BUSY
;
764 * Logical drive command
766 if (megasas_is_ldio(scmd
))
767 frame_count
= megasas_build_ldio(instance
, scmd
, cmd
);
769 frame_count
= megasas_build_dcdb(instance
, scmd
, cmd
);
775 scmd
->SCp
.ptr
= (char *)cmd
;
776 scmd
->SCp
.sent_command
= jiffies
;
779 * Issue the command to the FW
781 spin_lock_irqsave(&instance
->instance_lock
, flags
);
782 instance
->fw_outstanding
++;
783 spin_unlock_irqrestore(&instance
->instance_lock
, flags
);
785 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,cmd
->frame_count
-1,instance
->reg_set
);
790 megasas_return_cmd(instance
, cmd
);
796 static int megasas_slave_configure(struct scsi_device
*sdev
)
799 * Don't export physical disk devices to the disk driver.
801 * FIXME: Currently we don't export them to the midlayer at all.
802 * That will be fixed once LSI engineers have audited the
803 * firmware for possible issues.
805 if (sdev
->channel
< MEGASAS_MAX_PD_CHANNELS
&& sdev
->type
== TYPE_DISK
)
811 * megasas_wait_for_outstanding - Wait for all outstanding cmds
812 * @instance: Adapter soft state
814 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
815 * complete all its outstanding commands. Returns error if one or more IOs
816 * are pending after this time period. It also marks the controller dead.
818 static int megasas_wait_for_outstanding(struct megasas_instance
*instance
)
821 u32 wait_time
= MEGASAS_RESET_WAIT_TIME
;
823 for (i
= 0; i
< wait_time
; i
++) {
825 if (!instance
->fw_outstanding
)
828 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
829 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
830 "commands to complete\n", i
,
831 instance
->fw_outstanding
);
837 if (instance
->fw_outstanding
) {
838 instance
->hw_crit_error
= 1;
846 * megasas_generic_reset - Generic reset routine
847 * @scmd: Mid-layer SCSI command
849 * This routine implements a generic reset handler for device, bus and host
850 * reset requests. Device, bus and host specific reset handlers can use this
851 * function after they do their specific tasks.
853 static int megasas_generic_reset(struct scsi_cmnd
*scmd
)
856 struct megasas_instance
*instance
;
858 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
860 scmd_printk(KERN_NOTICE
, scmd
, "megasas: RESET -%ld cmd=%x\n",
861 scmd
->serial_number
, scmd
->cmnd
[0]);
863 if (instance
->hw_crit_error
) {
864 printk(KERN_ERR
"megasas: cannot recover from previous reset "
869 ret_val
= megasas_wait_for_outstanding(instance
);
870 if (ret_val
== SUCCESS
)
871 printk(KERN_NOTICE
"megasas: reset successful \n");
873 printk(KERN_ERR
"megasas: failed to do reset\n");
878 static enum scsi_eh_timer_return
megasas_reset_timer(struct scsi_cmnd
*scmd
)
880 unsigned long seconds
;
883 seconds
= (jiffies
- scmd
->SCp
.sent_command
) / HZ
;
886 return EH_RESET_TIMER
;
888 return EH_NOT_HANDLED
;
896 * megasas_reset_device - Device reset handler entry point
898 static int megasas_reset_device(struct scsi_cmnd
*scmd
)
903 * First wait for all commands to complete
905 ret
= megasas_generic_reset(scmd
);
911 * megasas_reset_bus_host - Bus & host reset handler entry point
913 static int megasas_reset_bus_host(struct scsi_cmnd
*scmd
)
918 * Frist wait for all commands to complete
920 ret
= megasas_generic_reset(scmd
);
926 * megasas_service_aen - Processes an event notification
927 * @instance: Adapter soft state
928 * @cmd: AEN command completed by the ISR
930 * For AEN, driver sends a command down to FW that is held by the FW till an
931 * event occurs. When an event of interest occurs, FW completes the command
932 * that it was previously holding.
934 * This routines sends SIGIO signal to processes that have registered with the
938 megasas_service_aen(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
941 * Don't signal app if it is just an aborted previously registered aen
944 kill_fasync(&megasas_async_queue
, SIGIO
, POLL_IN
);
948 instance
->aen_cmd
= NULL
;
949 megasas_return_cmd(instance
, cmd
);
953 * Scsi host template for megaraid_sas driver
955 static struct scsi_host_template megasas_template
= {
957 .module
= THIS_MODULE
,
958 .name
= "LSI Logic SAS based MegaRAID driver",
959 .proc_name
= "megaraid_sas",
960 .slave_configure
= megasas_slave_configure
,
961 .queuecommand
= megasas_queue_command
,
962 .eh_device_reset_handler
= megasas_reset_device
,
963 .eh_bus_reset_handler
= megasas_reset_bus_host
,
964 .eh_host_reset_handler
= megasas_reset_bus_host
,
965 .eh_timed_out
= megasas_reset_timer
,
966 .use_clustering
= ENABLE_CLUSTERING
,
970 * megasas_complete_int_cmd - Completes an internal command
971 * @instance: Adapter soft state
972 * @cmd: Command to be completed
974 * The megasas_issue_blocked_cmd() function waits for a command to complete
975 * after it issues a command. This function wakes up that waiting routine by
976 * calling wake_up() on the wait queue.
979 megasas_complete_int_cmd(struct megasas_instance
*instance
,
980 struct megasas_cmd
*cmd
)
982 cmd
->cmd_status
= cmd
->frame
->io
.cmd_status
;
984 if (cmd
->cmd_status
== ENODATA
) {
987 wake_up(&instance
->int_cmd_wait_q
);
991 * megasas_complete_abort - Completes aborting a command
992 * @instance: Adapter soft state
993 * @cmd: Cmd that was issued to abort another cmd
995 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
996 * after it issues an abort on a previously issued command. This function
997 * wakes up all functions waiting on the same wait queue.
1000 megasas_complete_abort(struct megasas_instance
*instance
,
1001 struct megasas_cmd
*cmd
)
1003 if (cmd
->sync_cmd
) {
1005 cmd
->cmd_status
= 0;
1006 wake_up(&instance
->abort_cmd_wait_q
);
1013 * megasas_unmap_sgbuf - Unmap SG buffers
1014 * @instance: Adapter soft state
1015 * @cmd: Completed command
1018 megasas_unmap_sgbuf(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
1023 if (cmd
->scmd
->use_sg
) {
1024 pci_unmap_sg(instance
->pdev
, cmd
->scmd
->request_buffer
,
1025 cmd
->scmd
->use_sg
, cmd
->scmd
->sc_data_direction
);
1029 if (!cmd
->scmd
->request_bufflen
)
1032 opcode
= cmd
->frame
->hdr
.cmd
;
1034 if ((opcode
== MFI_CMD_LD_READ
) || (opcode
== MFI_CMD_LD_WRITE
)) {
1036 buf_h
= cmd
->frame
->io
.sgl
.sge64
[0].phys_addr
;
1038 buf_h
= cmd
->frame
->io
.sgl
.sge32
[0].phys_addr
;
1041 buf_h
= cmd
->frame
->pthru
.sgl
.sge64
[0].phys_addr
;
1043 buf_h
= cmd
->frame
->pthru
.sgl
.sge32
[0].phys_addr
;
1046 pci_unmap_single(instance
->pdev
, buf_h
, cmd
->scmd
->request_bufflen
,
1047 cmd
->scmd
->sc_data_direction
);
1052 * megasas_complete_cmd - Completes a command
1053 * @instance: Adapter soft state
1054 * @cmd: Command to be completed
1055 * @alt_status: If non-zero, use this value as status to
1056 * SCSI mid-layer instead of the value returned
1057 * by the FW. This should be used if caller wants
1058 * an alternate status (as in the case of aborted
1062 megasas_complete_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
1066 struct megasas_header
*hdr
= &cmd
->frame
->hdr
;
1067 unsigned long flags
;
1070 cmd
->scmd
->SCp
.ptr
= (char *)0;
1075 case MFI_CMD_PD_SCSI_IO
:
1076 case MFI_CMD_LD_SCSI_IO
:
1079 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1080 * issued either through an IO path or an IOCTL path. If it
1081 * was via IOCTL, we will send it to internal completion.
1083 if (cmd
->sync_cmd
) {
1085 megasas_complete_int_cmd(instance
, cmd
);
1089 case MFI_CMD_LD_READ
:
1090 case MFI_CMD_LD_WRITE
:
1093 cmd
->scmd
->result
= alt_status
<< 16;
1099 spin_lock_irqsave(&instance
->instance_lock
, flags
);
1100 instance
->fw_outstanding
--;
1101 spin_unlock_irqrestore(&instance
->instance_lock
, flags
);
1103 megasas_unmap_sgbuf(instance
, cmd
);
1104 cmd
->scmd
->scsi_done(cmd
->scmd
);
1105 megasas_return_cmd(instance
, cmd
);
1110 switch (hdr
->cmd_status
) {
1113 cmd
->scmd
->result
= DID_OK
<< 16;
1116 case MFI_STAT_SCSI_IO_FAILED
:
1117 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1119 (DID_ERROR
<< 16) | hdr
->scsi_status
;
1122 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1124 cmd
->scmd
->result
= (DID_OK
<< 16) | hdr
->scsi_status
;
1126 if (hdr
->scsi_status
== SAM_STAT_CHECK_CONDITION
) {
1127 memset(cmd
->scmd
->sense_buffer
, 0,
1128 SCSI_SENSE_BUFFERSIZE
);
1129 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1132 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1137 case MFI_STAT_LD_OFFLINE
:
1138 case MFI_STAT_DEVICE_NOT_FOUND
:
1139 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1143 printk(KERN_DEBUG
"megasas: MFI FW status %#x\n",
1145 cmd
->scmd
->result
= DID_ERROR
<< 16;
1149 spin_lock_irqsave(&instance
->instance_lock
, flags
);
1150 instance
->fw_outstanding
--;
1151 spin_unlock_irqrestore(&instance
->instance_lock
, flags
);
1153 megasas_unmap_sgbuf(instance
, cmd
);
1154 cmd
->scmd
->scsi_done(cmd
->scmd
);
1155 megasas_return_cmd(instance
, cmd
);
1164 * See if got an event notification
1166 if (cmd
->frame
->dcmd
.opcode
== MR_DCMD_CTRL_EVENT_WAIT
)
1167 megasas_service_aen(instance
, cmd
);
1169 megasas_complete_int_cmd(instance
, cmd
);
1175 * Cmd issued to abort another cmd returned
1177 megasas_complete_abort(instance
, cmd
);
1181 printk("megasas: Unknown command completed! [0x%X]\n",
1188 * megasas_deplete_reply_queue - Processes all completed commands
1189 * @instance: Adapter soft state
1190 * @alt_status: Alternate status to be returned to
1191 * SCSI mid-layer instead of the status
1192 * returned by the FW
1195 megasas_deplete_reply_queue(struct megasas_instance
*instance
, u8 alt_status
)
1200 struct megasas_cmd
*cmd
;
1203 * Check if it is our interrupt
1204 * Clear the interrupt
1206 if(instance
->instancet
->clear_intr(instance
->reg_set
))
1209 producer
= *instance
->producer
;
1210 consumer
= *instance
->consumer
;
1212 while (consumer
!= producer
) {
1213 context
= instance
->reply_queue
[consumer
];
1215 cmd
= instance
->cmd_list
[context
];
1217 megasas_complete_cmd(instance
, cmd
, alt_status
);
1220 if (consumer
== (instance
->max_fw_cmds
+ 1)) {
1225 *instance
->consumer
= producer
;
1231 * megasas_isr - isr entry point
1233 static irqreturn_t
megasas_isr(int irq
, void *devp
, struct pt_regs
*regs
)
1235 return megasas_deplete_reply_queue((struct megasas_instance
*)devp
,
1240 * megasas_transition_to_ready - Move the FW to READY state
1241 * @instance: Adapter soft state
1243 * During the initialization, FW passes can potentially be in any one of
1244 * several possible states. If the FW in operational, waiting-for-handshake
1245 * states, driver must take steps to bring it to ready state. Otherwise, it
1246 * has to wait for the ready state.
1249 megasas_transition_to_ready(struct megasas_instance
* instance
)
1256 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) & MFI_STATE_MASK
;
1258 while (fw_state
!= MFI_STATE_READY
) {
1260 printk(KERN_INFO
"megasas: Waiting for FW to come to ready"
1264 case MFI_STATE_FAULT
:
1266 printk(KERN_DEBUG
"megasas: FW in FAULT state!!\n");
1269 case MFI_STATE_WAIT_HANDSHAKE
:
1271 * Set the CLR bit in inbound doorbell
1273 writel(MFI_INIT_CLEAR_HANDSHAKE
,
1274 &instance
->reg_set
->inbound_doorbell
);
1277 cur_state
= MFI_STATE_WAIT_HANDSHAKE
;
1280 case MFI_STATE_OPERATIONAL
:
1282 * Bring it to READY state; assuming max wait 2 secs
1284 megasas_disable_intr(instance
->reg_set
);
1285 writel(MFI_INIT_READY
, &instance
->reg_set
->inbound_doorbell
);
1288 cur_state
= MFI_STATE_OPERATIONAL
;
1291 case MFI_STATE_UNDEFINED
:
1293 * This state should not last for more than 2 seconds
1296 cur_state
= MFI_STATE_UNDEFINED
;
1299 case MFI_STATE_BB_INIT
:
1301 cur_state
= MFI_STATE_BB_INIT
;
1304 case MFI_STATE_FW_INIT
:
1306 cur_state
= MFI_STATE_FW_INIT
;
1309 case MFI_STATE_FW_INIT_2
:
1311 cur_state
= MFI_STATE_FW_INIT_2
;
1314 case MFI_STATE_DEVICE_SCAN
:
1316 cur_state
= MFI_STATE_DEVICE_SCAN
;
1319 case MFI_STATE_FLUSH_CACHE
:
1321 cur_state
= MFI_STATE_FLUSH_CACHE
;
1325 printk(KERN_DEBUG
"megasas: Unknown state 0x%x\n",
1331 * The cur_state should not last for more than max_wait secs
1333 for (i
= 0; i
< (max_wait
* 1000); i
++) {
1334 fw_state
= instance
->instancet
->read_fw_status_reg(instance
->reg_set
) &
1337 if (fw_state
== cur_state
) {
1344 * Return error if fw_state hasn't changed after max_wait
1346 if (fw_state
== cur_state
) {
1347 printk(KERN_DEBUG
"FW state [%d] hasn't changed "
1348 "in %d secs\n", fw_state
, max_wait
);
1357 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1358 * @instance: Adapter soft state
1360 static void megasas_teardown_frame_pool(struct megasas_instance
*instance
)
1363 u32 max_cmd
= instance
->max_fw_cmds
;
1364 struct megasas_cmd
*cmd
;
1366 if (!instance
->frame_dma_pool
)
1370 * Return all frames to pool
1372 for (i
= 0; i
< max_cmd
; i
++) {
1374 cmd
= instance
->cmd_list
[i
];
1377 pci_pool_free(instance
->frame_dma_pool
, cmd
->frame
,
1378 cmd
->frame_phys_addr
);
1381 pci_pool_free(instance
->sense_dma_pool
, cmd
->frame
,
1382 cmd
->sense_phys_addr
);
1386 * Now destroy the pool itself
1388 pci_pool_destroy(instance
->frame_dma_pool
);
1389 pci_pool_destroy(instance
->sense_dma_pool
);
1391 instance
->frame_dma_pool
= NULL
;
1392 instance
->sense_dma_pool
= NULL
;
1396 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1397 * @instance: Adapter soft state
1399 * Each command packet has an embedded DMA memory buffer that is used for
1400 * filling MFI frame and the SG list that immediately follows the frame. This
1401 * function creates those DMA memory buffers for each command packet by using
1402 * PCI pool facility.
1404 static int megasas_create_frame_pool(struct megasas_instance
*instance
)
1412 struct megasas_cmd
*cmd
;
1414 max_cmd
= instance
->max_fw_cmds
;
1417 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1418 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1420 sge_sz
= (IS_DMA64
) ? sizeof(struct megasas_sge64
) :
1421 sizeof(struct megasas_sge32
);
1424 * Calculated the number of 64byte frames required for SGL
1426 sgl_sz
= sge_sz
* instance
->max_num_sge
;
1427 frame_count
= (sgl_sz
+ MEGAMFI_FRAME_SIZE
- 1) / MEGAMFI_FRAME_SIZE
;
1430 * We need one extra frame for the MFI command
1434 total_sz
= MEGAMFI_FRAME_SIZE
* frame_count
;
1436 * Use DMA pool facility provided by PCI layer
1438 instance
->frame_dma_pool
= pci_pool_create("megasas frame pool",
1439 instance
->pdev
, total_sz
, 64,
1442 if (!instance
->frame_dma_pool
) {
1443 printk(KERN_DEBUG
"megasas: failed to setup frame pool\n");
1447 instance
->sense_dma_pool
= pci_pool_create("megasas sense pool",
1448 instance
->pdev
, 128, 4, 0);
1450 if (!instance
->sense_dma_pool
) {
1451 printk(KERN_DEBUG
"megasas: failed to setup sense pool\n");
1453 pci_pool_destroy(instance
->frame_dma_pool
);
1454 instance
->frame_dma_pool
= NULL
;
1460 * Allocate and attach a frame to each of the commands in cmd_list.
1461 * By making cmd->index as the context instead of the &cmd, we can
1462 * always use 32bit context regardless of the architecture
1464 for (i
= 0; i
< max_cmd
; i
++) {
1466 cmd
= instance
->cmd_list
[i
];
1468 cmd
->frame
= pci_pool_alloc(instance
->frame_dma_pool
,
1469 GFP_KERNEL
, &cmd
->frame_phys_addr
);
1471 cmd
->sense
= pci_pool_alloc(instance
->sense_dma_pool
,
1472 GFP_KERNEL
, &cmd
->sense_phys_addr
);
1475 * megasas_teardown_frame_pool() takes care of freeing
1476 * whatever has been allocated
1478 if (!cmd
->frame
|| !cmd
->sense
) {
1479 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed \n");
1480 megasas_teardown_frame_pool(instance
);
1484 cmd
->frame
->io
.context
= cmd
->index
;
1491 * megasas_free_cmds - Free all the cmds in the free cmd pool
1492 * @instance: Adapter soft state
1494 static void megasas_free_cmds(struct megasas_instance
*instance
)
1497 /* First free the MFI frame pool */
1498 megasas_teardown_frame_pool(instance
);
1500 /* Free all the commands in the cmd_list */
1501 for (i
= 0; i
< instance
->max_fw_cmds
; i
++)
1502 kfree(instance
->cmd_list
[i
]);
1504 /* Free the cmd_list buffer itself */
1505 kfree(instance
->cmd_list
);
1506 instance
->cmd_list
= NULL
;
1508 INIT_LIST_HEAD(&instance
->cmd_pool
);
1512 * megasas_alloc_cmds - Allocates the command packets
1513 * @instance: Adapter soft state
1515 * Each command that is issued to the FW, whether IO commands from the OS or
1516 * internal commands like IOCTLs, are wrapped in local data structure called
1517 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1520 * Each frame has a 32-bit field called context (tag). This context is used
1521 * to get back the megasas_cmd from the frame when a frame gets completed in
1522 * the ISR. Typically the address of the megasas_cmd itself would be used as
1523 * the context. But we wanted to keep the differences between 32 and 64 bit
1524 * systems to the mininum. We always use 32 bit integers for the context. In
1525 * this driver, the 32 bit values are the indices into an array cmd_list.
1526 * This array is used only to look up the megasas_cmd given the context. The
1527 * free commands themselves are maintained in a linked list called cmd_pool.
1529 static int megasas_alloc_cmds(struct megasas_instance
*instance
)
1534 struct megasas_cmd
*cmd
;
1536 max_cmd
= instance
->max_fw_cmds
;
1539 * instance->cmd_list is an array of struct megasas_cmd pointers.
1540 * Allocate the dynamic array first and then allocate individual
1543 instance
->cmd_list
= kmalloc(sizeof(struct megasas_cmd
*) * max_cmd
,
1546 if (!instance
->cmd_list
) {
1547 printk(KERN_DEBUG
"megasas: out of memory\n");
1551 memset(instance
->cmd_list
, 0, sizeof(struct megasas_cmd
*) * max_cmd
);
1553 for (i
= 0; i
< max_cmd
; i
++) {
1554 instance
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd
),
1557 if (!instance
->cmd_list
[i
]) {
1559 for (j
= 0; j
< i
; j
++)
1560 kfree(instance
->cmd_list
[j
]);
1562 kfree(instance
->cmd_list
);
1563 instance
->cmd_list
= NULL
;
1570 * Add all the commands to command pool (instance->cmd_pool)
1572 for (i
= 0; i
< max_cmd
; i
++) {
1573 cmd
= instance
->cmd_list
[i
];
1574 memset(cmd
, 0, sizeof(struct megasas_cmd
));
1576 cmd
->instance
= instance
;
1578 list_add_tail(&cmd
->list
, &instance
->cmd_pool
);
1582 * Create a frame pool and assign one frame to each cmd
1584 if (megasas_create_frame_pool(instance
)) {
1585 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
1586 megasas_free_cmds(instance
);
1593 * megasas_get_controller_info - Returns FW's controller structure
1594 * @instance: Adapter soft state
1595 * @ctrl_info: Controller information structure
1597 * Issues an internal command (DCMD) to get the FW's controller structure.
1598 * This information is mainly used to find out the maximum IO transfer per
1599 * command supported by the FW.
1602 megasas_get_ctrl_info(struct megasas_instance
*instance
,
1603 struct megasas_ctrl_info
*ctrl_info
)
1606 struct megasas_cmd
*cmd
;
1607 struct megasas_dcmd_frame
*dcmd
;
1608 struct megasas_ctrl_info
*ci
;
1609 dma_addr_t ci_h
= 0;
1611 cmd
= megasas_get_cmd(instance
);
1614 printk(KERN_DEBUG
"megasas: Failed to get a free cmd\n");
1618 dcmd
= &cmd
->frame
->dcmd
;
1620 ci
= pci_alloc_consistent(instance
->pdev
,
1621 sizeof(struct megasas_ctrl_info
), &ci_h
);
1624 printk(KERN_DEBUG
"Failed to alloc mem for ctrl info\n");
1625 megasas_return_cmd(instance
, cmd
);
1629 memset(ci
, 0, sizeof(*ci
));
1630 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1632 dcmd
->cmd
= MFI_CMD_DCMD
;
1633 dcmd
->cmd_status
= 0xFF;
1634 dcmd
->sge_count
= 1;
1635 dcmd
->flags
= MFI_FRAME_DIR_READ
;
1637 dcmd
->data_xfer_len
= sizeof(struct megasas_ctrl_info
);
1638 dcmd
->opcode
= MR_DCMD_CTRL_GET_INFO
;
1639 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
1640 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_ctrl_info
);
1642 if (!megasas_issue_polled(instance
, cmd
)) {
1644 memcpy(ctrl_info
, ci
, sizeof(struct megasas_ctrl_info
));
1649 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_ctrl_info
),
1652 megasas_return_cmd(instance
, cmd
);
1657 * megasas_init_mfi - Initializes the FW
1658 * @instance: Adapter soft state
1660 * This is the main function for initializing MFI firmware.
1662 static int megasas_init_mfi(struct megasas_instance
*instance
)
1668 struct megasas_register_set __iomem
*reg_set
;
1670 struct megasas_cmd
*cmd
;
1671 struct megasas_ctrl_info
*ctrl_info
;
1673 struct megasas_init_frame
*init_frame
;
1674 struct megasas_init_queue_info
*initq_info
;
1675 dma_addr_t init_frame_h
;
1676 dma_addr_t initq_info_h
;
1679 * Map the message registers
1681 instance
->base_addr
= pci_resource_start(instance
->pdev
, 0);
1683 if (pci_request_regions(instance
->pdev
, "megasas: LSI Logic")) {
1684 printk(KERN_DEBUG
"megasas: IO memory region busy!\n");
1688 instance
->reg_set
= ioremap_nocache(instance
->base_addr
, 8192);
1690 if (!instance
->reg_set
) {
1691 printk(KERN_DEBUG
"megasas: Failed to map IO mem\n");
1695 reg_set
= instance
->reg_set
;
1697 switch(instance
->pdev
->device
)
1699 case PCI_DEVICE_ID_LSI_SAS1078R
:
1700 instance
->instancet
= &megasas_instance_template_ppc
;
1702 case PCI_DEVICE_ID_LSI_SAS1064R
:
1703 case PCI_DEVICE_ID_DELL_PERC5
:
1705 instance
->instancet
= &megasas_instance_template_xscale
;
1710 * We expect the FW state to be READY
1712 if (megasas_transition_to_ready(instance
))
1713 goto fail_ready_state
;
1716 * Get various operational parameters from status register
1718 instance
->max_fw_cmds
= instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
1719 instance
->max_num_sge
= (instance
->instancet
->read_fw_status_reg(reg_set
) & 0xFF0000) >>
1722 * Create a pool of commands
1724 if (megasas_alloc_cmds(instance
))
1725 goto fail_alloc_cmds
;
1728 * Allocate memory for reply queue. Length of reply queue should
1729 * be _one_ more than the maximum commands handled by the firmware.
1731 * Note: When FW completes commands, it places corresponding contex
1732 * values in this circular reply queue. This circular queue is a fairly
1733 * typical producer-consumer queue. FW is the producer (of completed
1734 * commands) and the driver is the consumer.
1736 context_sz
= sizeof(u32
);
1737 reply_q_sz
= context_sz
* (instance
->max_fw_cmds
+ 1);
1739 instance
->reply_queue
= pci_alloc_consistent(instance
->pdev
,
1741 &instance
->reply_queue_h
);
1743 if (!instance
->reply_queue
) {
1744 printk(KERN_DEBUG
"megasas: Out of DMA mem for reply queue\n");
1745 goto fail_reply_queue
;
1749 * Prepare a init frame. Note the init frame points to queue info
1750 * structure. Each frame has SGL allocated after first 64 bytes. For
1751 * this frame - since we don't need any SGL - we use SGL's space as
1752 * queue info structure
1754 * We will not get a NULL command below. We just created the pool.
1756 cmd
= megasas_get_cmd(instance
);
1758 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
1759 initq_info
= (struct megasas_init_queue_info
*)
1760 ((unsigned long)init_frame
+ 64);
1762 init_frame_h
= cmd
->frame_phys_addr
;
1763 initq_info_h
= init_frame_h
+ 64;
1765 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
1766 memset(initq_info
, 0, sizeof(struct megasas_init_queue_info
));
1768 initq_info
->reply_queue_entries
= instance
->max_fw_cmds
+ 1;
1769 initq_info
->reply_queue_start_phys_addr_lo
= instance
->reply_queue_h
;
1771 initq_info
->producer_index_phys_addr_lo
= instance
->producer_h
;
1772 initq_info
->consumer_index_phys_addr_lo
= instance
->consumer_h
;
1774 init_frame
->cmd
= MFI_CMD_INIT
;
1775 init_frame
->cmd_status
= 0xFF;
1776 init_frame
->queue_info_new_phys_addr_lo
= initq_info_h
;
1778 init_frame
->data_xfer_len
= sizeof(struct megasas_init_queue_info
);
1781 * Issue the init frame in polled mode
1783 if (megasas_issue_polled(instance
, cmd
)) {
1784 printk(KERN_DEBUG
"megasas: Failed to init firmware\n");
1788 megasas_return_cmd(instance
, cmd
);
1790 ctrl_info
= kmalloc(sizeof(struct megasas_ctrl_info
), GFP_KERNEL
);
1793 * Compute the max allowed sectors per IO: The controller info has two
1794 * limits on max sectors. Driver should use the minimum of these two.
1796 * 1 << stripe_sz_ops.min = max sectors per strip
1798 * Note that older firmwares ( < FW ver 30) didn't report information
1799 * to calculate max_sectors_1. So the number ended up as zero always.
1801 if (ctrl_info
&& !megasas_get_ctrl_info(instance
, ctrl_info
)) {
1803 max_sectors_1
= (1 << ctrl_info
->stripe_sz_ops
.min
) *
1804 ctrl_info
->max_strips_per_io
;
1805 max_sectors_2
= ctrl_info
->max_request_size
;
1807 instance
->max_sectors_per_req
= (max_sectors_1
< max_sectors_2
)
1808 ? max_sectors_1
: max_sectors_2
;
1810 instance
->max_sectors_per_req
= instance
->max_num_sge
*
1818 megasas_return_cmd(instance
, cmd
);
1820 pci_free_consistent(instance
->pdev
, reply_q_sz
,
1821 instance
->reply_queue
, instance
->reply_queue_h
);
1823 megasas_free_cmds(instance
);
1827 iounmap(instance
->reg_set
);
1830 pci_release_regions(instance
->pdev
);
1836 * megasas_release_mfi - Reverses the FW initialization
1837 * @intance: Adapter soft state
1839 static void megasas_release_mfi(struct megasas_instance
*instance
)
1841 u32 reply_q_sz
= sizeof(u32
) * (instance
->max_fw_cmds
+ 1);
1843 pci_free_consistent(instance
->pdev
, reply_q_sz
,
1844 instance
->reply_queue
, instance
->reply_queue_h
);
1846 megasas_free_cmds(instance
);
1848 iounmap(instance
->reg_set
);
1850 pci_release_regions(instance
->pdev
);
1854 * megasas_get_seq_num - Gets latest event sequence numbers
1855 * @instance: Adapter soft state
1856 * @eli: FW event log sequence numbers information
1858 * FW maintains a log of all events in a non-volatile area. Upper layers would
1859 * usually find out the latest sequence number of the events, the seq number at
1860 * the boot etc. They would "read" all the events below the latest seq number
1861 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1862 * number), they would subsribe to AEN (asynchronous event notification) and
1863 * wait for the events to happen.
1866 megasas_get_seq_num(struct megasas_instance
*instance
,
1867 struct megasas_evt_log_info
*eli
)
1869 struct megasas_cmd
*cmd
;
1870 struct megasas_dcmd_frame
*dcmd
;
1871 struct megasas_evt_log_info
*el_info
;
1872 dma_addr_t el_info_h
= 0;
1874 cmd
= megasas_get_cmd(instance
);
1880 dcmd
= &cmd
->frame
->dcmd
;
1881 el_info
= pci_alloc_consistent(instance
->pdev
,
1882 sizeof(struct megasas_evt_log_info
),
1886 megasas_return_cmd(instance
, cmd
);
1890 memset(el_info
, 0, sizeof(*el_info
));
1891 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1893 dcmd
->cmd
= MFI_CMD_DCMD
;
1894 dcmd
->cmd_status
= 0x0;
1895 dcmd
->sge_count
= 1;
1896 dcmd
->flags
= MFI_FRAME_DIR_READ
;
1898 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_log_info
);
1899 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_GET_INFO
;
1900 dcmd
->sgl
.sge32
[0].phys_addr
= el_info_h
;
1901 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_log_info
);
1903 megasas_issue_blocked_cmd(instance
, cmd
);
1906 * Copy the data back into callers buffer
1908 memcpy(eli
, el_info
, sizeof(struct megasas_evt_log_info
));
1910 pci_free_consistent(instance
->pdev
, sizeof(struct megasas_evt_log_info
),
1911 el_info
, el_info_h
);
1913 megasas_return_cmd(instance
, cmd
);
1919 * megasas_register_aen - Registers for asynchronous event notification
1920 * @instance: Adapter soft state
1921 * @seq_num: The starting sequence number
1922 * @class_locale: Class of the event
1924 * This function subscribes for AEN for events beyond the @seq_num. It requests
1925 * to be notified if and only if the event is of type @class_locale
1928 megasas_register_aen(struct megasas_instance
*instance
, u32 seq_num
,
1929 u32 class_locale_word
)
1932 struct megasas_cmd
*cmd
;
1933 struct megasas_dcmd_frame
*dcmd
;
1934 union megasas_evt_class_locale curr_aen
;
1935 union megasas_evt_class_locale prev_aen
;
1938 * If there an AEN pending already (aen_cmd), check if the
1939 * class_locale of that pending AEN is inclusive of the new
1940 * AEN request we currently have. If it is, then we don't have
1941 * to do anything. In other words, whichever events the current
1942 * AEN request is subscribing to, have already been subscribed
1945 * If the old_cmd is _not_ inclusive, then we have to abort
1946 * that command, form a class_locale that is superset of both
1947 * old and current and re-issue to the FW
1950 curr_aen
.word
= class_locale_word
;
1952 if (instance
->aen_cmd
) {
1954 prev_aen
.word
= instance
->aen_cmd
->frame
->dcmd
.mbox
.w
[1];
1957 * A class whose enum value is smaller is inclusive of all
1958 * higher values. If a PROGRESS (= -1) was previously
1959 * registered, then a new registration requests for higher
1960 * classes need not be sent to FW. They are automatically
1963 * Locale numbers don't have such hierarchy. They are bitmap
1966 if ((prev_aen
.members
.class <= curr_aen
.members
.class) &&
1967 !((prev_aen
.members
.locale
& curr_aen
.members
.locale
) ^
1968 curr_aen
.members
.locale
)) {
1970 * Previously issued event registration includes
1971 * current request. Nothing to do.
1975 curr_aen
.members
.locale
|= prev_aen
.members
.locale
;
1977 if (prev_aen
.members
.class < curr_aen
.members
.class)
1978 curr_aen
.members
.class = prev_aen
.members
.class;
1980 instance
->aen_cmd
->abort_aen
= 1;
1981 ret_val
= megasas_issue_blocked_abort_cmd(instance
,
1986 printk(KERN_DEBUG
"megasas: Failed to abort "
1987 "previous AEN command\n");
1993 cmd
= megasas_get_cmd(instance
);
1998 dcmd
= &cmd
->frame
->dcmd
;
2000 memset(instance
->evt_detail
, 0, sizeof(struct megasas_evt_detail
));
2003 * Prepare DCMD for aen registration
2005 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2007 dcmd
->cmd
= MFI_CMD_DCMD
;
2008 dcmd
->cmd_status
= 0x0;
2009 dcmd
->sge_count
= 1;
2010 dcmd
->flags
= MFI_FRAME_DIR_READ
;
2012 dcmd
->data_xfer_len
= sizeof(struct megasas_evt_detail
);
2013 dcmd
->opcode
= MR_DCMD_CTRL_EVENT_WAIT
;
2014 dcmd
->mbox
.w
[0] = seq_num
;
2015 dcmd
->mbox
.w
[1] = curr_aen
.word
;
2016 dcmd
->sgl
.sge32
[0].phys_addr
= (u32
) instance
->evt_detail_h
;
2017 dcmd
->sgl
.sge32
[0].length
= sizeof(struct megasas_evt_detail
);
2020 * Store reference to the cmd used to register for AEN. When an
2021 * application wants us to register for AEN, we have to abort this
2022 * cmd and re-register with a new EVENT LOCALE supplied by that app
2024 instance
->aen_cmd
= cmd
;
2027 * Issue the aen registration frame
2029 instance
->instancet
->fire_cmd(cmd
->frame_phys_addr
,0,instance
->reg_set
);
2035 * megasas_start_aen - Subscribes to AEN during driver load time
2036 * @instance: Adapter soft state
2038 static int megasas_start_aen(struct megasas_instance
*instance
)
2040 struct megasas_evt_log_info eli
;
2041 union megasas_evt_class_locale class_locale
;
2044 * Get the latest sequence number from FW
2046 memset(&eli
, 0, sizeof(eli
));
2048 if (megasas_get_seq_num(instance
, &eli
))
2052 * Register AEN with FW for latest sequence number plus 1
2054 class_locale
.members
.reserved
= 0;
2055 class_locale
.members
.locale
= MR_EVT_LOCALE_ALL
;
2056 class_locale
.members
.class = MR_EVT_CLASS_DEBUG
;
2058 return megasas_register_aen(instance
, eli
.newest_seq_num
+ 1,
2063 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2064 * @instance: Adapter soft state
2066 static int megasas_io_attach(struct megasas_instance
*instance
)
2068 struct Scsi_Host
*host
= instance
->host
;
2071 * Export parameters required by SCSI mid-layer
2073 host
->irq
= instance
->pdev
->irq
;
2074 host
->unique_id
= instance
->unique_id
;
2075 host
->can_queue
= instance
->max_fw_cmds
- MEGASAS_INT_CMDS
;
2076 host
->this_id
= instance
->init_id
;
2077 host
->sg_tablesize
= instance
->max_num_sge
;
2078 host
->max_sectors
= instance
->max_sectors_per_req
;
2079 host
->cmd_per_lun
= 128;
2080 host
->max_channel
= MEGASAS_MAX_CHANNELS
- 1;
2081 host
->max_id
= MEGASAS_MAX_DEV_PER_CHANNEL
;
2082 host
->max_lun
= MEGASAS_MAX_LUN
;
2083 host
->max_cmd_len
= 16;
2086 * Notify the mid-layer about the new controller
2088 if (scsi_add_host(host
, &instance
->pdev
->dev
)) {
2089 printk(KERN_DEBUG
"megasas: scsi_add_host failed\n");
2094 * Trigger SCSI to scan our drives
2096 scsi_scan_host(host
);
2101 * megasas_probe_one - PCI hotplug entry point
2102 * @pdev: PCI device structure
2103 * @id: PCI ids of supported hotplugged adapter
2105 static int __devinit
2106 megasas_probe_one(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
2109 struct Scsi_Host
*host
;
2110 struct megasas_instance
*instance
;
2113 * Announce PCI information
2115 printk(KERN_INFO
"megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2116 pdev
->vendor
, pdev
->device
, pdev
->subsystem_vendor
,
2117 pdev
->subsystem_device
);
2119 printk("bus %d:slot %d:func %d\n",
2120 pdev
->bus
->number
, PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
2123 * PCI prepping: enable device set bus mastering and dma mask
2125 rval
= pci_enable_device(pdev
);
2131 pci_set_master(pdev
);
2134 * All our contollers are capable of performing 64-bit DMA
2137 if (pci_set_dma_mask(pdev
, DMA_64BIT_MASK
) != 0) {
2139 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) != 0)
2140 goto fail_set_dma_mask
;
2143 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
) != 0)
2144 goto fail_set_dma_mask
;
2147 host
= scsi_host_alloc(&megasas_template
,
2148 sizeof(struct megasas_instance
));
2151 printk(KERN_DEBUG
"megasas: scsi_host_alloc failed\n");
2152 goto fail_alloc_instance
;
2155 instance
= (struct megasas_instance
*)host
->hostdata
;
2156 memset(instance
, 0, sizeof(*instance
));
2158 instance
->producer
= pci_alloc_consistent(pdev
, sizeof(u32
),
2159 &instance
->producer_h
);
2160 instance
->consumer
= pci_alloc_consistent(pdev
, sizeof(u32
),
2161 &instance
->consumer_h
);
2163 if (!instance
->producer
|| !instance
->consumer
) {
2164 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
2165 "producer, consumer\n");
2166 goto fail_alloc_dma_buf
;
2169 *instance
->producer
= 0;
2170 *instance
->consumer
= 0;
2172 instance
->evt_detail
= pci_alloc_consistent(pdev
,
2174 megasas_evt_detail
),
2175 &instance
->evt_detail_h
);
2177 if (!instance
->evt_detail
) {
2178 printk(KERN_DEBUG
"megasas: Failed to allocate memory for "
2179 "event detail structure\n");
2180 goto fail_alloc_dma_buf
;
2184 * Initialize locks and queues
2186 INIT_LIST_HEAD(&instance
->cmd_pool
);
2188 init_waitqueue_head(&instance
->int_cmd_wait_q
);
2189 init_waitqueue_head(&instance
->abort_cmd_wait_q
);
2191 spin_lock_init(&instance
->cmd_pool_lock
);
2192 spin_lock_init(&instance
->instance_lock
);
2194 sema_init(&instance
->aen_mutex
, 1);
2195 sema_init(&instance
->ioctl_sem
, MEGASAS_INT_CMDS
);
2198 * Initialize PCI related and misc parameters
2200 instance
->pdev
= pdev
;
2201 instance
->host
= host
;
2202 instance
->unique_id
= pdev
->bus
->number
<< 8 | pdev
->devfn
;
2203 instance
->init_id
= MEGASAS_DEFAULT_INIT_ID
;
2206 * Initialize MFI Firmware
2208 if (megasas_init_mfi(instance
))
2214 if (request_irq(pdev
->irq
, megasas_isr
, SA_SHIRQ
, "megasas", instance
)) {
2215 printk(KERN_DEBUG
"megasas: Failed to register IRQ\n");
2219 instance
->instancet
->enable_intr(instance
->reg_set
);
2222 * Store instance in PCI softstate
2224 pci_set_drvdata(pdev
, instance
);
2227 * Add this controller to megasas_mgmt_info structure so that it
2228 * can be exported to management applications
2230 megasas_mgmt_info
.count
++;
2231 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = instance
;
2232 megasas_mgmt_info
.max_index
++;
2235 * Initiate AEN (Asynchronous Event Notification)
2237 if (megasas_start_aen(instance
)) {
2238 printk(KERN_DEBUG
"megasas: start aen failed\n");
2239 goto fail_start_aen
;
2243 * Register with SCSI mid-layer
2245 if (megasas_io_attach(instance
))
2246 goto fail_io_attach
;
2252 megasas_mgmt_info
.count
--;
2253 megasas_mgmt_info
.instance
[megasas_mgmt_info
.max_index
] = NULL
;
2254 megasas_mgmt_info
.max_index
--;
2256 pci_set_drvdata(pdev
, NULL
);
2257 megasas_disable_intr(instance
->reg_set
);
2258 free_irq(instance
->pdev
->irq
, instance
);
2260 megasas_release_mfi(instance
);
2265 if (instance
->evt_detail
)
2266 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
2267 instance
->evt_detail
,
2268 instance
->evt_detail_h
);
2270 if (instance
->producer
)
2271 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
2272 instance
->producer_h
);
2273 if (instance
->consumer
)
2274 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
2275 instance
->consumer_h
);
2276 scsi_host_put(host
);
2278 fail_alloc_instance
:
2280 pci_disable_device(pdev
);
2286 * megasas_flush_cache - Requests FW to flush all its caches
2287 * @instance: Adapter soft state
2289 static void megasas_flush_cache(struct megasas_instance
*instance
)
2291 struct megasas_cmd
*cmd
;
2292 struct megasas_dcmd_frame
*dcmd
;
2294 cmd
= megasas_get_cmd(instance
);
2299 dcmd
= &cmd
->frame
->dcmd
;
2301 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2303 dcmd
->cmd
= MFI_CMD_DCMD
;
2304 dcmd
->cmd_status
= 0x0;
2305 dcmd
->sge_count
= 0;
2306 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
2308 dcmd
->data_xfer_len
= 0;
2309 dcmd
->opcode
= MR_DCMD_CTRL_CACHE_FLUSH
;
2310 dcmd
->mbox
.b
[0] = MR_FLUSH_CTRL_CACHE
| MR_FLUSH_DISK_CACHE
;
2312 megasas_issue_blocked_cmd(instance
, cmd
);
2314 megasas_return_cmd(instance
, cmd
);
2320 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2321 * @instance: Adapter soft state
2323 static void megasas_shutdown_controller(struct megasas_instance
*instance
)
2325 struct megasas_cmd
*cmd
;
2326 struct megasas_dcmd_frame
*dcmd
;
2328 cmd
= megasas_get_cmd(instance
);
2333 if (instance
->aen_cmd
)
2334 megasas_issue_blocked_abort_cmd(instance
, instance
->aen_cmd
);
2336 dcmd
= &cmd
->frame
->dcmd
;
2338 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
2340 dcmd
->cmd
= MFI_CMD_DCMD
;
2341 dcmd
->cmd_status
= 0x0;
2342 dcmd
->sge_count
= 0;
2343 dcmd
->flags
= MFI_FRAME_DIR_NONE
;
2345 dcmd
->data_xfer_len
= 0;
2346 dcmd
->opcode
= MR_DCMD_CTRL_SHUTDOWN
;
2348 megasas_issue_blocked_cmd(instance
, cmd
);
2350 megasas_return_cmd(instance
, cmd
);
2356 * megasas_detach_one - PCI hot"un"plug entry point
2357 * @pdev: PCI device structure
2359 static void megasas_detach_one(struct pci_dev
*pdev
)
2362 struct Scsi_Host
*host
;
2363 struct megasas_instance
*instance
;
2365 instance
= pci_get_drvdata(pdev
);
2366 host
= instance
->host
;
2368 scsi_remove_host(instance
->host
);
2369 megasas_flush_cache(instance
);
2370 megasas_shutdown_controller(instance
);
2373 * Take the instance off the instance array. Note that we will not
2374 * decrement the max_index. We let this array be sparse array
2376 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
2377 if (megasas_mgmt_info
.instance
[i
] == instance
) {
2378 megasas_mgmt_info
.count
--;
2379 megasas_mgmt_info
.instance
[i
] = NULL
;
2385 pci_set_drvdata(instance
->pdev
, NULL
);
2387 megasas_disable_intr(instance
->reg_set
);
2389 free_irq(instance
->pdev
->irq
, instance
);
2391 megasas_release_mfi(instance
);
2393 pci_free_consistent(pdev
, sizeof(struct megasas_evt_detail
),
2394 instance
->evt_detail
, instance
->evt_detail_h
);
2396 pci_free_consistent(pdev
, sizeof(u32
), instance
->producer
,
2397 instance
->producer_h
);
2399 pci_free_consistent(pdev
, sizeof(u32
), instance
->consumer
,
2400 instance
->consumer_h
);
2402 scsi_host_put(host
);
2404 pci_set_drvdata(pdev
, NULL
);
2406 pci_disable_device(pdev
);
2412 * megasas_shutdown - Shutdown entry point
2413 * @device: Generic device structure
2415 static void megasas_shutdown(struct pci_dev
*pdev
)
2417 struct megasas_instance
*instance
= pci_get_drvdata(pdev
);
2418 megasas_flush_cache(instance
);
2422 * megasas_mgmt_open - char node "open" entry point
2424 static int megasas_mgmt_open(struct inode
*inode
, struct file
*filep
)
2427 * Allow only those users with admin rights
2429 if (!capable(CAP_SYS_ADMIN
))
2436 * megasas_mgmt_release - char node "release" entry point
2438 static int megasas_mgmt_release(struct inode
*inode
, struct file
*filep
)
2440 filep
->private_data
= NULL
;
2441 fasync_helper(-1, filep
, 0, &megasas_async_queue
);
2447 * megasas_mgmt_fasync - Async notifier registration from applications
2449 * This function adds the calling process to a driver global queue. When an
2450 * event occurs, SIGIO will be sent to all processes in this queue.
2452 static int megasas_mgmt_fasync(int fd
, struct file
*filep
, int mode
)
2456 mutex_lock(&megasas_async_queue_mutex
);
2458 rc
= fasync_helper(fd
, filep
, mode
, &megasas_async_queue
);
2460 mutex_unlock(&megasas_async_queue_mutex
);
2463 /* For sanity check when we get ioctl */
2464 filep
->private_data
= filep
;
2468 printk(KERN_DEBUG
"megasas: fasync_helper failed [%d]\n", rc
);
2474 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2475 * @instance: Adapter soft state
2476 * @argp: User's ioctl packet
2479 megasas_mgmt_fw_ioctl(struct megasas_instance
*instance
,
2480 struct megasas_iocpacket __user
* user_ioc
,
2481 struct megasas_iocpacket
*ioc
)
2483 struct megasas_sge32
*kern_sge32
;
2484 struct megasas_cmd
*cmd
;
2485 void *kbuff_arr
[MAX_IOCTL_SGE
];
2486 dma_addr_t buf_handle
= 0;
2489 dma_addr_t sense_handle
;
2492 memset(kbuff_arr
, 0, sizeof(kbuff_arr
));
2494 if (ioc
->sge_count
> MAX_IOCTL_SGE
) {
2495 printk(KERN_DEBUG
"megasas: SGE count [%d] > max limit [%d]\n",
2496 ioc
->sge_count
, MAX_IOCTL_SGE
);
2500 cmd
= megasas_get_cmd(instance
);
2502 printk(KERN_DEBUG
"megasas: Failed to get a cmd packet\n");
2507 * User's IOCTL packet has 2 frames (maximum). Copy those two
2508 * frames into our cmd's frames. cmd->frame's context will get
2509 * overwritten when we copy from user's frames. So set that value
2512 memcpy(cmd
->frame
, ioc
->frame
.raw
, 2 * MEGAMFI_FRAME_SIZE
);
2513 cmd
->frame
->hdr
.context
= cmd
->index
;
2516 * The management interface between applications and the fw uses
2517 * MFI frames. E.g, RAID configuration changes, LD property changes
2518 * etc are accomplishes through different kinds of MFI frames. The
2519 * driver needs to care only about substituting user buffers with
2520 * kernel buffers in SGLs. The location of SGL is embedded in the
2521 * struct iocpacket itself.
2523 kern_sge32
= (struct megasas_sge32
*)
2524 ((unsigned long)cmd
->frame
+ ioc
->sgl_off
);
2527 * For each user buffer, create a mirror buffer and copy in
2529 for (i
= 0; i
< ioc
->sge_count
; i
++) {
2530 kbuff_arr
[i
] = pci_alloc_consistent(instance
->pdev
,
2531 ioc
->sgl
[i
].iov_len
,
2533 if (!kbuff_arr
[i
]) {
2534 printk(KERN_DEBUG
"megasas: Failed to alloc "
2535 "kernel SGL buffer for IOCTL \n");
2541 * We don't change the dma_coherent_mask, so
2542 * pci_alloc_consistent only returns 32bit addresses
2544 kern_sge32
[i
].phys_addr
= (u32
) buf_handle
;
2545 kern_sge32
[i
].length
= ioc
->sgl
[i
].iov_len
;
2548 * We created a kernel buffer corresponding to the
2549 * user buffer. Now copy in from the user buffer
2551 if (copy_from_user(kbuff_arr
[i
], ioc
->sgl
[i
].iov_base
,
2552 (u32
) (ioc
->sgl
[i
].iov_len
))) {
2558 if (ioc
->sense_len
) {
2559 sense
= pci_alloc_consistent(instance
->pdev
, ioc
->sense_len
,
2567 (u32
*) ((unsigned long)cmd
->frame
+ ioc
->sense_off
);
2568 *sense_ptr
= sense_handle
;
2572 * Set the sync_cmd flag so that the ISR knows not to complete this
2573 * cmd to the SCSI mid-layer
2576 megasas_issue_blocked_cmd(instance
, cmd
);
2580 * copy out the kernel buffers to user buffers
2582 for (i
= 0; i
< ioc
->sge_count
; i
++) {
2583 if (copy_to_user(ioc
->sgl
[i
].iov_base
, kbuff_arr
[i
],
2584 ioc
->sgl
[i
].iov_len
)) {
2591 * copy out the sense
2593 if (ioc
->sense_len
) {
2595 * sense_ptr points to the location that has the user
2596 * sense buffer address
2598 sense_ptr
= (u32
*) ((unsigned long)ioc
->frame
.raw
+
2601 if (copy_to_user((void __user
*)((unsigned long)(*sense_ptr
)),
2602 sense
, ioc
->sense_len
)) {
2609 * copy the status codes returned by the fw
2611 if (copy_to_user(&user_ioc
->frame
.hdr
.cmd_status
,
2612 &cmd
->frame
->hdr
.cmd_status
, sizeof(u8
))) {
2613 printk(KERN_DEBUG
"megasas: Error copying out cmd_status\n");
2619 pci_free_consistent(instance
->pdev
, ioc
->sense_len
,
2620 sense
, sense_handle
);
2623 for (i
= 0; i
< ioc
->sge_count
&& kbuff_arr
[i
]; i
++) {
2624 pci_free_consistent(instance
->pdev
,
2625 kern_sge32
[i
].length
,
2626 kbuff_arr
[i
], kern_sge32
[i
].phys_addr
);
2629 megasas_return_cmd(instance
, cmd
);
2633 static struct megasas_instance
*megasas_lookup_instance(u16 host_no
)
2637 for (i
= 0; i
< megasas_mgmt_info
.max_index
; i
++) {
2639 if ((megasas_mgmt_info
.instance
[i
]) &&
2640 (megasas_mgmt_info
.instance
[i
]->host
->host_no
== host_no
))
2641 return megasas_mgmt_info
.instance
[i
];
2647 static int megasas_mgmt_ioctl_fw(struct file
*file
, unsigned long arg
)
2649 struct megasas_iocpacket __user
*user_ioc
=
2650 (struct megasas_iocpacket __user
*)arg
;
2651 struct megasas_iocpacket
*ioc
;
2652 struct megasas_instance
*instance
;
2655 ioc
= kmalloc(sizeof(*ioc
), GFP_KERNEL
);
2659 if (copy_from_user(ioc
, user_ioc
, sizeof(*ioc
))) {
2664 instance
= megasas_lookup_instance(ioc
->host_no
);
2671 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2673 if (down_interruptible(&instance
->ioctl_sem
)) {
2674 error
= -ERESTARTSYS
;
2677 error
= megasas_mgmt_fw_ioctl(instance
, user_ioc
, ioc
);
2678 up(&instance
->ioctl_sem
);
2685 static int megasas_mgmt_ioctl_aen(struct file
*file
, unsigned long arg
)
2687 struct megasas_instance
*instance
;
2688 struct megasas_aen aen
;
2691 if (file
->private_data
!= file
) {
2692 printk(KERN_DEBUG
"megasas: fasync_helper was not "
2697 if (copy_from_user(&aen
, (void __user
*)arg
, sizeof(aen
)))
2700 instance
= megasas_lookup_instance(aen
.host_no
);
2705 down(&instance
->aen_mutex
);
2706 error
= megasas_register_aen(instance
, aen
.seq_num
,
2707 aen
.class_locale_word
);
2708 up(&instance
->aen_mutex
);
2713 * megasas_mgmt_ioctl - char node ioctl entry point
2716 megasas_mgmt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2719 case MEGASAS_IOC_FIRMWARE
:
2720 return megasas_mgmt_ioctl_fw(file
, arg
);
2722 case MEGASAS_IOC_GET_AEN
:
2723 return megasas_mgmt_ioctl_aen(file
, arg
);
2729 #ifdef CONFIG_COMPAT
2730 static int megasas_mgmt_compat_ioctl_fw(struct file
*file
, unsigned long arg
)
2732 struct compat_megasas_iocpacket __user
*cioc
=
2733 (struct compat_megasas_iocpacket __user
*)arg
;
2734 struct megasas_iocpacket __user
*ioc
=
2735 compat_alloc_user_space(sizeof(struct megasas_iocpacket
));
2739 clear_user(ioc
, sizeof(*ioc
));
2741 if (copy_in_user(&ioc
->host_no
, &cioc
->host_no
, sizeof(u16
)) ||
2742 copy_in_user(&ioc
->sgl_off
, &cioc
->sgl_off
, sizeof(u32
)) ||
2743 copy_in_user(&ioc
->sense_off
, &cioc
->sense_off
, sizeof(u32
)) ||
2744 copy_in_user(&ioc
->sense_len
, &cioc
->sense_len
, sizeof(u32
)) ||
2745 copy_in_user(ioc
->frame
.raw
, cioc
->frame
.raw
, 128) ||
2746 copy_in_user(&ioc
->sge_count
, &cioc
->sge_count
, sizeof(u32
)))
2749 for (i
= 0; i
< MAX_IOCTL_SGE
; i
++) {
2752 if (get_user(ptr
, &cioc
->sgl
[i
].iov_base
) ||
2753 put_user(compat_ptr(ptr
), &ioc
->sgl
[i
].iov_base
) ||
2754 copy_in_user(&ioc
->sgl
[i
].iov_len
,
2755 &cioc
->sgl
[i
].iov_len
, sizeof(compat_size_t
)))
2759 error
= megasas_mgmt_ioctl_fw(file
, (unsigned long)ioc
);
2761 if (copy_in_user(&cioc
->frame
.hdr
.cmd_status
,
2762 &ioc
->frame
.hdr
.cmd_status
, sizeof(u8
))) {
2763 printk(KERN_DEBUG
"megasas: error copy_in_user cmd_status\n");
2770 megasas_mgmt_compat_ioctl(struct file
*file
, unsigned int cmd
,
2774 case MEGASAS_IOC_FIRMWARE32
:
2775 return megasas_mgmt_compat_ioctl_fw(file
, arg
);
2776 case MEGASAS_IOC_GET_AEN
:
2777 return megasas_mgmt_ioctl_aen(file
, arg
);
2785 * File operations structure for management interface
2787 static struct file_operations megasas_mgmt_fops
= {
2788 .owner
= THIS_MODULE
,
2789 .open
= megasas_mgmt_open
,
2790 .release
= megasas_mgmt_release
,
2791 .fasync
= megasas_mgmt_fasync
,
2792 .unlocked_ioctl
= megasas_mgmt_ioctl
,
2793 #ifdef CONFIG_COMPAT
2794 .compat_ioctl
= megasas_mgmt_compat_ioctl
,
2799 * PCI hotplug support registration structure
2801 static struct pci_driver megasas_pci_driver
= {
2803 .name
= "megaraid_sas",
2804 .id_table
= megasas_pci_table
,
2805 .probe
= megasas_probe_one
,
2806 .remove
= __devexit_p(megasas_detach_one
),
2807 .shutdown
= megasas_shutdown
,
2811 * Sysfs driver attributes
2813 static ssize_t
megasas_sysfs_show_version(struct device_driver
*dd
, char *buf
)
2815 return snprintf(buf
, strlen(MEGASAS_VERSION
) + 2, "%s\n",
2819 static DRIVER_ATTR(version
, S_IRUGO
, megasas_sysfs_show_version
, NULL
);
2822 megasas_sysfs_show_release_date(struct device_driver
*dd
, char *buf
)
2824 return snprintf(buf
, strlen(MEGASAS_RELDATE
) + 2, "%s\n",
2828 static DRIVER_ATTR(release_date
, S_IRUGO
, megasas_sysfs_show_release_date
,
2832 * megasas_init - Driver load entry point
2834 static int __init
megasas_init(void)
2839 * Announce driver version and other information
2841 printk(KERN_INFO
"megasas: %s %s\n", MEGASAS_VERSION
,
2842 MEGASAS_EXT_VERSION
);
2844 memset(&megasas_mgmt_info
, 0, sizeof(megasas_mgmt_info
));
2847 * Register character device node
2849 rval
= register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops
);
2852 printk(KERN_DEBUG
"megasas: failed to open device node\n");
2856 megasas_mgmt_majorno
= rval
;
2859 * Register ourselves as PCI hotplug module
2861 rval
= pci_module_init(&megasas_pci_driver
);
2864 printk(KERN_DEBUG
"megasas: PCI hotplug regisration failed \n");
2865 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
2868 driver_create_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
2869 driver_create_file(&megasas_pci_driver
.driver
,
2870 &driver_attr_release_date
);
2876 * megasas_exit - Driver unload entry point
2878 static void __exit
megasas_exit(void)
2880 driver_remove_file(&megasas_pci_driver
.driver
, &driver_attr_version
);
2881 driver_remove_file(&megasas_pci_driver
.driver
,
2882 &driver_attr_release_date
);
2884 pci_unregister_driver(&megasas_pci_driver
);
2885 unregister_chrdev(megasas_mgmt_majorno
, "megaraid_sas_ioctl");
2888 module_init(megasas_init
);
2889 module_exit(megasas_exit
);