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.00-rc4
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>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_cmnd.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_host.h>
43 #include "megaraid_sas.h"
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(MEGASAS_VERSION);
47 MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
48 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
51 * PCI ID table for all supported controllers
53 static struct pci_device_id megasas_pci_table[] = {
56 PCI_VENDOR_ID_LSI_LOGIC,
57 PCI_DEVICE_ID_LSI_SAS1064R,
63 PCI_DEVICE_ID_DELL_PERC5,
67 {0} /* Terminating entry */
70 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
72 static int megasas_mgmt_majorno;
73 static struct megasas_mgmt_info megasas_mgmt_info;
74 static struct fasync_struct *megasas_async_queue;
75 static DECLARE_MUTEX(megasas_async_queue_mutex);
78 * megasas_get_cmd - Get a command from the free pool
79 * @instance: Adapter soft state
81 * Returns a free command from the pool
83 static inline struct megasas_cmd *megasas_get_cmd(struct megasas_instance
87 struct megasas_cmd *cmd = NULL;
89 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
91 if (!list_empty(&instance->cmd_pool)) {
92 cmd = list_entry((&instance->cmd_pool)->next,
93 struct megasas_cmd, list);
94 list_del_init(&cmd->list);
96 printk(KERN_ERR "megasas: Command pool empty!\n");
99 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
104 * megasas_return_cmd - Return a cmd to free command pool
105 * @instance: Adapter soft state
106 * @cmd: Command packet to be returned to free command pool
109 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
113 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
116 list_add_tail(&cmd->list, &instance->cmd_pool);
118 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
122 * megasas_enable_intr - Enables interrupts
123 * @regs: MFI register set
126 megasas_enable_intr(struct megasas_register_set __iomem * regs)
128 writel(1, &(regs)->outbound_intr_mask);
130 /* Dummy readl to force pci flush */
131 readl(®s->outbound_intr_mask);
135 * megasas_disable_intr - Disables interrupts
136 * @regs: MFI register set
139 megasas_disable_intr(struct megasas_register_set __iomem * regs)
141 u32 mask = readl(®s->outbound_intr_mask) & (~0x00000001);
142 writel(mask, ®s->outbound_intr_mask);
144 /* Dummy readl to force pci flush */
145 readl(®s->outbound_intr_mask);
149 * megasas_issue_polled - Issues a polling command
150 * @instance: Adapter soft state
151 * @cmd: Command packet to be issued
153 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
156 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
159 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
161 struct megasas_header *frame_hdr = &cmd->frame->hdr;
163 frame_hdr->cmd_status = 0xFF;
164 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
167 * Issue the frame using inbound queue port
169 writel(cmd->frame_phys_addr >> 3,
170 &instance->reg_set->inbound_queue_port);
173 * Wait for cmd_status to change
175 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
180 if (frame_hdr->cmd_status == 0xff)
187 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
188 * @instance: Adapter soft state
189 * @cmd: Command to be issued
191 * This function waits on an event for the command to be returned from ISR.
192 * Used to issue ioctl commands.
195 megasas_issue_blocked_cmd(struct megasas_instance *instance,
196 struct megasas_cmd *cmd)
198 cmd->cmd_status = ENODATA;
200 writel(cmd->frame_phys_addr >> 3,
201 &instance->reg_set->inbound_queue_port);
203 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
209 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
210 * @instance: Adapter soft state
211 * @cmd_to_abort: Previously issued cmd to be aborted
213 * MFI firmware can abort previously issued AEN comamnd (automatic event
214 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
215 * cmd and blocks till it is completed.
218 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
219 struct megasas_cmd *cmd_to_abort)
221 struct megasas_cmd *cmd;
222 struct megasas_abort_frame *abort_fr;
224 cmd = megasas_get_cmd(instance);
229 abort_fr = &cmd->frame->abort;
232 * Prepare and issue the abort frame
234 abort_fr->cmd = MFI_CMD_ABORT;
235 abort_fr->cmd_status = 0xFF;
237 abort_fr->abort_context = cmd_to_abort->index;
238 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
239 abort_fr->abort_mfi_phys_addr_hi = 0;
242 cmd->cmd_status = 0xFF;
244 writel(cmd->frame_phys_addr >> 3,
245 &instance->reg_set->inbound_queue_port);
248 * Wait for this cmd to complete
250 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
252 megasas_return_cmd(instance, cmd);
257 * megasas_make_sgl32 - Prepares 32-bit SGL
258 * @instance: Adapter soft state
259 * @scp: SCSI command from the mid-layer
260 * @mfi_sgl: SGL to be filled in
262 * If successful, this function returns the number of SG elements. Otherwise,
266 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
267 union megasas_sgl *mfi_sgl)
271 struct scatterlist *os_sgl;
274 * Return 0 if there is no data transfer
276 if (!scp->request_buffer || !scp->request_bufflen)
280 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
287 mfi_sgl->sge32[0].length = scp->request_bufflen;
292 os_sgl = (struct scatterlist *)scp->request_buffer;
293 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
294 scp->sc_data_direction);
296 for (i = 0; i < sge_count; i++, os_sgl++) {
297 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
298 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
305 * megasas_make_sgl64 - Prepares 64-bit SGL
306 * @instance: Adapter soft state
307 * @scp: SCSI command from the mid-layer
308 * @mfi_sgl: SGL to be filled in
310 * If successful, this function returns the number of SG elements. Otherwise,
314 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
315 union megasas_sgl *mfi_sgl)
319 struct scatterlist *os_sgl;
322 * Return 0 if there is no data transfer
324 if (!scp->request_buffer || !scp->request_bufflen)
328 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
336 mfi_sgl->sge64[0].length = scp->request_bufflen;
341 os_sgl = (struct scatterlist *)scp->request_buffer;
342 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
343 scp->sc_data_direction);
345 for (i = 0; i < sge_count; i++, os_sgl++) {
346 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
347 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
354 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
355 * @instance: Adapter soft state
357 * @cmd: Command to be prepared in
359 * This function prepares CDB commands. These are typcially pass-through
360 * commands to the devices.
363 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
364 struct megasas_cmd *cmd)
371 struct megasas_pthru_frame *pthru;
373 is_logical = MEGASAS_IS_LOGICAL(scp);
374 device_id = MEGASAS_DEV_INDEX(instance, scp);
375 pthru = (struct megasas_pthru_frame *)cmd->frame;
377 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
378 flags = MFI_FRAME_DIR_WRITE;
379 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
380 flags = MFI_FRAME_DIR_READ;
381 else if (scp->sc_data_direction == PCI_DMA_NONE)
382 flags = MFI_FRAME_DIR_NONE;
385 * Prepare the DCDB frame
387 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
388 pthru->cmd_status = 0x0;
389 pthru->scsi_status = 0x0;
390 pthru->target_id = device_id;
391 pthru->lun = scp->device->lun;
392 pthru->cdb_len = scp->cmd_len;
394 pthru->flags = flags;
395 pthru->data_xfer_len = scp->request_bufflen;
397 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
402 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
403 sizeof(struct megasas_sge32);
406 pthru->flags |= MFI_FRAME_SGL64;
407 pthru->sge_count = megasas_make_sgl64(instance, scp,
410 pthru->sge_count = megasas_make_sgl32(instance, scp,
414 * Sense info specific
416 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
417 pthru->sense_buf_phys_addr_hi = 0;
418 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
420 sge_bytes = sge_sz * pthru->sge_count;
423 * Compute the total number of frames this command consumes. FW uses
424 * this number to pull sufficient number of frames from host memory.
426 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
427 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
429 if (cmd->frame_count > 7)
430 cmd->frame_count = 8;
432 return cmd->frame_count;
436 * megasas_build_ldio - Prepares IOs to logical devices
437 * @instance: Adapter soft state
439 * @cmd: Command to to be prepared
441 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
444 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
445 struct megasas_cmd *cmd)
450 u8 sc = scp->cmnd[0];
452 struct megasas_io_frame *ldio;
454 device_id = MEGASAS_DEV_INDEX(instance, scp);
455 ldio = (struct megasas_io_frame *)cmd->frame;
457 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
458 flags = MFI_FRAME_DIR_WRITE;
459 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
460 flags = MFI_FRAME_DIR_READ;
463 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
465 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
466 ldio->cmd_status = 0x0;
467 ldio->scsi_status = 0x0;
468 ldio->target_id = device_id;
470 ldio->reserved_0 = 0;
473 ldio->start_lba_hi = 0;
474 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
477 * 6-byte READ(0x08) or WRITE(0x0A) cdb
479 if (scp->cmd_len == 6) {
480 ldio->lba_count = (u32) scp->cmnd[4];
481 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
482 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
484 ldio->start_lba_lo &= 0x1FFFFF;
488 * 10-byte READ(0x28) or WRITE(0x2A) cdb
490 else if (scp->cmd_len == 10) {
491 ldio->lba_count = (u32) scp->cmnd[8] |
492 ((u32) scp->cmnd[7] << 8);
493 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
494 ((u32) scp->cmnd[3] << 16) |
495 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
499 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
501 else if (scp->cmd_len == 12) {
502 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
503 ((u32) scp->cmnd[7] << 16) |
504 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
506 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
507 ((u32) scp->cmnd[3] << 16) |
508 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
512 * 16-byte READ(0x88) or WRITE(0x8A) cdb
514 else if (scp->cmd_len == 16) {
515 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
516 ((u32) scp->cmnd[11] << 16) |
517 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
519 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
520 ((u32) scp->cmnd[7] << 16) |
521 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
523 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
524 ((u32) scp->cmnd[3] << 16) |
525 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
532 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
533 sizeof(struct megasas_sge32);
536 ldio->flags |= MFI_FRAME_SGL64;
537 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
539 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
542 * Sense info specific
544 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
545 ldio->sense_buf_phys_addr_hi = 0;
546 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
548 sge_bytes = sge_sz * ldio->sge_count;
550 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
551 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
553 if (cmd->frame_count > 7)
554 cmd->frame_count = 8;
556 return cmd->frame_count;
560 * megasas_build_cmd - Prepares a command packet
561 * @instance: Adapter soft state
563 * @frame_count: [OUT] Number of frames used to prepare this command
565 static inline struct megasas_cmd *megasas_build_cmd(struct megasas_instance
567 struct scsi_cmnd *scp,
571 struct megasas_cmd *cmd;
574 * Find out if this is logical or physical drive command.
576 logical_cmd = MEGASAS_IS_LOGICAL(scp);
579 * Logical drive command
583 if (scp->device->id >= MEGASAS_MAX_LD) {
584 scp->result = DID_BAD_TARGET << 16;
588 switch (scp->cmnd[0]) {
601 if (scp->device->lun) {
602 scp->result = DID_BAD_TARGET << 16;
606 cmd = megasas_get_cmd(instance);
609 scp->result = DID_IMM_RETRY << 16;
613 *frame_count = megasas_build_ldio(instance, scp, cmd);
615 if (!(*frame_count)) {
616 megasas_return_cmd(instance, cmd);
626 if (scp->device->lun) {
627 scp->result = DID_BAD_TARGET << 16;
631 cmd = megasas_get_cmd(instance);
634 scp->result = DID_IMM_RETRY << 16;
638 *frame_count = megasas_build_dcdb(instance, scp, cmd);
640 if (!(*frame_count)) {
641 megasas_return_cmd(instance, cmd);
648 cmd = megasas_get_cmd(instance);
651 scp->result = DID_IMM_RETRY << 16;
655 *frame_count = megasas_build_dcdb(instance, scp, cmd);
657 if (!(*frame_count)) {
658 megasas_return_cmd(instance, cmd);
669 * megasas_queue_command - Queue entry point
670 * @scmd: SCSI command to be queued
671 * @done: Callback entry point
674 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
678 struct megasas_cmd *cmd;
679 struct megasas_instance *instance;
681 instance = (struct megasas_instance *)
682 scmd->device->host->hostdata;
683 scmd->scsi_done = done;
686 cmd = megasas_build_cmd(instance, scmd, &frame_count);
694 scmd->SCp.ptr = (char *)cmd;
695 scmd->SCp.sent_command = jiffies;
698 * Issue the command to the FW
700 spin_lock_irqsave(&instance->instance_lock, flags);
701 instance->fw_outstanding++;
702 spin_unlock_irqrestore(&instance->instance_lock, flags);
704 writel(((cmd->frame_phys_addr >> 3) | (cmd->frame_count - 1)),
705 &instance->reg_set->inbound_queue_port);
711 * megasas_wait_for_outstanding - Wait for all outstanding cmds
712 * @instance: Adapter soft state
714 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
715 * complete all its outstanding commands. Returns error if one or more IOs
716 * are pending after this time period. It also marks the controller dead.
718 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
721 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
723 for (i = 0; i < wait_time; i++) {
725 if (!instance->fw_outstanding)
728 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
729 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
730 "commands to complete\n", i,
731 instance->fw_outstanding);
737 if (instance->fw_outstanding) {
738 instance->hw_crit_error = 1;
746 * megasas_generic_reset - Generic reset routine
747 * @scmd: Mid-layer SCSI command
749 * This routine implements a generic reset handler for device, bus and host
750 * reset requests. Device, bus and host specific reset handlers can use this
751 * function after they do their specific tasks.
753 static int megasas_generic_reset(struct scsi_cmnd *scmd)
756 struct megasas_instance *instance;
758 instance = (struct megasas_instance *)scmd->device->host->hostdata;
760 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
761 scmd->serial_number, scmd->cmnd[0]);
763 if (instance->hw_crit_error) {
764 printk(KERN_ERR "megasas: cannot recover from previous reset "
769 ret_val = megasas_wait_for_outstanding(instance);
770 if (ret_val == SUCCESS)
771 printk(KERN_NOTICE "megasas: reset successful \n");
773 printk(KERN_ERR "megasas: failed to do reset\n");
778 static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
780 unsigned long seconds;
783 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
786 return EH_RESET_TIMER;
788 return EH_NOT_HANDLED;
796 * megasas_reset_device - Device reset handler entry point
798 static int megasas_reset_device(struct scsi_cmnd *scmd)
803 * First wait for all commands to complete
805 ret = megasas_generic_reset(scmd);
811 * megasas_reset_bus_host - Bus & host reset handler entry point
813 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
818 * Frist wait for all commands to complete
820 ret = megasas_generic_reset(scmd);
826 * megasas_service_aen - Processes an event notification
827 * @instance: Adapter soft state
828 * @cmd: AEN command completed by the ISR
830 * For AEN, driver sends a command down to FW that is held by the FW till an
831 * event occurs. When an event of interest occurs, FW completes the command
832 * that it was previously holding.
834 * This routines sends SIGIO signal to processes that have registered with the
838 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
841 * Don't signal app if it is just an aborted previously registered aen
844 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
848 instance->aen_cmd = NULL;
849 megasas_return_cmd(instance, cmd);
853 * Scsi host template for megaraid_sas driver
855 static struct scsi_host_template megasas_template = {
857 .module = THIS_MODULE,
858 .name = "LSI Logic SAS based MegaRAID driver",
859 .proc_name = "megaraid_sas",
860 .queuecommand = megasas_queue_command,
861 .eh_device_reset_handler = megasas_reset_device,
862 .eh_bus_reset_handler = megasas_reset_bus_host,
863 .eh_host_reset_handler = megasas_reset_bus_host,
864 .eh_timed_out = megasas_reset_timer,
865 .use_clustering = ENABLE_CLUSTERING,
869 * megasas_complete_int_cmd - Completes an internal command
870 * @instance: Adapter soft state
871 * @cmd: Command to be completed
873 * The megasas_issue_blocked_cmd() function waits for a command to complete
874 * after it issues a command. This function wakes up that waiting routine by
875 * calling wake_up() on the wait queue.
878 megasas_complete_int_cmd(struct megasas_instance *instance,
879 struct megasas_cmd *cmd)
881 cmd->cmd_status = cmd->frame->io.cmd_status;
883 if (cmd->cmd_status == ENODATA) {
886 wake_up(&instance->int_cmd_wait_q);
890 * megasas_complete_abort - Completes aborting a command
891 * @instance: Adapter soft state
892 * @cmd: Cmd that was issued to abort another cmd
894 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
895 * after it issues an abort on a previously issued command. This function
896 * wakes up all functions waiting on the same wait queue.
899 megasas_complete_abort(struct megasas_instance *instance,
900 struct megasas_cmd *cmd)
905 wake_up(&instance->abort_cmd_wait_q);
912 * megasas_unmap_sgbuf - Unmap SG buffers
913 * @instance: Adapter soft state
914 * @cmd: Completed command
917 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
922 if (cmd->scmd->use_sg) {
923 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
924 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
928 if (!cmd->scmd->request_bufflen)
931 opcode = cmd->frame->hdr.cmd;
933 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
935 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
937 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
940 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
942 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
945 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
946 cmd->scmd->sc_data_direction);
951 * megasas_complete_cmd - Completes a command
952 * @instance: Adapter soft state
953 * @cmd: Command to be completed
954 * @alt_status: If non-zero, use this value as status to
955 * SCSI mid-layer instead of the value returned
956 * by the FW. This should be used if caller wants
957 * an alternate status (as in the case of aborted
961 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
965 struct megasas_header *hdr = &cmd->frame->hdr;
969 cmd->scmd->SCp.ptr = (char *)0;
974 case MFI_CMD_PD_SCSI_IO:
975 case MFI_CMD_LD_SCSI_IO:
978 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
979 * issued either through an IO path or an IOCTL path. If it
980 * was via IOCTL, we will send it to internal completion.
984 megasas_complete_int_cmd(instance, cmd);
989 * Don't export physical disk devices to mid-layer.
991 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
992 (hdr->cmd_status == MFI_STAT_OK) &&
993 (cmd->scmd->cmnd[0] == INQUIRY)) {
995 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
997 cmd->scmd->result = DID_BAD_TARGET << 16;
1002 case MFI_CMD_LD_READ:
1003 case MFI_CMD_LD_WRITE:
1006 cmd->scmd->result = alt_status << 16;
1012 spin_lock_irqsave(&instance->instance_lock, flags);
1013 instance->fw_outstanding--;
1014 spin_unlock_irqrestore(&instance->instance_lock, flags);
1016 megasas_unmap_sgbuf(instance, cmd);
1017 cmd->scmd->scsi_done(cmd->scmd);
1018 megasas_return_cmd(instance, cmd);
1023 switch (hdr->cmd_status) {
1026 cmd->scmd->result = DID_OK << 16;
1029 case MFI_STAT_SCSI_IO_FAILED:
1030 case MFI_STAT_LD_INIT_IN_PROGRESS:
1032 (DID_ERROR << 16) | hdr->scsi_status;
1035 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1037 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1039 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1040 memset(cmd->scmd->sense_buffer, 0,
1041 SCSI_SENSE_BUFFERSIZE);
1042 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1045 cmd->scmd->result |= DRIVER_SENSE << 24;
1050 case MFI_STAT_LD_OFFLINE:
1051 case MFI_STAT_DEVICE_NOT_FOUND:
1052 cmd->scmd->result = DID_BAD_TARGET << 16;
1056 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1058 cmd->scmd->result = DID_ERROR << 16;
1062 spin_lock_irqsave(&instance->instance_lock, flags);
1063 instance->fw_outstanding--;
1064 spin_unlock_irqrestore(&instance->instance_lock, flags);
1066 megasas_unmap_sgbuf(instance, cmd);
1067 cmd->scmd->scsi_done(cmd->scmd);
1068 megasas_return_cmd(instance, cmd);
1077 * See if got an event notification
1079 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1080 megasas_service_aen(instance, cmd);
1082 megasas_complete_int_cmd(instance, cmd);
1088 * Cmd issued to abort another cmd returned
1090 megasas_complete_abort(instance, cmd);
1094 printk("megasas: Unknown command completed! [0x%X]\n",
1101 * megasas_deplete_reply_queue - Processes all completed commands
1102 * @instance: Adapter soft state
1103 * @alt_status: Alternate status to be returned to
1104 * SCSI mid-layer instead of the status
1105 * returned by the FW
1108 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1114 struct megasas_cmd *cmd;
1117 * Check if it is our interrupt
1119 status = readl(&instance->reg_set->outbound_intr_status);
1121 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1126 * Clear the interrupt by writing back the same value
1128 writel(status, &instance->reg_set->outbound_intr_status);
1130 producer = *instance->producer;
1131 consumer = *instance->consumer;
1133 while (consumer != producer) {
1134 context = instance->reply_queue[consumer];
1136 cmd = instance->cmd_list[context];
1138 megasas_complete_cmd(instance, cmd, alt_status);
1141 if (consumer == (instance->max_fw_cmds + 1)) {
1146 *instance->consumer = producer;
1152 * megasas_isr - isr entry point
1154 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1156 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1161 * megasas_transition_to_ready - Move the FW to READY state
1162 * @reg_set: MFI register set
1164 * During the initialization, FW passes can potentially be in any one of
1165 * several possible states. If the FW in operational, waiting-for-handshake
1166 * states, driver must take steps to bring it to ready state. Otherwise, it
1167 * has to wait for the ready state.
1170 megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1177 fw_state = readl(®_set->outbound_msg_0) & MFI_STATE_MASK;
1179 while (fw_state != MFI_STATE_READY) {
1181 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1185 case MFI_STATE_FAULT:
1187 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1190 case MFI_STATE_WAIT_HANDSHAKE:
1192 * Set the CLR bit in inbound doorbell
1194 writel(MFI_INIT_CLEAR_HANDSHAKE,
1195 ®_set->inbound_doorbell);
1198 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1201 case MFI_STATE_OPERATIONAL:
1203 * Bring it to READY state; assuming max wait 2 secs
1205 megasas_disable_intr(reg_set);
1206 writel(MFI_INIT_READY, ®_set->inbound_doorbell);
1209 cur_state = MFI_STATE_OPERATIONAL;
1212 case MFI_STATE_UNDEFINED:
1214 * This state should not last for more than 2 seconds
1217 cur_state = MFI_STATE_UNDEFINED;
1220 case MFI_STATE_BB_INIT:
1222 cur_state = MFI_STATE_BB_INIT;
1225 case MFI_STATE_FW_INIT:
1227 cur_state = MFI_STATE_FW_INIT;
1230 case MFI_STATE_FW_INIT_2:
1232 cur_state = MFI_STATE_FW_INIT_2;
1235 case MFI_STATE_DEVICE_SCAN:
1237 cur_state = MFI_STATE_DEVICE_SCAN;
1240 case MFI_STATE_FLUSH_CACHE:
1242 cur_state = MFI_STATE_FLUSH_CACHE;
1246 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1252 * The cur_state should not last for more than max_wait secs
1254 for (i = 0; i < (max_wait * 1000); i++) {
1255 fw_state = MFI_STATE_MASK &
1256 readl(®_set->outbound_msg_0);
1258 if (fw_state == cur_state) {
1265 * Return error if fw_state hasn't changed after max_wait
1267 if (fw_state == cur_state) {
1268 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1269 "in %d secs\n", fw_state, max_wait);
1278 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1279 * @instance: Adapter soft state
1281 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1284 u32 max_cmd = instance->max_fw_cmds;
1285 struct megasas_cmd *cmd;
1287 if (!instance->frame_dma_pool)
1291 * Return all frames to pool
1293 for (i = 0; i < max_cmd; i++) {
1295 cmd = instance->cmd_list[i];
1298 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1299 cmd->frame_phys_addr);
1302 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1303 cmd->sense_phys_addr);
1307 * Now destroy the pool itself
1309 pci_pool_destroy(instance->frame_dma_pool);
1310 pci_pool_destroy(instance->sense_dma_pool);
1312 instance->frame_dma_pool = NULL;
1313 instance->sense_dma_pool = NULL;
1317 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1318 * @instance: Adapter soft state
1320 * Each command packet has an embedded DMA memory buffer that is used for
1321 * filling MFI frame and the SG list that immediately follows the frame. This
1322 * function creates those DMA memory buffers for each command packet by using
1323 * PCI pool facility.
1325 static int megasas_create_frame_pool(struct megasas_instance *instance)
1333 struct megasas_cmd *cmd;
1335 max_cmd = instance->max_fw_cmds;
1338 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1339 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1341 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1342 sizeof(struct megasas_sge32);
1345 * Calculated the number of 64byte frames required for SGL
1347 sgl_sz = sge_sz * instance->max_num_sge;
1348 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1351 * We need one extra frame for the MFI command
1355 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1357 * Use DMA pool facility provided by PCI layer
1359 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1360 instance->pdev, total_sz, 64,
1363 if (!instance->frame_dma_pool) {
1364 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1368 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1369 instance->pdev, 128, 4, 0);
1371 if (!instance->sense_dma_pool) {
1372 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1374 pci_pool_destroy(instance->frame_dma_pool);
1375 instance->frame_dma_pool = NULL;
1381 * Allocate and attach a frame to each of the commands in cmd_list.
1382 * By making cmd->index as the context instead of the &cmd, we can
1383 * always use 32bit context regardless of the architecture
1385 for (i = 0; i < max_cmd; i++) {
1387 cmd = instance->cmd_list[i];
1389 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1390 GFP_KERNEL, &cmd->frame_phys_addr);
1392 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1393 GFP_KERNEL, &cmd->sense_phys_addr);
1396 * megasas_teardown_frame_pool() takes care of freeing
1397 * whatever has been allocated
1399 if (!cmd->frame || !cmd->sense) {
1400 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1401 megasas_teardown_frame_pool(instance);
1405 cmd->frame->io.context = cmd->index;
1412 * megasas_free_cmds - Free all the cmds in the free cmd pool
1413 * @instance: Adapter soft state
1415 static void megasas_free_cmds(struct megasas_instance *instance)
1418 /* First free the MFI frame pool */
1419 megasas_teardown_frame_pool(instance);
1421 /* Free all the commands in the cmd_list */
1422 for (i = 0; i < instance->max_fw_cmds; i++)
1423 kfree(instance->cmd_list[i]);
1425 /* Free the cmd_list buffer itself */
1426 kfree(instance->cmd_list);
1427 instance->cmd_list = NULL;
1429 INIT_LIST_HEAD(&instance->cmd_pool);
1433 * megasas_alloc_cmds - Allocates the command packets
1434 * @instance: Adapter soft state
1436 * Each command that is issued to the FW, whether IO commands from the OS or
1437 * internal commands like IOCTLs, are wrapped in local data structure called
1438 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1441 * Each frame has a 32-bit field called context (tag). This context is used
1442 * to get back the megasas_cmd from the frame when a frame gets completed in
1443 * the ISR. Typically the address of the megasas_cmd itself would be used as
1444 * the context. But we wanted to keep the differences between 32 and 64 bit
1445 * systems to the mininum. We always use 32 bit integers for the context. In
1446 * this driver, the 32 bit values are the indices into an array cmd_list.
1447 * This array is used only to look up the megasas_cmd given the context. The
1448 * free commands themselves are maintained in a linked list called cmd_pool.
1450 static int megasas_alloc_cmds(struct megasas_instance *instance)
1455 struct megasas_cmd *cmd;
1457 max_cmd = instance->max_fw_cmds;
1460 * instance->cmd_list is an array of struct megasas_cmd pointers.
1461 * Allocate the dynamic array first and then allocate individual
1464 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1467 if (!instance->cmd_list) {
1468 printk(KERN_DEBUG "megasas: out of memory\n");
1472 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1474 for (i = 0; i < max_cmd; i++) {
1475 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1478 if (!instance->cmd_list[i]) {
1480 for (j = 0; j < i; j++)
1481 kfree(instance->cmd_list[j]);
1483 kfree(instance->cmd_list);
1484 instance->cmd_list = NULL;
1491 * Add all the commands to command pool (instance->cmd_pool)
1493 for (i = 0; i < max_cmd; i++) {
1494 cmd = instance->cmd_list[i];
1495 memset(cmd, 0, sizeof(struct megasas_cmd));
1497 cmd->instance = instance;
1499 list_add_tail(&cmd->list, &instance->cmd_pool);
1503 * Create a frame pool and assign one frame to each cmd
1505 if (megasas_create_frame_pool(instance)) {
1506 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1507 megasas_free_cmds(instance);
1514 * megasas_get_controller_info - Returns FW's controller structure
1515 * @instance: Adapter soft state
1516 * @ctrl_info: Controller information structure
1518 * Issues an internal command (DCMD) to get the FW's controller structure.
1519 * This information is mainly used to find out the maximum IO transfer per
1520 * command supported by the FW.
1523 megasas_get_ctrl_info(struct megasas_instance *instance,
1524 struct megasas_ctrl_info *ctrl_info)
1527 struct megasas_cmd *cmd;
1528 struct megasas_dcmd_frame *dcmd;
1529 struct megasas_ctrl_info *ci;
1530 dma_addr_t ci_h = 0;
1532 cmd = megasas_get_cmd(instance);
1535 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1539 dcmd = &cmd->frame->dcmd;
1541 ci = pci_alloc_consistent(instance->pdev,
1542 sizeof(struct megasas_ctrl_info), &ci_h);
1545 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1546 megasas_return_cmd(instance, cmd);
1550 memset(ci, 0, sizeof(*ci));
1551 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1553 dcmd->cmd = MFI_CMD_DCMD;
1554 dcmd->cmd_status = 0xFF;
1555 dcmd->sge_count = 1;
1556 dcmd->flags = MFI_FRAME_DIR_READ;
1558 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1559 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1560 dcmd->sgl.sge32[0].phys_addr = ci_h;
1561 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1563 if (!megasas_issue_polled(instance, cmd)) {
1565 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1570 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1573 megasas_return_cmd(instance, cmd);
1578 * megasas_init_mfi - Initializes the FW
1579 * @instance: Adapter soft state
1581 * This is the main function for initializing MFI firmware.
1583 static int megasas_init_mfi(struct megasas_instance *instance)
1589 struct megasas_register_set __iomem *reg_set;
1591 struct megasas_cmd *cmd;
1592 struct megasas_ctrl_info *ctrl_info;
1594 struct megasas_init_frame *init_frame;
1595 struct megasas_init_queue_info *initq_info;
1596 dma_addr_t init_frame_h;
1597 dma_addr_t initq_info_h;
1600 * Map the message registers
1602 instance->base_addr = pci_resource_start(instance->pdev, 0);
1604 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1605 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1609 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1611 if (!instance->reg_set) {
1612 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1616 reg_set = instance->reg_set;
1619 * We expect the FW state to be READY
1621 if (megasas_transition_to_ready(instance->reg_set))
1622 goto fail_ready_state;
1625 * Get various operational parameters from status register
1627 instance->max_fw_cmds = readl(®_set->outbound_msg_0) & 0x00FFFF;
1628 instance->max_num_sge = (readl(®_set->outbound_msg_0) & 0xFF0000) >>
1631 * Create a pool of commands
1633 if (megasas_alloc_cmds(instance))
1634 goto fail_alloc_cmds;
1637 * Allocate memory for reply queue. Length of reply queue should
1638 * be _one_ more than the maximum commands handled by the firmware.
1640 * Note: When FW completes commands, it places corresponding contex
1641 * values in this circular reply queue. This circular queue is a fairly
1642 * typical producer-consumer queue. FW is the producer (of completed
1643 * commands) and the driver is the consumer.
1645 context_sz = sizeof(u32);
1646 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1648 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1650 &instance->reply_queue_h);
1652 if (!instance->reply_queue) {
1653 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1654 goto fail_reply_queue;
1658 * Prepare a init frame. Note the init frame points to queue info
1659 * structure. Each frame has SGL allocated after first 64 bytes. For
1660 * this frame - since we don't need any SGL - we use SGL's space as
1661 * queue info structure
1663 * We will not get a NULL command below. We just created the pool.
1665 cmd = megasas_get_cmd(instance);
1667 init_frame = (struct megasas_init_frame *)cmd->frame;
1668 initq_info = (struct megasas_init_queue_info *)
1669 ((unsigned long)init_frame + 64);
1671 init_frame_h = cmd->frame_phys_addr;
1672 initq_info_h = init_frame_h + 64;
1674 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1675 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1677 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1678 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1680 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1681 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1683 init_frame->cmd = MFI_CMD_INIT;
1684 init_frame->cmd_status = 0xFF;
1685 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1687 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1690 * Issue the init frame in polled mode
1692 if (megasas_issue_polled(instance, cmd)) {
1693 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1697 megasas_return_cmd(instance, cmd);
1699 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1702 * Compute the max allowed sectors per IO: The controller info has two
1703 * limits on max sectors. Driver should use the minimum of these two.
1705 * 1 << stripe_sz_ops.min = max sectors per strip
1707 * Note that older firmwares ( < FW ver 30) didn't report information
1708 * to calculate max_sectors_1. So the number ended up as zero always.
1710 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1712 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1713 ctrl_info->max_strips_per_io;
1714 max_sectors_2 = ctrl_info->max_request_size;
1716 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1717 ? max_sectors_1 : max_sectors_2;
1719 instance->max_sectors_per_req = instance->max_num_sge *
1727 megasas_return_cmd(instance, cmd);
1729 pci_free_consistent(instance->pdev, reply_q_sz,
1730 instance->reply_queue, instance->reply_queue_h);
1732 megasas_free_cmds(instance);
1736 iounmap(instance->reg_set);
1739 pci_release_regions(instance->pdev);
1745 * megasas_release_mfi - Reverses the FW initialization
1746 * @intance: Adapter soft state
1748 static void megasas_release_mfi(struct megasas_instance *instance)
1750 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1752 pci_free_consistent(instance->pdev, reply_q_sz,
1753 instance->reply_queue, instance->reply_queue_h);
1755 megasas_free_cmds(instance);
1757 iounmap(instance->reg_set);
1759 pci_release_regions(instance->pdev);
1763 * megasas_get_seq_num - Gets latest event sequence numbers
1764 * @instance: Adapter soft state
1765 * @eli: FW event log sequence numbers information
1767 * FW maintains a log of all events in a non-volatile area. Upper layers would
1768 * usually find out the latest sequence number of the events, the seq number at
1769 * the boot etc. They would "read" all the events below the latest seq number
1770 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1771 * number), they would subsribe to AEN (asynchronous event notification) and
1772 * wait for the events to happen.
1775 megasas_get_seq_num(struct megasas_instance *instance,
1776 struct megasas_evt_log_info *eli)
1778 struct megasas_cmd *cmd;
1779 struct megasas_dcmd_frame *dcmd;
1780 struct megasas_evt_log_info *el_info;
1781 dma_addr_t el_info_h = 0;
1783 cmd = megasas_get_cmd(instance);
1789 dcmd = &cmd->frame->dcmd;
1790 el_info = pci_alloc_consistent(instance->pdev,
1791 sizeof(struct megasas_evt_log_info),
1795 megasas_return_cmd(instance, cmd);
1799 memset(el_info, 0, sizeof(*el_info));
1800 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1802 dcmd->cmd = MFI_CMD_DCMD;
1803 dcmd->cmd_status = 0x0;
1804 dcmd->sge_count = 1;
1805 dcmd->flags = MFI_FRAME_DIR_READ;
1807 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1808 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1809 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1810 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1812 megasas_issue_blocked_cmd(instance, cmd);
1815 * Copy the data back into callers buffer
1817 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1819 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1820 el_info, el_info_h);
1822 megasas_return_cmd(instance, cmd);
1828 * megasas_register_aen - Registers for asynchronous event notification
1829 * @instance: Adapter soft state
1830 * @seq_num: The starting sequence number
1831 * @class_locale: Class of the event
1833 * This function subscribes for AEN for events beyond the @seq_num. It requests
1834 * to be notified if and only if the event is of type @class_locale
1837 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1838 u32 class_locale_word)
1841 struct megasas_cmd *cmd;
1842 struct megasas_dcmd_frame *dcmd;
1843 union megasas_evt_class_locale curr_aen;
1844 union megasas_evt_class_locale prev_aen;
1847 * If there an AEN pending already (aen_cmd), check if the
1848 * class_locale of that pending AEN is inclusive of the new
1849 * AEN request we currently have. If it is, then we don't have
1850 * to do anything. In other words, whichever events the current
1851 * AEN request is subscribing to, have already been subscribed
1854 * If the old_cmd is _not_ inclusive, then we have to abort
1855 * that command, form a class_locale that is superset of both
1856 * old and current and re-issue to the FW
1859 curr_aen.word = class_locale_word;
1861 if (instance->aen_cmd) {
1863 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1866 * A class whose enum value is smaller is inclusive of all
1867 * higher values. If a PROGRESS (= -1) was previously
1868 * registered, then a new registration requests for higher
1869 * classes need not be sent to FW. They are automatically
1872 * Locale numbers don't have such hierarchy. They are bitmap
1875 if ((prev_aen.members.class <= curr_aen.members.class) &&
1876 !((prev_aen.members.locale & curr_aen.members.locale) ^
1877 curr_aen.members.locale)) {
1879 * Previously issued event registration includes
1880 * current request. Nothing to do.
1884 curr_aen.members.locale |= prev_aen.members.locale;
1886 if (prev_aen.members.class < curr_aen.members.class)
1887 curr_aen.members.class = prev_aen.members.class;
1889 instance->aen_cmd->abort_aen = 1;
1890 ret_val = megasas_issue_blocked_abort_cmd(instance,
1895 printk(KERN_DEBUG "megasas: Failed to abort "
1896 "previous AEN command\n");
1902 cmd = megasas_get_cmd(instance);
1907 dcmd = &cmd->frame->dcmd;
1909 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1912 * Prepare DCMD for aen registration
1914 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1916 dcmd->cmd = MFI_CMD_DCMD;
1917 dcmd->cmd_status = 0x0;
1918 dcmd->sge_count = 1;
1919 dcmd->flags = MFI_FRAME_DIR_READ;
1921 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1922 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1923 dcmd->mbox.w[0] = seq_num;
1924 dcmd->mbox.w[1] = curr_aen.word;
1925 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1926 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1929 * Store reference to the cmd used to register for AEN. When an
1930 * application wants us to register for AEN, we have to abort this
1931 * cmd and re-register with a new EVENT LOCALE supplied by that app
1933 instance->aen_cmd = cmd;
1936 * Issue the aen registration frame
1938 writel(cmd->frame_phys_addr >> 3,
1939 &instance->reg_set->inbound_queue_port);
1945 * megasas_start_aen - Subscribes to AEN during driver load time
1946 * @instance: Adapter soft state
1948 static int megasas_start_aen(struct megasas_instance *instance)
1950 struct megasas_evt_log_info eli;
1951 union megasas_evt_class_locale class_locale;
1954 * Get the latest sequence number from FW
1956 memset(&eli, 0, sizeof(eli));
1958 if (megasas_get_seq_num(instance, &eli))
1962 * Register AEN with FW for latest sequence number plus 1
1964 class_locale.members.reserved = 0;
1965 class_locale.members.locale = MR_EVT_LOCALE_ALL;
1966 class_locale.members.class = MR_EVT_CLASS_DEBUG;
1968 return megasas_register_aen(instance, eli.newest_seq_num + 1,
1973 * megasas_io_attach - Attaches this driver to SCSI mid-layer
1974 * @instance: Adapter soft state
1976 static int megasas_io_attach(struct megasas_instance *instance)
1978 struct Scsi_Host *host = instance->host;
1981 * Export parameters required by SCSI mid-layer
1983 host->irq = instance->pdev->irq;
1984 host->unique_id = instance->unique_id;
1985 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1986 host->this_id = instance->init_id;
1987 host->sg_tablesize = instance->max_num_sge;
1988 host->max_sectors = instance->max_sectors_per_req;
1989 host->cmd_per_lun = 128;
1990 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1991 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1992 host->max_lun = MEGASAS_MAX_LUN;
1995 * Notify the mid-layer about the new controller
1997 if (scsi_add_host(host, &instance->pdev->dev)) {
1998 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2003 * Trigger SCSI to scan our drives
2005 scsi_scan_host(host);
2010 * megasas_probe_one - PCI hotplug entry point
2011 * @pdev: PCI device structure
2012 * @id: PCI ids of supported hotplugged adapter
2014 static int __devinit
2015 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2018 struct Scsi_Host *host;
2019 struct megasas_instance *instance;
2022 * Announce PCI information
2024 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2025 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2026 pdev->subsystem_device);
2028 printk("bus %d:slot %d:func %d\n",
2029 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2032 * PCI prepping: enable device set bus mastering and dma mask
2034 rval = pci_enable_device(pdev);
2040 pci_set_master(pdev);
2043 * All our contollers are capable of performing 64-bit DMA
2046 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2048 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2049 goto fail_set_dma_mask;
2052 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2053 goto fail_set_dma_mask;
2056 host = scsi_host_alloc(&megasas_template,
2057 sizeof(struct megasas_instance));
2060 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2061 goto fail_alloc_instance;
2064 instance = (struct megasas_instance *)host->hostdata;
2065 memset(instance, 0, sizeof(*instance));
2067 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2068 &instance->producer_h);
2069 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2070 &instance->consumer_h);
2072 if (!instance->producer || !instance->consumer) {
2073 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2074 "producer, consumer\n");
2075 goto fail_alloc_dma_buf;
2078 *instance->producer = 0;
2079 *instance->consumer = 0;
2081 instance->evt_detail = pci_alloc_consistent(pdev,
2083 megasas_evt_detail),
2084 &instance->evt_detail_h);
2086 if (!instance->evt_detail) {
2087 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2088 "event detail structure\n");
2089 goto fail_alloc_dma_buf;
2093 * Initialize locks and queues
2095 INIT_LIST_HEAD(&instance->cmd_pool);
2097 init_waitqueue_head(&instance->int_cmd_wait_q);
2098 init_waitqueue_head(&instance->abort_cmd_wait_q);
2100 spin_lock_init(&instance->cmd_pool_lock);
2101 spin_lock_init(&instance->instance_lock);
2103 sema_init(&instance->aen_mutex, 1);
2104 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2107 * Initialize PCI related and misc parameters
2109 instance->pdev = pdev;
2110 instance->host = host;
2111 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2112 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2115 * Initialize MFI Firmware
2117 if (megasas_init_mfi(instance))
2123 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2124 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2128 megasas_enable_intr(instance->reg_set);
2131 * Store instance in PCI softstate
2133 pci_set_drvdata(pdev, instance);
2136 * Add this controller to megasas_mgmt_info structure so that it
2137 * can be exported to management applications
2139 megasas_mgmt_info.count++;
2140 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2141 megasas_mgmt_info.max_index++;
2144 * Initiate AEN (Asynchronous Event Notification)
2146 if (megasas_start_aen(instance)) {
2147 printk(KERN_DEBUG "megasas: start aen failed\n");
2148 goto fail_start_aen;
2152 * Register with SCSI mid-layer
2154 if (megasas_io_attach(instance))
2155 goto fail_io_attach;
2161 megasas_mgmt_info.count--;
2162 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2163 megasas_mgmt_info.max_index--;
2165 pci_set_drvdata(pdev, NULL);
2166 megasas_disable_intr(instance->reg_set);
2167 free_irq(instance->pdev->irq, instance);
2169 megasas_release_mfi(instance);
2174 if (instance->evt_detail)
2175 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2176 instance->evt_detail,
2177 instance->evt_detail_h);
2179 if (instance->producer)
2180 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2181 instance->producer_h);
2182 if (instance->consumer)
2183 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2184 instance->consumer_h);
2185 scsi_host_put(host);
2187 fail_alloc_instance:
2189 pci_disable_device(pdev);
2195 * megasas_flush_cache - Requests FW to flush all its caches
2196 * @instance: Adapter soft state
2198 static void megasas_flush_cache(struct megasas_instance *instance)
2200 struct megasas_cmd *cmd;
2201 struct megasas_dcmd_frame *dcmd;
2203 cmd = megasas_get_cmd(instance);
2208 dcmd = &cmd->frame->dcmd;
2210 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2212 dcmd->cmd = MFI_CMD_DCMD;
2213 dcmd->cmd_status = 0x0;
2214 dcmd->sge_count = 0;
2215 dcmd->flags = MFI_FRAME_DIR_NONE;
2217 dcmd->data_xfer_len = 0;
2218 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2219 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2221 megasas_issue_blocked_cmd(instance, cmd);
2223 megasas_return_cmd(instance, cmd);
2229 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2230 * @instance: Adapter soft state
2232 static void megasas_shutdown_controller(struct megasas_instance *instance)
2234 struct megasas_cmd *cmd;
2235 struct megasas_dcmd_frame *dcmd;
2237 cmd = megasas_get_cmd(instance);
2242 if (instance->aen_cmd)
2243 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2245 dcmd = &cmd->frame->dcmd;
2247 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2249 dcmd->cmd = MFI_CMD_DCMD;
2250 dcmd->cmd_status = 0x0;
2251 dcmd->sge_count = 0;
2252 dcmd->flags = MFI_FRAME_DIR_NONE;
2254 dcmd->data_xfer_len = 0;
2255 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2257 megasas_issue_blocked_cmd(instance, cmd);
2259 megasas_return_cmd(instance, cmd);
2265 * megasas_detach_one - PCI hot"un"plug entry point
2266 * @pdev: PCI device structure
2268 static void megasas_detach_one(struct pci_dev *pdev)
2271 struct Scsi_Host *host;
2272 struct megasas_instance *instance;
2274 instance = pci_get_drvdata(pdev);
2275 host = instance->host;
2277 scsi_remove_host(instance->host);
2278 megasas_flush_cache(instance);
2279 megasas_shutdown_controller(instance);
2282 * Take the instance off the instance array. Note that we will not
2283 * decrement the max_index. We let this array be sparse array
2285 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2286 if (megasas_mgmt_info.instance[i] == instance) {
2287 megasas_mgmt_info.count--;
2288 megasas_mgmt_info.instance[i] = NULL;
2294 pci_set_drvdata(instance->pdev, NULL);
2296 megasas_disable_intr(instance->reg_set);
2298 free_irq(instance->pdev->irq, instance);
2300 megasas_release_mfi(instance);
2302 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2303 instance->evt_detail, instance->evt_detail_h);
2305 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2306 instance->producer_h);
2308 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2309 instance->consumer_h);
2311 scsi_host_put(host);
2313 pci_set_drvdata(pdev, NULL);
2315 pci_disable_device(pdev);
2321 * megasas_shutdown - Shutdown entry point
2322 * @device: Generic device structure
2324 static void megasas_shutdown(struct pci_dev *pdev)
2326 struct megasas_instance *instance = pci_get_drvdata(pdev);
2327 megasas_flush_cache(instance);
2331 * megasas_mgmt_open - char node "open" entry point
2333 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2336 * Allow only those users with admin rights
2338 if (!capable(CAP_SYS_ADMIN))
2345 * megasas_mgmt_release - char node "release" entry point
2347 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2349 filep->private_data = NULL;
2350 fasync_helper(-1, filep, 0, &megasas_async_queue);
2356 * megasas_mgmt_fasync - Async notifier registration from applications
2358 * This function adds the calling process to a driver global queue. When an
2359 * event occurs, SIGIO will be sent to all processes in this queue.
2361 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2365 down(&megasas_async_queue_mutex);
2367 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2369 up(&megasas_async_queue_mutex);
2372 /* For sanity check when we get ioctl */
2373 filep->private_data = filep;
2377 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2383 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2384 * @instance: Adapter soft state
2385 * @argp: User's ioctl packet
2388 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2389 struct megasas_iocpacket __user * user_ioc,
2390 struct megasas_iocpacket *ioc)
2392 struct megasas_sge32 *kern_sge32;
2393 struct megasas_cmd *cmd;
2394 void *kbuff_arr[MAX_IOCTL_SGE];
2395 dma_addr_t buf_handle = 0;
2398 dma_addr_t sense_handle;
2401 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2403 if (ioc->sge_count > MAX_IOCTL_SGE) {
2404 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2405 ioc->sge_count, MAX_IOCTL_SGE);
2409 cmd = megasas_get_cmd(instance);
2411 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2416 * User's IOCTL packet has 2 frames (maximum). Copy those two
2417 * frames into our cmd's frames. cmd->frame's context will get
2418 * overwritten when we copy from user's frames. So set that value
2421 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2422 cmd->frame->hdr.context = cmd->index;
2425 * The management interface between applications and the fw uses
2426 * MFI frames. E.g, RAID configuration changes, LD property changes
2427 * etc are accomplishes through different kinds of MFI frames. The
2428 * driver needs to care only about substituting user buffers with
2429 * kernel buffers in SGLs. The location of SGL is embedded in the
2430 * struct iocpacket itself.
2432 kern_sge32 = (struct megasas_sge32 *)
2433 ((unsigned long)cmd->frame + ioc->sgl_off);
2436 * For each user buffer, create a mirror buffer and copy in
2438 for (i = 0; i < ioc->sge_count; i++) {
2439 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2440 ioc->sgl[i].iov_len,
2442 if (!kbuff_arr[i]) {
2443 printk(KERN_DEBUG "megasas: Failed to alloc "
2444 "kernel SGL buffer for IOCTL \n");
2450 * We don't change the dma_coherent_mask, so
2451 * pci_alloc_consistent only returns 32bit addresses
2453 kern_sge32[i].phys_addr = (u32) buf_handle;
2454 kern_sge32[i].length = ioc->sgl[i].iov_len;
2457 * We created a kernel buffer corresponding to the
2458 * user buffer. Now copy in from the user buffer
2460 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2461 (u32) (ioc->sgl[i].iov_len))) {
2467 if (ioc->sense_len) {
2468 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2476 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2477 *sense_ptr = sense_handle;
2481 * Set the sync_cmd flag so that the ISR knows not to complete this
2482 * cmd to the SCSI mid-layer
2485 megasas_issue_blocked_cmd(instance, cmd);
2489 * copy out the kernel buffers to user buffers
2491 for (i = 0; i < ioc->sge_count; i++) {
2492 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2493 ioc->sgl[i].iov_len)) {
2500 * copy out the sense
2502 if (ioc->sense_len) {
2504 * sense_ptr points to the location that has the user
2505 * sense buffer address
2507 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2510 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2511 sense, ioc->sense_len)) {
2518 * copy the status codes returned by the fw
2520 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2521 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2522 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2528 pci_free_consistent(instance->pdev, ioc->sense_len,
2529 sense, sense_handle);
2532 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2533 pci_free_consistent(instance->pdev,
2534 kern_sge32[i].length,
2535 kbuff_arr[i], kern_sge32[i].phys_addr);
2538 megasas_return_cmd(instance, cmd);
2542 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2546 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2548 if ((megasas_mgmt_info.instance[i]) &&
2549 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2550 return megasas_mgmt_info.instance[i];
2556 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2558 struct megasas_iocpacket __user *user_ioc =
2559 (struct megasas_iocpacket __user *)arg;
2560 struct megasas_iocpacket *ioc;
2561 struct megasas_instance *instance;
2564 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2568 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2573 instance = megasas_lookup_instance(ioc->host_no);
2580 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2582 if (down_interruptible(&instance->ioctl_sem)) {
2583 error = -ERESTARTSYS;
2586 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2587 up(&instance->ioctl_sem);
2594 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2596 struct megasas_instance *instance;
2597 struct megasas_aen aen;
2600 if (file->private_data != file) {
2601 printk(KERN_DEBUG "megasas: fasync_helper was not "
2606 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2609 instance = megasas_lookup_instance(aen.host_no);
2614 down(&instance->aen_mutex);
2615 error = megasas_register_aen(instance, aen.seq_num,
2616 aen.class_locale_word);
2617 up(&instance->aen_mutex);
2622 * megasas_mgmt_ioctl - char node ioctl entry point
2625 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2628 case MEGASAS_IOC_FIRMWARE:
2629 return megasas_mgmt_ioctl_fw(file, arg);
2631 case MEGASAS_IOC_GET_AEN:
2632 return megasas_mgmt_ioctl_aen(file, arg);
2638 #ifdef CONFIG_COMPAT
2639 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2641 struct compat_megasas_iocpacket __user *cioc =
2642 (struct compat_megasas_iocpacket __user *)arg;
2643 struct megasas_iocpacket __user *ioc =
2644 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2648 clear_user(ioc, sizeof(*ioc));
2650 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2651 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2652 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2653 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2654 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2655 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2658 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2661 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2662 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2663 copy_in_user(&ioc->sgl[i].iov_len,
2664 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2668 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2670 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2671 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2672 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2679 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2683 case MEGASAS_IOC_FIRMWARE:{
2684 return megasas_mgmt_compat_ioctl_fw(file, arg);
2686 case MEGASAS_IOC_GET_AEN:
2687 return megasas_mgmt_ioctl_aen(file, arg);
2695 * File operations structure for management interface
2697 static struct file_operations megasas_mgmt_fops = {
2698 .owner = THIS_MODULE,
2699 .open = megasas_mgmt_open,
2700 .release = megasas_mgmt_release,
2701 .fasync = megasas_mgmt_fasync,
2702 .unlocked_ioctl = megasas_mgmt_ioctl,
2703 #ifdef CONFIG_COMPAT
2704 .compat_ioctl = megasas_mgmt_compat_ioctl,
2709 * PCI hotplug support registration structure
2711 static struct pci_driver megasas_pci_driver = {
2713 .name = "megaraid_sas",
2714 .id_table = megasas_pci_table,
2715 .probe = megasas_probe_one,
2716 .remove = __devexit_p(megasas_detach_one),
2717 .shutdown = megasas_shutdown,
2721 * Sysfs driver attributes
2723 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2725 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2729 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2732 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2734 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2738 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2742 * megasas_init - Driver load entry point
2744 static int __init megasas_init(void)
2749 * Announce driver version and other information
2751 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2752 MEGASAS_EXT_VERSION);
2754 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2757 * Register character device node
2759 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2762 printk(KERN_DEBUG "megasas: failed to open device node\n");
2766 megasas_mgmt_majorno = rval;
2769 * Register ourselves as PCI hotplug module
2771 rval = pci_module_init(&megasas_pci_driver);
2774 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2775 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2778 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2779 driver_create_file(&megasas_pci_driver.driver,
2780 &driver_attr_release_date);
2786 * megasas_exit - Driver unload entry point
2788 static void __exit megasas_exit(void)
2790 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2791 driver_remove_file(&megasas_pci_driver.driver,
2792 &driver_attr_release_date);
2794 pci_unregister_driver(&megasas_pci_driver);
2795 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2798 module_init(megasas_init);
2799 module_exit(megasas_exit);