3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_sas.c
13 * Version : v00.00.03.05
16 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
17 * Sumant Patro <Sumant.Patro@lsil.com>
19 * List of supported controllers
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/list.h>
29 #include <linux/moduleparam.h>
30 #include <linux/module.h>
31 #include <linux/spinlock.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
34 #include <linux/uio.h>
35 #include <asm/uaccess.h>
37 #include <linux/compat.h>
38 #include <linux/mutex.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include "megaraid_sas.h"
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(MEGASAS_VERSION);
48 MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
52 * PCI ID table for all supported controllers
54 static struct pci_device_id megasas_pci_table[] = {
56 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
58 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
60 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
61 /* xscale IOP, vega */
62 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
67 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
69 static int megasas_mgmt_majorno;
70 static struct megasas_mgmt_info megasas_mgmt_info;
71 static struct fasync_struct *megasas_async_queue;
72 static DEFINE_MUTEX(megasas_async_queue_mutex);
74 static u32 megasas_dbg_lvl;
77 * megasas_get_cmd - Get a command from the free pool
78 * @instance: Adapter soft state
80 * Returns a free command from the pool
82 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
86 struct megasas_cmd *cmd = NULL;
88 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
90 if (!list_empty(&instance->cmd_pool)) {
91 cmd = list_entry((&instance->cmd_pool)->next,
92 struct megasas_cmd, list);
93 list_del_init(&cmd->list);
95 printk(KERN_ERR "megasas: Command pool empty!\n");
98 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
103 * megasas_return_cmd - Return a cmd to free command pool
104 * @instance: Adapter soft state
105 * @cmd: Command packet to be returned to free command pool
108 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
112 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
115 list_add_tail(&cmd->list, &instance->cmd_pool);
117 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
122 * The following functions are defined for xscale
123 * (deviceid : 1064R, PERC5) controllers
127 * megasas_enable_intr_xscale - Enables interrupts
128 * @regs: MFI register set
131 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
133 writel(1, &(regs)->outbound_intr_mask);
135 /* Dummy readl to force pci flush */
136 readl(®s->outbound_intr_mask);
140 * megasas_disable_intr_xscale -Disables interrupt
141 * @regs: MFI register set
144 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
147 writel(mask, ®s->outbound_intr_mask);
148 /* Dummy readl to force pci flush */
149 readl(®s->outbound_intr_mask);
153 * megasas_read_fw_status_reg_xscale - returns the current FW status value
154 * @regs: MFI register set
157 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
159 return readl(&(regs)->outbound_msg_0);
162 * megasas_clear_interrupt_xscale - Check & clear interrupt
163 * @regs: MFI register set
166 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
170 * Check if it is our interrupt
172 status = readl(®s->outbound_intr_status);
174 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
179 * Clear the interrupt by writing back the same value
181 writel(status, ®s->outbound_intr_status);
187 * megasas_fire_cmd_xscale - Sends command to the FW
188 * @frame_phys_addr : Physical address of cmd
189 * @frame_count : Number of frames for the command
190 * @regs : MFI register set
193 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
195 writel((frame_phys_addr >> 3)|(frame_count),
196 &(regs)->inbound_queue_port);
199 static struct megasas_instance_template megasas_instance_template_xscale = {
201 .fire_cmd = megasas_fire_cmd_xscale,
202 .enable_intr = megasas_enable_intr_xscale,
203 .disable_intr = megasas_disable_intr_xscale,
204 .clear_intr = megasas_clear_intr_xscale,
205 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
209 * This is the end of set of functions & definitions specific
210 * to xscale (deviceid : 1064R, PERC5) controllers
214 * The following functions are defined for ppc (deviceid : 0x60)
219 * megasas_enable_intr_ppc - Enables interrupts
220 * @regs: MFI register set
223 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
225 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
227 writel(~0x80000004, &(regs)->outbound_intr_mask);
229 /* Dummy readl to force pci flush */
230 readl(®s->outbound_intr_mask);
234 * megasas_disable_intr_ppc - Disable interrupt
235 * @regs: MFI register set
238 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
240 u32 mask = 0xFFFFFFFF;
241 writel(mask, ®s->outbound_intr_mask);
242 /* Dummy readl to force pci flush */
243 readl(®s->outbound_intr_mask);
247 * megasas_read_fw_status_reg_ppc - returns the current FW status value
248 * @regs: MFI register set
251 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
253 return readl(&(regs)->outbound_scratch_pad);
257 * megasas_clear_interrupt_ppc - Check & clear interrupt
258 * @regs: MFI register set
261 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
265 * Check if it is our interrupt
267 status = readl(®s->outbound_intr_status);
269 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
274 * Clear the interrupt by writing back the same value
276 writel(status, ®s->outbound_doorbell_clear);
281 * megasas_fire_cmd_ppc - Sends command to the FW
282 * @frame_phys_addr : Physical address of cmd
283 * @frame_count : Number of frames for the command
284 * @regs : MFI register set
287 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
289 writel((frame_phys_addr | (frame_count<<1))|1,
290 &(regs)->inbound_queue_port);
293 static struct megasas_instance_template megasas_instance_template_ppc = {
295 .fire_cmd = megasas_fire_cmd_ppc,
296 .enable_intr = megasas_enable_intr_ppc,
297 .disable_intr = megasas_disable_intr_ppc,
298 .clear_intr = megasas_clear_intr_ppc,
299 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
303 * This is the end of set of functions & definitions
304 * specific to ppc (deviceid : 0x60) controllers
308 * megasas_issue_polled - Issues a polling command
309 * @instance: Adapter soft state
310 * @cmd: Command packet to be issued
312 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
315 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
318 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
320 struct megasas_header *frame_hdr = &cmd->frame->hdr;
322 frame_hdr->cmd_status = 0xFF;
323 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
326 * Issue the frame using inbound queue port
328 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
331 * Wait for cmd_status to change
333 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
338 if (frame_hdr->cmd_status == 0xff)
345 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
346 * @instance: Adapter soft state
347 * @cmd: Command to be issued
349 * This function waits on an event for the command to be returned from ISR.
350 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
351 * Used to issue ioctl commands.
354 megasas_issue_blocked_cmd(struct megasas_instance *instance,
355 struct megasas_cmd *cmd)
357 cmd->cmd_status = ENODATA;
359 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
361 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
362 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
368 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
369 * @instance: Adapter soft state
370 * @cmd_to_abort: Previously issued cmd to be aborted
372 * MFI firmware can abort previously issued AEN comamnd (automatic event
373 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
374 * cmd and waits for return status.
375 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
378 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
379 struct megasas_cmd *cmd_to_abort)
381 struct megasas_cmd *cmd;
382 struct megasas_abort_frame *abort_fr;
384 cmd = megasas_get_cmd(instance);
389 abort_fr = &cmd->frame->abort;
392 * Prepare and issue the abort frame
394 abort_fr->cmd = MFI_CMD_ABORT;
395 abort_fr->cmd_status = 0xFF;
397 abort_fr->abort_context = cmd_to_abort->index;
398 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
399 abort_fr->abort_mfi_phys_addr_hi = 0;
402 cmd->cmd_status = 0xFF;
404 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
407 * Wait for this cmd to complete
409 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
410 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
412 megasas_return_cmd(instance, cmd);
417 * megasas_make_sgl32 - Prepares 32-bit SGL
418 * @instance: Adapter soft state
419 * @scp: SCSI command from the mid-layer
420 * @mfi_sgl: SGL to be filled in
422 * If successful, this function returns the number of SG elements. Otherwise,
426 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
427 union megasas_sgl *mfi_sgl)
431 struct scatterlist *os_sgl;
434 * Return 0 if there is no data transfer
436 if (!scp->request_buffer || !scp->request_bufflen)
440 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
447 mfi_sgl->sge32[0].length = scp->request_bufflen;
452 os_sgl = (struct scatterlist *)scp->request_buffer;
453 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
454 scp->sc_data_direction);
456 for (i = 0; i < sge_count; i++, os_sgl++) {
457 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
458 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
465 * megasas_make_sgl64 - Prepares 64-bit SGL
466 * @instance: Adapter soft state
467 * @scp: SCSI command from the mid-layer
468 * @mfi_sgl: SGL to be filled in
470 * If successful, this function returns the number of SG elements. Otherwise,
474 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
475 union megasas_sgl *mfi_sgl)
479 struct scatterlist *os_sgl;
482 * Return 0 if there is no data transfer
484 if (!scp->request_buffer || !scp->request_bufflen)
488 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
496 mfi_sgl->sge64[0].length = scp->request_bufflen;
501 os_sgl = (struct scatterlist *)scp->request_buffer;
502 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
503 scp->sc_data_direction);
505 for (i = 0; i < sge_count; i++, os_sgl++) {
506 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
507 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
514 * megasas_get_frame_count - Computes the number of frames
515 * @sge_count : number of sg elements
517 * Returns the number of frames required for numnber of sge's (sge_count)
520 u32 megasas_get_frame_count(u8 sge_count)
527 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
528 sizeof(struct megasas_sge32);
531 * Main frame can contain 2 SGEs for 64-bit SGLs and
532 * 3 SGEs for 32-bit SGLs
535 num_cnt = sge_count - 2;
537 num_cnt = sge_count - 3;
540 sge_bytes = sge_sz * num_cnt;
542 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
543 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
554 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
555 * @instance: Adapter soft state
557 * @cmd: Command to be prepared in
559 * This function prepares CDB commands. These are typcially pass-through
560 * commands to the devices.
563 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
564 struct megasas_cmd *cmd)
569 struct megasas_pthru_frame *pthru;
571 is_logical = MEGASAS_IS_LOGICAL(scp);
572 device_id = MEGASAS_DEV_INDEX(instance, scp);
573 pthru = (struct megasas_pthru_frame *)cmd->frame;
575 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
576 flags = MFI_FRAME_DIR_WRITE;
577 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
578 flags = MFI_FRAME_DIR_READ;
579 else if (scp->sc_data_direction == PCI_DMA_NONE)
580 flags = MFI_FRAME_DIR_NONE;
583 * Prepare the DCDB frame
585 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
586 pthru->cmd_status = 0x0;
587 pthru->scsi_status = 0x0;
588 pthru->target_id = device_id;
589 pthru->lun = scp->device->lun;
590 pthru->cdb_len = scp->cmd_len;
592 pthru->flags = flags;
593 pthru->data_xfer_len = scp->request_bufflen;
595 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
601 pthru->flags |= MFI_FRAME_SGL64;
602 pthru->sge_count = megasas_make_sgl64(instance, scp,
605 pthru->sge_count = megasas_make_sgl32(instance, scp,
609 * Sense info specific
611 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
612 pthru->sense_buf_phys_addr_hi = 0;
613 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
616 * Compute the total number of frames this command consumes. FW uses
617 * this number to pull sufficient number of frames from host memory.
619 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
621 return cmd->frame_count;
625 * megasas_build_ldio - Prepares IOs to logical devices
626 * @instance: Adapter soft state
628 * @cmd: Command to to be prepared
630 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
633 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
634 struct megasas_cmd *cmd)
637 u8 sc = scp->cmnd[0];
639 struct megasas_io_frame *ldio;
641 device_id = MEGASAS_DEV_INDEX(instance, scp);
642 ldio = (struct megasas_io_frame *)cmd->frame;
644 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
645 flags = MFI_FRAME_DIR_WRITE;
646 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
647 flags = MFI_FRAME_DIR_READ;
650 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
652 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
653 ldio->cmd_status = 0x0;
654 ldio->scsi_status = 0x0;
655 ldio->target_id = device_id;
657 ldio->reserved_0 = 0;
660 ldio->start_lba_hi = 0;
661 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
664 * 6-byte READ(0x08) or WRITE(0x0A) cdb
666 if (scp->cmd_len == 6) {
667 ldio->lba_count = (u32) scp->cmnd[4];
668 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
669 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
671 ldio->start_lba_lo &= 0x1FFFFF;
675 * 10-byte READ(0x28) or WRITE(0x2A) cdb
677 else if (scp->cmd_len == 10) {
678 ldio->lba_count = (u32) scp->cmnd[8] |
679 ((u32) scp->cmnd[7] << 8);
680 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
681 ((u32) scp->cmnd[3] << 16) |
682 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
686 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
688 else if (scp->cmd_len == 12) {
689 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
690 ((u32) scp->cmnd[7] << 16) |
691 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
693 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
694 ((u32) scp->cmnd[3] << 16) |
695 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
699 * 16-byte READ(0x88) or WRITE(0x8A) cdb
701 else if (scp->cmd_len == 16) {
702 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
703 ((u32) scp->cmnd[11] << 16) |
704 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
706 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
707 ((u32) scp->cmnd[7] << 16) |
708 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
710 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
711 ((u32) scp->cmnd[3] << 16) |
712 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
720 ldio->flags |= MFI_FRAME_SGL64;
721 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
723 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
726 * Sense info specific
728 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
729 ldio->sense_buf_phys_addr_hi = 0;
730 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
733 * Compute the total number of frames this command consumes. FW uses
734 * this number to pull sufficient number of frames from host memory.
736 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
738 return cmd->frame_count;
742 * megasas_is_ldio - Checks if the cmd is for logical drive
743 * @scmd: SCSI command
745 * Called by megasas_queue_command to find out if the command to be queued
746 * is a logical drive command
748 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
750 if (!MEGASAS_IS_LOGICAL(cmd))
752 switch (cmd->cmnd[0]) {
768 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
770 * @instance: Adapter soft state
773 megasas_dump_pending_frames(struct megasas_instance *instance)
775 struct megasas_cmd *cmd;
777 union megasas_sgl *mfi_sgl;
778 struct megasas_io_frame *ldio;
779 struct megasas_pthru_frame *pthru;
781 u32 max_cmd = instance->max_fw_cmds;
783 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
784 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
786 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
788 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
790 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
791 for (i = 0; i < max_cmd; i++) {
792 cmd = instance->cmd_list[i];
795 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
796 if (megasas_is_ldio(cmd->scmd)){
797 ldio = (struct megasas_io_frame *)cmd->frame;
798 mfi_sgl = &ldio->sgl;
799 sgcount = ldio->sge_count;
800 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount);
803 pthru = (struct megasas_pthru_frame *) cmd->frame;
804 mfi_sgl = &pthru->sgl;
805 sgcount = pthru->sge_count;
806 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount);
808 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
809 for (n = 0; n < sgcount; n++){
811 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ;
813 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
816 printk(KERN_ERR "\n");
818 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
819 for (i = 0; i < max_cmd; i++) {
821 cmd = instance->cmd_list[i];
823 if(cmd->sync_cmd == 1){
824 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
827 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
831 * megasas_queue_command - Queue entry point
832 * @scmd: SCSI command to be queued
833 * @done: Callback entry point
836 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
839 struct megasas_cmd *cmd;
840 struct megasas_instance *instance;
842 instance = (struct megasas_instance *)
843 scmd->device->host->hostdata;
844 scmd->scsi_done = done;
847 if (MEGASAS_IS_LOGICAL(scmd) &&
848 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
849 scmd->result = DID_BAD_TARGET << 16;
853 cmd = megasas_get_cmd(instance);
855 return SCSI_MLQUEUE_HOST_BUSY;
858 * Logical drive command
860 if (megasas_is_ldio(scmd))
861 frame_count = megasas_build_ldio(instance, scmd, cmd);
863 frame_count = megasas_build_dcdb(instance, scmd, cmd);
871 * Issue the command to the FW
873 atomic_inc(&instance->fw_outstanding);
875 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
880 megasas_return_cmd(instance, cmd);
886 static int megasas_slave_configure(struct scsi_device *sdev)
889 * Don't export physical disk devices to the disk driver.
891 * FIXME: Currently we don't export them to the midlayer at all.
892 * That will be fixed once LSI engineers have audited the
893 * firmware for possible issues.
895 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && sdev->type == TYPE_DISK)
899 * The RAID firmware may require extended timeouts.
901 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
902 sdev->timeout = 90 * HZ;
907 * megasas_wait_for_outstanding - Wait for all outstanding cmds
908 * @instance: Adapter soft state
910 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
911 * complete all its outstanding commands. Returns error if one or more IOs
912 * are pending after this time period. It also marks the controller dead.
914 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
917 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
919 for (i = 0; i < wait_time; i++) {
921 int outstanding = atomic_read(&instance->fw_outstanding);
926 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
927 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
928 "commands to complete\n",i,outstanding);
934 if (atomic_read(&instance->fw_outstanding)) {
936 * Send signal to FW to stop processing any pending cmds.
937 * The controller will be taken offline by the OS now.
940 &instance->reg_set->inbound_doorbell);
941 megasas_dump_pending_frames(instance);
942 instance->hw_crit_error = 1;
950 * megasas_generic_reset - Generic reset routine
951 * @scmd: Mid-layer SCSI command
953 * This routine implements a generic reset handler for device, bus and host
954 * reset requests. Device, bus and host specific reset handlers can use this
955 * function after they do their specific tasks.
957 static int megasas_generic_reset(struct scsi_cmnd *scmd)
960 struct megasas_instance *instance;
962 instance = (struct megasas_instance *)scmd->device->host->hostdata;
964 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
965 scmd->serial_number, scmd->cmnd[0]);
967 if (instance->hw_crit_error) {
968 printk(KERN_ERR "megasas: cannot recover from previous reset "
973 ret_val = megasas_wait_for_outstanding(instance);
974 if (ret_val == SUCCESS)
975 printk(KERN_NOTICE "megasas: reset successful \n");
977 printk(KERN_ERR "megasas: failed to do reset\n");
983 * megasas_reset_device - Device reset handler entry point
985 static int megasas_reset_device(struct scsi_cmnd *scmd)
990 * First wait for all commands to complete
992 ret = megasas_generic_reset(scmd);
998 * megasas_reset_bus_host - Bus & host reset handler entry point
1000 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1005 * First wait for all commands to complete
1007 ret = megasas_generic_reset(scmd);
1013 * megasas_service_aen - Processes an event notification
1014 * @instance: Adapter soft state
1015 * @cmd: AEN command completed by the ISR
1017 * For AEN, driver sends a command down to FW that is held by the FW till an
1018 * event occurs. When an event of interest occurs, FW completes the command
1019 * that it was previously holding.
1021 * This routines sends SIGIO signal to processes that have registered with the
1025 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1028 * Don't signal app if it is just an aborted previously registered aen
1030 if (!cmd->abort_aen)
1031 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1035 instance->aen_cmd = NULL;
1036 megasas_return_cmd(instance, cmd);
1040 * Scsi host template for megaraid_sas driver
1042 static struct scsi_host_template megasas_template = {
1044 .module = THIS_MODULE,
1045 .name = "LSI Logic SAS based MegaRAID driver",
1046 .proc_name = "megaraid_sas",
1047 .slave_configure = megasas_slave_configure,
1048 .queuecommand = megasas_queue_command,
1049 .eh_device_reset_handler = megasas_reset_device,
1050 .eh_bus_reset_handler = megasas_reset_bus_host,
1051 .eh_host_reset_handler = megasas_reset_bus_host,
1052 .use_clustering = ENABLE_CLUSTERING,
1056 * megasas_complete_int_cmd - Completes an internal command
1057 * @instance: Adapter soft state
1058 * @cmd: Command to be completed
1060 * The megasas_issue_blocked_cmd() function waits for a command to complete
1061 * after it issues a command. This function wakes up that waiting routine by
1062 * calling wake_up() on the wait queue.
1065 megasas_complete_int_cmd(struct megasas_instance *instance,
1066 struct megasas_cmd *cmd)
1068 cmd->cmd_status = cmd->frame->io.cmd_status;
1070 if (cmd->cmd_status == ENODATA) {
1071 cmd->cmd_status = 0;
1073 wake_up(&instance->int_cmd_wait_q);
1077 * megasas_complete_abort - Completes aborting a command
1078 * @instance: Adapter soft state
1079 * @cmd: Cmd that was issued to abort another cmd
1081 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1082 * after it issues an abort on a previously issued command. This function
1083 * wakes up all functions waiting on the same wait queue.
1086 megasas_complete_abort(struct megasas_instance *instance,
1087 struct megasas_cmd *cmd)
1089 if (cmd->sync_cmd) {
1091 cmd->cmd_status = 0;
1092 wake_up(&instance->abort_cmd_wait_q);
1099 * megasas_unmap_sgbuf - Unmap SG buffers
1100 * @instance: Adapter soft state
1101 * @cmd: Completed command
1104 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
1109 if (cmd->scmd->use_sg) {
1110 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
1111 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
1115 if (!cmd->scmd->request_bufflen)
1118 opcode = cmd->frame->hdr.cmd;
1120 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
1122 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
1124 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
1127 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
1129 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
1132 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
1133 cmd->scmd->sc_data_direction);
1138 * megasas_complete_cmd - Completes a command
1139 * @instance: Adapter soft state
1140 * @cmd: Command to be completed
1141 * @alt_status: If non-zero, use this value as status to
1142 * SCSI mid-layer instead of the value returned
1143 * by the FW. This should be used if caller wants
1144 * an alternate status (as in the case of aborted
1148 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1152 struct megasas_header *hdr = &cmd->frame->hdr;
1155 cmd->scmd->SCp.ptr = (char *)0;
1160 case MFI_CMD_PD_SCSI_IO:
1161 case MFI_CMD_LD_SCSI_IO:
1164 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1165 * issued either through an IO path or an IOCTL path. If it
1166 * was via IOCTL, we will send it to internal completion.
1168 if (cmd->sync_cmd) {
1170 megasas_complete_int_cmd(instance, cmd);
1174 case MFI_CMD_LD_READ:
1175 case MFI_CMD_LD_WRITE:
1178 cmd->scmd->result = alt_status << 16;
1184 atomic_dec(&instance->fw_outstanding);
1186 megasas_unmap_sgbuf(instance, cmd);
1187 cmd->scmd->scsi_done(cmd->scmd);
1188 megasas_return_cmd(instance, cmd);
1193 switch (hdr->cmd_status) {
1196 cmd->scmd->result = DID_OK << 16;
1199 case MFI_STAT_SCSI_IO_FAILED:
1200 case MFI_STAT_LD_INIT_IN_PROGRESS:
1202 (DID_ERROR << 16) | hdr->scsi_status;
1205 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1207 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1209 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1210 memset(cmd->scmd->sense_buffer, 0,
1211 SCSI_SENSE_BUFFERSIZE);
1212 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1215 cmd->scmd->result |= DRIVER_SENSE << 24;
1220 case MFI_STAT_LD_OFFLINE:
1221 case MFI_STAT_DEVICE_NOT_FOUND:
1222 cmd->scmd->result = DID_BAD_TARGET << 16;
1226 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1228 cmd->scmd->result = DID_ERROR << 16;
1232 atomic_dec(&instance->fw_outstanding);
1234 megasas_unmap_sgbuf(instance, cmd);
1235 cmd->scmd->scsi_done(cmd->scmd);
1236 megasas_return_cmd(instance, cmd);
1245 * See if got an event notification
1247 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1248 megasas_service_aen(instance, cmd);
1250 megasas_complete_int_cmd(instance, cmd);
1256 * Cmd issued to abort another cmd returned
1258 megasas_complete_abort(instance, cmd);
1262 printk("megasas: Unknown command completed! [0x%X]\n",
1269 * megasas_deplete_reply_queue - Processes all completed commands
1270 * @instance: Adapter soft state
1271 * @alt_status: Alternate status to be returned to
1272 * SCSI mid-layer instead of the status
1273 * returned by the FW
1276 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1279 * Check if it is our interrupt
1280 * Clear the interrupt
1282 if(instance->instancet->clear_intr(instance->reg_set))
1286 * Schedule the tasklet for cmd completion
1288 tasklet_schedule(&instance->isr_tasklet);
1294 * megasas_isr - isr entry point
1296 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1298 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1303 * megasas_transition_to_ready - Move the FW to READY state
1304 * @instance: Adapter soft state
1306 * During the initialization, FW passes can potentially be in any one of
1307 * several possible states. If the FW in operational, waiting-for-handshake
1308 * states, driver must take steps to bring it to ready state. Otherwise, it
1309 * has to wait for the ready state.
1312 megasas_transition_to_ready(struct megasas_instance* instance)
1319 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1321 if (fw_state != MFI_STATE_READY)
1322 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1325 while (fw_state != MFI_STATE_READY) {
1329 case MFI_STATE_FAULT:
1331 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1334 case MFI_STATE_WAIT_HANDSHAKE:
1336 * Set the CLR bit in inbound doorbell
1338 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1339 &instance->reg_set->inbound_doorbell);
1342 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1345 case MFI_STATE_BOOT_MESSAGE_PENDING:
1346 writel(MFI_INIT_HOTPLUG,
1347 &instance->reg_set->inbound_doorbell);
1350 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1353 case MFI_STATE_OPERATIONAL:
1355 * Bring it to READY state; assuming max wait 10 secs
1357 instance->instancet->disable_intr(instance->reg_set);
1358 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1361 cur_state = MFI_STATE_OPERATIONAL;
1364 case MFI_STATE_UNDEFINED:
1366 * This state should not last for more than 2 seconds
1369 cur_state = MFI_STATE_UNDEFINED;
1372 case MFI_STATE_BB_INIT:
1374 cur_state = MFI_STATE_BB_INIT;
1377 case MFI_STATE_FW_INIT:
1379 cur_state = MFI_STATE_FW_INIT;
1382 case MFI_STATE_FW_INIT_2:
1384 cur_state = MFI_STATE_FW_INIT_2;
1387 case MFI_STATE_DEVICE_SCAN:
1389 cur_state = MFI_STATE_DEVICE_SCAN;
1392 case MFI_STATE_FLUSH_CACHE:
1394 cur_state = MFI_STATE_FLUSH_CACHE;
1398 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1404 * The cur_state should not last for more than max_wait secs
1406 for (i = 0; i < (max_wait * 1000); i++) {
1407 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1410 if (fw_state == cur_state) {
1417 * Return error if fw_state hasn't changed after max_wait
1419 if (fw_state == cur_state) {
1420 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1421 "in %d secs\n", fw_state, max_wait);
1425 printk(KERN_INFO "megasas: FW now in Ready state\n");
1431 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1432 * @instance: Adapter soft state
1434 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1437 u32 max_cmd = instance->max_fw_cmds;
1438 struct megasas_cmd *cmd;
1440 if (!instance->frame_dma_pool)
1444 * Return all frames to pool
1446 for (i = 0; i < max_cmd; i++) {
1448 cmd = instance->cmd_list[i];
1451 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1452 cmd->frame_phys_addr);
1455 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1456 cmd->sense_phys_addr);
1460 * Now destroy the pool itself
1462 pci_pool_destroy(instance->frame_dma_pool);
1463 pci_pool_destroy(instance->sense_dma_pool);
1465 instance->frame_dma_pool = NULL;
1466 instance->sense_dma_pool = NULL;
1470 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1471 * @instance: Adapter soft state
1473 * Each command packet has an embedded DMA memory buffer that is used for
1474 * filling MFI frame and the SG list that immediately follows the frame. This
1475 * function creates those DMA memory buffers for each command packet by using
1476 * PCI pool facility.
1478 static int megasas_create_frame_pool(struct megasas_instance *instance)
1486 struct megasas_cmd *cmd;
1488 max_cmd = instance->max_fw_cmds;
1491 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1492 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1494 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1495 sizeof(struct megasas_sge32);
1498 * Calculated the number of 64byte frames required for SGL
1500 sgl_sz = sge_sz * instance->max_num_sge;
1501 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1504 * We need one extra frame for the MFI command
1508 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1510 * Use DMA pool facility provided by PCI layer
1512 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1513 instance->pdev, total_sz, 64,
1516 if (!instance->frame_dma_pool) {
1517 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1521 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1522 instance->pdev, 128, 4, 0);
1524 if (!instance->sense_dma_pool) {
1525 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1527 pci_pool_destroy(instance->frame_dma_pool);
1528 instance->frame_dma_pool = NULL;
1534 * Allocate and attach a frame to each of the commands in cmd_list.
1535 * By making cmd->index as the context instead of the &cmd, we can
1536 * always use 32bit context regardless of the architecture
1538 for (i = 0; i < max_cmd; i++) {
1540 cmd = instance->cmd_list[i];
1542 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1543 GFP_KERNEL, &cmd->frame_phys_addr);
1545 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1546 GFP_KERNEL, &cmd->sense_phys_addr);
1549 * megasas_teardown_frame_pool() takes care of freeing
1550 * whatever has been allocated
1552 if (!cmd->frame || !cmd->sense) {
1553 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1554 megasas_teardown_frame_pool(instance);
1558 cmd->frame->io.context = cmd->index;
1565 * megasas_free_cmds - Free all the cmds in the free cmd pool
1566 * @instance: Adapter soft state
1568 static void megasas_free_cmds(struct megasas_instance *instance)
1571 /* First free the MFI frame pool */
1572 megasas_teardown_frame_pool(instance);
1574 /* Free all the commands in the cmd_list */
1575 for (i = 0; i < instance->max_fw_cmds; i++)
1576 kfree(instance->cmd_list[i]);
1578 /* Free the cmd_list buffer itself */
1579 kfree(instance->cmd_list);
1580 instance->cmd_list = NULL;
1582 INIT_LIST_HEAD(&instance->cmd_pool);
1586 * megasas_alloc_cmds - Allocates the command packets
1587 * @instance: Adapter soft state
1589 * Each command that is issued to the FW, whether IO commands from the OS or
1590 * internal commands like IOCTLs, are wrapped in local data structure called
1591 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1594 * Each frame has a 32-bit field called context (tag). This context is used
1595 * to get back the megasas_cmd from the frame when a frame gets completed in
1596 * the ISR. Typically the address of the megasas_cmd itself would be used as
1597 * the context. But we wanted to keep the differences between 32 and 64 bit
1598 * systems to the mininum. We always use 32 bit integers for the context. In
1599 * this driver, the 32 bit values are the indices into an array cmd_list.
1600 * This array is used only to look up the megasas_cmd given the context. The
1601 * free commands themselves are maintained in a linked list called cmd_pool.
1603 static int megasas_alloc_cmds(struct megasas_instance *instance)
1608 struct megasas_cmd *cmd;
1610 max_cmd = instance->max_fw_cmds;
1613 * instance->cmd_list is an array of struct megasas_cmd pointers.
1614 * Allocate the dynamic array first and then allocate individual
1617 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1620 if (!instance->cmd_list) {
1621 printk(KERN_DEBUG "megasas: out of memory\n");
1625 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1627 for (i = 0; i < max_cmd; i++) {
1628 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1631 if (!instance->cmd_list[i]) {
1633 for (j = 0; j < i; j++)
1634 kfree(instance->cmd_list[j]);
1636 kfree(instance->cmd_list);
1637 instance->cmd_list = NULL;
1644 * Add all the commands to command pool (instance->cmd_pool)
1646 for (i = 0; i < max_cmd; i++) {
1647 cmd = instance->cmd_list[i];
1648 memset(cmd, 0, sizeof(struct megasas_cmd));
1650 cmd->instance = instance;
1652 list_add_tail(&cmd->list, &instance->cmd_pool);
1656 * Create a frame pool and assign one frame to each cmd
1658 if (megasas_create_frame_pool(instance)) {
1659 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1660 megasas_free_cmds(instance);
1667 * megasas_get_controller_info - Returns FW's controller structure
1668 * @instance: Adapter soft state
1669 * @ctrl_info: Controller information structure
1671 * Issues an internal command (DCMD) to get the FW's controller structure.
1672 * This information is mainly used to find out the maximum IO transfer per
1673 * command supported by the FW.
1676 megasas_get_ctrl_info(struct megasas_instance *instance,
1677 struct megasas_ctrl_info *ctrl_info)
1680 struct megasas_cmd *cmd;
1681 struct megasas_dcmd_frame *dcmd;
1682 struct megasas_ctrl_info *ci;
1683 dma_addr_t ci_h = 0;
1685 cmd = megasas_get_cmd(instance);
1688 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1692 dcmd = &cmd->frame->dcmd;
1694 ci = pci_alloc_consistent(instance->pdev,
1695 sizeof(struct megasas_ctrl_info), &ci_h);
1698 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1699 megasas_return_cmd(instance, cmd);
1703 memset(ci, 0, sizeof(*ci));
1704 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1706 dcmd->cmd = MFI_CMD_DCMD;
1707 dcmd->cmd_status = 0xFF;
1708 dcmd->sge_count = 1;
1709 dcmd->flags = MFI_FRAME_DIR_READ;
1711 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1712 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1713 dcmd->sgl.sge32[0].phys_addr = ci_h;
1714 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1716 if (!megasas_issue_polled(instance, cmd)) {
1718 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1723 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1726 megasas_return_cmd(instance, cmd);
1731 * megasas_complete_cmd_dpc - Returns FW's controller structure
1732 * @instance_addr: Address of adapter soft state
1734 * Tasklet to complete cmds
1736 void megasas_complete_cmd_dpc(unsigned long instance_addr)
1741 struct megasas_cmd *cmd;
1742 struct megasas_instance *instance = (struct megasas_instance *)instance_addr;
1744 producer = *instance->producer;
1745 consumer = *instance->consumer;
1747 while (consumer != producer) {
1748 context = instance->reply_queue[consumer];
1750 cmd = instance->cmd_list[context];
1752 megasas_complete_cmd(instance, cmd, DID_OK);
1755 if (consumer == (instance->max_fw_cmds + 1)) {
1760 *instance->consumer = producer;
1764 * megasas_init_mfi - Initializes the FW
1765 * @instance: Adapter soft state
1767 * This is the main function for initializing MFI firmware.
1769 static int megasas_init_mfi(struct megasas_instance *instance)
1775 struct megasas_register_set __iomem *reg_set;
1777 struct megasas_cmd *cmd;
1778 struct megasas_ctrl_info *ctrl_info;
1780 struct megasas_init_frame *init_frame;
1781 struct megasas_init_queue_info *initq_info;
1782 dma_addr_t init_frame_h;
1783 dma_addr_t initq_info_h;
1786 * Map the message registers
1788 instance->base_addr = pci_resource_start(instance->pdev, 0);
1790 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1791 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1795 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1797 if (!instance->reg_set) {
1798 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1802 reg_set = instance->reg_set;
1804 switch(instance->pdev->device)
1806 case PCI_DEVICE_ID_LSI_SAS1078R:
1807 instance->instancet = &megasas_instance_template_ppc;
1809 case PCI_DEVICE_ID_LSI_SAS1064R:
1810 case PCI_DEVICE_ID_DELL_PERC5:
1812 instance->instancet = &megasas_instance_template_xscale;
1817 * We expect the FW state to be READY
1819 if (megasas_transition_to_ready(instance))
1820 goto fail_ready_state;
1823 * Get various operational parameters from status register
1825 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1827 * Reduce the max supported cmds by 1. This is to ensure that the
1828 * reply_q_sz (1 more than the max cmd that driver may send)
1829 * does not exceed max cmds that the FW can support
1831 instance->max_fw_cmds = instance->max_fw_cmds-1;
1832 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
1835 * Create a pool of commands
1837 if (megasas_alloc_cmds(instance))
1838 goto fail_alloc_cmds;
1841 * Allocate memory for reply queue. Length of reply queue should
1842 * be _one_ more than the maximum commands handled by the firmware.
1844 * Note: When FW completes commands, it places corresponding contex
1845 * values in this circular reply queue. This circular queue is a fairly
1846 * typical producer-consumer queue. FW is the producer (of completed
1847 * commands) and the driver is the consumer.
1849 context_sz = sizeof(u32);
1850 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1852 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1854 &instance->reply_queue_h);
1856 if (!instance->reply_queue) {
1857 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1858 goto fail_reply_queue;
1862 * Prepare a init frame. Note the init frame points to queue info
1863 * structure. Each frame has SGL allocated after first 64 bytes. For
1864 * this frame - since we don't need any SGL - we use SGL's space as
1865 * queue info structure
1867 * We will not get a NULL command below. We just created the pool.
1869 cmd = megasas_get_cmd(instance);
1871 init_frame = (struct megasas_init_frame *)cmd->frame;
1872 initq_info = (struct megasas_init_queue_info *)
1873 ((unsigned long)init_frame + 64);
1875 init_frame_h = cmd->frame_phys_addr;
1876 initq_info_h = init_frame_h + 64;
1878 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1879 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1881 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1882 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1884 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1885 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1887 init_frame->cmd = MFI_CMD_INIT;
1888 init_frame->cmd_status = 0xFF;
1889 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1891 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1894 * disable the intr before firing the init frame to FW
1896 instance->instancet->disable_intr(instance->reg_set);
1899 * Issue the init frame in polled mode
1901 if (megasas_issue_polled(instance, cmd)) {
1902 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1906 megasas_return_cmd(instance, cmd);
1908 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1911 * Compute the max allowed sectors per IO: The controller info has two
1912 * limits on max sectors. Driver should use the minimum of these two.
1914 * 1 << stripe_sz_ops.min = max sectors per strip
1916 * Note that older firmwares ( < FW ver 30) didn't report information
1917 * to calculate max_sectors_1. So the number ended up as zero always.
1919 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1921 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1922 ctrl_info->max_strips_per_io;
1923 max_sectors_2 = ctrl_info->max_request_size;
1925 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1926 ? max_sectors_1 : max_sectors_2;
1928 instance->max_sectors_per_req = instance->max_num_sge *
1934 * Setup tasklet for cmd completion
1937 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
1938 (unsigned long)instance);
1942 megasas_return_cmd(instance, cmd);
1944 pci_free_consistent(instance->pdev, reply_q_sz,
1945 instance->reply_queue, instance->reply_queue_h);
1947 megasas_free_cmds(instance);
1951 iounmap(instance->reg_set);
1954 pci_release_regions(instance->pdev);
1960 * megasas_release_mfi - Reverses the FW initialization
1961 * @intance: Adapter soft state
1963 static void megasas_release_mfi(struct megasas_instance *instance)
1965 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1967 pci_free_consistent(instance->pdev, reply_q_sz,
1968 instance->reply_queue, instance->reply_queue_h);
1970 megasas_free_cmds(instance);
1972 iounmap(instance->reg_set);
1974 pci_release_regions(instance->pdev);
1978 * megasas_get_seq_num - Gets latest event sequence numbers
1979 * @instance: Adapter soft state
1980 * @eli: FW event log sequence numbers information
1982 * FW maintains a log of all events in a non-volatile area. Upper layers would
1983 * usually find out the latest sequence number of the events, the seq number at
1984 * the boot etc. They would "read" all the events below the latest seq number
1985 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1986 * number), they would subsribe to AEN (asynchronous event notification) and
1987 * wait for the events to happen.
1990 megasas_get_seq_num(struct megasas_instance *instance,
1991 struct megasas_evt_log_info *eli)
1993 struct megasas_cmd *cmd;
1994 struct megasas_dcmd_frame *dcmd;
1995 struct megasas_evt_log_info *el_info;
1996 dma_addr_t el_info_h = 0;
1998 cmd = megasas_get_cmd(instance);
2004 dcmd = &cmd->frame->dcmd;
2005 el_info = pci_alloc_consistent(instance->pdev,
2006 sizeof(struct megasas_evt_log_info),
2010 megasas_return_cmd(instance, cmd);
2014 memset(el_info, 0, sizeof(*el_info));
2015 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2017 dcmd->cmd = MFI_CMD_DCMD;
2018 dcmd->cmd_status = 0x0;
2019 dcmd->sge_count = 1;
2020 dcmd->flags = MFI_FRAME_DIR_READ;
2022 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2023 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2024 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2025 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2027 megasas_issue_blocked_cmd(instance, cmd);
2030 * Copy the data back into callers buffer
2032 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2034 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2035 el_info, el_info_h);
2037 megasas_return_cmd(instance, cmd);
2043 * megasas_register_aen - Registers for asynchronous event notification
2044 * @instance: Adapter soft state
2045 * @seq_num: The starting sequence number
2046 * @class_locale: Class of the event
2048 * This function subscribes for AEN for events beyond the @seq_num. It requests
2049 * to be notified if and only if the event is of type @class_locale
2052 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2053 u32 class_locale_word)
2056 struct megasas_cmd *cmd;
2057 struct megasas_dcmd_frame *dcmd;
2058 union megasas_evt_class_locale curr_aen;
2059 union megasas_evt_class_locale prev_aen;
2062 * If there an AEN pending already (aen_cmd), check if the
2063 * class_locale of that pending AEN is inclusive of the new
2064 * AEN request we currently have. If it is, then we don't have
2065 * to do anything. In other words, whichever events the current
2066 * AEN request is subscribing to, have already been subscribed
2069 * If the old_cmd is _not_ inclusive, then we have to abort
2070 * that command, form a class_locale that is superset of both
2071 * old and current and re-issue to the FW
2074 curr_aen.word = class_locale_word;
2076 if (instance->aen_cmd) {
2078 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2081 * A class whose enum value is smaller is inclusive of all
2082 * higher values. If a PROGRESS (= -1) was previously
2083 * registered, then a new registration requests for higher
2084 * classes need not be sent to FW. They are automatically
2087 * Locale numbers don't have such hierarchy. They are bitmap
2090 if ((prev_aen.members.class <= curr_aen.members.class) &&
2091 !((prev_aen.members.locale & curr_aen.members.locale) ^
2092 curr_aen.members.locale)) {
2094 * Previously issued event registration includes
2095 * current request. Nothing to do.
2099 curr_aen.members.locale |= prev_aen.members.locale;
2101 if (prev_aen.members.class < curr_aen.members.class)
2102 curr_aen.members.class = prev_aen.members.class;
2104 instance->aen_cmd->abort_aen = 1;
2105 ret_val = megasas_issue_blocked_abort_cmd(instance,
2110 printk(KERN_DEBUG "megasas: Failed to abort "
2111 "previous AEN command\n");
2117 cmd = megasas_get_cmd(instance);
2122 dcmd = &cmd->frame->dcmd;
2124 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2127 * Prepare DCMD for aen registration
2129 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2131 dcmd->cmd = MFI_CMD_DCMD;
2132 dcmd->cmd_status = 0x0;
2133 dcmd->sge_count = 1;
2134 dcmd->flags = MFI_FRAME_DIR_READ;
2136 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2137 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2138 dcmd->mbox.w[0] = seq_num;
2139 dcmd->mbox.w[1] = curr_aen.word;
2140 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2141 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2144 * Store reference to the cmd used to register for AEN. When an
2145 * application wants us to register for AEN, we have to abort this
2146 * cmd and re-register with a new EVENT LOCALE supplied by that app
2148 instance->aen_cmd = cmd;
2151 * Issue the aen registration frame
2153 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2159 * megasas_start_aen - Subscribes to AEN during driver load time
2160 * @instance: Adapter soft state
2162 static int megasas_start_aen(struct megasas_instance *instance)
2164 struct megasas_evt_log_info eli;
2165 union megasas_evt_class_locale class_locale;
2168 * Get the latest sequence number from FW
2170 memset(&eli, 0, sizeof(eli));
2172 if (megasas_get_seq_num(instance, &eli))
2176 * Register AEN with FW for latest sequence number plus 1
2178 class_locale.members.reserved = 0;
2179 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2180 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2182 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2187 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2188 * @instance: Adapter soft state
2190 static int megasas_io_attach(struct megasas_instance *instance)
2192 struct Scsi_Host *host = instance->host;
2195 * Export parameters required by SCSI mid-layer
2197 host->irq = instance->pdev->irq;
2198 host->unique_id = instance->unique_id;
2199 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2200 host->this_id = instance->init_id;
2201 host->sg_tablesize = instance->max_num_sge;
2202 host->max_sectors = instance->max_sectors_per_req;
2203 host->cmd_per_lun = 128;
2204 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2205 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2206 host->max_lun = MEGASAS_MAX_LUN;
2207 host->max_cmd_len = 16;
2210 * Notify the mid-layer about the new controller
2212 if (scsi_add_host(host, &instance->pdev->dev)) {
2213 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2218 * Trigger SCSI to scan our drives
2220 scsi_scan_host(host);
2225 * megasas_probe_one - PCI hotplug entry point
2226 * @pdev: PCI device structure
2227 * @id: PCI ids of supported hotplugged adapter
2229 static int __devinit
2230 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2233 struct Scsi_Host *host;
2234 struct megasas_instance *instance;
2237 * Announce PCI information
2239 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2240 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2241 pdev->subsystem_device);
2243 printk("bus %d:slot %d:func %d\n",
2244 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2247 * PCI prepping: enable device set bus mastering and dma mask
2249 rval = pci_enable_device(pdev);
2255 pci_set_master(pdev);
2258 * All our contollers are capable of performing 64-bit DMA
2261 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2263 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2264 goto fail_set_dma_mask;
2267 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2268 goto fail_set_dma_mask;
2271 host = scsi_host_alloc(&megasas_template,
2272 sizeof(struct megasas_instance));
2275 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2276 goto fail_alloc_instance;
2279 instance = (struct megasas_instance *)host->hostdata;
2280 memset(instance, 0, sizeof(*instance));
2282 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2283 &instance->producer_h);
2284 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2285 &instance->consumer_h);
2287 if (!instance->producer || !instance->consumer) {
2288 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2289 "producer, consumer\n");
2290 goto fail_alloc_dma_buf;
2293 *instance->producer = 0;
2294 *instance->consumer = 0;
2296 instance->evt_detail = pci_alloc_consistent(pdev,
2298 megasas_evt_detail),
2299 &instance->evt_detail_h);
2301 if (!instance->evt_detail) {
2302 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2303 "event detail structure\n");
2304 goto fail_alloc_dma_buf;
2308 * Initialize locks and queues
2310 INIT_LIST_HEAD(&instance->cmd_pool);
2312 atomic_set(&instance->fw_outstanding,0);
2314 init_waitqueue_head(&instance->int_cmd_wait_q);
2315 init_waitqueue_head(&instance->abort_cmd_wait_q);
2317 spin_lock_init(&instance->cmd_pool_lock);
2319 sema_init(&instance->aen_mutex, 1);
2320 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2323 * Initialize PCI related and misc parameters
2325 instance->pdev = pdev;
2326 instance->host = host;
2327 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2328 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2330 megasas_dbg_lvl = 0;
2333 * Initialize MFI Firmware
2335 if (megasas_init_mfi(instance))
2341 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2342 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2346 instance->instancet->enable_intr(instance->reg_set);
2349 * Store instance in PCI softstate
2351 pci_set_drvdata(pdev, instance);
2354 * Add this controller to megasas_mgmt_info structure so that it
2355 * can be exported to management applications
2357 megasas_mgmt_info.count++;
2358 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2359 megasas_mgmt_info.max_index++;
2362 * Initiate AEN (Asynchronous Event Notification)
2364 if (megasas_start_aen(instance)) {
2365 printk(KERN_DEBUG "megasas: start aen failed\n");
2366 goto fail_start_aen;
2370 * Register with SCSI mid-layer
2372 if (megasas_io_attach(instance))
2373 goto fail_io_attach;
2379 megasas_mgmt_info.count--;
2380 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2381 megasas_mgmt_info.max_index--;
2383 pci_set_drvdata(pdev, NULL);
2384 instance->instancet->disable_intr(instance->reg_set);
2385 free_irq(instance->pdev->irq, instance);
2387 megasas_release_mfi(instance);
2392 if (instance->evt_detail)
2393 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2394 instance->evt_detail,
2395 instance->evt_detail_h);
2397 if (instance->producer)
2398 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2399 instance->producer_h);
2400 if (instance->consumer)
2401 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2402 instance->consumer_h);
2403 scsi_host_put(host);
2405 fail_alloc_instance:
2407 pci_disable_device(pdev);
2413 * megasas_flush_cache - Requests FW to flush all its caches
2414 * @instance: Adapter soft state
2416 static void megasas_flush_cache(struct megasas_instance *instance)
2418 struct megasas_cmd *cmd;
2419 struct megasas_dcmd_frame *dcmd;
2421 cmd = megasas_get_cmd(instance);
2426 dcmd = &cmd->frame->dcmd;
2428 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2430 dcmd->cmd = MFI_CMD_DCMD;
2431 dcmd->cmd_status = 0x0;
2432 dcmd->sge_count = 0;
2433 dcmd->flags = MFI_FRAME_DIR_NONE;
2435 dcmd->data_xfer_len = 0;
2436 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2437 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2439 megasas_issue_blocked_cmd(instance, cmd);
2441 megasas_return_cmd(instance, cmd);
2447 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2448 * @instance: Adapter soft state
2450 static void megasas_shutdown_controller(struct megasas_instance *instance)
2452 struct megasas_cmd *cmd;
2453 struct megasas_dcmd_frame *dcmd;
2455 cmd = megasas_get_cmd(instance);
2460 if (instance->aen_cmd)
2461 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2463 dcmd = &cmd->frame->dcmd;
2465 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2467 dcmd->cmd = MFI_CMD_DCMD;
2468 dcmd->cmd_status = 0x0;
2469 dcmd->sge_count = 0;
2470 dcmd->flags = MFI_FRAME_DIR_NONE;
2472 dcmd->data_xfer_len = 0;
2473 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2475 megasas_issue_blocked_cmd(instance, cmd);
2477 megasas_return_cmd(instance, cmd);
2483 * megasas_detach_one - PCI hot"un"plug entry point
2484 * @pdev: PCI device structure
2486 static void megasas_detach_one(struct pci_dev *pdev)
2489 struct Scsi_Host *host;
2490 struct megasas_instance *instance;
2492 instance = pci_get_drvdata(pdev);
2493 host = instance->host;
2495 scsi_remove_host(instance->host);
2496 megasas_flush_cache(instance);
2497 megasas_shutdown_controller(instance);
2498 tasklet_kill(&instance->isr_tasklet);
2501 * Take the instance off the instance array. Note that we will not
2502 * decrement the max_index. We let this array be sparse array
2504 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2505 if (megasas_mgmt_info.instance[i] == instance) {
2506 megasas_mgmt_info.count--;
2507 megasas_mgmt_info.instance[i] = NULL;
2513 pci_set_drvdata(instance->pdev, NULL);
2515 instance->instancet->disable_intr(instance->reg_set);
2517 free_irq(instance->pdev->irq, instance);
2519 megasas_release_mfi(instance);
2521 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2522 instance->evt_detail, instance->evt_detail_h);
2524 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2525 instance->producer_h);
2527 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2528 instance->consumer_h);
2530 scsi_host_put(host);
2532 pci_set_drvdata(pdev, NULL);
2534 pci_disable_device(pdev);
2540 * megasas_shutdown - Shutdown entry point
2541 * @device: Generic device structure
2543 static void megasas_shutdown(struct pci_dev *pdev)
2545 struct megasas_instance *instance = pci_get_drvdata(pdev);
2546 megasas_flush_cache(instance);
2550 * megasas_mgmt_open - char node "open" entry point
2552 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2555 * Allow only those users with admin rights
2557 if (!capable(CAP_SYS_ADMIN))
2564 * megasas_mgmt_release - char node "release" entry point
2566 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2568 filep->private_data = NULL;
2569 fasync_helper(-1, filep, 0, &megasas_async_queue);
2575 * megasas_mgmt_fasync - Async notifier registration from applications
2577 * This function adds the calling process to a driver global queue. When an
2578 * event occurs, SIGIO will be sent to all processes in this queue.
2580 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2584 mutex_lock(&megasas_async_queue_mutex);
2586 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2588 mutex_unlock(&megasas_async_queue_mutex);
2591 /* For sanity check when we get ioctl */
2592 filep->private_data = filep;
2596 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2602 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2603 * @instance: Adapter soft state
2604 * @argp: User's ioctl packet
2607 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2608 struct megasas_iocpacket __user * user_ioc,
2609 struct megasas_iocpacket *ioc)
2611 struct megasas_sge32 *kern_sge32;
2612 struct megasas_cmd *cmd;
2613 void *kbuff_arr[MAX_IOCTL_SGE];
2614 dma_addr_t buf_handle = 0;
2617 dma_addr_t sense_handle;
2620 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2622 if (ioc->sge_count > MAX_IOCTL_SGE) {
2623 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2624 ioc->sge_count, MAX_IOCTL_SGE);
2628 cmd = megasas_get_cmd(instance);
2630 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2635 * User's IOCTL packet has 2 frames (maximum). Copy those two
2636 * frames into our cmd's frames. cmd->frame's context will get
2637 * overwritten when we copy from user's frames. So set that value
2640 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2641 cmd->frame->hdr.context = cmd->index;
2644 * The management interface between applications and the fw uses
2645 * MFI frames. E.g, RAID configuration changes, LD property changes
2646 * etc are accomplishes through different kinds of MFI frames. The
2647 * driver needs to care only about substituting user buffers with
2648 * kernel buffers in SGLs. The location of SGL is embedded in the
2649 * struct iocpacket itself.
2651 kern_sge32 = (struct megasas_sge32 *)
2652 ((unsigned long)cmd->frame + ioc->sgl_off);
2655 * For each user buffer, create a mirror buffer and copy in
2657 for (i = 0; i < ioc->sge_count; i++) {
2658 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2659 ioc->sgl[i].iov_len,
2661 if (!kbuff_arr[i]) {
2662 printk(KERN_DEBUG "megasas: Failed to alloc "
2663 "kernel SGL buffer for IOCTL \n");
2669 * We don't change the dma_coherent_mask, so
2670 * pci_alloc_consistent only returns 32bit addresses
2672 kern_sge32[i].phys_addr = (u32) buf_handle;
2673 kern_sge32[i].length = ioc->sgl[i].iov_len;
2676 * We created a kernel buffer corresponding to the
2677 * user buffer. Now copy in from the user buffer
2679 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2680 (u32) (ioc->sgl[i].iov_len))) {
2686 if (ioc->sense_len) {
2687 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2695 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2696 *sense_ptr = sense_handle;
2700 * Set the sync_cmd flag so that the ISR knows not to complete this
2701 * cmd to the SCSI mid-layer
2704 megasas_issue_blocked_cmd(instance, cmd);
2708 * copy out the kernel buffers to user buffers
2710 for (i = 0; i < ioc->sge_count; i++) {
2711 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2712 ioc->sgl[i].iov_len)) {
2719 * copy out the sense
2721 if (ioc->sense_len) {
2723 * sense_ptr points to the location that has the user
2724 * sense buffer address
2726 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2729 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2730 sense, ioc->sense_len)) {
2737 * copy the status codes returned by the fw
2739 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2740 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2741 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2747 pci_free_consistent(instance->pdev, ioc->sense_len,
2748 sense, sense_handle);
2751 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2752 pci_free_consistent(instance->pdev,
2753 kern_sge32[i].length,
2754 kbuff_arr[i], kern_sge32[i].phys_addr);
2757 megasas_return_cmd(instance, cmd);
2761 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2765 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2767 if ((megasas_mgmt_info.instance[i]) &&
2768 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2769 return megasas_mgmt_info.instance[i];
2775 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2777 struct megasas_iocpacket __user *user_ioc =
2778 (struct megasas_iocpacket __user *)arg;
2779 struct megasas_iocpacket *ioc;
2780 struct megasas_instance *instance;
2783 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2787 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2792 instance = megasas_lookup_instance(ioc->host_no);
2799 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2801 if (down_interruptible(&instance->ioctl_sem)) {
2802 error = -ERESTARTSYS;
2805 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2806 up(&instance->ioctl_sem);
2813 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2815 struct megasas_instance *instance;
2816 struct megasas_aen aen;
2819 if (file->private_data != file) {
2820 printk(KERN_DEBUG "megasas: fasync_helper was not "
2825 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2828 instance = megasas_lookup_instance(aen.host_no);
2833 down(&instance->aen_mutex);
2834 error = megasas_register_aen(instance, aen.seq_num,
2835 aen.class_locale_word);
2836 up(&instance->aen_mutex);
2841 * megasas_mgmt_ioctl - char node ioctl entry point
2844 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2847 case MEGASAS_IOC_FIRMWARE:
2848 return megasas_mgmt_ioctl_fw(file, arg);
2850 case MEGASAS_IOC_GET_AEN:
2851 return megasas_mgmt_ioctl_aen(file, arg);
2857 #ifdef CONFIG_COMPAT
2858 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2860 struct compat_megasas_iocpacket __user *cioc =
2861 (struct compat_megasas_iocpacket __user *)arg;
2862 struct megasas_iocpacket __user *ioc =
2863 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2867 if (clear_user(ioc, sizeof(*ioc)))
2870 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2871 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2872 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2873 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2874 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2875 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2878 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2881 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2882 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2883 copy_in_user(&ioc->sgl[i].iov_len,
2884 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2888 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2890 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2891 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2892 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2899 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2903 case MEGASAS_IOC_FIRMWARE32:
2904 return megasas_mgmt_compat_ioctl_fw(file, arg);
2905 case MEGASAS_IOC_GET_AEN:
2906 return megasas_mgmt_ioctl_aen(file, arg);
2914 * File operations structure for management interface
2916 static struct file_operations megasas_mgmt_fops = {
2917 .owner = THIS_MODULE,
2918 .open = megasas_mgmt_open,
2919 .release = megasas_mgmt_release,
2920 .fasync = megasas_mgmt_fasync,
2921 .unlocked_ioctl = megasas_mgmt_ioctl,
2922 #ifdef CONFIG_COMPAT
2923 .compat_ioctl = megasas_mgmt_compat_ioctl,
2928 * PCI hotplug support registration structure
2930 static struct pci_driver megasas_pci_driver = {
2932 .name = "megaraid_sas",
2933 .id_table = megasas_pci_table,
2934 .probe = megasas_probe_one,
2935 .remove = __devexit_p(megasas_detach_one),
2936 .shutdown = megasas_shutdown,
2940 * Sysfs driver attributes
2942 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2944 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2948 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2951 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2953 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2957 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2961 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
2963 return sprintf(buf,"%u",megasas_dbg_lvl);
2967 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
2970 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
2971 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
2977 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
2978 megasas_sysfs_set_dbg_lvl);
2981 * megasas_init - Driver load entry point
2983 static int __init megasas_init(void)
2988 * Announce driver version and other information
2990 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2991 MEGASAS_EXT_VERSION);
2993 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2996 * Register character device node
2998 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3001 printk(KERN_DEBUG "megasas: failed to open device node\n");
3005 megasas_mgmt_majorno = rval;
3008 * Register ourselves as PCI hotplug module
3010 rval = pci_register_driver(&megasas_pci_driver);
3013 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3017 rval = driver_create_file(&megasas_pci_driver.driver,
3018 &driver_attr_version);
3020 goto err_dcf_attr_ver;
3021 rval = driver_create_file(&megasas_pci_driver.driver,
3022 &driver_attr_release_date);
3024 goto err_dcf_rel_date;
3025 rval = driver_create_file(&megasas_pci_driver.driver,
3026 &driver_attr_dbg_lvl);
3028 goto err_dcf_dbg_lvl;
3032 driver_remove_file(&megasas_pci_driver.driver,
3033 &driver_attr_release_date);
3035 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3037 pci_unregister_driver(&megasas_pci_driver);
3039 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3044 * megasas_exit - Driver unload entry point
3046 static void __exit megasas_exit(void)
3048 driver_remove_file(&megasas_pci_driver.driver,
3049 &driver_attr_dbg_lvl);
3050 driver_remove_file(&megasas_pci_driver.driver,
3051 &driver_attr_release_date);
3052 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3054 pci_unregister_driver(&megasas_pci_driver);
3055 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3058 module_init(megasas_init);
3059 module_exit(megasas_exit);