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 spin_unlock(scmd->device->host->host_lock);
771 ret_val = megasas_wait_for_outstanding(instance);
773 if (ret_val == SUCCESS)
774 printk(KERN_NOTICE "megasas: reset successful \n");
776 printk(KERN_ERR "megasas: failed to do reset\n");
778 spin_lock(scmd->device->host->host_lock);
783 static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
785 unsigned long seconds;
788 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
791 return EH_RESET_TIMER;
793 return EH_NOT_HANDLED;
801 * megasas_reset_device - Device reset handler entry point
803 static int megasas_reset_device(struct scsi_cmnd *scmd)
808 * First wait for all commands to complete
810 ret = megasas_generic_reset(scmd);
816 * megasas_reset_bus_host - Bus & host reset handler entry point
818 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
823 * Frist wait for all commands to complete
825 ret = megasas_generic_reset(scmd);
831 * megasas_service_aen - Processes an event notification
832 * @instance: Adapter soft state
833 * @cmd: AEN command completed by the ISR
835 * For AEN, driver sends a command down to FW that is held by the FW till an
836 * event occurs. When an event of interest occurs, FW completes the command
837 * that it was previously holding.
839 * This routines sends SIGIO signal to processes that have registered with the
843 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
846 * Don't signal app if it is just an aborted previously registered aen
849 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
853 instance->aen_cmd = NULL;
854 megasas_return_cmd(instance, cmd);
858 * Scsi host template for megaraid_sas driver
860 static struct scsi_host_template megasas_template = {
862 .module = THIS_MODULE,
863 .name = "LSI Logic SAS based MegaRAID driver",
864 .proc_name = "megaraid_sas",
865 .queuecommand = megasas_queue_command,
866 .eh_device_reset_handler = megasas_reset_device,
867 .eh_bus_reset_handler = megasas_reset_bus_host,
868 .eh_host_reset_handler = megasas_reset_bus_host,
869 .eh_timed_out = megasas_reset_timer,
870 .use_clustering = ENABLE_CLUSTERING,
874 * megasas_complete_int_cmd - Completes an internal command
875 * @instance: Adapter soft state
876 * @cmd: Command to be completed
878 * The megasas_issue_blocked_cmd() function waits for a command to complete
879 * after it issues a command. This function wakes up that waiting routine by
880 * calling wake_up() on the wait queue.
883 megasas_complete_int_cmd(struct megasas_instance *instance,
884 struct megasas_cmd *cmd)
886 cmd->cmd_status = cmd->frame->io.cmd_status;
888 if (cmd->cmd_status == ENODATA) {
891 wake_up(&instance->int_cmd_wait_q);
895 * megasas_complete_abort - Completes aborting a command
896 * @instance: Adapter soft state
897 * @cmd: Cmd that was issued to abort another cmd
899 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
900 * after it issues an abort on a previously issued command. This function
901 * wakes up all functions waiting on the same wait queue.
904 megasas_complete_abort(struct megasas_instance *instance,
905 struct megasas_cmd *cmd)
910 wake_up(&instance->abort_cmd_wait_q);
917 * megasas_unmap_sgbuf - Unmap SG buffers
918 * @instance: Adapter soft state
919 * @cmd: Completed command
922 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
927 if (cmd->scmd->use_sg) {
928 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
929 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
933 if (!cmd->scmd->request_bufflen)
936 opcode = cmd->frame->hdr.cmd;
938 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
940 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
942 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
945 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
947 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
950 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
951 cmd->scmd->sc_data_direction);
956 * megasas_complete_cmd - Completes a command
957 * @instance: Adapter soft state
958 * @cmd: Command to be completed
959 * @alt_status: If non-zero, use this value as status to
960 * SCSI mid-layer instead of the value returned
961 * by the FW. This should be used if caller wants
962 * an alternate status (as in the case of aborted
966 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
970 struct megasas_header *hdr = &cmd->frame->hdr;
974 cmd->scmd->SCp.ptr = (char *)0;
979 case MFI_CMD_PD_SCSI_IO:
980 case MFI_CMD_LD_SCSI_IO:
983 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
984 * issued either through an IO path or an IOCTL path. If it
985 * was via IOCTL, we will send it to internal completion.
989 megasas_complete_int_cmd(instance, cmd);
994 * Don't export physical disk devices to mid-layer.
996 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
997 (hdr->cmd_status == MFI_STAT_OK) &&
998 (cmd->scmd->cmnd[0] == INQUIRY)) {
1000 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
1002 cmd->scmd->result = DID_BAD_TARGET << 16;
1007 case MFI_CMD_LD_READ:
1008 case MFI_CMD_LD_WRITE:
1011 cmd->scmd->result = alt_status << 16;
1017 spin_lock_irqsave(&instance->instance_lock, flags);
1018 instance->fw_outstanding--;
1019 spin_unlock_irqrestore(&instance->instance_lock, flags);
1021 megasas_unmap_sgbuf(instance, cmd);
1022 cmd->scmd->scsi_done(cmd->scmd);
1023 megasas_return_cmd(instance, cmd);
1028 switch (hdr->cmd_status) {
1031 cmd->scmd->result = DID_OK << 16;
1034 case MFI_STAT_SCSI_IO_FAILED:
1035 case MFI_STAT_LD_INIT_IN_PROGRESS:
1037 (DID_ERROR << 16) | hdr->scsi_status;
1040 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1042 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1044 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1045 memset(cmd->scmd->sense_buffer, 0,
1046 SCSI_SENSE_BUFFERSIZE);
1047 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1050 cmd->scmd->result |= DRIVER_SENSE << 24;
1055 case MFI_STAT_LD_OFFLINE:
1056 case MFI_STAT_DEVICE_NOT_FOUND:
1057 cmd->scmd->result = DID_BAD_TARGET << 16;
1061 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1063 cmd->scmd->result = DID_ERROR << 16;
1067 spin_lock_irqsave(&instance->instance_lock, flags);
1068 instance->fw_outstanding--;
1069 spin_unlock_irqrestore(&instance->instance_lock, flags);
1071 megasas_unmap_sgbuf(instance, cmd);
1072 cmd->scmd->scsi_done(cmd->scmd);
1073 megasas_return_cmd(instance, cmd);
1082 * See if got an event notification
1084 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1085 megasas_service_aen(instance, cmd);
1087 megasas_complete_int_cmd(instance, cmd);
1093 * Cmd issued to abort another cmd returned
1095 megasas_complete_abort(instance, cmd);
1099 printk("megasas: Unknown command completed! [0x%X]\n",
1106 * megasas_deplete_reply_queue - Processes all completed commands
1107 * @instance: Adapter soft state
1108 * @alt_status: Alternate status to be returned to
1109 * SCSI mid-layer instead of the status
1110 * returned by the FW
1113 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1119 struct megasas_cmd *cmd;
1122 * Check if it is our interrupt
1124 status = readl(&instance->reg_set->outbound_intr_status);
1126 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1131 * Clear the interrupt by writing back the same value
1133 writel(status, &instance->reg_set->outbound_intr_status);
1135 producer = *instance->producer;
1136 consumer = *instance->consumer;
1138 while (consumer != producer) {
1139 context = instance->reply_queue[consumer];
1141 cmd = instance->cmd_list[context];
1143 megasas_complete_cmd(instance, cmd, alt_status);
1146 if (consumer == (instance->max_fw_cmds + 1)) {
1151 *instance->consumer = producer;
1157 * megasas_isr - isr entry point
1159 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1161 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1166 * megasas_transition_to_ready - Move the FW to READY state
1167 * @reg_set: MFI register set
1169 * During the initialization, FW passes can potentially be in any one of
1170 * several possible states. If the FW in operational, waiting-for-handshake
1171 * states, driver must take steps to bring it to ready state. Otherwise, it
1172 * has to wait for the ready state.
1175 megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1182 fw_state = readl(®_set->outbound_msg_0) & MFI_STATE_MASK;
1184 while (fw_state != MFI_STATE_READY) {
1186 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1190 case MFI_STATE_FAULT:
1192 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1195 case MFI_STATE_WAIT_HANDSHAKE:
1197 * Set the CLR bit in inbound doorbell
1199 writel(MFI_INIT_CLEAR_HANDSHAKE,
1200 ®_set->inbound_doorbell);
1203 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1206 case MFI_STATE_OPERATIONAL:
1208 * Bring it to READY state; assuming max wait 2 secs
1210 megasas_disable_intr(reg_set);
1211 writel(MFI_INIT_READY, ®_set->inbound_doorbell);
1214 cur_state = MFI_STATE_OPERATIONAL;
1217 case MFI_STATE_UNDEFINED:
1219 * This state should not last for more than 2 seconds
1222 cur_state = MFI_STATE_UNDEFINED;
1225 case MFI_STATE_BB_INIT:
1227 cur_state = MFI_STATE_BB_INIT;
1230 case MFI_STATE_FW_INIT:
1232 cur_state = MFI_STATE_FW_INIT;
1235 case MFI_STATE_FW_INIT_2:
1237 cur_state = MFI_STATE_FW_INIT_2;
1240 case MFI_STATE_DEVICE_SCAN:
1242 cur_state = MFI_STATE_DEVICE_SCAN;
1245 case MFI_STATE_FLUSH_CACHE:
1247 cur_state = MFI_STATE_FLUSH_CACHE;
1251 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1257 * The cur_state should not last for more than max_wait secs
1259 for (i = 0; i < (max_wait * 1000); i++) {
1260 fw_state = MFI_STATE_MASK &
1261 readl(®_set->outbound_msg_0);
1263 if (fw_state == cur_state) {
1270 * Return error if fw_state hasn't changed after max_wait
1272 if (fw_state == cur_state) {
1273 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1274 "in %d secs\n", fw_state, max_wait);
1283 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1284 * @instance: Adapter soft state
1286 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1289 u32 max_cmd = instance->max_fw_cmds;
1290 struct megasas_cmd *cmd;
1292 if (!instance->frame_dma_pool)
1296 * Return all frames to pool
1298 for (i = 0; i < max_cmd; i++) {
1300 cmd = instance->cmd_list[i];
1303 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1304 cmd->frame_phys_addr);
1307 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1308 cmd->sense_phys_addr);
1312 * Now destroy the pool itself
1314 pci_pool_destroy(instance->frame_dma_pool);
1315 pci_pool_destroy(instance->sense_dma_pool);
1317 instance->frame_dma_pool = NULL;
1318 instance->sense_dma_pool = NULL;
1322 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1323 * @instance: Adapter soft state
1325 * Each command packet has an embedded DMA memory buffer that is used for
1326 * filling MFI frame and the SG list that immediately follows the frame. This
1327 * function creates those DMA memory buffers for each command packet by using
1328 * PCI pool facility.
1330 static int megasas_create_frame_pool(struct megasas_instance *instance)
1338 struct megasas_cmd *cmd;
1340 max_cmd = instance->max_fw_cmds;
1343 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1344 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1346 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1347 sizeof(struct megasas_sge32);
1350 * Calculated the number of 64byte frames required for SGL
1352 sgl_sz = sge_sz * instance->max_num_sge;
1353 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1356 * We need one extra frame for the MFI command
1360 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1362 * Use DMA pool facility provided by PCI layer
1364 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1365 instance->pdev, total_sz, 64,
1368 if (!instance->frame_dma_pool) {
1369 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1373 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1374 instance->pdev, 128, 4, 0);
1376 if (!instance->sense_dma_pool) {
1377 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1379 pci_pool_destroy(instance->frame_dma_pool);
1380 instance->frame_dma_pool = NULL;
1386 * Allocate and attach a frame to each of the commands in cmd_list.
1387 * By making cmd->index as the context instead of the &cmd, we can
1388 * always use 32bit context regardless of the architecture
1390 for (i = 0; i < max_cmd; i++) {
1392 cmd = instance->cmd_list[i];
1394 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1395 GFP_KERNEL, &cmd->frame_phys_addr);
1397 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1398 GFP_KERNEL, &cmd->sense_phys_addr);
1401 * megasas_teardown_frame_pool() takes care of freeing
1402 * whatever has been allocated
1404 if (!cmd->frame || !cmd->sense) {
1405 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1406 megasas_teardown_frame_pool(instance);
1410 cmd->frame->io.context = cmd->index;
1417 * megasas_free_cmds - Free all the cmds in the free cmd pool
1418 * @instance: Adapter soft state
1420 static void megasas_free_cmds(struct megasas_instance *instance)
1423 /* First free the MFI frame pool */
1424 megasas_teardown_frame_pool(instance);
1426 /* Free all the commands in the cmd_list */
1427 for (i = 0; i < instance->max_fw_cmds; i++)
1428 kfree(instance->cmd_list[i]);
1430 /* Free the cmd_list buffer itself */
1431 kfree(instance->cmd_list);
1432 instance->cmd_list = NULL;
1434 INIT_LIST_HEAD(&instance->cmd_pool);
1438 * megasas_alloc_cmds - Allocates the command packets
1439 * @instance: Adapter soft state
1441 * Each command that is issued to the FW, whether IO commands from the OS or
1442 * internal commands like IOCTLs, are wrapped in local data structure called
1443 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1446 * Each frame has a 32-bit field called context (tag). This context is used
1447 * to get back the megasas_cmd from the frame when a frame gets completed in
1448 * the ISR. Typically the address of the megasas_cmd itself would be used as
1449 * the context. But we wanted to keep the differences between 32 and 64 bit
1450 * systems to the mininum. We always use 32 bit integers for the context. In
1451 * this driver, the 32 bit values are the indices into an array cmd_list.
1452 * This array is used only to look up the megasas_cmd given the context. The
1453 * free commands themselves are maintained in a linked list called cmd_pool.
1455 static int megasas_alloc_cmds(struct megasas_instance *instance)
1460 struct megasas_cmd *cmd;
1462 max_cmd = instance->max_fw_cmds;
1465 * instance->cmd_list is an array of struct megasas_cmd pointers.
1466 * Allocate the dynamic array first and then allocate individual
1469 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1472 if (!instance->cmd_list) {
1473 printk(KERN_DEBUG "megasas: out of memory\n");
1477 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1479 for (i = 0; i < max_cmd; i++) {
1480 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1483 if (!instance->cmd_list[i]) {
1485 for (j = 0; j < i; j++)
1486 kfree(instance->cmd_list[j]);
1488 kfree(instance->cmd_list);
1489 instance->cmd_list = NULL;
1496 * Add all the commands to command pool (instance->cmd_pool)
1498 for (i = 0; i < max_cmd; i++) {
1499 cmd = instance->cmd_list[i];
1500 memset(cmd, 0, sizeof(struct megasas_cmd));
1502 cmd->instance = instance;
1504 list_add_tail(&cmd->list, &instance->cmd_pool);
1508 * Create a frame pool and assign one frame to each cmd
1510 if (megasas_create_frame_pool(instance)) {
1511 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1512 megasas_free_cmds(instance);
1519 * megasas_get_controller_info - Returns FW's controller structure
1520 * @instance: Adapter soft state
1521 * @ctrl_info: Controller information structure
1523 * Issues an internal command (DCMD) to get the FW's controller structure.
1524 * This information is mainly used to find out the maximum IO transfer per
1525 * command supported by the FW.
1528 megasas_get_ctrl_info(struct megasas_instance *instance,
1529 struct megasas_ctrl_info *ctrl_info)
1532 struct megasas_cmd *cmd;
1533 struct megasas_dcmd_frame *dcmd;
1534 struct megasas_ctrl_info *ci;
1535 dma_addr_t ci_h = 0;
1537 cmd = megasas_get_cmd(instance);
1540 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1544 dcmd = &cmd->frame->dcmd;
1546 ci = pci_alloc_consistent(instance->pdev,
1547 sizeof(struct megasas_ctrl_info), &ci_h);
1550 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1551 megasas_return_cmd(instance, cmd);
1555 memset(ci, 0, sizeof(*ci));
1556 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1558 dcmd->cmd = MFI_CMD_DCMD;
1559 dcmd->cmd_status = 0xFF;
1560 dcmd->sge_count = 1;
1561 dcmd->flags = MFI_FRAME_DIR_READ;
1563 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1564 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1565 dcmd->sgl.sge32[0].phys_addr = ci_h;
1566 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1568 if (!megasas_issue_polled(instance, cmd)) {
1570 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1575 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1578 megasas_return_cmd(instance, cmd);
1583 * megasas_init_mfi - Initializes the FW
1584 * @instance: Adapter soft state
1586 * This is the main function for initializing MFI firmware.
1588 static int megasas_init_mfi(struct megasas_instance *instance)
1594 struct megasas_register_set __iomem *reg_set;
1596 struct megasas_cmd *cmd;
1597 struct megasas_ctrl_info *ctrl_info;
1599 struct megasas_init_frame *init_frame;
1600 struct megasas_init_queue_info *initq_info;
1601 dma_addr_t init_frame_h;
1602 dma_addr_t initq_info_h;
1605 * Map the message registers
1607 instance->base_addr = pci_resource_start(instance->pdev, 0);
1609 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1610 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1614 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1616 if (!instance->reg_set) {
1617 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1621 reg_set = instance->reg_set;
1624 * We expect the FW state to be READY
1626 if (megasas_transition_to_ready(instance->reg_set))
1627 goto fail_ready_state;
1630 * Get various operational parameters from status register
1632 instance->max_fw_cmds = readl(®_set->outbound_msg_0) & 0x00FFFF;
1633 instance->max_num_sge = (readl(®_set->outbound_msg_0) & 0xFF0000) >>
1636 * Create a pool of commands
1638 if (megasas_alloc_cmds(instance))
1639 goto fail_alloc_cmds;
1642 * Allocate memory for reply queue. Length of reply queue should
1643 * be _one_ more than the maximum commands handled by the firmware.
1645 * Note: When FW completes commands, it places corresponding contex
1646 * values in this circular reply queue. This circular queue is a fairly
1647 * typical producer-consumer queue. FW is the producer (of completed
1648 * commands) and the driver is the consumer.
1650 context_sz = sizeof(u32);
1651 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1653 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1655 &instance->reply_queue_h);
1657 if (!instance->reply_queue) {
1658 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1659 goto fail_reply_queue;
1663 * Prepare a init frame. Note the init frame points to queue info
1664 * structure. Each frame has SGL allocated after first 64 bytes. For
1665 * this frame - since we don't need any SGL - we use SGL's space as
1666 * queue info structure
1668 * We will not get a NULL command below. We just created the pool.
1670 cmd = megasas_get_cmd(instance);
1672 init_frame = (struct megasas_init_frame *)cmd->frame;
1673 initq_info = (struct megasas_init_queue_info *)
1674 ((unsigned long)init_frame + 64);
1676 init_frame_h = cmd->frame_phys_addr;
1677 initq_info_h = init_frame_h + 64;
1679 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1680 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1682 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1683 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1685 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1686 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1688 init_frame->cmd = MFI_CMD_INIT;
1689 init_frame->cmd_status = 0xFF;
1690 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1692 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1695 * Issue the init frame in polled mode
1697 if (megasas_issue_polled(instance, cmd)) {
1698 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1702 megasas_return_cmd(instance, cmd);
1704 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1707 * Compute the max allowed sectors per IO: The controller info has two
1708 * limits on max sectors. Driver should use the minimum of these two.
1710 * 1 << stripe_sz_ops.min = max sectors per strip
1712 * Note that older firmwares ( < FW ver 30) didn't report information
1713 * to calculate max_sectors_1. So the number ended up as zero always.
1715 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1717 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1718 ctrl_info->max_strips_per_io;
1719 max_sectors_2 = ctrl_info->max_request_size;
1721 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1722 ? max_sectors_1 : max_sectors_2;
1724 instance->max_sectors_per_req = instance->max_num_sge *
1732 megasas_return_cmd(instance, cmd);
1734 pci_free_consistent(instance->pdev, reply_q_sz,
1735 instance->reply_queue, instance->reply_queue_h);
1737 megasas_free_cmds(instance);
1741 iounmap(instance->reg_set);
1744 pci_release_regions(instance->pdev);
1750 * megasas_release_mfi - Reverses the FW initialization
1751 * @intance: Adapter soft state
1753 static void megasas_release_mfi(struct megasas_instance *instance)
1755 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1757 pci_free_consistent(instance->pdev, reply_q_sz,
1758 instance->reply_queue, instance->reply_queue_h);
1760 megasas_free_cmds(instance);
1762 iounmap(instance->reg_set);
1764 pci_release_regions(instance->pdev);
1768 * megasas_get_seq_num - Gets latest event sequence numbers
1769 * @instance: Adapter soft state
1770 * @eli: FW event log sequence numbers information
1772 * FW maintains a log of all events in a non-volatile area. Upper layers would
1773 * usually find out the latest sequence number of the events, the seq number at
1774 * the boot etc. They would "read" all the events below the latest seq number
1775 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1776 * number), they would subsribe to AEN (asynchronous event notification) and
1777 * wait for the events to happen.
1780 megasas_get_seq_num(struct megasas_instance *instance,
1781 struct megasas_evt_log_info *eli)
1783 struct megasas_cmd *cmd;
1784 struct megasas_dcmd_frame *dcmd;
1785 struct megasas_evt_log_info *el_info;
1786 dma_addr_t el_info_h = 0;
1788 cmd = megasas_get_cmd(instance);
1794 dcmd = &cmd->frame->dcmd;
1795 el_info = pci_alloc_consistent(instance->pdev,
1796 sizeof(struct megasas_evt_log_info),
1800 megasas_return_cmd(instance, cmd);
1804 memset(el_info, 0, sizeof(*el_info));
1805 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1807 dcmd->cmd = MFI_CMD_DCMD;
1808 dcmd->cmd_status = 0x0;
1809 dcmd->sge_count = 1;
1810 dcmd->flags = MFI_FRAME_DIR_READ;
1812 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1813 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1814 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1815 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1817 megasas_issue_blocked_cmd(instance, cmd);
1820 * Copy the data back into callers buffer
1822 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1824 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1825 el_info, el_info_h);
1827 megasas_return_cmd(instance, cmd);
1833 * megasas_register_aen - Registers for asynchronous event notification
1834 * @instance: Adapter soft state
1835 * @seq_num: The starting sequence number
1836 * @class_locale: Class of the event
1838 * This function subscribes for AEN for events beyond the @seq_num. It requests
1839 * to be notified if and only if the event is of type @class_locale
1842 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1843 u32 class_locale_word)
1846 struct megasas_cmd *cmd;
1847 struct megasas_dcmd_frame *dcmd;
1848 union megasas_evt_class_locale curr_aen;
1849 union megasas_evt_class_locale prev_aen;
1852 * If there an AEN pending already (aen_cmd), check if the
1853 * class_locale of that pending AEN is inclusive of the new
1854 * AEN request we currently have. If it is, then we don't have
1855 * to do anything. In other words, whichever events the current
1856 * AEN request is subscribing to, have already been subscribed
1859 * If the old_cmd is _not_ inclusive, then we have to abort
1860 * that command, form a class_locale that is superset of both
1861 * old and current and re-issue to the FW
1864 curr_aen.word = class_locale_word;
1866 if (instance->aen_cmd) {
1868 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1871 * A class whose enum value is smaller is inclusive of all
1872 * higher values. If a PROGRESS (= -1) was previously
1873 * registered, then a new registration requests for higher
1874 * classes need not be sent to FW. They are automatically
1877 * Locale numbers don't have such hierarchy. They are bitmap
1880 if ((prev_aen.members.class <= curr_aen.members.class) &&
1881 !((prev_aen.members.locale & curr_aen.members.locale) ^
1882 curr_aen.members.locale)) {
1884 * Previously issued event registration includes
1885 * current request. Nothing to do.
1889 curr_aen.members.locale |= prev_aen.members.locale;
1891 if (prev_aen.members.class < curr_aen.members.class)
1892 curr_aen.members.class = prev_aen.members.class;
1894 instance->aen_cmd->abort_aen = 1;
1895 ret_val = megasas_issue_blocked_abort_cmd(instance,
1900 printk(KERN_DEBUG "megasas: Failed to abort "
1901 "previous AEN command\n");
1907 cmd = megasas_get_cmd(instance);
1912 dcmd = &cmd->frame->dcmd;
1914 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1917 * Prepare DCMD for aen registration
1919 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1921 dcmd->cmd = MFI_CMD_DCMD;
1922 dcmd->cmd_status = 0x0;
1923 dcmd->sge_count = 1;
1924 dcmd->flags = MFI_FRAME_DIR_READ;
1926 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1927 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1928 dcmd->mbox.w[0] = seq_num;
1929 dcmd->mbox.w[1] = curr_aen.word;
1930 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1931 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1934 * Store reference to the cmd used to register for AEN. When an
1935 * application wants us to register for AEN, we have to abort this
1936 * cmd and re-register with a new EVENT LOCALE supplied by that app
1938 instance->aen_cmd = cmd;
1941 * Issue the aen registration frame
1943 writel(cmd->frame_phys_addr >> 3,
1944 &instance->reg_set->inbound_queue_port);
1950 * megasas_start_aen - Subscribes to AEN during driver load time
1951 * @instance: Adapter soft state
1953 static int megasas_start_aen(struct megasas_instance *instance)
1955 struct megasas_evt_log_info eli;
1956 union megasas_evt_class_locale class_locale;
1959 * Get the latest sequence number from FW
1961 memset(&eli, 0, sizeof(eli));
1963 if (megasas_get_seq_num(instance, &eli))
1967 * Register AEN with FW for latest sequence number plus 1
1969 class_locale.members.reserved = 0;
1970 class_locale.members.locale = MR_EVT_LOCALE_ALL;
1971 class_locale.members.class = MR_EVT_CLASS_DEBUG;
1973 return megasas_register_aen(instance, eli.newest_seq_num + 1,
1978 * megasas_io_attach - Attaches this driver to SCSI mid-layer
1979 * @instance: Adapter soft state
1981 static int megasas_io_attach(struct megasas_instance *instance)
1983 struct Scsi_Host *host = instance->host;
1986 * Export parameters required by SCSI mid-layer
1988 host->irq = instance->pdev->irq;
1989 host->unique_id = instance->unique_id;
1990 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1991 host->this_id = instance->init_id;
1992 host->sg_tablesize = instance->max_num_sge;
1993 host->max_sectors = instance->max_sectors_per_req;
1994 host->cmd_per_lun = 128;
1995 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1996 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1997 host->max_lun = MEGASAS_MAX_LUN;
2000 * Notify the mid-layer about the new controller
2002 if (scsi_add_host(host, &instance->pdev->dev)) {
2003 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2008 * Trigger SCSI to scan our drives
2010 scsi_scan_host(host);
2015 * megasas_probe_one - PCI hotplug entry point
2016 * @pdev: PCI device structure
2017 * @id: PCI ids of supported hotplugged adapter
2019 static int __devinit
2020 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2023 struct Scsi_Host *host;
2024 struct megasas_instance *instance;
2027 * Announce PCI information
2029 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2030 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2031 pdev->subsystem_device);
2033 printk("bus %d:slot %d:func %d\n",
2034 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2037 * PCI prepping: enable device set bus mastering and dma mask
2039 rval = pci_enable_device(pdev);
2045 pci_set_master(pdev);
2048 * All our contollers are capable of performing 64-bit DMA
2051 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2053 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2054 goto fail_set_dma_mask;
2057 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2058 goto fail_set_dma_mask;
2061 host = scsi_host_alloc(&megasas_template,
2062 sizeof(struct megasas_instance));
2065 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2066 goto fail_alloc_instance;
2069 instance = (struct megasas_instance *)host->hostdata;
2070 memset(instance, 0, sizeof(*instance));
2072 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2073 &instance->producer_h);
2074 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2075 &instance->consumer_h);
2077 if (!instance->producer || !instance->consumer) {
2078 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2079 "producer, consumer\n");
2080 goto fail_alloc_dma_buf;
2083 *instance->producer = 0;
2084 *instance->consumer = 0;
2086 instance->evt_detail = pci_alloc_consistent(pdev,
2088 megasas_evt_detail),
2089 &instance->evt_detail_h);
2091 if (!instance->evt_detail) {
2092 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2093 "event detail structure\n");
2094 goto fail_alloc_dma_buf;
2098 * Initialize locks and queues
2100 INIT_LIST_HEAD(&instance->cmd_pool);
2102 init_waitqueue_head(&instance->int_cmd_wait_q);
2103 init_waitqueue_head(&instance->abort_cmd_wait_q);
2105 spin_lock_init(&instance->cmd_pool_lock);
2106 spin_lock_init(&instance->instance_lock);
2108 sema_init(&instance->aen_mutex, 1);
2109 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2112 * Initialize PCI related and misc parameters
2114 instance->pdev = pdev;
2115 instance->host = host;
2116 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2117 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2120 * Initialize MFI Firmware
2122 if (megasas_init_mfi(instance))
2128 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2129 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2133 megasas_enable_intr(instance->reg_set);
2136 * Store instance in PCI softstate
2138 pci_set_drvdata(pdev, instance);
2141 * Add this controller to megasas_mgmt_info structure so that it
2142 * can be exported to management applications
2144 megasas_mgmt_info.count++;
2145 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2146 megasas_mgmt_info.max_index++;
2149 * Initiate AEN (Asynchronous Event Notification)
2151 if (megasas_start_aen(instance)) {
2152 printk(KERN_DEBUG "megasas: start aen failed\n");
2153 goto fail_start_aen;
2157 * Register with SCSI mid-layer
2159 if (megasas_io_attach(instance))
2160 goto fail_io_attach;
2166 megasas_mgmt_info.count--;
2167 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2168 megasas_mgmt_info.max_index--;
2170 pci_set_drvdata(pdev, NULL);
2171 megasas_disable_intr(instance->reg_set);
2172 free_irq(instance->pdev->irq, instance);
2174 megasas_release_mfi(instance);
2179 if (instance->evt_detail)
2180 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2181 instance->evt_detail,
2182 instance->evt_detail_h);
2184 if (instance->producer)
2185 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2186 instance->producer_h);
2187 if (instance->consumer)
2188 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2189 instance->consumer_h);
2190 scsi_host_put(host);
2192 fail_alloc_instance:
2194 pci_disable_device(pdev);
2200 * megasas_flush_cache - Requests FW to flush all its caches
2201 * @instance: Adapter soft state
2203 static void megasas_flush_cache(struct megasas_instance *instance)
2205 struct megasas_cmd *cmd;
2206 struct megasas_dcmd_frame *dcmd;
2208 cmd = megasas_get_cmd(instance);
2213 dcmd = &cmd->frame->dcmd;
2215 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2217 dcmd->cmd = MFI_CMD_DCMD;
2218 dcmd->cmd_status = 0x0;
2219 dcmd->sge_count = 0;
2220 dcmd->flags = MFI_FRAME_DIR_NONE;
2222 dcmd->data_xfer_len = 0;
2223 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2224 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2226 megasas_issue_blocked_cmd(instance, cmd);
2228 megasas_return_cmd(instance, cmd);
2234 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2235 * @instance: Adapter soft state
2237 static void megasas_shutdown_controller(struct megasas_instance *instance)
2239 struct megasas_cmd *cmd;
2240 struct megasas_dcmd_frame *dcmd;
2242 cmd = megasas_get_cmd(instance);
2247 if (instance->aen_cmd)
2248 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2250 dcmd = &cmd->frame->dcmd;
2252 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2254 dcmd->cmd = MFI_CMD_DCMD;
2255 dcmd->cmd_status = 0x0;
2256 dcmd->sge_count = 0;
2257 dcmd->flags = MFI_FRAME_DIR_NONE;
2259 dcmd->data_xfer_len = 0;
2260 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2262 megasas_issue_blocked_cmd(instance, cmd);
2264 megasas_return_cmd(instance, cmd);
2270 * megasas_detach_one - PCI hot"un"plug entry point
2271 * @pdev: PCI device structure
2273 static void megasas_detach_one(struct pci_dev *pdev)
2276 struct Scsi_Host *host;
2277 struct megasas_instance *instance;
2279 instance = pci_get_drvdata(pdev);
2280 host = instance->host;
2282 scsi_remove_host(instance->host);
2283 megasas_flush_cache(instance);
2284 megasas_shutdown_controller(instance);
2287 * Take the instance off the instance array. Note that we will not
2288 * decrement the max_index. We let this array be sparse array
2290 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2291 if (megasas_mgmt_info.instance[i] == instance) {
2292 megasas_mgmt_info.count--;
2293 megasas_mgmt_info.instance[i] = NULL;
2299 pci_set_drvdata(instance->pdev, NULL);
2301 megasas_disable_intr(instance->reg_set);
2303 free_irq(instance->pdev->irq, instance);
2305 megasas_release_mfi(instance);
2307 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2308 instance->evt_detail, instance->evt_detail_h);
2310 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2311 instance->producer_h);
2313 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2314 instance->consumer_h);
2316 scsi_host_put(host);
2318 pci_set_drvdata(pdev, NULL);
2320 pci_disable_device(pdev);
2326 * megasas_shutdown - Shutdown entry point
2327 * @device: Generic device structure
2329 static void megasas_shutdown(struct pci_dev *pdev)
2331 struct megasas_instance *instance = pci_get_drvdata(pdev);
2332 megasas_flush_cache(instance);
2336 * megasas_mgmt_open - char node "open" entry point
2338 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2341 * Allow only those users with admin rights
2343 if (!capable(CAP_SYS_ADMIN))
2350 * megasas_mgmt_release - char node "release" entry point
2352 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2354 filep->private_data = NULL;
2355 fasync_helper(-1, filep, 0, &megasas_async_queue);
2361 * megasas_mgmt_fasync - Async notifier registration from applications
2363 * This function adds the calling process to a driver global queue. When an
2364 * event occurs, SIGIO will be sent to all processes in this queue.
2366 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2370 down(&megasas_async_queue_mutex);
2372 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2374 up(&megasas_async_queue_mutex);
2377 /* For sanity check when we get ioctl */
2378 filep->private_data = filep;
2382 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2388 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2389 * @instance: Adapter soft state
2390 * @argp: User's ioctl packet
2393 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2394 struct megasas_iocpacket __user * user_ioc,
2395 struct megasas_iocpacket *ioc)
2397 struct megasas_sge32 *kern_sge32;
2398 struct megasas_cmd *cmd;
2399 void *kbuff_arr[MAX_IOCTL_SGE];
2400 dma_addr_t buf_handle = 0;
2403 dma_addr_t sense_handle;
2406 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2408 if (ioc->sge_count > MAX_IOCTL_SGE) {
2409 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2410 ioc->sge_count, MAX_IOCTL_SGE);
2414 cmd = megasas_get_cmd(instance);
2416 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2421 * User's IOCTL packet has 2 frames (maximum). Copy those two
2422 * frames into our cmd's frames. cmd->frame's context will get
2423 * overwritten when we copy from user's frames. So set that value
2426 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2427 cmd->frame->hdr.context = cmd->index;
2430 * The management interface between applications and the fw uses
2431 * MFI frames. E.g, RAID configuration changes, LD property changes
2432 * etc are accomplishes through different kinds of MFI frames. The
2433 * driver needs to care only about substituting user buffers with
2434 * kernel buffers in SGLs. The location of SGL is embedded in the
2435 * struct iocpacket itself.
2437 kern_sge32 = (struct megasas_sge32 *)
2438 ((unsigned long)cmd->frame + ioc->sgl_off);
2441 * For each user buffer, create a mirror buffer and copy in
2443 for (i = 0; i < ioc->sge_count; i++) {
2444 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2445 ioc->sgl[i].iov_len,
2447 if (!kbuff_arr[i]) {
2448 printk(KERN_DEBUG "megasas: Failed to alloc "
2449 "kernel SGL buffer for IOCTL \n");
2455 * We don't change the dma_coherent_mask, so
2456 * pci_alloc_consistent only returns 32bit addresses
2458 kern_sge32[i].phys_addr = (u32) buf_handle;
2459 kern_sge32[i].length = ioc->sgl[i].iov_len;
2462 * We created a kernel buffer corresponding to the
2463 * user buffer. Now copy in from the user buffer
2465 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2466 (u32) (ioc->sgl[i].iov_len))) {
2472 if (ioc->sense_len) {
2473 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2481 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2482 *sense_ptr = sense_handle;
2486 * Set the sync_cmd flag so that the ISR knows not to complete this
2487 * cmd to the SCSI mid-layer
2490 megasas_issue_blocked_cmd(instance, cmd);
2494 * copy out the kernel buffers to user buffers
2496 for (i = 0; i < ioc->sge_count; i++) {
2497 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2498 ioc->sgl[i].iov_len)) {
2505 * copy out the sense
2507 if (ioc->sense_len) {
2509 * sense_ptr points to the location that has the user
2510 * sense buffer address
2512 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2515 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2516 sense, ioc->sense_len)) {
2523 * copy the status codes returned by the fw
2525 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2526 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2527 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2533 pci_free_consistent(instance->pdev, ioc->sense_len,
2534 sense, sense_handle);
2537 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2538 pci_free_consistent(instance->pdev,
2539 kern_sge32[i].length,
2540 kbuff_arr[i], kern_sge32[i].phys_addr);
2543 megasas_return_cmd(instance, cmd);
2547 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2551 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2553 if ((megasas_mgmt_info.instance[i]) &&
2554 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2555 return megasas_mgmt_info.instance[i];
2561 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2563 struct megasas_iocpacket __user *user_ioc =
2564 (struct megasas_iocpacket __user *)arg;
2565 struct megasas_iocpacket *ioc;
2566 struct megasas_instance *instance;
2569 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2573 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2578 instance = megasas_lookup_instance(ioc->host_no);
2585 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2587 if (down_interruptible(&instance->ioctl_sem)) {
2588 error = -ERESTARTSYS;
2591 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2592 up(&instance->ioctl_sem);
2599 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2601 struct megasas_instance *instance;
2602 struct megasas_aen aen;
2605 if (file->private_data != file) {
2606 printk(KERN_DEBUG "megasas: fasync_helper was not "
2611 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2614 instance = megasas_lookup_instance(aen.host_no);
2619 down(&instance->aen_mutex);
2620 error = megasas_register_aen(instance, aen.seq_num,
2621 aen.class_locale_word);
2622 up(&instance->aen_mutex);
2627 * megasas_mgmt_ioctl - char node ioctl entry point
2630 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2633 case MEGASAS_IOC_FIRMWARE:
2634 return megasas_mgmt_ioctl_fw(file, arg);
2636 case MEGASAS_IOC_GET_AEN:
2637 return megasas_mgmt_ioctl_aen(file, arg);
2643 #ifdef CONFIG_COMPAT
2644 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2646 struct compat_megasas_iocpacket __user *cioc =
2647 (struct compat_megasas_iocpacket __user *)arg;
2648 struct megasas_iocpacket __user *ioc =
2649 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2653 clear_user(ioc, sizeof(*ioc));
2655 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2656 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2657 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2658 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2659 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2660 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2663 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2666 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2667 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2668 copy_in_user(&ioc->sgl[i].iov_len,
2669 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2673 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2675 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2676 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2677 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2684 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2688 case MEGASAS_IOC_FIRMWARE:{
2689 return megasas_mgmt_compat_ioctl_fw(file, arg);
2691 case MEGASAS_IOC_GET_AEN:
2692 return megasas_mgmt_ioctl_aen(file, arg);
2700 * File operations structure for management interface
2702 static struct file_operations megasas_mgmt_fops = {
2703 .owner = THIS_MODULE,
2704 .open = megasas_mgmt_open,
2705 .release = megasas_mgmt_release,
2706 .fasync = megasas_mgmt_fasync,
2707 .unlocked_ioctl = megasas_mgmt_ioctl,
2708 #ifdef CONFIG_COMPAT
2709 .compat_ioctl = megasas_mgmt_compat_ioctl,
2714 * PCI hotplug support registration structure
2716 static struct pci_driver megasas_pci_driver = {
2718 .name = "megaraid_sas",
2719 .id_table = megasas_pci_table,
2720 .probe = megasas_probe_one,
2721 .remove = __devexit_p(megasas_detach_one),
2722 .shutdown = megasas_shutdown,
2726 * Sysfs driver attributes
2728 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2730 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2734 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2737 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2739 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2743 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2747 * megasas_init - Driver load entry point
2749 static int __init megasas_init(void)
2754 * Announce driver version and other information
2756 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2757 MEGASAS_EXT_VERSION);
2759 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2762 * Register character device node
2764 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2767 printk(KERN_DEBUG "megasas: failed to open device node\n");
2771 megasas_mgmt_majorno = rval;
2774 * Register ourselves as PCI hotplug module
2776 rval = pci_module_init(&megasas_pci_driver);
2779 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2780 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2783 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2784 driver_create_file(&megasas_pci_driver.driver,
2785 &driver_attr_release_date);
2791 * megasas_exit - Driver unload entry point
2793 static void __exit megasas_exit(void)
2795 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2796 driver_remove_file(&megasas_pci_driver.driver,
2797 &driver_attr_release_date);
2799 pci_unregister_driver(&megasas_pci_driver);
2800 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2803 module_init(megasas_init);
2804 module_exit(megasas_exit);