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/version.h>
30 #include <linux/moduleparam.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/interrupt.h>
34 #include <linux/delay.h>
35 #include <linux/uio.h>
36 #include <asm/uaccess.h>
38 #include <linux/compat.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include "megaraid_sas.h"
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(MEGASAS_VERSION);
48 MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
52 * PCI ID table for all supported controllers
54 static struct pci_device_id megasas_pci_table[] = {
57 PCI_VENDOR_ID_LSI_LOGIC,
58 PCI_DEVICE_ID_LSI_SAS1064R,
64 PCI_DEVICE_ID_DELL_PERC5,
68 {0} /* Terminating entry */
71 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
73 static int megasas_mgmt_majorno;
74 static struct megasas_mgmt_info megasas_mgmt_info;
75 static struct fasync_struct *megasas_async_queue;
76 static DECLARE_MUTEX(megasas_async_queue_mutex);
79 * megasas_get_cmd - Get a command from the free pool
80 * @instance: Adapter soft state
82 * Returns a free command from the pool
84 static inline struct megasas_cmd *megasas_get_cmd(struct megasas_instance
88 struct megasas_cmd *cmd = NULL;
90 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
92 if (!list_empty(&instance->cmd_pool)) {
93 cmd = list_entry((&instance->cmd_pool)->next,
94 struct megasas_cmd, list);
95 list_del_init(&cmd->list);
97 printk(KERN_ERR "megasas: Command pool empty!\n");
100 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
105 * megasas_return_cmd - Return a cmd to free command pool
106 * @instance: Adapter soft state
107 * @cmd: Command packet to be returned to free command pool
110 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
114 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
117 list_add_tail(&cmd->list, &instance->cmd_pool);
119 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
123 * megasas_enable_intr - Enables interrupts
124 * @regs: MFI register set
127 megasas_enable_intr(struct megasas_register_set __iomem * regs)
129 writel(1, &(regs)->outbound_intr_mask);
131 /* Dummy readl to force pci flush */
132 readl(®s->outbound_intr_mask);
136 * megasas_disable_intr - Disables interrupts
137 * @regs: MFI register set
140 megasas_disable_intr(struct megasas_register_set __iomem * regs)
142 u32 mask = readl(®s->outbound_intr_mask) & (~0x00000001);
143 writel(mask, ®s->outbound_intr_mask);
145 /* Dummy readl to force pci flush */
146 readl(®s->outbound_intr_mask);
150 * megasas_issue_polled - Issues a polling command
151 * @instance: Adapter soft state
152 * @cmd: Command packet to be issued
154 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
157 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
160 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
162 struct megasas_header *frame_hdr = &cmd->frame->hdr;
164 frame_hdr->cmd_status = 0xFF;
165 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
168 * Issue the frame using inbound queue port
170 writel(cmd->frame_phys_addr >> 3,
171 &instance->reg_set->inbound_queue_port);
174 * Wait for cmd_status to change
176 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
181 if (frame_hdr->cmd_status == 0xff)
188 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
189 * @instance: Adapter soft state
190 * @cmd: Command to be issued
192 * This function waits on an event for the command to be returned from ISR.
193 * Used to issue ioctl commands.
196 megasas_issue_blocked_cmd(struct megasas_instance *instance,
197 struct megasas_cmd *cmd)
199 cmd->cmd_status = ENODATA;
201 writel(cmd->frame_phys_addr >> 3,
202 &instance->reg_set->inbound_queue_port);
204 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
210 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
211 * @instance: Adapter soft state
212 * @cmd_to_abort: Previously issued cmd to be aborted
214 * MFI firmware can abort previously issued AEN comamnd (automatic event
215 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
216 * cmd and blocks till it is completed.
219 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
220 struct megasas_cmd *cmd_to_abort)
222 struct megasas_cmd *cmd;
223 struct megasas_abort_frame *abort_fr;
225 cmd = megasas_get_cmd(instance);
230 abort_fr = &cmd->frame->abort;
233 * Prepare and issue the abort frame
235 abort_fr->cmd = MFI_CMD_ABORT;
236 abort_fr->cmd_status = 0xFF;
238 abort_fr->abort_context = cmd_to_abort->index;
239 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
240 abort_fr->abort_mfi_phys_addr_hi = 0;
243 cmd->cmd_status = 0xFF;
245 writel(cmd->frame_phys_addr >> 3,
246 &instance->reg_set->inbound_queue_port);
249 * Wait for this cmd to complete
251 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
253 megasas_return_cmd(instance, cmd);
258 * megasas_make_sgl32 - Prepares 32-bit SGL
259 * @instance: Adapter soft state
260 * @scp: SCSI command from the mid-layer
261 * @mfi_sgl: SGL to be filled in
263 * If successful, this function returns the number of SG elements. Otherwise,
267 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
268 union megasas_sgl *mfi_sgl)
272 struct scatterlist *os_sgl;
275 * Return 0 if there is no data transfer
277 if (!scp->request_buffer || !scp->request_bufflen)
281 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
288 mfi_sgl->sge32[0].length = scp->request_bufflen;
293 os_sgl = (struct scatterlist *)scp->request_buffer;
294 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
295 scp->sc_data_direction);
297 for (i = 0; i < sge_count; i++, os_sgl++) {
298 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
299 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
306 * megasas_make_sgl64 - Prepares 64-bit SGL
307 * @instance: Adapter soft state
308 * @scp: SCSI command from the mid-layer
309 * @mfi_sgl: SGL to be filled in
311 * If successful, this function returns the number of SG elements. Otherwise,
315 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
316 union megasas_sgl *mfi_sgl)
320 struct scatterlist *os_sgl;
323 * Return 0 if there is no data transfer
325 if (!scp->request_buffer || !scp->request_bufflen)
329 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
337 mfi_sgl->sge64[0].length = scp->request_bufflen;
342 os_sgl = (struct scatterlist *)scp->request_buffer;
343 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
344 scp->sc_data_direction);
346 for (i = 0; i < sge_count; i++, os_sgl++) {
347 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
348 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
355 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
356 * @instance: Adapter soft state
358 * @cmd: Command to be prepared in
360 * This function prepares CDB commands. These are typcially pass-through
361 * commands to the devices.
364 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
365 struct megasas_cmd *cmd)
372 struct megasas_pthru_frame *pthru;
374 is_logical = MEGASAS_IS_LOGICAL(scp);
375 device_id = MEGASAS_DEV_INDEX(instance, scp);
376 pthru = (struct megasas_pthru_frame *)cmd->frame;
378 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
379 flags = MFI_FRAME_DIR_WRITE;
380 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
381 flags = MFI_FRAME_DIR_READ;
382 else if (scp->sc_data_direction == PCI_DMA_NONE)
383 flags = MFI_FRAME_DIR_NONE;
386 * Prepare the DCDB frame
388 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
389 pthru->cmd_status = 0x0;
390 pthru->scsi_status = 0x0;
391 pthru->target_id = device_id;
392 pthru->lun = scp->device->lun;
393 pthru->cdb_len = scp->cmd_len;
395 pthru->flags = flags;
396 pthru->data_xfer_len = scp->request_bufflen;
398 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
403 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
404 sizeof(struct megasas_sge32);
407 pthru->flags |= MFI_FRAME_SGL64;
408 pthru->sge_count = megasas_make_sgl64(instance, scp,
411 pthru->sge_count = megasas_make_sgl32(instance, scp,
415 * Sense info specific
417 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
418 pthru->sense_buf_phys_addr_hi = 0;
419 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
421 sge_bytes = sge_sz * pthru->sge_count;
424 * Compute the total number of frames this command consumes. FW uses
425 * this number to pull sufficient number of frames from host memory.
427 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
428 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
430 if (cmd->frame_count > 7)
431 cmd->frame_count = 8;
433 return cmd->frame_count;
437 * megasas_build_ldio - Prepares IOs to logical devices
438 * @instance: Adapter soft state
440 * @cmd: Command to to be prepared
442 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
445 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
446 struct megasas_cmd *cmd)
451 u8 sc = scp->cmnd[0];
453 struct megasas_io_frame *ldio;
455 device_id = MEGASAS_DEV_INDEX(instance, scp);
456 ldio = (struct megasas_io_frame *)cmd->frame;
458 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
459 flags = MFI_FRAME_DIR_WRITE;
460 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
461 flags = MFI_FRAME_DIR_READ;
464 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
466 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
467 ldio->cmd_status = 0x0;
468 ldio->scsi_status = 0x0;
469 ldio->target_id = device_id;
471 ldio->reserved_0 = 0;
474 ldio->start_lba_hi = 0;
475 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
478 * 6-byte READ(0x08) or WRITE(0x0A) cdb
480 if (scp->cmd_len == 6) {
481 ldio->lba_count = (u32) scp->cmnd[4];
482 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
483 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
485 ldio->start_lba_lo &= 0x1FFFFF;
489 * 10-byte READ(0x28) or WRITE(0x2A) cdb
491 else if (scp->cmd_len == 10) {
492 ldio->lba_count = (u32) scp->cmnd[8] |
493 ((u32) scp->cmnd[7] << 8);
494 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
495 ((u32) scp->cmnd[3] << 16) |
496 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
500 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
502 else if (scp->cmd_len == 12) {
503 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
504 ((u32) scp->cmnd[7] << 16) |
505 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
507 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
508 ((u32) scp->cmnd[3] << 16) |
509 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
513 * 16-byte READ(0x88) or WRITE(0x8A) cdb
515 else if (scp->cmd_len == 16) {
516 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
517 ((u32) scp->cmnd[11] << 16) |
518 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
520 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
521 ((u32) scp->cmnd[7] << 16) |
522 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
524 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
525 ((u32) scp->cmnd[3] << 16) |
526 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
533 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
534 sizeof(struct megasas_sge32);
537 ldio->flags |= MFI_FRAME_SGL64;
538 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
540 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
543 * Sense info specific
545 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
546 ldio->sense_buf_phys_addr_hi = 0;
547 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
549 sge_bytes = sge_sz * ldio->sge_count;
551 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
552 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
554 if (cmd->frame_count > 7)
555 cmd->frame_count = 8;
557 return cmd->frame_count;
561 * megasas_build_cmd - Prepares a command packet
562 * @instance: Adapter soft state
564 * @frame_count: [OUT] Number of frames used to prepare this command
566 static inline struct megasas_cmd *megasas_build_cmd(struct megasas_instance
568 struct scsi_cmnd *scp,
572 struct megasas_cmd *cmd;
575 * Find out if this is logical or physical drive command.
577 logical_cmd = MEGASAS_IS_LOGICAL(scp);
580 * Logical drive command
584 if (scp->device->id >= MEGASAS_MAX_LD) {
585 scp->result = DID_BAD_TARGET << 16;
589 switch (scp->cmnd[0]) {
602 if (scp->device->lun) {
603 scp->result = DID_BAD_TARGET << 16;
607 cmd = megasas_get_cmd(instance);
610 scp->result = DID_IMM_RETRY << 16;
614 *frame_count = megasas_build_ldio(instance, scp, cmd);
616 if (!(*frame_count)) {
617 megasas_return_cmd(instance, cmd);
627 if (scp->device->lun) {
628 scp->result = DID_BAD_TARGET << 16;
632 cmd = megasas_get_cmd(instance);
635 scp->result = DID_IMM_RETRY << 16;
639 *frame_count = megasas_build_dcdb(instance, scp, cmd);
641 if (!(*frame_count)) {
642 megasas_return_cmd(instance, cmd);
649 cmd = megasas_get_cmd(instance);
652 scp->result = DID_IMM_RETRY << 16;
656 *frame_count = megasas_build_dcdb(instance, scp, cmd);
658 if (!(*frame_count)) {
659 megasas_return_cmd(instance, cmd);
670 * megasas_queue_command - Queue entry point
671 * @scmd: SCSI command to be queued
672 * @done: Callback entry point
675 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
679 struct megasas_cmd *cmd;
680 struct megasas_instance *instance;
682 instance = (struct megasas_instance *)
683 scmd->device->host->hostdata;
684 scmd->scsi_done = done;
687 cmd = megasas_build_cmd(instance, scmd, &frame_count);
695 scmd->SCp.ptr = (char *)cmd;
696 scmd->SCp.sent_command = jiffies;
699 * Issue the command to the FW
701 spin_lock_irqsave(&instance->instance_lock, flags);
702 instance->fw_outstanding++;
703 spin_unlock_irqrestore(&instance->instance_lock, flags);
705 writel(((cmd->frame_phys_addr >> 3) | (cmd->frame_count - 1)),
706 &instance->reg_set->inbound_queue_port);
712 * megasas_wait_for_outstanding - Wait for all outstanding cmds
713 * @instance: Adapter soft state
715 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
716 * complete all its outstanding commands. Returns error if one or more IOs
717 * are pending after this time period. It also marks the controller dead.
719 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
722 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
724 for (i = 0; i < wait_time; i++) {
726 if (!instance->fw_outstanding)
729 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
730 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
731 "commands to complete\n", i,
732 instance->fw_outstanding);
738 if (instance->fw_outstanding) {
739 instance->hw_crit_error = 1;
747 * megasas_generic_reset - Generic reset routine
748 * @scmd: Mid-layer SCSI command
750 * This routine implements a generic reset handler for device, bus and host
751 * reset requests. Device, bus and host specific reset handlers can use this
752 * function after they do their specific tasks.
754 static int megasas_generic_reset(struct scsi_cmnd *scmd)
757 struct megasas_instance *instance;
759 instance = (struct megasas_instance *)scmd->device->host->hostdata;
761 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
762 scmd->serial_number, scmd->cmnd[0]);
764 if (instance->hw_crit_error) {
765 printk(KERN_ERR "megasas: cannot recover from previous reset "
770 spin_unlock(scmd->device->host->host_lock);
772 ret_val = megasas_wait_for_outstanding(instance);
774 if (ret_val == SUCCESS)
775 printk(KERN_NOTICE "megasas: reset successful \n");
777 printk(KERN_ERR "megasas: failed to do reset\n");
779 spin_lock(scmd->device->host->host_lock);
784 static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
786 unsigned long seconds;
789 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
792 return EH_RESET_TIMER;
794 return EH_NOT_HANDLED;
802 * megasas_reset_device - Device reset handler entry point
804 static int megasas_reset_device(struct scsi_cmnd *scmd)
809 * First wait for all commands to complete
811 ret = megasas_generic_reset(scmd);
817 * megasas_reset_bus_host - Bus & host reset handler entry point
819 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
824 * Frist wait for all commands to complete
826 ret = megasas_generic_reset(scmd);
832 * megasas_service_aen - Processes an event notification
833 * @instance: Adapter soft state
834 * @cmd: AEN command completed by the ISR
836 * For AEN, driver sends a command down to FW that is held by the FW till an
837 * event occurs. When an event of interest occurs, FW completes the command
838 * that it was previously holding.
840 * This routines sends SIGIO signal to processes that have registered with the
844 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
847 * Don't signal app if it is just an aborted previously registered aen
850 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
854 instance->aen_cmd = NULL;
855 megasas_return_cmd(instance, cmd);
859 * Scsi host template for megaraid_sas driver
861 static struct scsi_host_template megasas_template = {
863 .module = THIS_MODULE,
864 .name = "LSI Logic SAS based MegaRAID driver",
865 .proc_name = "megaraid_sas",
866 .queuecommand = megasas_queue_command,
867 .eh_device_reset_handler = megasas_reset_device,
868 .eh_bus_reset_handler = megasas_reset_bus_host,
869 .eh_host_reset_handler = megasas_reset_bus_host,
870 .eh_timed_out = megasas_reset_timer,
871 .use_clustering = ENABLE_CLUSTERING,
875 * megasas_complete_int_cmd - Completes an internal command
876 * @instance: Adapter soft state
877 * @cmd: Command to be completed
879 * The megasas_issue_blocked_cmd() function waits for a command to complete
880 * after it issues a command. This function wakes up that waiting routine by
881 * calling wake_up() on the wait queue.
884 megasas_complete_int_cmd(struct megasas_instance *instance,
885 struct megasas_cmd *cmd)
887 cmd->cmd_status = cmd->frame->io.cmd_status;
889 if (cmd->cmd_status == ENODATA) {
892 wake_up(&instance->int_cmd_wait_q);
896 * megasas_complete_abort - Completes aborting a command
897 * @instance: Adapter soft state
898 * @cmd: Cmd that was issued to abort another cmd
900 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
901 * after it issues an abort on a previously issued command. This function
902 * wakes up all functions waiting on the same wait queue.
905 megasas_complete_abort(struct megasas_instance *instance,
906 struct megasas_cmd *cmd)
911 wake_up(&instance->abort_cmd_wait_q);
918 * megasas_unmap_sgbuf - Unmap SG buffers
919 * @instance: Adapter soft state
920 * @cmd: Completed command
923 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
928 if (cmd->scmd->use_sg) {
929 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
930 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
934 if (!cmd->scmd->request_bufflen)
937 opcode = cmd->frame->hdr.cmd;
939 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
941 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
943 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
946 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
948 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
951 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
952 cmd->scmd->sc_data_direction);
957 * megasas_complete_cmd - Completes a command
958 * @instance: Adapter soft state
959 * @cmd: Command to be completed
960 * @alt_status: If non-zero, use this value as status to
961 * SCSI mid-layer instead of the value returned
962 * by the FW. This should be used if caller wants
963 * an alternate status (as in the case of aborted
967 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
971 struct megasas_header *hdr = &cmd->frame->hdr;
975 cmd->scmd->SCp.ptr = (char *)0;
980 case MFI_CMD_PD_SCSI_IO:
981 case MFI_CMD_LD_SCSI_IO:
984 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
985 * issued either through an IO path or an IOCTL path. If it
986 * was via IOCTL, we will send it to internal completion.
990 megasas_complete_int_cmd(instance, cmd);
995 * Don't export physical disk devices to mid-layer.
997 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
998 (hdr->cmd_status == MFI_STAT_OK) &&
999 (cmd->scmd->cmnd[0] == INQUIRY)) {
1001 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
1003 cmd->scmd->result = DID_BAD_TARGET << 16;
1008 case MFI_CMD_LD_READ:
1009 case MFI_CMD_LD_WRITE:
1012 cmd->scmd->result = alt_status << 16;
1018 spin_lock_irqsave(&instance->instance_lock, flags);
1019 instance->fw_outstanding--;
1020 spin_unlock_irqrestore(&instance->instance_lock, flags);
1022 megasas_unmap_sgbuf(instance, cmd);
1023 cmd->scmd->scsi_done(cmd->scmd);
1024 megasas_return_cmd(instance, cmd);
1029 switch (hdr->cmd_status) {
1032 cmd->scmd->result = DID_OK << 16;
1035 case MFI_STAT_SCSI_IO_FAILED:
1036 case MFI_STAT_LD_INIT_IN_PROGRESS:
1038 (DID_ERROR << 16) | hdr->scsi_status;
1041 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1043 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1045 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1046 memset(cmd->scmd->sense_buffer, 0,
1047 SCSI_SENSE_BUFFERSIZE);
1048 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1051 cmd->scmd->result |= DRIVER_SENSE << 24;
1056 case MFI_STAT_LD_OFFLINE:
1057 case MFI_STAT_DEVICE_NOT_FOUND:
1058 cmd->scmd->result = DID_BAD_TARGET << 16;
1062 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1064 cmd->scmd->result = DID_ERROR << 16;
1068 spin_lock_irqsave(&instance->instance_lock, flags);
1069 instance->fw_outstanding--;
1070 spin_unlock_irqrestore(&instance->instance_lock, flags);
1072 megasas_unmap_sgbuf(instance, cmd);
1073 cmd->scmd->scsi_done(cmd->scmd);
1074 megasas_return_cmd(instance, cmd);
1083 * See if got an event notification
1085 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1086 megasas_service_aen(instance, cmd);
1088 megasas_complete_int_cmd(instance, cmd);
1094 * Cmd issued to abort another cmd returned
1096 megasas_complete_abort(instance, cmd);
1100 printk("megasas: Unknown command completed! [0x%X]\n",
1107 * megasas_deplete_reply_queue - Processes all completed commands
1108 * @instance: Adapter soft state
1109 * @alt_status: Alternate status to be returned to
1110 * SCSI mid-layer instead of the status
1111 * returned by the FW
1114 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1120 struct megasas_cmd *cmd;
1123 * Check if it is our interrupt
1125 status = readl(&instance->reg_set->outbound_intr_status);
1127 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1132 * Clear the interrupt by writing back the same value
1134 writel(status, &instance->reg_set->outbound_intr_status);
1136 producer = *instance->producer;
1137 consumer = *instance->consumer;
1139 while (consumer != producer) {
1140 context = instance->reply_queue[consumer];
1142 cmd = instance->cmd_list[context];
1144 megasas_complete_cmd(instance, cmd, alt_status);
1147 if (consumer == (instance->max_fw_cmds + 1)) {
1152 *instance->consumer = producer;
1158 * megasas_isr - isr entry point
1160 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1162 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1167 * megasas_transition_to_ready - Move the FW to READY state
1168 * @reg_set: MFI register set
1170 * During the initialization, FW passes can potentially be in any one of
1171 * several possible states. If the FW in operational, waiting-for-handshake
1172 * states, driver must take steps to bring it to ready state. Otherwise, it
1173 * has to wait for the ready state.
1176 megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1183 fw_state = readl(®_set->outbound_msg_0) & MFI_STATE_MASK;
1185 while (fw_state != MFI_STATE_READY) {
1187 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1191 case MFI_STATE_FAULT:
1193 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1196 case MFI_STATE_WAIT_HANDSHAKE:
1198 * Set the CLR bit in inbound doorbell
1200 writel(MFI_INIT_CLEAR_HANDSHAKE,
1201 ®_set->inbound_doorbell);
1204 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1207 case MFI_STATE_OPERATIONAL:
1209 * Bring it to READY state; assuming max wait 2 secs
1211 megasas_disable_intr(reg_set);
1212 writel(MFI_INIT_READY, ®_set->inbound_doorbell);
1215 cur_state = MFI_STATE_OPERATIONAL;
1218 case MFI_STATE_UNDEFINED:
1220 * This state should not last for more than 2 seconds
1223 cur_state = MFI_STATE_UNDEFINED;
1226 case MFI_STATE_BB_INIT:
1228 cur_state = MFI_STATE_BB_INIT;
1231 case MFI_STATE_FW_INIT:
1233 cur_state = MFI_STATE_FW_INIT;
1236 case MFI_STATE_FW_INIT_2:
1238 cur_state = MFI_STATE_FW_INIT_2;
1241 case MFI_STATE_DEVICE_SCAN:
1243 cur_state = MFI_STATE_DEVICE_SCAN;
1246 case MFI_STATE_FLUSH_CACHE:
1248 cur_state = MFI_STATE_FLUSH_CACHE;
1252 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1258 * The cur_state should not last for more than max_wait secs
1260 for (i = 0; i < (max_wait * 1000); i++) {
1261 fw_state = MFI_STATE_MASK &
1262 readl(®_set->outbound_msg_0);
1264 if (fw_state == cur_state) {
1271 * Return error if fw_state hasn't changed after max_wait
1273 if (fw_state == cur_state) {
1274 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1275 "in %d secs\n", fw_state, max_wait);
1284 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1285 * @instance: Adapter soft state
1287 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1290 u32 max_cmd = instance->max_fw_cmds;
1291 struct megasas_cmd *cmd;
1293 if (!instance->frame_dma_pool)
1297 * Return all frames to pool
1299 for (i = 0; i < max_cmd; i++) {
1301 cmd = instance->cmd_list[i];
1304 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1305 cmd->frame_phys_addr);
1308 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1309 cmd->sense_phys_addr);
1313 * Now destroy the pool itself
1315 pci_pool_destroy(instance->frame_dma_pool);
1316 pci_pool_destroy(instance->sense_dma_pool);
1318 instance->frame_dma_pool = NULL;
1319 instance->sense_dma_pool = NULL;
1323 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1324 * @instance: Adapter soft state
1326 * Each command packet has an embedded DMA memory buffer that is used for
1327 * filling MFI frame and the SG list that immediately follows the frame. This
1328 * function creates those DMA memory buffers for each command packet by using
1329 * PCI pool facility.
1331 static int megasas_create_frame_pool(struct megasas_instance *instance)
1339 struct megasas_cmd *cmd;
1341 max_cmd = instance->max_fw_cmds;
1344 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1345 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1347 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1348 sizeof(struct megasas_sge32);
1351 * Calculated the number of 64byte frames required for SGL
1353 sgl_sz = sge_sz * instance->max_num_sge;
1354 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1357 * We need one extra frame for the MFI command
1361 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1363 * Use DMA pool facility provided by PCI layer
1365 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1366 instance->pdev, total_sz, 64,
1369 if (!instance->frame_dma_pool) {
1370 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1374 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1375 instance->pdev, 128, 4, 0);
1377 if (!instance->sense_dma_pool) {
1378 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1380 pci_pool_destroy(instance->frame_dma_pool);
1381 instance->frame_dma_pool = NULL;
1387 * Allocate and attach a frame to each of the commands in cmd_list.
1388 * By making cmd->index as the context instead of the &cmd, we can
1389 * always use 32bit context regardless of the architecture
1391 for (i = 0; i < max_cmd; i++) {
1393 cmd = instance->cmd_list[i];
1395 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1396 GFP_KERNEL, &cmd->frame_phys_addr);
1398 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1399 GFP_KERNEL, &cmd->sense_phys_addr);
1402 * megasas_teardown_frame_pool() takes care of freeing
1403 * whatever has been allocated
1405 if (!cmd->frame || !cmd->sense) {
1406 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1407 megasas_teardown_frame_pool(instance);
1411 cmd->frame->io.context = cmd->index;
1418 * megasas_free_cmds - Free all the cmds in the free cmd pool
1419 * @instance: Adapter soft state
1421 static void megasas_free_cmds(struct megasas_instance *instance)
1424 /* First free the MFI frame pool */
1425 megasas_teardown_frame_pool(instance);
1427 /* Free all the commands in the cmd_list */
1428 for (i = 0; i < instance->max_fw_cmds; i++)
1429 kfree(instance->cmd_list[i]);
1431 /* Free the cmd_list buffer itself */
1432 kfree(instance->cmd_list);
1433 instance->cmd_list = NULL;
1435 INIT_LIST_HEAD(&instance->cmd_pool);
1439 * megasas_alloc_cmds - Allocates the command packets
1440 * @instance: Adapter soft state
1442 * Each command that is issued to the FW, whether IO commands from the OS or
1443 * internal commands like IOCTLs, are wrapped in local data structure called
1444 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1447 * Each frame has a 32-bit field called context (tag). This context is used
1448 * to get back the megasas_cmd from the frame when a frame gets completed in
1449 * the ISR. Typically the address of the megasas_cmd itself would be used as
1450 * the context. But we wanted to keep the differences between 32 and 64 bit
1451 * systems to the mininum. We always use 32 bit integers for the context. In
1452 * this driver, the 32 bit values are the indices into an array cmd_list.
1453 * This array is used only to look up the megasas_cmd given the context. The
1454 * free commands themselves are maintained in a linked list called cmd_pool.
1456 static int megasas_alloc_cmds(struct megasas_instance *instance)
1461 struct megasas_cmd *cmd;
1463 max_cmd = instance->max_fw_cmds;
1466 * instance->cmd_list is an array of struct megasas_cmd pointers.
1467 * Allocate the dynamic array first and then allocate individual
1470 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1473 if (!instance->cmd_list) {
1474 printk(KERN_DEBUG "megasas: out of memory\n");
1478 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1480 for (i = 0; i < max_cmd; i++) {
1481 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1484 if (!instance->cmd_list[i]) {
1486 for (j = 0; j < i; j++)
1487 kfree(instance->cmd_list[j]);
1489 kfree(instance->cmd_list);
1490 instance->cmd_list = NULL;
1497 * Add all the commands to command pool (instance->cmd_pool)
1499 for (i = 0; i < max_cmd; i++) {
1500 cmd = instance->cmd_list[i];
1501 memset(cmd, 0, sizeof(struct megasas_cmd));
1503 cmd->instance = instance;
1505 list_add_tail(&cmd->list, &instance->cmd_pool);
1509 * Create a frame pool and assign one frame to each cmd
1511 if (megasas_create_frame_pool(instance)) {
1512 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1513 megasas_free_cmds(instance);
1520 * megasas_get_controller_info - Returns FW's controller structure
1521 * @instance: Adapter soft state
1522 * @ctrl_info: Controller information structure
1524 * Issues an internal command (DCMD) to get the FW's controller structure.
1525 * This information is mainly used to find out the maximum IO transfer per
1526 * command supported by the FW.
1529 megasas_get_ctrl_info(struct megasas_instance *instance,
1530 struct megasas_ctrl_info *ctrl_info)
1533 struct megasas_cmd *cmd;
1534 struct megasas_dcmd_frame *dcmd;
1535 struct megasas_ctrl_info *ci;
1536 dma_addr_t ci_h = 0;
1538 cmd = megasas_get_cmd(instance);
1541 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1545 dcmd = &cmd->frame->dcmd;
1547 ci = pci_alloc_consistent(instance->pdev,
1548 sizeof(struct megasas_ctrl_info), &ci_h);
1551 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1552 megasas_return_cmd(instance, cmd);
1556 memset(ci, 0, sizeof(*ci));
1557 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1559 dcmd->cmd = MFI_CMD_DCMD;
1560 dcmd->cmd_status = 0xFF;
1561 dcmd->sge_count = 1;
1562 dcmd->flags = MFI_FRAME_DIR_READ;
1564 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1565 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1566 dcmd->sgl.sge32[0].phys_addr = ci_h;
1567 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1569 if (!megasas_issue_polled(instance, cmd)) {
1571 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1576 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1579 megasas_return_cmd(instance, cmd);
1584 * megasas_init_mfi - Initializes the FW
1585 * @instance: Adapter soft state
1587 * This is the main function for initializing MFI firmware.
1589 static int megasas_init_mfi(struct megasas_instance *instance)
1595 struct megasas_register_set __iomem *reg_set;
1597 struct megasas_cmd *cmd;
1598 struct megasas_ctrl_info *ctrl_info;
1600 struct megasas_init_frame *init_frame;
1601 struct megasas_init_queue_info *initq_info;
1602 dma_addr_t init_frame_h;
1603 dma_addr_t initq_info_h;
1606 * Map the message registers
1608 instance->base_addr = pci_resource_start(instance->pdev, 0);
1610 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1611 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1615 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1617 if (!instance->reg_set) {
1618 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1622 reg_set = instance->reg_set;
1625 * We expect the FW state to be READY
1627 if (megasas_transition_to_ready(instance->reg_set))
1628 goto fail_ready_state;
1631 * Get various operational parameters from status register
1633 instance->max_fw_cmds = readl(®_set->outbound_msg_0) & 0x00FFFF;
1634 instance->max_num_sge = (readl(®_set->outbound_msg_0) & 0xFF0000) >>
1637 * Create a pool of commands
1639 if (megasas_alloc_cmds(instance))
1640 goto fail_alloc_cmds;
1643 * Allocate memory for reply queue. Length of reply queue should
1644 * be _one_ more than the maximum commands handled by the firmware.
1646 * Note: When FW completes commands, it places corresponding contex
1647 * values in this circular reply queue. This circular queue is a fairly
1648 * typical producer-consumer queue. FW is the producer (of completed
1649 * commands) and the driver is the consumer.
1651 context_sz = sizeof(u32);
1652 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1654 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1656 &instance->reply_queue_h);
1658 if (!instance->reply_queue) {
1659 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1660 goto fail_reply_queue;
1664 * Prepare a init frame. Note the init frame points to queue info
1665 * structure. Each frame has SGL allocated after first 64 bytes. For
1666 * this frame - since we don't need any SGL - we use SGL's space as
1667 * queue info structure
1669 * We will not get a NULL command below. We just created the pool.
1671 cmd = megasas_get_cmd(instance);
1673 init_frame = (struct megasas_init_frame *)cmd->frame;
1674 initq_info = (struct megasas_init_queue_info *)
1675 ((unsigned long)init_frame + 64);
1677 init_frame_h = cmd->frame_phys_addr;
1678 initq_info_h = init_frame_h + 64;
1680 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1681 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1683 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1684 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1686 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1687 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1689 init_frame->cmd = MFI_CMD_INIT;
1690 init_frame->cmd_status = 0xFF;
1691 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1693 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1696 * Issue the init frame in polled mode
1698 if (megasas_issue_polled(instance, cmd)) {
1699 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1703 megasas_return_cmd(instance, cmd);
1705 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1708 * Compute the max allowed sectors per IO: The controller info has two
1709 * limits on max sectors. Driver should use the minimum of these two.
1711 * 1 << stripe_sz_ops.min = max sectors per strip
1713 * Note that older firmwares ( < FW ver 30) didn't report information
1714 * to calculate max_sectors_1. So the number ended up as zero always.
1716 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1718 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1719 ctrl_info->max_strips_per_io;
1720 max_sectors_2 = ctrl_info->max_request_size;
1722 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1723 ? max_sectors_1 : max_sectors_2;
1725 instance->max_sectors_per_req = instance->max_num_sge *
1733 megasas_return_cmd(instance, cmd);
1735 pci_free_consistent(instance->pdev, reply_q_sz,
1736 instance->reply_queue, instance->reply_queue_h);
1738 megasas_free_cmds(instance);
1742 iounmap(instance->reg_set);
1745 pci_release_regions(instance->pdev);
1751 * megasas_release_mfi - Reverses the FW initialization
1752 * @intance: Adapter soft state
1754 static void megasas_release_mfi(struct megasas_instance *instance)
1756 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1758 pci_free_consistent(instance->pdev, reply_q_sz,
1759 instance->reply_queue, instance->reply_queue_h);
1761 megasas_free_cmds(instance);
1763 iounmap(instance->reg_set);
1765 pci_release_regions(instance->pdev);
1769 * megasas_get_seq_num - Gets latest event sequence numbers
1770 * @instance: Adapter soft state
1771 * @eli: FW event log sequence numbers information
1773 * FW maintains a log of all events in a non-volatile area. Upper layers would
1774 * usually find out the latest sequence number of the events, the seq number at
1775 * the boot etc. They would "read" all the events below the latest seq number
1776 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1777 * number), they would subsribe to AEN (asynchronous event notification) and
1778 * wait for the events to happen.
1781 megasas_get_seq_num(struct megasas_instance *instance,
1782 struct megasas_evt_log_info *eli)
1784 struct megasas_cmd *cmd;
1785 struct megasas_dcmd_frame *dcmd;
1786 struct megasas_evt_log_info *el_info;
1787 dma_addr_t el_info_h = 0;
1789 cmd = megasas_get_cmd(instance);
1795 dcmd = &cmd->frame->dcmd;
1796 el_info = pci_alloc_consistent(instance->pdev,
1797 sizeof(struct megasas_evt_log_info),
1801 megasas_return_cmd(instance, cmd);
1805 memset(el_info, 0, sizeof(*el_info));
1806 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1808 dcmd->cmd = MFI_CMD_DCMD;
1809 dcmd->cmd_status = 0x0;
1810 dcmd->sge_count = 1;
1811 dcmd->flags = MFI_FRAME_DIR_READ;
1813 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1814 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1815 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1816 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1818 megasas_issue_blocked_cmd(instance, cmd);
1821 * Copy the data back into callers buffer
1823 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1825 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1826 el_info, el_info_h);
1828 megasas_return_cmd(instance, cmd);
1834 * megasas_register_aen - Registers for asynchronous event notification
1835 * @instance: Adapter soft state
1836 * @seq_num: The starting sequence number
1837 * @class_locale: Class of the event
1839 * This function subscribes for AEN for events beyond the @seq_num. It requests
1840 * to be notified if and only if the event is of type @class_locale
1843 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1844 u32 class_locale_word)
1847 struct megasas_cmd *cmd;
1848 struct megasas_dcmd_frame *dcmd;
1849 union megasas_evt_class_locale curr_aen;
1850 union megasas_evt_class_locale prev_aen;
1853 * If there an AEN pending already (aen_cmd), check if the
1854 * class_locale of that pending AEN is inclusive of the new
1855 * AEN request we currently have. If it is, then we don't have
1856 * to do anything. In other words, whichever events the current
1857 * AEN request is subscribing to, have already been subscribed
1860 * If the old_cmd is _not_ inclusive, then we have to abort
1861 * that command, form a class_locale that is superset of both
1862 * old and current and re-issue to the FW
1865 curr_aen.word = class_locale_word;
1867 if (instance->aen_cmd) {
1869 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1872 * A class whose enum value is smaller is inclusive of all
1873 * higher values. If a PROGRESS (= -1) was previously
1874 * registered, then a new registration requests for higher
1875 * classes need not be sent to FW. They are automatically
1878 * Locale numbers don't have such hierarchy. They are bitmap
1881 if ((prev_aen.members.class <= curr_aen.members.class) &&
1882 !((prev_aen.members.locale & curr_aen.members.locale) ^
1883 curr_aen.members.locale)) {
1885 * Previously issued event registration includes
1886 * current request. Nothing to do.
1890 curr_aen.members.locale |= prev_aen.members.locale;
1892 if (prev_aen.members.class < curr_aen.members.class)
1893 curr_aen.members.class = prev_aen.members.class;
1895 instance->aen_cmd->abort_aen = 1;
1896 ret_val = megasas_issue_blocked_abort_cmd(instance,
1901 printk(KERN_DEBUG "megasas: Failed to abort "
1902 "previous AEN command\n");
1908 cmd = megasas_get_cmd(instance);
1913 dcmd = &cmd->frame->dcmd;
1915 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1918 * Prepare DCMD for aen registration
1920 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1922 dcmd->cmd = MFI_CMD_DCMD;
1923 dcmd->cmd_status = 0x0;
1924 dcmd->sge_count = 1;
1925 dcmd->flags = MFI_FRAME_DIR_READ;
1927 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1928 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1929 dcmd->mbox.w[0] = seq_num;
1930 dcmd->mbox.w[1] = curr_aen.word;
1931 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1932 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1935 * Store reference to the cmd used to register for AEN. When an
1936 * application wants us to register for AEN, we have to abort this
1937 * cmd and re-register with a new EVENT LOCALE supplied by that app
1939 instance->aen_cmd = cmd;
1942 * Issue the aen registration frame
1944 writel(cmd->frame_phys_addr >> 3,
1945 &instance->reg_set->inbound_queue_port);
1951 * megasas_start_aen - Subscribes to AEN during driver load time
1952 * @instance: Adapter soft state
1954 static int megasas_start_aen(struct megasas_instance *instance)
1956 struct megasas_evt_log_info eli;
1957 union megasas_evt_class_locale class_locale;
1960 * Get the latest sequence number from FW
1962 memset(&eli, 0, sizeof(eli));
1964 if (megasas_get_seq_num(instance, &eli))
1968 * Register AEN with FW for latest sequence number plus 1
1970 class_locale.members.reserved = 0;
1971 class_locale.members.locale = MR_EVT_LOCALE_ALL;
1972 class_locale.members.class = MR_EVT_CLASS_DEBUG;
1974 return megasas_register_aen(instance, eli.newest_seq_num + 1,
1979 * megasas_io_attach - Attaches this driver to SCSI mid-layer
1980 * @instance: Adapter soft state
1982 static int megasas_io_attach(struct megasas_instance *instance)
1984 struct Scsi_Host *host = instance->host;
1987 * Export parameters required by SCSI mid-layer
1989 host->irq = instance->pdev->irq;
1990 host->unique_id = instance->unique_id;
1991 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1992 host->this_id = instance->init_id;
1993 host->sg_tablesize = instance->max_num_sge;
1994 host->max_sectors = instance->max_sectors_per_req;
1995 host->cmd_per_lun = 128;
1996 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1997 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1998 host->max_lun = MEGASAS_MAX_LUN;
2001 * Notify the mid-layer about the new controller
2003 if (scsi_add_host(host, &instance->pdev->dev)) {
2004 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2009 * Trigger SCSI to scan our drives
2011 scsi_scan_host(host);
2016 * megasas_probe_one - PCI hotplug entry point
2017 * @pdev: PCI device structure
2018 * @id: PCI ids of supported hotplugged adapter
2020 static int __devinit
2021 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2024 struct Scsi_Host *host;
2025 struct megasas_instance *instance;
2028 * Announce PCI information
2030 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2031 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2032 pdev->subsystem_device);
2034 printk("bus %d:slot %d:func %d\n",
2035 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2038 * PCI prepping: enable device set bus mastering and dma mask
2040 rval = pci_enable_device(pdev);
2046 pci_set_master(pdev);
2049 * All our contollers are capable of performing 64-bit DMA
2052 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2054 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2055 goto fail_set_dma_mask;
2058 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2059 goto fail_set_dma_mask;
2062 host = scsi_host_alloc(&megasas_template,
2063 sizeof(struct megasas_instance));
2066 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2067 goto fail_alloc_instance;
2070 instance = (struct megasas_instance *)host->hostdata;
2071 memset(instance, 0, sizeof(*instance));
2073 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2074 &instance->producer_h);
2075 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2076 &instance->consumer_h);
2078 if (!instance->producer || !instance->consumer) {
2079 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2080 "producer, consumer\n");
2081 goto fail_alloc_dma_buf;
2084 *instance->producer = 0;
2085 *instance->consumer = 0;
2087 instance->evt_detail = pci_alloc_consistent(pdev,
2089 megasas_evt_detail),
2090 &instance->evt_detail_h);
2092 if (!instance->evt_detail) {
2093 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2094 "event detail structure\n");
2095 goto fail_alloc_dma_buf;
2099 * Initialize locks and queues
2101 INIT_LIST_HEAD(&instance->cmd_pool);
2103 init_waitqueue_head(&instance->int_cmd_wait_q);
2104 init_waitqueue_head(&instance->abort_cmd_wait_q);
2106 spin_lock_init(&instance->cmd_pool_lock);
2107 spin_lock_init(&instance->instance_lock);
2109 sema_init(&instance->aen_mutex, 1);
2110 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2113 * Initialize PCI related and misc parameters
2115 instance->pdev = pdev;
2116 instance->host = host;
2117 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2118 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2121 * Initialize MFI Firmware
2123 if (megasas_init_mfi(instance))
2129 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2130 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2134 megasas_enable_intr(instance->reg_set);
2137 * Store instance in PCI softstate
2139 pci_set_drvdata(pdev, instance);
2142 * Add this controller to megasas_mgmt_info structure so that it
2143 * can be exported to management applications
2145 megasas_mgmt_info.count++;
2146 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2147 megasas_mgmt_info.max_index++;
2150 * Initiate AEN (Asynchronous Event Notification)
2152 if (megasas_start_aen(instance)) {
2153 printk(KERN_DEBUG "megasas: start aen failed\n");
2154 goto fail_start_aen;
2158 * Register with SCSI mid-layer
2160 if (megasas_io_attach(instance))
2161 goto fail_io_attach;
2167 megasas_mgmt_info.count--;
2168 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2169 megasas_mgmt_info.max_index--;
2171 pci_set_drvdata(pdev, NULL);
2172 megasas_disable_intr(instance->reg_set);
2173 free_irq(instance->pdev->irq, instance);
2175 megasas_release_mfi(instance);
2180 if (instance->evt_detail)
2181 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2182 instance->evt_detail,
2183 instance->evt_detail_h);
2185 if (instance->producer)
2186 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2187 instance->producer_h);
2188 if (instance->consumer)
2189 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2190 instance->consumer_h);
2191 scsi_host_put(host);
2193 fail_alloc_instance:
2195 pci_disable_device(pdev);
2201 * megasas_flush_cache - Requests FW to flush all its caches
2202 * @instance: Adapter soft state
2204 static void megasas_flush_cache(struct megasas_instance *instance)
2206 struct megasas_cmd *cmd;
2207 struct megasas_dcmd_frame *dcmd;
2209 cmd = megasas_get_cmd(instance);
2214 dcmd = &cmd->frame->dcmd;
2216 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2218 dcmd->cmd = MFI_CMD_DCMD;
2219 dcmd->cmd_status = 0x0;
2220 dcmd->sge_count = 0;
2221 dcmd->flags = MFI_FRAME_DIR_NONE;
2223 dcmd->data_xfer_len = 0;
2224 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2225 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2227 megasas_issue_blocked_cmd(instance, cmd);
2229 megasas_return_cmd(instance, cmd);
2235 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2236 * @instance: Adapter soft state
2238 static void megasas_shutdown_controller(struct megasas_instance *instance)
2240 struct megasas_cmd *cmd;
2241 struct megasas_dcmd_frame *dcmd;
2243 cmd = megasas_get_cmd(instance);
2248 if (instance->aen_cmd)
2249 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2251 dcmd = &cmd->frame->dcmd;
2253 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2255 dcmd->cmd = MFI_CMD_DCMD;
2256 dcmd->cmd_status = 0x0;
2257 dcmd->sge_count = 0;
2258 dcmd->flags = MFI_FRAME_DIR_NONE;
2260 dcmd->data_xfer_len = 0;
2261 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2263 megasas_issue_blocked_cmd(instance, cmd);
2265 megasas_return_cmd(instance, cmd);
2271 * megasas_detach_one - PCI hot"un"plug entry point
2272 * @pdev: PCI device structure
2274 static void megasas_detach_one(struct pci_dev *pdev)
2277 struct Scsi_Host *host;
2278 struct megasas_instance *instance;
2280 instance = pci_get_drvdata(pdev);
2281 host = instance->host;
2283 scsi_remove_host(instance->host);
2284 megasas_flush_cache(instance);
2285 megasas_shutdown_controller(instance);
2288 * Take the instance off the instance array. Note that we will not
2289 * decrement the max_index. We let this array be sparse array
2291 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2292 if (megasas_mgmt_info.instance[i] == instance) {
2293 megasas_mgmt_info.count--;
2294 megasas_mgmt_info.instance[i] = NULL;
2300 pci_set_drvdata(instance->pdev, NULL);
2302 megasas_disable_intr(instance->reg_set);
2304 free_irq(instance->pdev->irq, instance);
2306 megasas_release_mfi(instance);
2308 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2309 instance->evt_detail, instance->evt_detail_h);
2311 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2312 instance->producer_h);
2314 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2315 instance->consumer_h);
2317 scsi_host_put(host);
2319 pci_set_drvdata(pdev, NULL);
2321 pci_disable_device(pdev);
2327 * megasas_shutdown - Shutdown entry point
2328 * @device: Generic device structure
2330 static void megasas_shutdown(struct pci_dev *pdev)
2332 struct megasas_instance *instance = pci_get_drvdata(pdev);
2333 megasas_flush_cache(instance);
2337 * megasas_mgmt_open - char node "open" entry point
2339 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2342 * Allow only those users with admin rights
2344 if (!capable(CAP_SYS_ADMIN))
2351 * megasas_mgmt_release - char node "release" entry point
2353 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2355 filep->private_data = NULL;
2356 fasync_helper(-1, filep, 0, &megasas_async_queue);
2362 * megasas_mgmt_fasync - Async notifier registration from applications
2364 * This function adds the calling process to a driver global queue. When an
2365 * event occurs, SIGIO will be sent to all processes in this queue.
2367 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2371 down(&megasas_async_queue_mutex);
2373 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2375 up(&megasas_async_queue_mutex);
2378 /* For sanity check when we get ioctl */
2379 filep->private_data = filep;
2383 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2389 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2390 * @instance: Adapter soft state
2391 * @argp: User's ioctl packet
2394 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2395 struct megasas_iocpacket __user * user_ioc,
2396 struct megasas_iocpacket *ioc)
2398 struct megasas_sge32 *kern_sge32;
2399 struct megasas_cmd *cmd;
2400 void *kbuff_arr[MAX_IOCTL_SGE];
2401 dma_addr_t buf_handle = 0;
2404 dma_addr_t sense_handle;
2407 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2409 if (ioc->sge_count > MAX_IOCTL_SGE) {
2410 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2411 ioc->sge_count, MAX_IOCTL_SGE);
2415 cmd = megasas_get_cmd(instance);
2417 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2422 * User's IOCTL packet has 2 frames (maximum). Copy those two
2423 * frames into our cmd's frames. cmd->frame's context will get
2424 * overwritten when we copy from user's frames. So set that value
2427 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2428 cmd->frame->hdr.context = cmd->index;
2431 * The management interface between applications and the fw uses
2432 * MFI frames. E.g, RAID configuration changes, LD property changes
2433 * etc are accomplishes through different kinds of MFI frames. The
2434 * driver needs to care only about substituting user buffers with
2435 * kernel buffers in SGLs. The location of SGL is embedded in the
2436 * struct iocpacket itself.
2438 kern_sge32 = (struct megasas_sge32 *)
2439 ((unsigned long)cmd->frame + ioc->sgl_off);
2442 * For each user buffer, create a mirror buffer and copy in
2444 for (i = 0; i < ioc->sge_count; i++) {
2445 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2446 ioc->sgl[i].iov_len,
2448 if (!kbuff_arr[i]) {
2449 printk(KERN_DEBUG "megasas: Failed to alloc "
2450 "kernel SGL buffer for IOCTL \n");
2456 * We don't change the dma_coherent_mask, so
2457 * pci_alloc_consistent only returns 32bit addresses
2459 kern_sge32[i].phys_addr = (u32) buf_handle;
2460 kern_sge32[i].length = ioc->sgl[i].iov_len;
2463 * We created a kernel buffer corresponding to the
2464 * user buffer. Now copy in from the user buffer
2466 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2467 (u32) (ioc->sgl[i].iov_len))) {
2473 if (ioc->sense_len) {
2474 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2482 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2483 *sense_ptr = sense_handle;
2487 * Set the sync_cmd flag so that the ISR knows not to complete this
2488 * cmd to the SCSI mid-layer
2491 megasas_issue_blocked_cmd(instance, cmd);
2495 * copy out the kernel buffers to user buffers
2497 for (i = 0; i < ioc->sge_count; i++) {
2498 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2499 ioc->sgl[i].iov_len)) {
2506 * copy out the sense
2508 if (ioc->sense_len) {
2510 * sense_ptr points to the location that has the user
2511 * sense buffer address
2513 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2516 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2517 sense, ioc->sense_len)) {
2524 * copy the status codes returned by the fw
2526 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2527 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2528 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2534 pci_free_consistent(instance->pdev, ioc->sense_len,
2535 sense, sense_handle);
2538 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2539 pci_free_consistent(instance->pdev,
2540 kern_sge32[i].length,
2541 kbuff_arr[i], kern_sge32[i].phys_addr);
2544 megasas_return_cmd(instance, cmd);
2548 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2552 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2554 if ((megasas_mgmt_info.instance[i]) &&
2555 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2556 return megasas_mgmt_info.instance[i];
2562 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2564 struct megasas_iocpacket __user *user_ioc =
2565 (struct megasas_iocpacket __user *)arg;
2566 struct megasas_iocpacket *ioc;
2567 struct megasas_instance *instance;
2570 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2574 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2579 instance = megasas_lookup_instance(ioc->host_no);
2586 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2588 if (down_interruptible(&instance->ioctl_sem)) {
2589 error = -ERESTARTSYS;
2592 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2593 up(&instance->ioctl_sem);
2600 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2602 struct megasas_instance *instance;
2603 struct megasas_aen aen;
2606 if (file->private_data != file) {
2607 printk(KERN_DEBUG "megasas: fasync_helper was not "
2612 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2615 instance = megasas_lookup_instance(aen.host_no);
2620 down(&instance->aen_mutex);
2621 error = megasas_register_aen(instance, aen.seq_num,
2622 aen.class_locale_word);
2623 up(&instance->aen_mutex);
2628 * megasas_mgmt_ioctl - char node ioctl entry point
2631 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2634 case MEGASAS_IOC_FIRMWARE:
2635 return megasas_mgmt_ioctl_fw(file, arg);
2637 case MEGASAS_IOC_GET_AEN:
2638 return megasas_mgmt_ioctl_aen(file, arg);
2644 #ifdef CONFIG_COMPAT
2645 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2647 struct compat_megasas_iocpacket __user *cioc =
2648 (struct compat_megasas_iocpacket __user *)arg;
2649 struct megasas_iocpacket __user *ioc =
2650 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2654 clear_user(ioc, sizeof(*ioc));
2656 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2657 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2658 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2659 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2660 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2661 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2664 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2667 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2668 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2669 copy_in_user(&ioc->sgl[i].iov_len,
2670 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2674 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2676 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2677 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2678 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2685 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2689 case MEGASAS_IOC_FIRMWARE:{
2690 return megasas_mgmt_compat_ioctl_fw(file, arg);
2692 case MEGASAS_IOC_GET_AEN:
2693 return megasas_mgmt_ioctl_aen(file, arg);
2701 * File operations structure for management interface
2703 static struct file_operations megasas_mgmt_fops = {
2704 .owner = THIS_MODULE,
2705 .open = megasas_mgmt_open,
2706 .release = megasas_mgmt_release,
2707 .fasync = megasas_mgmt_fasync,
2708 .unlocked_ioctl = megasas_mgmt_ioctl,
2709 #ifdef CONFIG_COMPAT
2710 .compat_ioctl = megasas_mgmt_compat_ioctl,
2715 * PCI hotplug support registration structure
2717 static struct pci_driver megasas_pci_driver = {
2719 .name = "megaraid_sas",
2720 .id_table = megasas_pci_table,
2721 .probe = megasas_probe_one,
2722 .remove = __devexit_p(megasas_detach_one),
2723 .shutdown = megasas_shutdown,
2727 * Sysfs driver attributes
2729 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2731 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2735 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2738 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2740 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2744 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2748 * megasas_init - Driver load entry point
2750 static int __init megasas_init(void)
2755 * Announce driver version and other information
2757 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2758 MEGASAS_EXT_VERSION);
2760 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2763 * Register character device node
2765 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2768 printk(KERN_DEBUG "megasas: failed to open device node\n");
2772 megasas_mgmt_majorno = rval;
2775 * Register ourselves as PCI hotplug module
2777 rval = pci_module_init(&megasas_pci_driver);
2780 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2781 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2784 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2785 driver_create_file(&megasas_pci_driver.driver,
2786 &driver_attr_release_date);
2792 * megasas_exit - Driver unload entry point
2794 static void __exit megasas_exit(void)
2796 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2797 driver_remove_file(&megasas_pci_driver.driver,
2798 &driver_attr_release_date);
2800 pci_unregister_driver(&megasas_pci_driver);
2801 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2804 module_init(megasas_init);
2805 module_exit(megasas_exit);