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.01
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[] = {
56 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
58 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
60 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
61 /* xscale IOP, vega */
62 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
67 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
69 static int megasas_mgmt_majorno;
70 static struct megasas_mgmt_info megasas_mgmt_info;
71 static struct fasync_struct *megasas_async_queue;
72 static DEFINE_MUTEX(megasas_async_queue_mutex);
75 * megasas_get_cmd - Get a command from the free pool
76 * @instance: Adapter soft state
78 * Returns a free command from the pool
80 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
84 struct megasas_cmd *cmd = NULL;
86 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
88 if (!list_empty(&instance->cmd_pool)) {
89 cmd = list_entry((&instance->cmd_pool)->next,
90 struct megasas_cmd, list);
91 list_del_init(&cmd->list);
93 printk(KERN_ERR "megasas: Command pool empty!\n");
96 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
101 * megasas_return_cmd - Return a cmd to free command pool
102 * @instance: Adapter soft state
103 * @cmd: Command packet to be returned to free command pool
106 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
110 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
113 list_add_tail(&cmd->list, &instance->cmd_pool);
115 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
120 * The following functions are defined for xscale
121 * (deviceid : 1064R, PERC5) controllers
125 * megasas_enable_intr_xscale - Enables interrupts
126 * @regs: MFI register set
129 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
131 writel(1, &(regs)->outbound_intr_mask);
133 /* Dummy readl to force pci flush */
134 readl(®s->outbound_intr_mask);
138 * megasas_read_fw_status_reg_xscale - returns the current FW status value
139 * @regs: MFI register set
142 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
144 return readl(&(regs)->outbound_msg_0);
147 * megasas_clear_interrupt_xscale - Check & clear interrupt
148 * @regs: MFI register set
151 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
155 * Check if it is our interrupt
157 status = readl(®s->outbound_intr_status);
159 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
164 * Clear the interrupt by writing back the same value
166 writel(status, ®s->outbound_intr_status);
172 * megasas_fire_cmd_xscale - Sends command to the FW
173 * @frame_phys_addr : Physical address of cmd
174 * @frame_count : Number of frames for the command
175 * @regs : MFI register set
178 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
180 writel((frame_phys_addr >> 3)|(frame_count),
181 &(regs)->inbound_queue_port);
184 static struct megasas_instance_template megasas_instance_template_xscale = {
186 .fire_cmd = megasas_fire_cmd_xscale,
187 .enable_intr = megasas_enable_intr_xscale,
188 .clear_intr = megasas_clear_intr_xscale,
189 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
193 * This is the end of set of functions & definitions specific
194 * to xscale (deviceid : 1064R, PERC5) controllers
198 * The following functions are defined for ppc (deviceid : 0x60)
203 * megasas_enable_intr_ppc - Enables interrupts
204 * @regs: MFI register set
207 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
209 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
211 writel(~0x80000004, &(regs)->outbound_intr_mask);
213 /* Dummy readl to force pci flush */
214 readl(®s->outbound_intr_mask);
218 * megasas_read_fw_status_reg_ppc - returns the current FW status value
219 * @regs: MFI register set
222 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
224 return readl(&(regs)->outbound_scratch_pad);
228 * megasas_clear_interrupt_ppc - Check & clear interrupt
229 * @regs: MFI register set
232 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
236 * Check if it is our interrupt
238 status = readl(®s->outbound_intr_status);
240 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
245 * Clear the interrupt by writing back the same value
247 writel(status, ®s->outbound_doorbell_clear);
252 * megasas_fire_cmd_ppc - Sends command to the FW
253 * @frame_phys_addr : Physical address of cmd
254 * @frame_count : Number of frames for the command
255 * @regs : MFI register set
258 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
260 writel((frame_phys_addr | (frame_count<<1))|1,
261 &(regs)->inbound_queue_port);
264 static struct megasas_instance_template megasas_instance_template_ppc = {
266 .fire_cmd = megasas_fire_cmd_ppc,
267 .enable_intr = megasas_enable_intr_ppc,
268 .clear_intr = megasas_clear_intr_ppc,
269 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
273 * This is the end of set of functions & definitions
274 * specific to ppc (deviceid : 0x60) controllers
278 * megasas_disable_intr - Disables interrupts
279 * @regs: MFI register set
282 megasas_disable_intr(struct megasas_instance *instance)
285 struct megasas_register_set __iomem *regs = instance->reg_set;
287 if(instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078R)
290 writel(mask, ®s->outbound_intr_mask);
292 /* Dummy readl to force pci flush */
293 readl(®s->outbound_intr_mask);
297 * megasas_issue_polled - Issues a polling command
298 * @instance: Adapter soft state
299 * @cmd: Command packet to be issued
301 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
304 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
307 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
309 struct megasas_header *frame_hdr = &cmd->frame->hdr;
311 frame_hdr->cmd_status = 0xFF;
312 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
315 * Issue the frame using inbound queue port
317 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
320 * Wait for cmd_status to change
322 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
327 if (frame_hdr->cmd_status == 0xff)
334 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
335 * @instance: Adapter soft state
336 * @cmd: Command to be issued
338 * This function waits on an event for the command to be returned from ISR.
339 * Used to issue ioctl commands.
342 megasas_issue_blocked_cmd(struct megasas_instance *instance,
343 struct megasas_cmd *cmd)
345 cmd->cmd_status = ENODATA;
347 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
349 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
355 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
356 * @instance: Adapter soft state
357 * @cmd_to_abort: Previously issued cmd to be aborted
359 * MFI firmware can abort previously issued AEN comamnd (automatic event
360 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
361 * cmd and blocks till it is completed.
364 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
365 struct megasas_cmd *cmd_to_abort)
367 struct megasas_cmd *cmd;
368 struct megasas_abort_frame *abort_fr;
370 cmd = megasas_get_cmd(instance);
375 abort_fr = &cmd->frame->abort;
378 * Prepare and issue the abort frame
380 abort_fr->cmd = MFI_CMD_ABORT;
381 abort_fr->cmd_status = 0xFF;
383 abort_fr->abort_context = cmd_to_abort->index;
384 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
385 abort_fr->abort_mfi_phys_addr_hi = 0;
388 cmd->cmd_status = 0xFF;
390 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
393 * Wait for this cmd to complete
395 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
397 megasas_return_cmd(instance, cmd);
402 * megasas_make_sgl32 - Prepares 32-bit SGL
403 * @instance: Adapter soft state
404 * @scp: SCSI command from the mid-layer
405 * @mfi_sgl: SGL to be filled in
407 * If successful, this function returns the number of SG elements. Otherwise,
411 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
412 union megasas_sgl *mfi_sgl)
416 struct scatterlist *os_sgl;
419 * Return 0 if there is no data transfer
421 if (!scp->request_buffer || !scp->request_bufflen)
425 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
432 mfi_sgl->sge32[0].length = scp->request_bufflen;
437 os_sgl = (struct scatterlist *)scp->request_buffer;
438 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
439 scp->sc_data_direction);
441 for (i = 0; i < sge_count; i++, os_sgl++) {
442 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
443 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
450 * megasas_make_sgl64 - Prepares 64-bit SGL
451 * @instance: Adapter soft state
452 * @scp: SCSI command from the mid-layer
453 * @mfi_sgl: SGL to be filled in
455 * If successful, this function returns the number of SG elements. Otherwise,
459 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
460 union megasas_sgl *mfi_sgl)
464 struct scatterlist *os_sgl;
467 * Return 0 if there is no data transfer
469 if (!scp->request_buffer || !scp->request_bufflen)
473 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
481 mfi_sgl->sge64[0].length = scp->request_bufflen;
486 os_sgl = (struct scatterlist *)scp->request_buffer;
487 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
488 scp->sc_data_direction);
490 for (i = 0; i < sge_count; i++, os_sgl++) {
491 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
492 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
499 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
500 * @instance: Adapter soft state
502 * @cmd: Command to be prepared in
504 * This function prepares CDB commands. These are typcially pass-through
505 * commands to the devices.
508 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
509 struct megasas_cmd *cmd)
516 struct megasas_pthru_frame *pthru;
518 is_logical = MEGASAS_IS_LOGICAL(scp);
519 device_id = MEGASAS_DEV_INDEX(instance, scp);
520 pthru = (struct megasas_pthru_frame *)cmd->frame;
522 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
523 flags = MFI_FRAME_DIR_WRITE;
524 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
525 flags = MFI_FRAME_DIR_READ;
526 else if (scp->sc_data_direction == PCI_DMA_NONE)
527 flags = MFI_FRAME_DIR_NONE;
530 * Prepare the DCDB frame
532 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
533 pthru->cmd_status = 0x0;
534 pthru->scsi_status = 0x0;
535 pthru->target_id = device_id;
536 pthru->lun = scp->device->lun;
537 pthru->cdb_len = scp->cmd_len;
539 pthru->flags = flags;
540 pthru->data_xfer_len = scp->request_bufflen;
542 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
547 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
548 sizeof(struct megasas_sge32);
551 pthru->flags |= MFI_FRAME_SGL64;
552 pthru->sge_count = megasas_make_sgl64(instance, scp,
555 pthru->sge_count = megasas_make_sgl32(instance, scp,
559 * Sense info specific
561 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
562 pthru->sense_buf_phys_addr_hi = 0;
563 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
565 sge_bytes = sge_sz * pthru->sge_count;
568 * Compute the total number of frames this command consumes. FW uses
569 * this number to pull sufficient number of frames from host memory.
571 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
572 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
574 if (cmd->frame_count > 7)
575 cmd->frame_count = 8;
577 return cmd->frame_count;
581 * megasas_build_ldio - Prepares IOs to logical devices
582 * @instance: Adapter soft state
584 * @cmd: Command to to be prepared
586 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
589 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
590 struct megasas_cmd *cmd)
595 u8 sc = scp->cmnd[0];
597 struct megasas_io_frame *ldio;
599 device_id = MEGASAS_DEV_INDEX(instance, scp);
600 ldio = (struct megasas_io_frame *)cmd->frame;
602 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
603 flags = MFI_FRAME_DIR_WRITE;
604 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
605 flags = MFI_FRAME_DIR_READ;
608 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
610 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
611 ldio->cmd_status = 0x0;
612 ldio->scsi_status = 0x0;
613 ldio->target_id = device_id;
615 ldio->reserved_0 = 0;
618 ldio->start_lba_hi = 0;
619 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
622 * 6-byte READ(0x08) or WRITE(0x0A) cdb
624 if (scp->cmd_len == 6) {
625 ldio->lba_count = (u32) scp->cmnd[4];
626 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
627 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
629 ldio->start_lba_lo &= 0x1FFFFF;
633 * 10-byte READ(0x28) or WRITE(0x2A) cdb
635 else if (scp->cmd_len == 10) {
636 ldio->lba_count = (u32) scp->cmnd[8] |
637 ((u32) scp->cmnd[7] << 8);
638 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
639 ((u32) scp->cmnd[3] << 16) |
640 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
644 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
646 else if (scp->cmd_len == 12) {
647 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
648 ((u32) scp->cmnd[7] << 16) |
649 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
651 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
652 ((u32) scp->cmnd[3] << 16) |
653 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
657 * 16-byte READ(0x88) or WRITE(0x8A) cdb
659 else if (scp->cmd_len == 16) {
660 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
661 ((u32) scp->cmnd[11] << 16) |
662 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
664 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
665 ((u32) scp->cmnd[7] << 16) |
666 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
668 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
669 ((u32) scp->cmnd[3] << 16) |
670 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
677 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
678 sizeof(struct megasas_sge32);
681 ldio->flags |= MFI_FRAME_SGL64;
682 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
684 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
687 * Sense info specific
689 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
690 ldio->sense_buf_phys_addr_hi = 0;
691 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
693 sge_bytes = sge_sz * ldio->sge_count;
695 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
696 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
698 if (cmd->frame_count > 7)
699 cmd->frame_count = 8;
701 return cmd->frame_count;
705 * megasas_is_ldio - Checks if the cmd is for logical drive
706 * @scmd: SCSI command
708 * Called by megasas_queue_command to find out if the command to be queued
709 * is a logical drive command
711 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
713 if (!MEGASAS_IS_LOGICAL(cmd))
715 switch (cmd->cmnd[0]) {
731 * megasas_queue_command - Queue entry point
732 * @scmd: SCSI command to be queued
733 * @done: Callback entry point
736 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
739 struct megasas_cmd *cmd;
740 struct megasas_instance *instance;
742 instance = (struct megasas_instance *)
743 scmd->device->host->hostdata;
744 scmd->scsi_done = done;
747 if (MEGASAS_IS_LOGICAL(scmd) &&
748 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
749 scmd->result = DID_BAD_TARGET << 16;
753 cmd = megasas_get_cmd(instance);
755 return SCSI_MLQUEUE_HOST_BUSY;
758 * Logical drive command
760 if (megasas_is_ldio(scmd))
761 frame_count = megasas_build_ldio(instance, scmd, cmd);
763 frame_count = megasas_build_dcdb(instance, scmd, cmd);
771 * Issue the command to the FW
773 atomic_inc(&instance->fw_outstanding);
775 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
780 megasas_return_cmd(instance, cmd);
786 static int megasas_slave_configure(struct scsi_device *sdev)
789 * Don't export physical disk devices to the disk driver.
791 * FIXME: Currently we don't export them to the midlayer at all.
792 * That will be fixed once LSI engineers have audited the
793 * firmware for possible issues.
795 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
799 * The RAID firmware may require extended timeouts.
801 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
802 sdev->timeout = 90 * HZ;
807 * megasas_wait_for_outstanding - Wait for all outstanding cmds
808 * @instance: Adapter soft state
810 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
811 * complete all its outstanding commands. Returns error if one or more IOs
812 * are pending after this time period. It also marks the controller dead.
814 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
817 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
819 for (i = 0; i < wait_time; i++) {
821 int outstanding = atomic_read(&instance->fw_outstanding);
826 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
827 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
828 "commands to complete\n",i,outstanding);
834 if (atomic_read(&instance->fw_outstanding)) {
835 instance->hw_crit_error = 1;
843 * megasas_generic_reset - Generic reset routine
844 * @scmd: Mid-layer SCSI command
846 * This routine implements a generic reset handler for device, bus and host
847 * reset requests. Device, bus and host specific reset handlers can use this
848 * function after they do their specific tasks.
850 static int megasas_generic_reset(struct scsi_cmnd *scmd)
853 struct megasas_instance *instance;
855 instance = (struct megasas_instance *)scmd->device->host->hostdata;
857 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
858 scmd->serial_number, scmd->cmnd[0]);
860 if (instance->hw_crit_error) {
861 printk(KERN_ERR "megasas: cannot recover from previous reset "
866 ret_val = megasas_wait_for_outstanding(instance);
867 if (ret_val == SUCCESS)
868 printk(KERN_NOTICE "megasas: reset successful \n");
870 printk(KERN_ERR "megasas: failed to do reset\n");
876 * megasas_reset_device - Device reset handler entry point
878 static int megasas_reset_device(struct scsi_cmnd *scmd)
883 * First wait for all commands to complete
885 ret = megasas_generic_reset(scmd);
891 * megasas_reset_bus_host - Bus & host reset handler entry point
893 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
898 * First wait for all commands to complete
900 ret = megasas_generic_reset(scmd);
906 * megasas_service_aen - Processes an event notification
907 * @instance: Adapter soft state
908 * @cmd: AEN command completed by the ISR
910 * For AEN, driver sends a command down to FW that is held by the FW till an
911 * event occurs. When an event of interest occurs, FW completes the command
912 * that it was previously holding.
914 * This routines sends SIGIO signal to processes that have registered with the
918 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
921 * Don't signal app if it is just an aborted previously registered aen
924 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
928 instance->aen_cmd = NULL;
929 megasas_return_cmd(instance, cmd);
933 * Scsi host template for megaraid_sas driver
935 static struct scsi_host_template megasas_template = {
937 .module = THIS_MODULE,
938 .name = "LSI Logic SAS based MegaRAID driver",
939 .proc_name = "megaraid_sas",
940 .slave_configure = megasas_slave_configure,
941 .queuecommand = megasas_queue_command,
942 .eh_device_reset_handler = megasas_reset_device,
943 .eh_bus_reset_handler = megasas_reset_bus_host,
944 .eh_host_reset_handler = megasas_reset_bus_host,
945 .use_clustering = ENABLE_CLUSTERING,
949 * megasas_complete_int_cmd - Completes an internal command
950 * @instance: Adapter soft state
951 * @cmd: Command to be completed
953 * The megasas_issue_blocked_cmd() function waits for a command to complete
954 * after it issues a command. This function wakes up that waiting routine by
955 * calling wake_up() on the wait queue.
958 megasas_complete_int_cmd(struct megasas_instance *instance,
959 struct megasas_cmd *cmd)
961 cmd->cmd_status = cmd->frame->io.cmd_status;
963 if (cmd->cmd_status == ENODATA) {
966 wake_up(&instance->int_cmd_wait_q);
970 * megasas_complete_abort - Completes aborting a command
971 * @instance: Adapter soft state
972 * @cmd: Cmd that was issued to abort another cmd
974 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
975 * after it issues an abort on a previously issued command. This function
976 * wakes up all functions waiting on the same wait queue.
979 megasas_complete_abort(struct megasas_instance *instance,
980 struct megasas_cmd *cmd)
985 wake_up(&instance->abort_cmd_wait_q);
992 * megasas_unmap_sgbuf - Unmap SG buffers
993 * @instance: Adapter soft state
994 * @cmd: Completed command
997 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
1002 if (cmd->scmd->use_sg) {
1003 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
1004 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
1008 if (!cmd->scmd->request_bufflen)
1011 opcode = cmd->frame->hdr.cmd;
1013 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
1015 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
1017 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
1020 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
1022 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
1025 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
1026 cmd->scmd->sc_data_direction);
1031 * megasas_complete_cmd - Completes a command
1032 * @instance: Adapter soft state
1033 * @cmd: Command to be completed
1034 * @alt_status: If non-zero, use this value as status to
1035 * SCSI mid-layer instead of the value returned
1036 * by the FW. This should be used if caller wants
1037 * an alternate status (as in the case of aborted
1041 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1045 struct megasas_header *hdr = &cmd->frame->hdr;
1048 cmd->scmd->SCp.ptr = (char *)0;
1053 case MFI_CMD_PD_SCSI_IO:
1054 case MFI_CMD_LD_SCSI_IO:
1057 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1058 * issued either through an IO path or an IOCTL path. If it
1059 * was via IOCTL, we will send it to internal completion.
1061 if (cmd->sync_cmd) {
1063 megasas_complete_int_cmd(instance, cmd);
1067 case MFI_CMD_LD_READ:
1068 case MFI_CMD_LD_WRITE:
1071 cmd->scmd->result = alt_status << 16;
1077 atomic_dec(&instance->fw_outstanding);
1079 megasas_unmap_sgbuf(instance, cmd);
1080 cmd->scmd->scsi_done(cmd->scmd);
1081 megasas_return_cmd(instance, cmd);
1086 switch (hdr->cmd_status) {
1089 cmd->scmd->result = DID_OK << 16;
1092 case MFI_STAT_SCSI_IO_FAILED:
1093 case MFI_STAT_LD_INIT_IN_PROGRESS:
1095 (DID_ERROR << 16) | hdr->scsi_status;
1098 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1100 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1102 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1103 memset(cmd->scmd->sense_buffer, 0,
1104 SCSI_SENSE_BUFFERSIZE);
1105 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1108 cmd->scmd->result |= DRIVER_SENSE << 24;
1113 case MFI_STAT_LD_OFFLINE:
1114 case MFI_STAT_DEVICE_NOT_FOUND:
1115 cmd->scmd->result = DID_BAD_TARGET << 16;
1119 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1121 cmd->scmd->result = DID_ERROR << 16;
1125 atomic_dec(&instance->fw_outstanding);
1127 megasas_unmap_sgbuf(instance, cmd);
1128 cmd->scmd->scsi_done(cmd->scmd);
1129 megasas_return_cmd(instance, cmd);
1138 * See if got an event notification
1140 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1141 megasas_service_aen(instance, cmd);
1143 megasas_complete_int_cmd(instance, cmd);
1149 * Cmd issued to abort another cmd returned
1151 megasas_complete_abort(instance, cmd);
1155 printk("megasas: Unknown command completed! [0x%X]\n",
1162 * megasas_deplete_reply_queue - Processes all completed commands
1163 * @instance: Adapter soft state
1164 * @alt_status: Alternate status to be returned to
1165 * SCSI mid-layer instead of the status
1166 * returned by the FW
1169 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1174 struct megasas_cmd *cmd;
1177 * Check if it is our interrupt
1178 * Clear the interrupt
1180 if(instance->instancet->clear_intr(instance->reg_set))
1183 producer = *instance->producer;
1184 consumer = *instance->consumer;
1186 while (consumer != producer) {
1187 context = instance->reply_queue[consumer];
1189 cmd = instance->cmd_list[context];
1191 megasas_complete_cmd(instance, cmd, alt_status);
1194 if (consumer == (instance->max_fw_cmds + 1)) {
1199 *instance->consumer = producer;
1205 * megasas_isr - isr entry point
1207 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1209 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1214 * megasas_transition_to_ready - Move the FW to READY state
1215 * @instance: Adapter soft state
1217 * During the initialization, FW passes can potentially be in any one of
1218 * several possible states. If the FW in operational, waiting-for-handshake
1219 * states, driver must take steps to bring it to ready state. Otherwise, it
1220 * has to wait for the ready state.
1223 megasas_transition_to_ready(struct megasas_instance* instance)
1230 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1232 while (fw_state != MFI_STATE_READY) {
1234 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1238 case MFI_STATE_FAULT:
1240 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1243 case MFI_STATE_WAIT_HANDSHAKE:
1245 * Set the CLR bit in inbound doorbell
1247 writel(MFI_INIT_CLEAR_HANDSHAKE,
1248 &instance->reg_set->inbound_doorbell);
1251 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1254 case MFI_STATE_OPERATIONAL:
1256 * Bring it to READY state; assuming max wait 2 secs
1258 megasas_disable_intr(instance);
1259 writel(MFI_INIT_READY, &instance->reg_set->inbound_doorbell);
1262 cur_state = MFI_STATE_OPERATIONAL;
1265 case MFI_STATE_UNDEFINED:
1267 * This state should not last for more than 2 seconds
1270 cur_state = MFI_STATE_UNDEFINED;
1273 case MFI_STATE_BB_INIT:
1275 cur_state = MFI_STATE_BB_INIT;
1278 case MFI_STATE_FW_INIT:
1280 cur_state = MFI_STATE_FW_INIT;
1283 case MFI_STATE_FW_INIT_2:
1285 cur_state = MFI_STATE_FW_INIT_2;
1288 case MFI_STATE_DEVICE_SCAN:
1290 cur_state = MFI_STATE_DEVICE_SCAN;
1293 case MFI_STATE_FLUSH_CACHE:
1295 cur_state = MFI_STATE_FLUSH_CACHE;
1299 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1305 * The cur_state should not last for more than max_wait secs
1307 for (i = 0; i < (max_wait * 1000); i++) {
1308 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1311 if (fw_state == cur_state) {
1318 * Return error if fw_state hasn't changed after max_wait
1320 if (fw_state == cur_state) {
1321 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1322 "in %d secs\n", fw_state, max_wait);
1331 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1332 * @instance: Adapter soft state
1334 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1337 u32 max_cmd = instance->max_fw_cmds;
1338 struct megasas_cmd *cmd;
1340 if (!instance->frame_dma_pool)
1344 * Return all frames to pool
1346 for (i = 0; i < max_cmd; i++) {
1348 cmd = instance->cmd_list[i];
1351 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1352 cmd->frame_phys_addr);
1355 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1356 cmd->sense_phys_addr);
1360 * Now destroy the pool itself
1362 pci_pool_destroy(instance->frame_dma_pool);
1363 pci_pool_destroy(instance->sense_dma_pool);
1365 instance->frame_dma_pool = NULL;
1366 instance->sense_dma_pool = NULL;
1370 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1371 * @instance: Adapter soft state
1373 * Each command packet has an embedded DMA memory buffer that is used for
1374 * filling MFI frame and the SG list that immediately follows the frame. This
1375 * function creates those DMA memory buffers for each command packet by using
1376 * PCI pool facility.
1378 static int megasas_create_frame_pool(struct megasas_instance *instance)
1386 struct megasas_cmd *cmd;
1388 max_cmd = instance->max_fw_cmds;
1391 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1392 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1394 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1395 sizeof(struct megasas_sge32);
1398 * Calculated the number of 64byte frames required for SGL
1400 sgl_sz = sge_sz * instance->max_num_sge;
1401 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1404 * We need one extra frame for the MFI command
1408 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1410 * Use DMA pool facility provided by PCI layer
1412 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1413 instance->pdev, total_sz, 64,
1416 if (!instance->frame_dma_pool) {
1417 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1421 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1422 instance->pdev, 128, 4, 0);
1424 if (!instance->sense_dma_pool) {
1425 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1427 pci_pool_destroy(instance->frame_dma_pool);
1428 instance->frame_dma_pool = NULL;
1434 * Allocate and attach a frame to each of the commands in cmd_list.
1435 * By making cmd->index as the context instead of the &cmd, we can
1436 * always use 32bit context regardless of the architecture
1438 for (i = 0; i < max_cmd; i++) {
1440 cmd = instance->cmd_list[i];
1442 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1443 GFP_KERNEL, &cmd->frame_phys_addr);
1445 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1446 GFP_KERNEL, &cmd->sense_phys_addr);
1449 * megasas_teardown_frame_pool() takes care of freeing
1450 * whatever has been allocated
1452 if (!cmd->frame || !cmd->sense) {
1453 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1454 megasas_teardown_frame_pool(instance);
1458 cmd->frame->io.context = cmd->index;
1465 * megasas_free_cmds - Free all the cmds in the free cmd pool
1466 * @instance: Adapter soft state
1468 static void megasas_free_cmds(struct megasas_instance *instance)
1471 /* First free the MFI frame pool */
1472 megasas_teardown_frame_pool(instance);
1474 /* Free all the commands in the cmd_list */
1475 for (i = 0; i < instance->max_fw_cmds; i++)
1476 kfree(instance->cmd_list[i]);
1478 /* Free the cmd_list buffer itself */
1479 kfree(instance->cmd_list);
1480 instance->cmd_list = NULL;
1482 INIT_LIST_HEAD(&instance->cmd_pool);
1486 * megasas_alloc_cmds - Allocates the command packets
1487 * @instance: Adapter soft state
1489 * Each command that is issued to the FW, whether IO commands from the OS or
1490 * internal commands like IOCTLs, are wrapped in local data structure called
1491 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1494 * Each frame has a 32-bit field called context (tag). This context is used
1495 * to get back the megasas_cmd from the frame when a frame gets completed in
1496 * the ISR. Typically the address of the megasas_cmd itself would be used as
1497 * the context. But we wanted to keep the differences between 32 and 64 bit
1498 * systems to the mininum. We always use 32 bit integers for the context. In
1499 * this driver, the 32 bit values are the indices into an array cmd_list.
1500 * This array is used only to look up the megasas_cmd given the context. The
1501 * free commands themselves are maintained in a linked list called cmd_pool.
1503 static int megasas_alloc_cmds(struct megasas_instance *instance)
1508 struct megasas_cmd *cmd;
1510 max_cmd = instance->max_fw_cmds;
1513 * instance->cmd_list is an array of struct megasas_cmd pointers.
1514 * Allocate the dynamic array first and then allocate individual
1517 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1520 if (!instance->cmd_list) {
1521 printk(KERN_DEBUG "megasas: out of memory\n");
1525 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1527 for (i = 0; i < max_cmd; i++) {
1528 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1531 if (!instance->cmd_list[i]) {
1533 for (j = 0; j < i; j++)
1534 kfree(instance->cmd_list[j]);
1536 kfree(instance->cmd_list);
1537 instance->cmd_list = NULL;
1544 * Add all the commands to command pool (instance->cmd_pool)
1546 for (i = 0; i < max_cmd; i++) {
1547 cmd = instance->cmd_list[i];
1548 memset(cmd, 0, sizeof(struct megasas_cmd));
1550 cmd->instance = instance;
1552 list_add_tail(&cmd->list, &instance->cmd_pool);
1556 * Create a frame pool and assign one frame to each cmd
1558 if (megasas_create_frame_pool(instance)) {
1559 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1560 megasas_free_cmds(instance);
1567 * megasas_get_controller_info - Returns FW's controller structure
1568 * @instance: Adapter soft state
1569 * @ctrl_info: Controller information structure
1571 * Issues an internal command (DCMD) to get the FW's controller structure.
1572 * This information is mainly used to find out the maximum IO transfer per
1573 * command supported by the FW.
1576 megasas_get_ctrl_info(struct megasas_instance *instance,
1577 struct megasas_ctrl_info *ctrl_info)
1580 struct megasas_cmd *cmd;
1581 struct megasas_dcmd_frame *dcmd;
1582 struct megasas_ctrl_info *ci;
1583 dma_addr_t ci_h = 0;
1585 cmd = megasas_get_cmd(instance);
1588 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1592 dcmd = &cmd->frame->dcmd;
1594 ci = pci_alloc_consistent(instance->pdev,
1595 sizeof(struct megasas_ctrl_info), &ci_h);
1598 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1599 megasas_return_cmd(instance, cmd);
1603 memset(ci, 0, sizeof(*ci));
1604 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1606 dcmd->cmd = MFI_CMD_DCMD;
1607 dcmd->cmd_status = 0xFF;
1608 dcmd->sge_count = 1;
1609 dcmd->flags = MFI_FRAME_DIR_READ;
1611 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1612 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1613 dcmd->sgl.sge32[0].phys_addr = ci_h;
1614 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1616 if (!megasas_issue_polled(instance, cmd)) {
1618 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1623 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1626 megasas_return_cmd(instance, cmd);
1631 * megasas_init_mfi - Initializes the FW
1632 * @instance: Adapter soft state
1634 * This is the main function for initializing MFI firmware.
1636 static int megasas_init_mfi(struct megasas_instance *instance)
1642 struct megasas_register_set __iomem *reg_set;
1644 struct megasas_cmd *cmd;
1645 struct megasas_ctrl_info *ctrl_info;
1647 struct megasas_init_frame *init_frame;
1648 struct megasas_init_queue_info *initq_info;
1649 dma_addr_t init_frame_h;
1650 dma_addr_t initq_info_h;
1653 * Map the message registers
1655 instance->base_addr = pci_resource_start(instance->pdev, 0);
1657 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1658 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1662 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1664 if (!instance->reg_set) {
1665 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1669 reg_set = instance->reg_set;
1671 switch(instance->pdev->device)
1673 case PCI_DEVICE_ID_LSI_SAS1078R:
1674 instance->instancet = &megasas_instance_template_ppc;
1676 case PCI_DEVICE_ID_LSI_SAS1064R:
1677 case PCI_DEVICE_ID_DELL_PERC5:
1679 instance->instancet = &megasas_instance_template_xscale;
1684 * We expect the FW state to be READY
1686 if (megasas_transition_to_ready(instance))
1687 goto fail_ready_state;
1690 * Get various operational parameters from status register
1692 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1693 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1696 * Create a pool of commands
1698 if (megasas_alloc_cmds(instance))
1699 goto fail_alloc_cmds;
1702 * Allocate memory for reply queue. Length of reply queue should
1703 * be _one_ more than the maximum commands handled by the firmware.
1705 * Note: When FW completes commands, it places corresponding contex
1706 * values in this circular reply queue. This circular queue is a fairly
1707 * typical producer-consumer queue. FW is the producer (of completed
1708 * commands) and the driver is the consumer.
1710 context_sz = sizeof(u32);
1711 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1713 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1715 &instance->reply_queue_h);
1717 if (!instance->reply_queue) {
1718 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1719 goto fail_reply_queue;
1723 * Prepare a init frame. Note the init frame points to queue info
1724 * structure. Each frame has SGL allocated after first 64 bytes. For
1725 * this frame - since we don't need any SGL - we use SGL's space as
1726 * queue info structure
1728 * We will not get a NULL command below. We just created the pool.
1730 cmd = megasas_get_cmd(instance);
1732 init_frame = (struct megasas_init_frame *)cmd->frame;
1733 initq_info = (struct megasas_init_queue_info *)
1734 ((unsigned long)init_frame + 64);
1736 init_frame_h = cmd->frame_phys_addr;
1737 initq_info_h = init_frame_h + 64;
1739 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1740 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1742 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1743 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1745 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1746 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1748 init_frame->cmd = MFI_CMD_INIT;
1749 init_frame->cmd_status = 0xFF;
1750 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1752 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1755 * disable the intr before firing the init frame to FW
1757 megasas_disable_intr(instance);
1760 * Issue the init frame in polled mode
1762 if (megasas_issue_polled(instance, cmd)) {
1763 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1767 megasas_return_cmd(instance, cmd);
1769 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1772 * Compute the max allowed sectors per IO: The controller info has two
1773 * limits on max sectors. Driver should use the minimum of these two.
1775 * 1 << stripe_sz_ops.min = max sectors per strip
1777 * Note that older firmwares ( < FW ver 30) didn't report information
1778 * to calculate max_sectors_1. So the number ended up as zero always.
1780 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1782 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1783 ctrl_info->max_strips_per_io;
1784 max_sectors_2 = ctrl_info->max_request_size;
1786 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1787 ? max_sectors_1 : max_sectors_2;
1789 instance->max_sectors_per_req = instance->max_num_sge *
1797 megasas_return_cmd(instance, cmd);
1799 pci_free_consistent(instance->pdev, reply_q_sz,
1800 instance->reply_queue, instance->reply_queue_h);
1802 megasas_free_cmds(instance);
1806 iounmap(instance->reg_set);
1809 pci_release_regions(instance->pdev);
1815 * megasas_release_mfi - Reverses the FW initialization
1816 * @intance: Adapter soft state
1818 static void megasas_release_mfi(struct megasas_instance *instance)
1820 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1822 pci_free_consistent(instance->pdev, reply_q_sz,
1823 instance->reply_queue, instance->reply_queue_h);
1825 megasas_free_cmds(instance);
1827 iounmap(instance->reg_set);
1829 pci_release_regions(instance->pdev);
1833 * megasas_get_seq_num - Gets latest event sequence numbers
1834 * @instance: Adapter soft state
1835 * @eli: FW event log sequence numbers information
1837 * FW maintains a log of all events in a non-volatile area. Upper layers would
1838 * usually find out the latest sequence number of the events, the seq number at
1839 * the boot etc. They would "read" all the events below the latest seq number
1840 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1841 * number), they would subsribe to AEN (asynchronous event notification) and
1842 * wait for the events to happen.
1845 megasas_get_seq_num(struct megasas_instance *instance,
1846 struct megasas_evt_log_info *eli)
1848 struct megasas_cmd *cmd;
1849 struct megasas_dcmd_frame *dcmd;
1850 struct megasas_evt_log_info *el_info;
1851 dma_addr_t el_info_h = 0;
1853 cmd = megasas_get_cmd(instance);
1859 dcmd = &cmd->frame->dcmd;
1860 el_info = pci_alloc_consistent(instance->pdev,
1861 sizeof(struct megasas_evt_log_info),
1865 megasas_return_cmd(instance, cmd);
1869 memset(el_info, 0, sizeof(*el_info));
1870 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1872 dcmd->cmd = MFI_CMD_DCMD;
1873 dcmd->cmd_status = 0x0;
1874 dcmd->sge_count = 1;
1875 dcmd->flags = MFI_FRAME_DIR_READ;
1877 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1878 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1879 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1880 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1882 megasas_issue_blocked_cmd(instance, cmd);
1885 * Copy the data back into callers buffer
1887 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1889 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1890 el_info, el_info_h);
1892 megasas_return_cmd(instance, cmd);
1898 * megasas_register_aen - Registers for asynchronous event notification
1899 * @instance: Adapter soft state
1900 * @seq_num: The starting sequence number
1901 * @class_locale: Class of the event
1903 * This function subscribes for AEN for events beyond the @seq_num. It requests
1904 * to be notified if and only if the event is of type @class_locale
1907 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1908 u32 class_locale_word)
1911 struct megasas_cmd *cmd;
1912 struct megasas_dcmd_frame *dcmd;
1913 union megasas_evt_class_locale curr_aen;
1914 union megasas_evt_class_locale prev_aen;
1917 * If there an AEN pending already (aen_cmd), check if the
1918 * class_locale of that pending AEN is inclusive of the new
1919 * AEN request we currently have. If it is, then we don't have
1920 * to do anything. In other words, whichever events the current
1921 * AEN request is subscribing to, have already been subscribed
1924 * If the old_cmd is _not_ inclusive, then we have to abort
1925 * that command, form a class_locale that is superset of both
1926 * old and current and re-issue to the FW
1929 curr_aen.word = class_locale_word;
1931 if (instance->aen_cmd) {
1933 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1936 * A class whose enum value is smaller is inclusive of all
1937 * higher values. If a PROGRESS (= -1) was previously
1938 * registered, then a new registration requests for higher
1939 * classes need not be sent to FW. They are automatically
1942 * Locale numbers don't have such hierarchy. They are bitmap
1945 if ((prev_aen.members.class <= curr_aen.members.class) &&
1946 !((prev_aen.members.locale & curr_aen.members.locale) ^
1947 curr_aen.members.locale)) {
1949 * Previously issued event registration includes
1950 * current request. Nothing to do.
1954 curr_aen.members.locale |= prev_aen.members.locale;
1956 if (prev_aen.members.class < curr_aen.members.class)
1957 curr_aen.members.class = prev_aen.members.class;
1959 instance->aen_cmd->abort_aen = 1;
1960 ret_val = megasas_issue_blocked_abort_cmd(instance,
1965 printk(KERN_DEBUG "megasas: Failed to abort "
1966 "previous AEN command\n");
1972 cmd = megasas_get_cmd(instance);
1977 dcmd = &cmd->frame->dcmd;
1979 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1982 * Prepare DCMD for aen registration
1984 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1986 dcmd->cmd = MFI_CMD_DCMD;
1987 dcmd->cmd_status = 0x0;
1988 dcmd->sge_count = 1;
1989 dcmd->flags = MFI_FRAME_DIR_READ;
1991 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1992 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1993 dcmd->mbox.w[0] = seq_num;
1994 dcmd->mbox.w[1] = curr_aen.word;
1995 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1996 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1999 * Store reference to the cmd used to register for AEN. When an
2000 * application wants us to register for AEN, we have to abort this
2001 * cmd and re-register with a new EVENT LOCALE supplied by that app
2003 instance->aen_cmd = cmd;
2006 * Issue the aen registration frame
2008 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2014 * megasas_start_aen - Subscribes to AEN during driver load time
2015 * @instance: Adapter soft state
2017 static int megasas_start_aen(struct megasas_instance *instance)
2019 struct megasas_evt_log_info eli;
2020 union megasas_evt_class_locale class_locale;
2023 * Get the latest sequence number from FW
2025 memset(&eli, 0, sizeof(eli));
2027 if (megasas_get_seq_num(instance, &eli))
2031 * Register AEN with FW for latest sequence number plus 1
2033 class_locale.members.reserved = 0;
2034 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2035 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2037 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2042 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2043 * @instance: Adapter soft state
2045 static int megasas_io_attach(struct megasas_instance *instance)
2047 struct Scsi_Host *host = instance->host;
2050 * Export parameters required by SCSI mid-layer
2052 host->irq = instance->pdev->irq;
2053 host->unique_id = instance->unique_id;
2054 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2055 host->this_id = instance->init_id;
2056 host->sg_tablesize = instance->max_num_sge;
2057 host->max_sectors = instance->max_sectors_per_req;
2058 host->cmd_per_lun = 128;
2059 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2060 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2061 host->max_lun = MEGASAS_MAX_LUN;
2062 host->max_cmd_len = 16;
2065 * Notify the mid-layer about the new controller
2067 if (scsi_add_host(host, &instance->pdev->dev)) {
2068 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2073 * Trigger SCSI to scan our drives
2075 scsi_scan_host(host);
2080 * megasas_probe_one - PCI hotplug entry point
2081 * @pdev: PCI device structure
2082 * @id: PCI ids of supported hotplugged adapter
2084 static int __devinit
2085 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2088 struct Scsi_Host *host;
2089 struct megasas_instance *instance;
2092 * Announce PCI information
2094 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2095 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2096 pdev->subsystem_device);
2098 printk("bus %d:slot %d:func %d\n",
2099 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2102 * PCI prepping: enable device set bus mastering and dma mask
2104 rval = pci_enable_device(pdev);
2110 pci_set_master(pdev);
2113 * All our contollers are capable of performing 64-bit DMA
2116 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2118 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2119 goto fail_set_dma_mask;
2122 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2123 goto fail_set_dma_mask;
2126 host = scsi_host_alloc(&megasas_template,
2127 sizeof(struct megasas_instance));
2130 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2131 goto fail_alloc_instance;
2134 instance = (struct megasas_instance *)host->hostdata;
2135 memset(instance, 0, sizeof(*instance));
2137 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2138 &instance->producer_h);
2139 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2140 &instance->consumer_h);
2142 if (!instance->producer || !instance->consumer) {
2143 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2144 "producer, consumer\n");
2145 goto fail_alloc_dma_buf;
2148 *instance->producer = 0;
2149 *instance->consumer = 0;
2151 instance->evt_detail = pci_alloc_consistent(pdev,
2153 megasas_evt_detail),
2154 &instance->evt_detail_h);
2156 if (!instance->evt_detail) {
2157 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2158 "event detail structure\n");
2159 goto fail_alloc_dma_buf;
2163 * Initialize locks and queues
2165 INIT_LIST_HEAD(&instance->cmd_pool);
2167 atomic_set(&instance->fw_outstanding,0);
2169 init_waitqueue_head(&instance->int_cmd_wait_q);
2170 init_waitqueue_head(&instance->abort_cmd_wait_q);
2172 spin_lock_init(&instance->cmd_pool_lock);
2174 sema_init(&instance->aen_mutex, 1);
2175 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2178 * Initialize PCI related and misc parameters
2180 instance->pdev = pdev;
2181 instance->host = host;
2182 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2183 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2186 * Initialize MFI Firmware
2188 if (megasas_init_mfi(instance))
2194 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2195 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2199 instance->instancet->enable_intr(instance->reg_set);
2202 * Store instance in PCI softstate
2204 pci_set_drvdata(pdev, instance);
2207 * Add this controller to megasas_mgmt_info structure so that it
2208 * can be exported to management applications
2210 megasas_mgmt_info.count++;
2211 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2212 megasas_mgmt_info.max_index++;
2215 * Initiate AEN (Asynchronous Event Notification)
2217 if (megasas_start_aen(instance)) {
2218 printk(KERN_DEBUG "megasas: start aen failed\n");
2219 goto fail_start_aen;
2223 * Register with SCSI mid-layer
2225 if (megasas_io_attach(instance))
2226 goto fail_io_attach;
2232 megasas_mgmt_info.count--;
2233 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2234 megasas_mgmt_info.max_index--;
2236 pci_set_drvdata(pdev, NULL);
2237 megasas_disable_intr(instance);
2238 free_irq(instance->pdev->irq, instance);
2240 megasas_release_mfi(instance);
2245 if (instance->evt_detail)
2246 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2247 instance->evt_detail,
2248 instance->evt_detail_h);
2250 if (instance->producer)
2251 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2252 instance->producer_h);
2253 if (instance->consumer)
2254 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2255 instance->consumer_h);
2256 scsi_host_put(host);
2258 fail_alloc_instance:
2260 pci_disable_device(pdev);
2266 * megasas_flush_cache - Requests FW to flush all its caches
2267 * @instance: Adapter soft state
2269 static void megasas_flush_cache(struct megasas_instance *instance)
2271 struct megasas_cmd *cmd;
2272 struct megasas_dcmd_frame *dcmd;
2274 cmd = megasas_get_cmd(instance);
2279 dcmd = &cmd->frame->dcmd;
2281 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2283 dcmd->cmd = MFI_CMD_DCMD;
2284 dcmd->cmd_status = 0x0;
2285 dcmd->sge_count = 0;
2286 dcmd->flags = MFI_FRAME_DIR_NONE;
2288 dcmd->data_xfer_len = 0;
2289 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2290 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2292 megasas_issue_blocked_cmd(instance, cmd);
2294 megasas_return_cmd(instance, cmd);
2300 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2301 * @instance: Adapter soft state
2303 static void megasas_shutdown_controller(struct megasas_instance *instance)
2305 struct megasas_cmd *cmd;
2306 struct megasas_dcmd_frame *dcmd;
2308 cmd = megasas_get_cmd(instance);
2313 if (instance->aen_cmd)
2314 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2316 dcmd = &cmd->frame->dcmd;
2318 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2320 dcmd->cmd = MFI_CMD_DCMD;
2321 dcmd->cmd_status = 0x0;
2322 dcmd->sge_count = 0;
2323 dcmd->flags = MFI_FRAME_DIR_NONE;
2325 dcmd->data_xfer_len = 0;
2326 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2328 megasas_issue_blocked_cmd(instance, cmd);
2330 megasas_return_cmd(instance, cmd);
2336 * megasas_detach_one - PCI hot"un"plug entry point
2337 * @pdev: PCI device structure
2339 static void megasas_detach_one(struct pci_dev *pdev)
2342 struct Scsi_Host *host;
2343 struct megasas_instance *instance;
2345 instance = pci_get_drvdata(pdev);
2346 host = instance->host;
2348 scsi_remove_host(instance->host);
2349 megasas_flush_cache(instance);
2350 megasas_shutdown_controller(instance);
2353 * Take the instance off the instance array. Note that we will not
2354 * decrement the max_index. We let this array be sparse array
2356 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2357 if (megasas_mgmt_info.instance[i] == instance) {
2358 megasas_mgmt_info.count--;
2359 megasas_mgmt_info.instance[i] = NULL;
2365 pci_set_drvdata(instance->pdev, NULL);
2367 megasas_disable_intr(instance);
2369 free_irq(instance->pdev->irq, instance);
2371 megasas_release_mfi(instance);
2373 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2374 instance->evt_detail, instance->evt_detail_h);
2376 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2377 instance->producer_h);
2379 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2380 instance->consumer_h);
2382 scsi_host_put(host);
2384 pci_set_drvdata(pdev, NULL);
2386 pci_disable_device(pdev);
2392 * megasas_shutdown - Shutdown entry point
2393 * @device: Generic device structure
2395 static void megasas_shutdown(struct pci_dev *pdev)
2397 struct megasas_instance *instance = pci_get_drvdata(pdev);
2398 megasas_flush_cache(instance);
2402 * megasas_mgmt_open - char node "open" entry point
2404 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2407 * Allow only those users with admin rights
2409 if (!capable(CAP_SYS_ADMIN))
2416 * megasas_mgmt_release - char node "release" entry point
2418 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2420 filep->private_data = NULL;
2421 fasync_helper(-1, filep, 0, &megasas_async_queue);
2427 * megasas_mgmt_fasync - Async notifier registration from applications
2429 * This function adds the calling process to a driver global queue. When an
2430 * event occurs, SIGIO will be sent to all processes in this queue.
2432 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2436 mutex_lock(&megasas_async_queue_mutex);
2438 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2440 mutex_unlock(&megasas_async_queue_mutex);
2443 /* For sanity check when we get ioctl */
2444 filep->private_data = filep;
2448 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2454 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2455 * @instance: Adapter soft state
2456 * @argp: User's ioctl packet
2459 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2460 struct megasas_iocpacket __user * user_ioc,
2461 struct megasas_iocpacket *ioc)
2463 struct megasas_sge32 *kern_sge32;
2464 struct megasas_cmd *cmd;
2465 void *kbuff_arr[MAX_IOCTL_SGE];
2466 dma_addr_t buf_handle = 0;
2469 dma_addr_t sense_handle;
2472 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2474 if (ioc->sge_count > MAX_IOCTL_SGE) {
2475 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2476 ioc->sge_count, MAX_IOCTL_SGE);
2480 cmd = megasas_get_cmd(instance);
2482 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2487 * User's IOCTL packet has 2 frames (maximum). Copy those two
2488 * frames into our cmd's frames. cmd->frame's context will get
2489 * overwritten when we copy from user's frames. So set that value
2492 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2493 cmd->frame->hdr.context = cmd->index;
2496 * The management interface between applications and the fw uses
2497 * MFI frames. E.g, RAID configuration changes, LD property changes
2498 * etc are accomplishes through different kinds of MFI frames. The
2499 * driver needs to care only about substituting user buffers with
2500 * kernel buffers in SGLs. The location of SGL is embedded in the
2501 * struct iocpacket itself.
2503 kern_sge32 = (struct megasas_sge32 *)
2504 ((unsigned long)cmd->frame + ioc->sgl_off);
2507 * For each user buffer, create a mirror buffer and copy in
2509 for (i = 0; i < ioc->sge_count; i++) {
2510 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2511 ioc->sgl[i].iov_len,
2513 if (!kbuff_arr[i]) {
2514 printk(KERN_DEBUG "megasas: Failed to alloc "
2515 "kernel SGL buffer for IOCTL \n");
2521 * We don't change the dma_coherent_mask, so
2522 * pci_alloc_consistent only returns 32bit addresses
2524 kern_sge32[i].phys_addr = (u32) buf_handle;
2525 kern_sge32[i].length = ioc->sgl[i].iov_len;
2528 * We created a kernel buffer corresponding to the
2529 * user buffer. Now copy in from the user buffer
2531 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2532 (u32) (ioc->sgl[i].iov_len))) {
2538 if (ioc->sense_len) {
2539 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2547 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2548 *sense_ptr = sense_handle;
2552 * Set the sync_cmd flag so that the ISR knows not to complete this
2553 * cmd to the SCSI mid-layer
2556 megasas_issue_blocked_cmd(instance, cmd);
2560 * copy out the kernel buffers to user buffers
2562 for (i = 0; i < ioc->sge_count; i++) {
2563 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2564 ioc->sgl[i].iov_len)) {
2571 * copy out the sense
2573 if (ioc->sense_len) {
2575 * sense_ptr points to the location that has the user
2576 * sense buffer address
2578 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2581 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2582 sense, ioc->sense_len)) {
2589 * copy the status codes returned by the fw
2591 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2592 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2593 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2599 pci_free_consistent(instance->pdev, ioc->sense_len,
2600 sense, sense_handle);
2603 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2604 pci_free_consistent(instance->pdev,
2605 kern_sge32[i].length,
2606 kbuff_arr[i], kern_sge32[i].phys_addr);
2609 megasas_return_cmd(instance, cmd);
2613 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2617 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2619 if ((megasas_mgmt_info.instance[i]) &&
2620 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2621 return megasas_mgmt_info.instance[i];
2627 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2629 struct megasas_iocpacket __user *user_ioc =
2630 (struct megasas_iocpacket __user *)arg;
2631 struct megasas_iocpacket *ioc;
2632 struct megasas_instance *instance;
2635 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2639 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2644 instance = megasas_lookup_instance(ioc->host_no);
2651 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2653 if (down_interruptible(&instance->ioctl_sem)) {
2654 error = -ERESTARTSYS;
2657 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2658 up(&instance->ioctl_sem);
2665 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2667 struct megasas_instance *instance;
2668 struct megasas_aen aen;
2671 if (file->private_data != file) {
2672 printk(KERN_DEBUG "megasas: fasync_helper was not "
2677 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2680 instance = megasas_lookup_instance(aen.host_no);
2685 down(&instance->aen_mutex);
2686 error = megasas_register_aen(instance, aen.seq_num,
2687 aen.class_locale_word);
2688 up(&instance->aen_mutex);
2693 * megasas_mgmt_ioctl - char node ioctl entry point
2696 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2699 case MEGASAS_IOC_FIRMWARE:
2700 return megasas_mgmt_ioctl_fw(file, arg);
2702 case MEGASAS_IOC_GET_AEN:
2703 return megasas_mgmt_ioctl_aen(file, arg);
2709 #ifdef CONFIG_COMPAT
2710 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2712 struct compat_megasas_iocpacket __user *cioc =
2713 (struct compat_megasas_iocpacket __user *)arg;
2714 struct megasas_iocpacket __user *ioc =
2715 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2719 clear_user(ioc, sizeof(*ioc));
2721 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2722 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2723 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2724 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2725 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2726 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2729 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2732 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2733 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2734 copy_in_user(&ioc->sgl[i].iov_len,
2735 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2739 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2741 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2742 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2743 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2750 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2754 case MEGASAS_IOC_FIRMWARE32:
2755 return megasas_mgmt_compat_ioctl_fw(file, arg);
2756 case MEGASAS_IOC_GET_AEN:
2757 return megasas_mgmt_ioctl_aen(file, arg);
2765 * File operations structure for management interface
2767 static struct file_operations megasas_mgmt_fops = {
2768 .owner = THIS_MODULE,
2769 .open = megasas_mgmt_open,
2770 .release = megasas_mgmt_release,
2771 .fasync = megasas_mgmt_fasync,
2772 .unlocked_ioctl = megasas_mgmt_ioctl,
2773 #ifdef CONFIG_COMPAT
2774 .compat_ioctl = megasas_mgmt_compat_ioctl,
2779 * PCI hotplug support registration structure
2781 static struct pci_driver megasas_pci_driver = {
2783 .name = "megaraid_sas",
2784 .id_table = megasas_pci_table,
2785 .probe = megasas_probe_one,
2786 .remove = __devexit_p(megasas_detach_one),
2787 .shutdown = megasas_shutdown,
2791 * Sysfs driver attributes
2793 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2795 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2799 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2802 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2804 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2808 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2812 * megasas_init - Driver load entry point
2814 static int __init megasas_init(void)
2819 * Announce driver version and other information
2821 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2822 MEGASAS_EXT_VERSION);
2824 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2827 * Register character device node
2829 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2832 printk(KERN_DEBUG "megasas: failed to open device node\n");
2836 megasas_mgmt_majorno = rval;
2839 * Register ourselves as PCI hotplug module
2841 rval = pci_register_driver(&megasas_pci_driver);
2844 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2845 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2848 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2849 driver_create_file(&megasas_pci_driver.driver,
2850 &driver_attr_release_date);
2856 * megasas_exit - Driver unload entry point
2858 static void __exit megasas_exit(void)
2860 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2861 driver_remove_file(&megasas_pci_driver.driver,
2862 &driver_attr_release_date);
2864 pci_unregister_driver(&megasas_pci_driver);
2865 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2868 module_init(megasas_init);
2869 module_exit(megasas_exit);