[POWERPC] 4xx: Reorganize 4xx defconfigs
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_sas.c
blob77a62a1b12c3e52cf76df0c3f65f58e607d293b8
1 /*
3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2005 LSI 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.16-rc1
15 * Authors:
16 * (email-id : megaraidlinux@lsi.com)
17 * Sreenivas Bagalkote
18 * Sumant Patro
19 * Bo Yang
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/list.h>
31 #include <linux/moduleparam.h>
32 #include <linux/module.h>
33 #include <linux/spinlock.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/uio.h>
37 #include <asm/uaccess.h>
38 #include <linux/fs.h>
39 #include <linux/compat.h>
40 #include <linux/blkdev.h>
41 #include <linux/mutex.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_device.h>
46 #include <scsi/scsi_host.h>
47 #include "megaraid_sas.h"
50 * poll_mode_io:1- schedule complete completion from q cmd
52 static unsigned int poll_mode_io;
53 module_param_named(poll_mode_io, poll_mode_io, int, 0);
54 MODULE_PARM_DESC(poll_mode_io,
55 "Complete cmds from IO path, (default=0)");
57 MODULE_LICENSE("GPL");
58 MODULE_VERSION(MEGASAS_VERSION);
59 MODULE_AUTHOR("megaraidlinux@lsi.com");
60 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
63 * PCI ID table for all supported controllers
65 static struct pci_device_id megasas_pci_table[] = {
67 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
68 /* xscale IOP */
69 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
70 /* ppc IOP */
71 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
72 /* xscale IOP, vega */
73 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
74 /* xscale IOP */
78 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
80 static int megasas_mgmt_majorno;
81 static struct megasas_mgmt_info megasas_mgmt_info;
82 static struct fasync_struct *megasas_async_queue;
83 static DEFINE_MUTEX(megasas_async_queue_mutex);
85 static u32 megasas_dbg_lvl;
87 static void
88 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
89 u8 alt_status);
91 /**
92 * megasas_get_cmd - Get a command from the free pool
93 * @instance: Adapter soft state
95 * Returns a free command from the pool
97 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
98 *instance)
100 unsigned long flags;
101 struct megasas_cmd *cmd = NULL;
103 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
105 if (!list_empty(&instance->cmd_pool)) {
106 cmd = list_entry((&instance->cmd_pool)->next,
107 struct megasas_cmd, list);
108 list_del_init(&cmd->list);
109 } else {
110 printk(KERN_ERR "megasas: Command pool empty!\n");
113 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
114 return cmd;
118 * megasas_return_cmd - Return a cmd to free command pool
119 * @instance: Adapter soft state
120 * @cmd: Command packet to be returned to free command pool
122 static inline void
123 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
125 unsigned long flags;
127 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
129 cmd->scmd = NULL;
130 list_add_tail(&cmd->list, &instance->cmd_pool);
132 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
137 * The following functions are defined for xscale
138 * (deviceid : 1064R, PERC5) controllers
142 * megasas_enable_intr_xscale - Enables interrupts
143 * @regs: MFI register set
145 static inline void
146 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
148 writel(1, &(regs)->outbound_intr_mask);
150 /* Dummy readl to force pci flush */
151 readl(&regs->outbound_intr_mask);
155 * megasas_disable_intr_xscale -Disables interrupt
156 * @regs: MFI register set
158 static inline void
159 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
161 u32 mask = 0x1f;
162 writel(mask, &regs->outbound_intr_mask);
163 /* Dummy readl to force pci flush */
164 readl(&regs->outbound_intr_mask);
168 * megasas_read_fw_status_reg_xscale - returns the current FW status value
169 * @regs: MFI register set
171 static u32
172 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
174 return readl(&(regs)->outbound_msg_0);
177 * megasas_clear_interrupt_xscale - Check & clear interrupt
178 * @regs: MFI register set
180 static int
181 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
183 u32 status;
185 * Check if it is our interrupt
187 status = readl(&regs->outbound_intr_status);
189 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
190 return 1;
194 * Clear the interrupt by writing back the same value
196 writel(status, &regs->outbound_intr_status);
198 return 0;
202 * megasas_fire_cmd_xscale - Sends command to the FW
203 * @frame_phys_addr : Physical address of cmd
204 * @frame_count : Number of frames for the command
205 * @regs : MFI register set
207 static inline void
208 megasas_fire_cmd_xscale(dma_addr_t frame_phys_addr,u32 frame_count, struct megasas_register_set __iomem *regs)
210 writel((frame_phys_addr >> 3)|(frame_count),
211 &(regs)->inbound_queue_port);
214 static struct megasas_instance_template megasas_instance_template_xscale = {
216 .fire_cmd = megasas_fire_cmd_xscale,
217 .enable_intr = megasas_enable_intr_xscale,
218 .disable_intr = megasas_disable_intr_xscale,
219 .clear_intr = megasas_clear_intr_xscale,
220 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
224 * This is the end of set of functions & definitions specific
225 * to xscale (deviceid : 1064R, PERC5) controllers
229 * The following functions are defined for ppc (deviceid : 0x60)
230 * controllers
234 * megasas_enable_intr_ppc - Enables interrupts
235 * @regs: MFI register set
237 static inline void
238 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
240 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
242 writel(~0x80000004, &(regs)->outbound_intr_mask);
244 /* Dummy readl to force pci flush */
245 readl(&regs->outbound_intr_mask);
249 * megasas_disable_intr_ppc - Disable interrupt
250 * @regs: MFI register set
252 static inline void
253 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
255 u32 mask = 0xFFFFFFFF;
256 writel(mask, &regs->outbound_intr_mask);
257 /* Dummy readl to force pci flush */
258 readl(&regs->outbound_intr_mask);
262 * megasas_read_fw_status_reg_ppc - returns the current FW status value
263 * @regs: MFI register set
265 static u32
266 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
268 return readl(&(regs)->outbound_scratch_pad);
272 * megasas_clear_interrupt_ppc - Check & clear interrupt
273 * @regs: MFI register set
275 static int
276 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
278 u32 status;
280 * Check if it is our interrupt
282 status = readl(&regs->outbound_intr_status);
284 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
285 return 1;
289 * Clear the interrupt by writing back the same value
291 writel(status, &regs->outbound_doorbell_clear);
293 return 0;
296 * megasas_fire_cmd_ppc - Sends command to the FW
297 * @frame_phys_addr : Physical address of cmd
298 * @frame_count : Number of frames for the command
299 * @regs : MFI register set
301 static inline void
302 megasas_fire_cmd_ppc(dma_addr_t frame_phys_addr, u32 frame_count, struct megasas_register_set __iomem *regs)
304 writel((frame_phys_addr | (frame_count<<1))|1,
305 &(regs)->inbound_queue_port);
308 static struct megasas_instance_template megasas_instance_template_ppc = {
310 .fire_cmd = megasas_fire_cmd_ppc,
311 .enable_intr = megasas_enable_intr_ppc,
312 .disable_intr = megasas_disable_intr_ppc,
313 .clear_intr = megasas_clear_intr_ppc,
314 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
318 * This is the end of set of functions & definitions
319 * specific to ppc (deviceid : 0x60) controllers
323 * megasas_issue_polled - Issues a polling command
324 * @instance: Adapter soft state
325 * @cmd: Command packet to be issued
327 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
329 static int
330 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
332 int i;
333 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
335 struct megasas_header *frame_hdr = &cmd->frame->hdr;
337 frame_hdr->cmd_status = 0xFF;
338 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
341 * Issue the frame using inbound queue port
343 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
346 * Wait for cmd_status to change
348 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
349 rmb();
350 msleep(1);
353 if (frame_hdr->cmd_status == 0xff)
354 return -ETIME;
356 return 0;
360 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
361 * @instance: Adapter soft state
362 * @cmd: Command to be issued
364 * This function waits on an event for the command to be returned from ISR.
365 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
366 * Used to issue ioctl commands.
368 static int
369 megasas_issue_blocked_cmd(struct megasas_instance *instance,
370 struct megasas_cmd *cmd)
372 cmd->cmd_status = ENODATA;
374 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
376 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
377 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
379 return 0;
383 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
384 * @instance: Adapter soft state
385 * @cmd_to_abort: Previously issued cmd to be aborted
387 * MFI firmware can abort previously issued AEN comamnd (automatic event
388 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
389 * cmd and waits for return status.
390 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
392 static int
393 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
394 struct megasas_cmd *cmd_to_abort)
396 struct megasas_cmd *cmd;
397 struct megasas_abort_frame *abort_fr;
399 cmd = megasas_get_cmd(instance);
401 if (!cmd)
402 return -1;
404 abort_fr = &cmd->frame->abort;
407 * Prepare and issue the abort frame
409 abort_fr->cmd = MFI_CMD_ABORT;
410 abort_fr->cmd_status = 0xFF;
411 abort_fr->flags = 0;
412 abort_fr->abort_context = cmd_to_abort->index;
413 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
414 abort_fr->abort_mfi_phys_addr_hi = 0;
416 cmd->sync_cmd = 1;
417 cmd->cmd_status = 0xFF;
419 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
422 * Wait for this cmd to complete
424 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
425 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
427 megasas_return_cmd(instance, cmd);
428 return 0;
432 * megasas_make_sgl32 - Prepares 32-bit SGL
433 * @instance: Adapter soft state
434 * @scp: SCSI command from the mid-layer
435 * @mfi_sgl: SGL to be filled in
437 * If successful, this function returns the number of SG elements. Otherwise,
438 * it returnes -1.
440 static int
441 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
442 union megasas_sgl *mfi_sgl)
444 int i;
445 int sge_count;
446 struct scatterlist *os_sgl;
448 sge_count = scsi_dma_map(scp);
449 BUG_ON(sge_count < 0);
451 if (sge_count) {
452 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
453 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
454 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
457 return sge_count;
461 * megasas_make_sgl64 - Prepares 64-bit SGL
462 * @instance: Adapter soft state
463 * @scp: SCSI command from the mid-layer
464 * @mfi_sgl: SGL to be filled in
466 * If successful, this function returns the number of SG elements. Otherwise,
467 * it returnes -1.
469 static int
470 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
471 union megasas_sgl *mfi_sgl)
473 int i;
474 int sge_count;
475 struct scatterlist *os_sgl;
477 sge_count = scsi_dma_map(scp);
478 BUG_ON(sge_count < 0);
480 if (sge_count) {
481 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
482 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
483 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
486 return sge_count;
490 * megasas_get_frame_count - Computes the number of frames
491 * @sge_count : number of sg elements
493 * Returns the number of frames required for numnber of sge's (sge_count)
496 static u32 megasas_get_frame_count(u8 sge_count)
498 int num_cnt;
499 int sge_bytes;
500 u32 sge_sz;
501 u32 frame_count=0;
503 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
504 sizeof(struct megasas_sge32);
507 * Main frame can contain 2 SGEs for 64-bit SGLs and
508 * 3 SGEs for 32-bit SGLs
510 if (IS_DMA64)
511 num_cnt = sge_count - 2;
512 else
513 num_cnt = sge_count - 3;
515 if(num_cnt>0){
516 sge_bytes = sge_sz * num_cnt;
518 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
519 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
521 /* Main frame */
522 frame_count +=1;
524 if (frame_count > 7)
525 frame_count = 8;
526 return frame_count;
530 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
531 * @instance: Adapter soft state
532 * @scp: SCSI command
533 * @cmd: Command to be prepared in
535 * This function prepares CDB commands. These are typcially pass-through
536 * commands to the devices.
538 static int
539 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
540 struct megasas_cmd *cmd)
542 u32 is_logical;
543 u32 device_id;
544 u16 flags = 0;
545 struct megasas_pthru_frame *pthru;
547 is_logical = MEGASAS_IS_LOGICAL(scp);
548 device_id = MEGASAS_DEV_INDEX(instance, scp);
549 pthru = (struct megasas_pthru_frame *)cmd->frame;
551 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
552 flags = MFI_FRAME_DIR_WRITE;
553 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
554 flags = MFI_FRAME_DIR_READ;
555 else if (scp->sc_data_direction == PCI_DMA_NONE)
556 flags = MFI_FRAME_DIR_NONE;
559 * Prepare the DCDB frame
561 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
562 pthru->cmd_status = 0x0;
563 pthru->scsi_status = 0x0;
564 pthru->target_id = device_id;
565 pthru->lun = scp->device->lun;
566 pthru->cdb_len = scp->cmd_len;
567 pthru->timeout = 0;
568 pthru->flags = flags;
569 pthru->data_xfer_len = scsi_bufflen(scp);
571 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
574 * Construct SGL
576 if (IS_DMA64) {
577 pthru->flags |= MFI_FRAME_SGL64;
578 pthru->sge_count = megasas_make_sgl64(instance, scp,
579 &pthru->sgl);
580 } else
581 pthru->sge_count = megasas_make_sgl32(instance, scp,
582 &pthru->sgl);
585 * Sense info specific
587 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
588 pthru->sense_buf_phys_addr_hi = 0;
589 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
592 * Compute the total number of frames this command consumes. FW uses
593 * this number to pull sufficient number of frames from host memory.
595 cmd->frame_count = megasas_get_frame_count(pthru->sge_count);
597 return cmd->frame_count;
601 * megasas_build_ldio - Prepares IOs to logical devices
602 * @instance: Adapter soft state
603 * @scp: SCSI command
604 * @cmd: Command to to be prepared
606 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
608 static int
609 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
610 struct megasas_cmd *cmd)
612 u32 device_id;
613 u8 sc = scp->cmnd[0];
614 u16 flags = 0;
615 struct megasas_io_frame *ldio;
617 device_id = MEGASAS_DEV_INDEX(instance, scp);
618 ldio = (struct megasas_io_frame *)cmd->frame;
620 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
621 flags = MFI_FRAME_DIR_WRITE;
622 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
623 flags = MFI_FRAME_DIR_READ;
626 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
628 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
629 ldio->cmd_status = 0x0;
630 ldio->scsi_status = 0x0;
631 ldio->target_id = device_id;
632 ldio->timeout = 0;
633 ldio->reserved_0 = 0;
634 ldio->pad_0 = 0;
635 ldio->flags = flags;
636 ldio->start_lba_hi = 0;
637 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
640 * 6-byte READ(0x08) or WRITE(0x0A) cdb
642 if (scp->cmd_len == 6) {
643 ldio->lba_count = (u32) scp->cmnd[4];
644 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
645 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
647 ldio->start_lba_lo &= 0x1FFFFF;
651 * 10-byte READ(0x28) or WRITE(0x2A) cdb
653 else if (scp->cmd_len == 10) {
654 ldio->lba_count = (u32) scp->cmnd[8] |
655 ((u32) scp->cmnd[7] << 8);
656 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
657 ((u32) scp->cmnd[3] << 16) |
658 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
662 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
664 else if (scp->cmd_len == 12) {
665 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
666 ((u32) scp->cmnd[7] << 16) |
667 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
669 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
670 ((u32) scp->cmnd[3] << 16) |
671 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
675 * 16-byte READ(0x88) or WRITE(0x8A) cdb
677 else if (scp->cmd_len == 16) {
678 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
679 ((u32) scp->cmnd[11] << 16) |
680 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
682 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
683 ((u32) scp->cmnd[7] << 16) |
684 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
686 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
687 ((u32) scp->cmnd[3] << 16) |
688 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
693 * Construct SGL
695 if (IS_DMA64) {
696 ldio->flags |= MFI_FRAME_SGL64;
697 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
698 } else
699 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
702 * Sense info specific
704 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
705 ldio->sense_buf_phys_addr_hi = 0;
706 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
709 * Compute the total number of frames this command consumes. FW uses
710 * this number to pull sufficient number of frames from host memory.
712 cmd->frame_count = megasas_get_frame_count(ldio->sge_count);
714 return cmd->frame_count;
718 * megasas_is_ldio - Checks if the cmd is for logical drive
719 * @scmd: SCSI command
721 * Called by megasas_queue_command to find out if the command to be queued
722 * is a logical drive command
724 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
726 if (!MEGASAS_IS_LOGICAL(cmd))
727 return 0;
728 switch (cmd->cmnd[0]) {
729 case READ_10:
730 case WRITE_10:
731 case READ_12:
732 case WRITE_12:
733 case READ_6:
734 case WRITE_6:
735 case READ_16:
736 case WRITE_16:
737 return 1;
738 default:
739 return 0;
744 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
745 * in FW
746 * @instance: Adapter soft state
748 static inline void
749 megasas_dump_pending_frames(struct megasas_instance *instance)
751 struct megasas_cmd *cmd;
752 int i,n;
753 union megasas_sgl *mfi_sgl;
754 struct megasas_io_frame *ldio;
755 struct megasas_pthru_frame *pthru;
756 u32 sgcount;
757 u32 max_cmd = instance->max_fw_cmds;
759 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
760 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
761 if (IS_DMA64)
762 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
763 else
764 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
766 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
767 for (i = 0; i < max_cmd; i++) {
768 cmd = instance->cmd_list[i];
769 if(!cmd->scmd)
770 continue;
771 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
772 if (megasas_is_ldio(cmd->scmd)){
773 ldio = (struct megasas_io_frame *)cmd->frame;
774 mfi_sgl = &ldio->sgl;
775 sgcount = ldio->sge_count;
776 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);
778 else {
779 pthru = (struct megasas_pthru_frame *) cmd->frame;
780 mfi_sgl = &pthru->sgl;
781 sgcount = pthru->sge_count;
782 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);
784 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
785 for (n = 0; n < sgcount; n++){
786 if (IS_DMA64)
787 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) ;
788 else
789 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
792 printk(KERN_ERR "\n");
793 } /*for max_cmd*/
794 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
795 for (i = 0; i < max_cmd; i++) {
797 cmd = instance->cmd_list[i];
799 if(cmd->sync_cmd == 1){
800 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
803 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
807 * megasas_queue_command - Queue entry point
808 * @scmd: SCSI command to be queued
809 * @done: Callback entry point
811 static int
812 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
814 u32 frame_count;
815 struct megasas_cmd *cmd;
816 struct megasas_instance *instance;
818 instance = (struct megasas_instance *)
819 scmd->device->host->hostdata;
821 /* Don't process if we have already declared adapter dead */
822 if (instance->hw_crit_error)
823 return SCSI_MLQUEUE_HOST_BUSY;
825 scmd->scsi_done = done;
826 scmd->result = 0;
828 if (MEGASAS_IS_LOGICAL(scmd) &&
829 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
830 scmd->result = DID_BAD_TARGET << 16;
831 goto out_done;
834 switch (scmd->cmnd[0]) {
835 case SYNCHRONIZE_CACHE:
837 * FW takes care of flush cache on its own
838 * No need to send it down
840 scmd->result = DID_OK << 16;
841 goto out_done;
842 default:
843 break;
846 cmd = megasas_get_cmd(instance);
847 if (!cmd)
848 return SCSI_MLQUEUE_HOST_BUSY;
851 * Logical drive command
853 if (megasas_is_ldio(scmd))
854 frame_count = megasas_build_ldio(instance, scmd, cmd);
855 else
856 frame_count = megasas_build_dcdb(instance, scmd, cmd);
858 if (!frame_count)
859 goto out_return_cmd;
861 cmd->scmd = scmd;
862 scmd->SCp.ptr = (char *)cmd;
865 * Issue the command to the FW
867 atomic_inc(&instance->fw_outstanding);
869 instance->instancet->fire_cmd(cmd->frame_phys_addr ,cmd->frame_count-1,instance->reg_set);
871 * Check if we have pend cmds to be completed
873 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
874 tasklet_schedule(&instance->isr_tasklet);
877 return 0;
879 out_return_cmd:
880 megasas_return_cmd(instance, cmd);
881 out_done:
882 done(scmd);
883 return 0;
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)
896 return -ENXIO;
899 * The RAID firmware may require extended timeouts.
901 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS)
902 sdev->timeout = MEGASAS_DEFAULT_CMD_TIMEOUT * HZ;
903 return 0;
907 * megasas_complete_cmd_dpc - Returns FW's controller structure
908 * @instance_addr: Address of adapter soft state
910 * Tasklet to complete cmds
912 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
914 u32 producer;
915 u32 consumer;
916 u32 context;
917 struct megasas_cmd *cmd;
918 struct megasas_instance *instance =
919 (struct megasas_instance *)instance_addr;
920 unsigned long flags;
922 /* If we have already declared adapter dead, donot complete cmds */
923 if (instance->hw_crit_error)
924 return;
926 spin_lock_irqsave(&instance->completion_lock, flags);
928 producer = *instance->producer;
929 consumer = *instance->consumer;
931 while (consumer != producer) {
932 context = instance->reply_queue[consumer];
934 cmd = instance->cmd_list[context];
936 megasas_complete_cmd(instance, cmd, DID_OK);
938 consumer++;
939 if (consumer == (instance->max_fw_cmds + 1)) {
940 consumer = 0;
944 *instance->consumer = producer;
946 spin_unlock_irqrestore(&instance->completion_lock, flags);
949 * Check if we can restore can_queue
951 if (instance->flag & MEGASAS_FW_BUSY
952 && time_after(jiffies, instance->last_time + 5 * HZ)
953 && atomic_read(&instance->fw_outstanding) < 17) {
955 spin_lock_irqsave(instance->host->host_lock, flags);
956 instance->flag &= ~MEGASAS_FW_BUSY;
957 instance->host->can_queue =
958 instance->max_fw_cmds - MEGASAS_INT_CMDS;
960 spin_unlock_irqrestore(instance->host->host_lock, flags);
965 * megasas_wait_for_outstanding - Wait for all outstanding cmds
966 * @instance: Adapter soft state
968 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
969 * complete all its outstanding commands. Returns error if one or more IOs
970 * are pending after this time period. It also marks the controller dead.
972 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
974 int i;
975 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
977 for (i = 0; i < wait_time; i++) {
979 int outstanding = atomic_read(&instance->fw_outstanding);
981 if (!outstanding)
982 break;
984 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
985 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
986 "commands to complete\n",i,outstanding);
988 * Call cmd completion routine. Cmd to be
989 * be completed directly without depending on isr.
991 megasas_complete_cmd_dpc((unsigned long)instance);
994 msleep(1000);
997 if (atomic_read(&instance->fw_outstanding)) {
999 * Send signal to FW to stop processing any pending cmds.
1000 * The controller will be taken offline by the OS now.
1002 writel(MFI_STOP_ADP,
1003 &instance->reg_set->inbound_doorbell);
1004 megasas_dump_pending_frames(instance);
1005 instance->hw_crit_error = 1;
1006 return FAILED;
1009 return SUCCESS;
1013 * megasas_generic_reset - Generic reset routine
1014 * @scmd: Mid-layer SCSI command
1016 * This routine implements a generic reset handler for device, bus and host
1017 * reset requests. Device, bus and host specific reset handlers can use this
1018 * function after they do their specific tasks.
1020 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1022 int ret_val;
1023 struct megasas_instance *instance;
1025 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1027 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1028 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1030 if (instance->hw_crit_error) {
1031 printk(KERN_ERR "megasas: cannot recover from previous reset "
1032 "failures\n");
1033 return FAILED;
1036 ret_val = megasas_wait_for_outstanding(instance);
1037 if (ret_val == SUCCESS)
1038 printk(KERN_NOTICE "megasas: reset successful \n");
1039 else
1040 printk(KERN_ERR "megasas: failed to do reset\n");
1042 return ret_val;
1046 * megasas_reset_timer - quiesce the adapter if required
1047 * @scmd: scsi cmnd
1049 * Sets the FW busy flag and reduces the host->can_queue if the
1050 * cmd has not been completed within the timeout period.
1052 static enum
1053 scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1055 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1056 struct megasas_instance *instance;
1057 unsigned long flags;
1059 if (time_after(jiffies, scmd->jiffies_at_alloc +
1060 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1061 return EH_NOT_HANDLED;
1064 instance = cmd->instance;
1065 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1066 /* FW is busy, throttle IO */
1067 spin_lock_irqsave(instance->host->host_lock, flags);
1069 instance->host->can_queue = 16;
1070 instance->last_time = jiffies;
1071 instance->flag |= MEGASAS_FW_BUSY;
1073 spin_unlock_irqrestore(instance->host->host_lock, flags);
1075 return EH_RESET_TIMER;
1079 * megasas_reset_device - Device reset handler entry point
1081 static int megasas_reset_device(struct scsi_cmnd *scmd)
1083 int ret;
1086 * First wait for all commands to complete
1088 ret = megasas_generic_reset(scmd);
1090 return ret;
1094 * megasas_reset_bus_host - Bus & host reset handler entry point
1096 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1098 int ret;
1101 * First wait for all commands to complete
1103 ret = megasas_generic_reset(scmd);
1105 return ret;
1109 * megasas_bios_param - Returns disk geometry for a disk
1110 * @sdev: device handle
1111 * @bdev: block device
1112 * @capacity: drive capacity
1113 * @geom: geometry parameters
1115 static int
1116 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1117 sector_t capacity, int geom[])
1119 int heads;
1120 int sectors;
1121 sector_t cylinders;
1122 unsigned long tmp;
1123 /* Default heads (64) & sectors (32) */
1124 heads = 64;
1125 sectors = 32;
1127 tmp = heads * sectors;
1128 cylinders = capacity;
1130 sector_div(cylinders, tmp);
1133 * Handle extended translation size for logical drives > 1Gb
1136 if (capacity >= 0x200000) {
1137 heads = 255;
1138 sectors = 63;
1139 tmp = heads*sectors;
1140 cylinders = capacity;
1141 sector_div(cylinders, tmp);
1144 geom[0] = heads;
1145 geom[1] = sectors;
1146 geom[2] = cylinders;
1148 return 0;
1152 * megasas_service_aen - Processes an event notification
1153 * @instance: Adapter soft state
1154 * @cmd: AEN command completed by the ISR
1156 * For AEN, driver sends a command down to FW that is held by the FW till an
1157 * event occurs. When an event of interest occurs, FW completes the command
1158 * that it was previously holding.
1160 * This routines sends SIGIO signal to processes that have registered with the
1161 * driver for AEN.
1163 static void
1164 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1167 * Don't signal app if it is just an aborted previously registered aen
1169 if (!cmd->abort_aen)
1170 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1171 else
1172 cmd->abort_aen = 0;
1174 instance->aen_cmd = NULL;
1175 megasas_return_cmd(instance, cmd);
1179 * Scsi host template for megaraid_sas driver
1181 static struct scsi_host_template megasas_template = {
1183 .module = THIS_MODULE,
1184 .name = "LSI SAS based MegaRAID driver",
1185 .proc_name = "megaraid_sas",
1186 .slave_configure = megasas_slave_configure,
1187 .queuecommand = megasas_queue_command,
1188 .eh_device_reset_handler = megasas_reset_device,
1189 .eh_bus_reset_handler = megasas_reset_bus_host,
1190 .eh_host_reset_handler = megasas_reset_bus_host,
1191 .eh_timed_out = megasas_reset_timer,
1192 .bios_param = megasas_bios_param,
1193 .use_clustering = ENABLE_CLUSTERING,
1197 * megasas_complete_int_cmd - Completes an internal command
1198 * @instance: Adapter soft state
1199 * @cmd: Command to be completed
1201 * The megasas_issue_blocked_cmd() function waits for a command to complete
1202 * after it issues a command. This function wakes up that waiting routine by
1203 * calling wake_up() on the wait queue.
1205 static void
1206 megasas_complete_int_cmd(struct megasas_instance *instance,
1207 struct megasas_cmd *cmd)
1209 cmd->cmd_status = cmd->frame->io.cmd_status;
1211 if (cmd->cmd_status == ENODATA) {
1212 cmd->cmd_status = 0;
1214 wake_up(&instance->int_cmd_wait_q);
1218 * megasas_complete_abort - Completes aborting a command
1219 * @instance: Adapter soft state
1220 * @cmd: Cmd that was issued to abort another cmd
1222 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1223 * after it issues an abort on a previously issued command. This function
1224 * wakes up all functions waiting on the same wait queue.
1226 static void
1227 megasas_complete_abort(struct megasas_instance *instance,
1228 struct megasas_cmd *cmd)
1230 if (cmd->sync_cmd) {
1231 cmd->sync_cmd = 0;
1232 cmd->cmd_status = 0;
1233 wake_up(&instance->abort_cmd_wait_q);
1236 return;
1240 * megasas_complete_cmd - Completes a command
1241 * @instance: Adapter soft state
1242 * @cmd: Command to be completed
1243 * @alt_status: If non-zero, use this value as status to
1244 * SCSI mid-layer instead of the value returned
1245 * by the FW. This should be used if caller wants
1246 * an alternate status (as in the case of aborted
1247 * commands)
1249 static void
1250 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1251 u8 alt_status)
1253 int exception = 0;
1254 struct megasas_header *hdr = &cmd->frame->hdr;
1256 if (cmd->scmd)
1257 cmd->scmd->SCp.ptr = NULL;
1259 switch (hdr->cmd) {
1261 case MFI_CMD_PD_SCSI_IO:
1262 case MFI_CMD_LD_SCSI_IO:
1265 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1266 * issued either through an IO path or an IOCTL path. If it
1267 * was via IOCTL, we will send it to internal completion.
1269 if (cmd->sync_cmd) {
1270 cmd->sync_cmd = 0;
1271 megasas_complete_int_cmd(instance, cmd);
1272 break;
1275 case MFI_CMD_LD_READ:
1276 case MFI_CMD_LD_WRITE:
1278 if (alt_status) {
1279 cmd->scmd->result = alt_status << 16;
1280 exception = 1;
1283 if (exception) {
1285 atomic_dec(&instance->fw_outstanding);
1287 scsi_dma_unmap(cmd->scmd);
1288 cmd->scmd->scsi_done(cmd->scmd);
1289 megasas_return_cmd(instance, cmd);
1291 break;
1294 switch (hdr->cmd_status) {
1296 case MFI_STAT_OK:
1297 cmd->scmd->result = DID_OK << 16;
1298 break;
1300 case MFI_STAT_SCSI_IO_FAILED:
1301 case MFI_STAT_LD_INIT_IN_PROGRESS:
1302 cmd->scmd->result =
1303 (DID_ERROR << 16) | hdr->scsi_status;
1304 break;
1306 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1308 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1310 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1311 memset(cmd->scmd->sense_buffer, 0,
1312 SCSI_SENSE_BUFFERSIZE);
1313 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1314 hdr->sense_len);
1316 cmd->scmd->result |= DRIVER_SENSE << 24;
1319 break;
1321 case MFI_STAT_LD_OFFLINE:
1322 case MFI_STAT_DEVICE_NOT_FOUND:
1323 cmd->scmd->result = DID_BAD_TARGET << 16;
1324 break;
1326 default:
1327 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1328 hdr->cmd_status);
1329 cmd->scmd->result = DID_ERROR << 16;
1330 break;
1333 atomic_dec(&instance->fw_outstanding);
1335 scsi_dma_unmap(cmd->scmd);
1336 cmd->scmd->scsi_done(cmd->scmd);
1337 megasas_return_cmd(instance, cmd);
1339 break;
1341 case MFI_CMD_SMP:
1342 case MFI_CMD_STP:
1343 case MFI_CMD_DCMD:
1346 * See if got an event notification
1348 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1349 megasas_service_aen(instance, cmd);
1350 else
1351 megasas_complete_int_cmd(instance, cmd);
1353 break;
1355 case MFI_CMD_ABORT:
1357 * Cmd issued to abort another cmd returned
1359 megasas_complete_abort(instance, cmd);
1360 break;
1362 default:
1363 printk("megasas: Unknown command completed! [0x%X]\n",
1364 hdr->cmd);
1365 break;
1370 * megasas_deplete_reply_queue - Processes all completed commands
1371 * @instance: Adapter soft state
1372 * @alt_status: Alternate status to be returned to
1373 * SCSI mid-layer instead of the status
1374 * returned by the FW
1376 static int
1377 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1380 * Check if it is our interrupt
1381 * Clear the interrupt
1383 if(instance->instancet->clear_intr(instance->reg_set))
1384 return IRQ_NONE;
1386 if (instance->hw_crit_error)
1387 goto out_done;
1389 * Schedule the tasklet for cmd completion
1391 tasklet_schedule(&instance->isr_tasklet);
1392 out_done:
1393 return IRQ_HANDLED;
1397 * megasas_isr - isr entry point
1399 static irqreturn_t megasas_isr(int irq, void *devp)
1401 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1402 DID_OK);
1406 * megasas_transition_to_ready - Move the FW to READY state
1407 * @instance: Adapter soft state
1409 * During the initialization, FW passes can potentially be in any one of
1410 * several possible states. If the FW in operational, waiting-for-handshake
1411 * states, driver must take steps to bring it to ready state. Otherwise, it
1412 * has to wait for the ready state.
1414 static int
1415 megasas_transition_to_ready(struct megasas_instance* instance)
1417 int i;
1418 u8 max_wait;
1419 u32 fw_state;
1420 u32 cur_state;
1422 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1424 if (fw_state != MFI_STATE_READY)
1425 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1426 " state\n");
1428 while (fw_state != MFI_STATE_READY) {
1430 switch (fw_state) {
1432 case MFI_STATE_FAULT:
1434 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1435 return -ENODEV;
1437 case MFI_STATE_WAIT_HANDSHAKE:
1439 * Set the CLR bit in inbound doorbell
1441 writel(MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1442 &instance->reg_set->inbound_doorbell);
1444 max_wait = 2;
1445 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1446 break;
1448 case MFI_STATE_BOOT_MESSAGE_PENDING:
1449 writel(MFI_INIT_HOTPLUG,
1450 &instance->reg_set->inbound_doorbell);
1452 max_wait = 10;
1453 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1454 break;
1456 case MFI_STATE_OPERATIONAL:
1458 * Bring it to READY state; assuming max wait 10 secs
1460 instance->instancet->disable_intr(instance->reg_set);
1461 writel(MFI_RESET_FLAGS, &instance->reg_set->inbound_doorbell);
1463 max_wait = 10;
1464 cur_state = MFI_STATE_OPERATIONAL;
1465 break;
1467 case MFI_STATE_UNDEFINED:
1469 * This state should not last for more than 2 seconds
1471 max_wait = 2;
1472 cur_state = MFI_STATE_UNDEFINED;
1473 break;
1475 case MFI_STATE_BB_INIT:
1476 max_wait = 2;
1477 cur_state = MFI_STATE_BB_INIT;
1478 break;
1480 case MFI_STATE_FW_INIT:
1481 max_wait = 20;
1482 cur_state = MFI_STATE_FW_INIT;
1483 break;
1485 case MFI_STATE_FW_INIT_2:
1486 max_wait = 20;
1487 cur_state = MFI_STATE_FW_INIT_2;
1488 break;
1490 case MFI_STATE_DEVICE_SCAN:
1491 max_wait = 20;
1492 cur_state = MFI_STATE_DEVICE_SCAN;
1493 break;
1495 case MFI_STATE_FLUSH_CACHE:
1496 max_wait = 20;
1497 cur_state = MFI_STATE_FLUSH_CACHE;
1498 break;
1500 default:
1501 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1502 fw_state);
1503 return -ENODEV;
1507 * The cur_state should not last for more than max_wait secs
1509 for (i = 0; i < (max_wait * 1000); i++) {
1510 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1511 MFI_STATE_MASK ;
1513 if (fw_state == cur_state) {
1514 msleep(1);
1515 } else
1516 break;
1520 * Return error if fw_state hasn't changed after max_wait
1522 if (fw_state == cur_state) {
1523 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1524 "in %d secs\n", fw_state, max_wait);
1525 return -ENODEV;
1528 printk(KERN_INFO "megasas: FW now in Ready state\n");
1530 return 0;
1534 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1535 * @instance: Adapter soft state
1537 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1539 int i;
1540 u32 max_cmd = instance->max_fw_cmds;
1541 struct megasas_cmd *cmd;
1543 if (!instance->frame_dma_pool)
1544 return;
1547 * Return all frames to pool
1549 for (i = 0; i < max_cmd; i++) {
1551 cmd = instance->cmd_list[i];
1553 if (cmd->frame)
1554 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1555 cmd->frame_phys_addr);
1557 if (cmd->sense)
1558 pci_pool_free(instance->sense_dma_pool, cmd->sense,
1559 cmd->sense_phys_addr);
1563 * Now destroy the pool itself
1565 pci_pool_destroy(instance->frame_dma_pool);
1566 pci_pool_destroy(instance->sense_dma_pool);
1568 instance->frame_dma_pool = NULL;
1569 instance->sense_dma_pool = NULL;
1573 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1574 * @instance: Adapter soft state
1576 * Each command packet has an embedded DMA memory buffer that is used for
1577 * filling MFI frame and the SG list that immediately follows the frame. This
1578 * function creates those DMA memory buffers for each command packet by using
1579 * PCI pool facility.
1581 static int megasas_create_frame_pool(struct megasas_instance *instance)
1583 int i;
1584 u32 max_cmd;
1585 u32 sge_sz;
1586 u32 sgl_sz;
1587 u32 total_sz;
1588 u32 frame_count;
1589 struct megasas_cmd *cmd;
1591 max_cmd = instance->max_fw_cmds;
1594 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1595 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1597 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1598 sizeof(struct megasas_sge32);
1601 * Calculated the number of 64byte frames required for SGL
1603 sgl_sz = sge_sz * instance->max_num_sge;
1604 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1607 * We need one extra frame for the MFI command
1609 frame_count++;
1611 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1613 * Use DMA pool facility provided by PCI layer
1615 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1616 instance->pdev, total_sz, 64,
1619 if (!instance->frame_dma_pool) {
1620 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1621 return -ENOMEM;
1624 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1625 instance->pdev, 128, 4, 0);
1627 if (!instance->sense_dma_pool) {
1628 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1630 pci_pool_destroy(instance->frame_dma_pool);
1631 instance->frame_dma_pool = NULL;
1633 return -ENOMEM;
1637 * Allocate and attach a frame to each of the commands in cmd_list.
1638 * By making cmd->index as the context instead of the &cmd, we can
1639 * always use 32bit context regardless of the architecture
1641 for (i = 0; i < max_cmd; i++) {
1643 cmd = instance->cmd_list[i];
1645 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1646 GFP_KERNEL, &cmd->frame_phys_addr);
1648 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1649 GFP_KERNEL, &cmd->sense_phys_addr);
1652 * megasas_teardown_frame_pool() takes care of freeing
1653 * whatever has been allocated
1655 if (!cmd->frame || !cmd->sense) {
1656 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1657 megasas_teardown_frame_pool(instance);
1658 return -ENOMEM;
1661 cmd->frame->io.context = cmd->index;
1664 return 0;
1668 * megasas_free_cmds - Free all the cmds in the free cmd pool
1669 * @instance: Adapter soft state
1671 static void megasas_free_cmds(struct megasas_instance *instance)
1673 int i;
1674 /* First free the MFI frame pool */
1675 megasas_teardown_frame_pool(instance);
1677 /* Free all the commands in the cmd_list */
1678 for (i = 0; i < instance->max_fw_cmds; i++)
1679 kfree(instance->cmd_list[i]);
1681 /* Free the cmd_list buffer itself */
1682 kfree(instance->cmd_list);
1683 instance->cmd_list = NULL;
1685 INIT_LIST_HEAD(&instance->cmd_pool);
1689 * megasas_alloc_cmds - Allocates the command packets
1690 * @instance: Adapter soft state
1692 * Each command that is issued to the FW, whether IO commands from the OS or
1693 * internal commands like IOCTLs, are wrapped in local data structure called
1694 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1695 * the FW.
1697 * Each frame has a 32-bit field called context (tag). This context is used
1698 * to get back the megasas_cmd from the frame when a frame gets completed in
1699 * the ISR. Typically the address of the megasas_cmd itself would be used as
1700 * the context. But we wanted to keep the differences between 32 and 64 bit
1701 * systems to the mininum. We always use 32 bit integers for the context. In
1702 * this driver, the 32 bit values are the indices into an array cmd_list.
1703 * This array is used only to look up the megasas_cmd given the context. The
1704 * free commands themselves are maintained in a linked list called cmd_pool.
1706 static int megasas_alloc_cmds(struct megasas_instance *instance)
1708 int i;
1709 int j;
1710 u32 max_cmd;
1711 struct megasas_cmd *cmd;
1713 max_cmd = instance->max_fw_cmds;
1716 * instance->cmd_list is an array of struct megasas_cmd pointers.
1717 * Allocate the dynamic array first and then allocate individual
1718 * commands.
1720 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
1722 if (!instance->cmd_list) {
1723 printk(KERN_DEBUG "megasas: out of memory\n");
1724 return -ENOMEM;
1728 for (i = 0; i < max_cmd; i++) {
1729 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1730 GFP_KERNEL);
1732 if (!instance->cmd_list[i]) {
1734 for (j = 0; j < i; j++)
1735 kfree(instance->cmd_list[j]);
1737 kfree(instance->cmd_list);
1738 instance->cmd_list = NULL;
1740 return -ENOMEM;
1745 * Add all the commands to command pool (instance->cmd_pool)
1747 for (i = 0; i < max_cmd; i++) {
1748 cmd = instance->cmd_list[i];
1749 memset(cmd, 0, sizeof(struct megasas_cmd));
1750 cmd->index = i;
1751 cmd->instance = instance;
1753 list_add_tail(&cmd->list, &instance->cmd_pool);
1757 * Create a frame pool and assign one frame to each cmd
1759 if (megasas_create_frame_pool(instance)) {
1760 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1761 megasas_free_cmds(instance);
1764 return 0;
1768 * megasas_get_controller_info - Returns FW's controller structure
1769 * @instance: Adapter soft state
1770 * @ctrl_info: Controller information structure
1772 * Issues an internal command (DCMD) to get the FW's controller structure.
1773 * This information is mainly used to find out the maximum IO transfer per
1774 * command supported by the FW.
1776 static int
1777 megasas_get_ctrl_info(struct megasas_instance *instance,
1778 struct megasas_ctrl_info *ctrl_info)
1780 int ret = 0;
1781 struct megasas_cmd *cmd;
1782 struct megasas_dcmd_frame *dcmd;
1783 struct megasas_ctrl_info *ci;
1784 dma_addr_t ci_h = 0;
1786 cmd = megasas_get_cmd(instance);
1788 if (!cmd) {
1789 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1790 return -ENOMEM;
1793 dcmd = &cmd->frame->dcmd;
1795 ci = pci_alloc_consistent(instance->pdev,
1796 sizeof(struct megasas_ctrl_info), &ci_h);
1798 if (!ci) {
1799 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1800 megasas_return_cmd(instance, cmd);
1801 return -ENOMEM;
1804 memset(ci, 0, sizeof(*ci));
1805 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1807 dcmd->cmd = MFI_CMD_DCMD;
1808 dcmd->cmd_status = 0xFF;
1809 dcmd->sge_count = 1;
1810 dcmd->flags = MFI_FRAME_DIR_READ;
1811 dcmd->timeout = 0;
1812 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1813 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1814 dcmd->sgl.sge32[0].phys_addr = ci_h;
1815 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1817 if (!megasas_issue_polled(instance, cmd)) {
1818 ret = 0;
1819 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1820 } else {
1821 ret = -1;
1824 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1825 ci, ci_h);
1827 megasas_return_cmd(instance, cmd);
1828 return ret;
1832 * megasas_issue_init_mfi - Initializes the FW
1833 * @instance: Adapter soft state
1835 * Issues the INIT MFI cmd
1837 static int
1838 megasas_issue_init_mfi(struct megasas_instance *instance)
1840 u32 context;
1842 struct megasas_cmd *cmd;
1844 struct megasas_init_frame *init_frame;
1845 struct megasas_init_queue_info *initq_info;
1846 dma_addr_t init_frame_h;
1847 dma_addr_t initq_info_h;
1850 * Prepare a init frame. Note the init frame points to queue info
1851 * structure. Each frame has SGL allocated after first 64 bytes. For
1852 * this frame - since we don't need any SGL - we use SGL's space as
1853 * queue info structure
1855 * We will not get a NULL command below. We just created the pool.
1857 cmd = megasas_get_cmd(instance);
1859 init_frame = (struct megasas_init_frame *)cmd->frame;
1860 initq_info = (struct megasas_init_queue_info *)
1861 ((unsigned long)init_frame + 64);
1863 init_frame_h = cmd->frame_phys_addr;
1864 initq_info_h = init_frame_h + 64;
1866 context = init_frame->context;
1867 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1868 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1869 init_frame->context = context;
1871 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1872 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1874 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1875 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1877 init_frame->cmd = MFI_CMD_INIT;
1878 init_frame->cmd_status = 0xFF;
1879 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1881 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1884 * disable the intr before firing the init frame to FW
1886 instance->instancet->disable_intr(instance->reg_set);
1889 * Issue the init frame in polled mode
1892 if (megasas_issue_polled(instance, cmd)) {
1893 printk(KERN_ERR "megasas: Failed to init firmware\n");
1894 megasas_return_cmd(instance, cmd);
1895 goto fail_fw_init;
1898 megasas_return_cmd(instance, cmd);
1900 return 0;
1902 fail_fw_init:
1903 return -EINVAL;
1907 * megasas_start_timer - Initializes a timer object
1908 * @instance: Adapter soft state
1909 * @timer: timer object to be initialized
1910 * @fn: timer function
1911 * @interval: time interval between timer function call
1913 static inline void
1914 megasas_start_timer(struct megasas_instance *instance,
1915 struct timer_list *timer,
1916 void *fn, unsigned long interval)
1918 init_timer(timer);
1919 timer->expires = jiffies + interval;
1920 timer->data = (unsigned long)instance;
1921 timer->function = fn;
1922 add_timer(timer);
1926 * megasas_io_completion_timer - Timer fn
1927 * @instance_addr: Address of adapter soft state
1929 * Schedules tasklet for cmd completion
1930 * if poll_mode_io is set
1932 static void
1933 megasas_io_completion_timer(unsigned long instance_addr)
1935 struct megasas_instance *instance =
1936 (struct megasas_instance *)instance_addr;
1938 if (atomic_read(&instance->fw_outstanding))
1939 tasklet_schedule(&instance->isr_tasklet);
1941 /* Restart timer */
1942 if (poll_mode_io)
1943 mod_timer(&instance->io_completion_timer,
1944 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
1948 * megasas_init_mfi - Initializes the FW
1949 * @instance: Adapter soft state
1951 * This is the main function for initializing MFI firmware.
1953 static int megasas_init_mfi(struct megasas_instance *instance)
1955 u32 context_sz;
1956 u32 reply_q_sz;
1957 u32 max_sectors_1;
1958 u32 max_sectors_2;
1959 u32 tmp_sectors;
1960 struct megasas_register_set __iomem *reg_set;
1961 struct megasas_ctrl_info *ctrl_info;
1963 * Map the message registers
1965 instance->base_addr = pci_resource_start(instance->pdev, 0);
1967 if (pci_request_regions(instance->pdev, "megasas: LSI")) {
1968 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1969 return -EBUSY;
1972 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1974 if (!instance->reg_set) {
1975 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1976 goto fail_ioremap;
1979 reg_set = instance->reg_set;
1981 switch(instance->pdev->device)
1983 case PCI_DEVICE_ID_LSI_SAS1078R:
1984 instance->instancet = &megasas_instance_template_ppc;
1985 break;
1986 case PCI_DEVICE_ID_LSI_SAS1064R:
1987 case PCI_DEVICE_ID_DELL_PERC5:
1988 default:
1989 instance->instancet = &megasas_instance_template_xscale;
1990 break;
1994 * We expect the FW state to be READY
1996 if (megasas_transition_to_ready(instance))
1997 goto fail_ready_state;
2000 * Get various operational parameters from status register
2002 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
2004 * Reduce the max supported cmds by 1. This is to ensure that the
2005 * reply_q_sz (1 more than the max cmd that driver may send)
2006 * does not exceed max cmds that the FW can support
2008 instance->max_fw_cmds = instance->max_fw_cmds-1;
2009 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2010 0x10;
2012 * Create a pool of commands
2014 if (megasas_alloc_cmds(instance))
2015 goto fail_alloc_cmds;
2018 * Allocate memory for reply queue. Length of reply queue should
2019 * be _one_ more than the maximum commands handled by the firmware.
2021 * Note: When FW completes commands, it places corresponding contex
2022 * values in this circular reply queue. This circular queue is a fairly
2023 * typical producer-consumer queue. FW is the producer (of completed
2024 * commands) and the driver is the consumer.
2026 context_sz = sizeof(u32);
2027 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2029 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2030 reply_q_sz,
2031 &instance->reply_queue_h);
2033 if (!instance->reply_queue) {
2034 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2035 goto fail_reply_queue;
2038 if (megasas_issue_init_mfi(instance))
2039 goto fail_fw_init;
2041 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2044 * Compute the max allowed sectors per IO: The controller info has two
2045 * limits on max sectors. Driver should use the minimum of these two.
2047 * 1 << stripe_sz_ops.min = max sectors per strip
2049 * Note that older firmwares ( < FW ver 30) didn't report information
2050 * to calculate max_sectors_1. So the number ended up as zero always.
2052 tmp_sectors = 0;
2053 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2055 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2056 ctrl_info->max_strips_per_io;
2057 max_sectors_2 = ctrl_info->max_request_size;
2059 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2062 instance->max_sectors_per_req = instance->max_num_sge *
2063 PAGE_SIZE / 512;
2064 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2065 instance->max_sectors_per_req = tmp_sectors;
2067 kfree(ctrl_info);
2070 * Setup tasklet for cmd completion
2073 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2074 (unsigned long)instance);
2076 /* Initialize the cmd completion timer */
2077 if (poll_mode_io)
2078 megasas_start_timer(instance, &instance->io_completion_timer,
2079 megasas_io_completion_timer,
2080 MEGASAS_COMPLETION_TIMER_INTERVAL);
2081 return 0;
2083 fail_fw_init:
2085 pci_free_consistent(instance->pdev, reply_q_sz,
2086 instance->reply_queue, instance->reply_queue_h);
2087 fail_reply_queue:
2088 megasas_free_cmds(instance);
2090 fail_alloc_cmds:
2091 fail_ready_state:
2092 iounmap(instance->reg_set);
2094 fail_ioremap:
2095 pci_release_regions(instance->pdev);
2097 return -EINVAL;
2101 * megasas_release_mfi - Reverses the FW initialization
2102 * @intance: Adapter soft state
2104 static void megasas_release_mfi(struct megasas_instance *instance)
2106 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2108 pci_free_consistent(instance->pdev, reply_q_sz,
2109 instance->reply_queue, instance->reply_queue_h);
2111 megasas_free_cmds(instance);
2113 iounmap(instance->reg_set);
2115 pci_release_regions(instance->pdev);
2119 * megasas_get_seq_num - Gets latest event sequence numbers
2120 * @instance: Adapter soft state
2121 * @eli: FW event log sequence numbers information
2123 * FW maintains a log of all events in a non-volatile area. Upper layers would
2124 * usually find out the latest sequence number of the events, the seq number at
2125 * the boot etc. They would "read" all the events below the latest seq number
2126 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2127 * number), they would subsribe to AEN (asynchronous event notification) and
2128 * wait for the events to happen.
2130 static int
2131 megasas_get_seq_num(struct megasas_instance *instance,
2132 struct megasas_evt_log_info *eli)
2134 struct megasas_cmd *cmd;
2135 struct megasas_dcmd_frame *dcmd;
2136 struct megasas_evt_log_info *el_info;
2137 dma_addr_t el_info_h = 0;
2139 cmd = megasas_get_cmd(instance);
2141 if (!cmd) {
2142 return -ENOMEM;
2145 dcmd = &cmd->frame->dcmd;
2146 el_info = pci_alloc_consistent(instance->pdev,
2147 sizeof(struct megasas_evt_log_info),
2148 &el_info_h);
2150 if (!el_info) {
2151 megasas_return_cmd(instance, cmd);
2152 return -ENOMEM;
2155 memset(el_info, 0, sizeof(*el_info));
2156 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2158 dcmd->cmd = MFI_CMD_DCMD;
2159 dcmd->cmd_status = 0x0;
2160 dcmd->sge_count = 1;
2161 dcmd->flags = MFI_FRAME_DIR_READ;
2162 dcmd->timeout = 0;
2163 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2164 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2165 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2166 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2168 megasas_issue_blocked_cmd(instance, cmd);
2171 * Copy the data back into callers buffer
2173 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2175 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2176 el_info, el_info_h);
2178 megasas_return_cmd(instance, cmd);
2180 return 0;
2184 * megasas_register_aen - Registers for asynchronous event notification
2185 * @instance: Adapter soft state
2186 * @seq_num: The starting sequence number
2187 * @class_locale: Class of the event
2189 * This function subscribes for AEN for events beyond the @seq_num. It requests
2190 * to be notified if and only if the event is of type @class_locale
2192 static int
2193 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2194 u32 class_locale_word)
2196 int ret_val;
2197 struct megasas_cmd *cmd;
2198 struct megasas_dcmd_frame *dcmd;
2199 union megasas_evt_class_locale curr_aen;
2200 union megasas_evt_class_locale prev_aen;
2203 * If there an AEN pending already (aen_cmd), check if the
2204 * class_locale of that pending AEN is inclusive of the new
2205 * AEN request we currently have. If it is, then we don't have
2206 * to do anything. In other words, whichever events the current
2207 * AEN request is subscribing to, have already been subscribed
2208 * to.
2210 * If the old_cmd is _not_ inclusive, then we have to abort
2211 * that command, form a class_locale that is superset of both
2212 * old and current and re-issue to the FW
2215 curr_aen.word = class_locale_word;
2217 if (instance->aen_cmd) {
2219 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2222 * A class whose enum value is smaller is inclusive of all
2223 * higher values. If a PROGRESS (= -1) was previously
2224 * registered, then a new registration requests for higher
2225 * classes need not be sent to FW. They are automatically
2226 * included.
2228 * Locale numbers don't have such hierarchy. They are bitmap
2229 * values
2231 if ((prev_aen.members.class <= curr_aen.members.class) &&
2232 !((prev_aen.members.locale & curr_aen.members.locale) ^
2233 curr_aen.members.locale)) {
2235 * Previously issued event registration includes
2236 * current request. Nothing to do.
2238 return 0;
2239 } else {
2240 curr_aen.members.locale |= prev_aen.members.locale;
2242 if (prev_aen.members.class < curr_aen.members.class)
2243 curr_aen.members.class = prev_aen.members.class;
2245 instance->aen_cmd->abort_aen = 1;
2246 ret_val = megasas_issue_blocked_abort_cmd(instance,
2247 instance->
2248 aen_cmd);
2250 if (ret_val) {
2251 printk(KERN_DEBUG "megasas: Failed to abort "
2252 "previous AEN command\n");
2253 return ret_val;
2258 cmd = megasas_get_cmd(instance);
2260 if (!cmd)
2261 return -ENOMEM;
2263 dcmd = &cmd->frame->dcmd;
2265 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2268 * Prepare DCMD for aen registration
2270 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2272 dcmd->cmd = MFI_CMD_DCMD;
2273 dcmd->cmd_status = 0x0;
2274 dcmd->sge_count = 1;
2275 dcmd->flags = MFI_FRAME_DIR_READ;
2276 dcmd->timeout = 0;
2277 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2278 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2279 dcmd->mbox.w[0] = seq_num;
2280 dcmd->mbox.w[1] = curr_aen.word;
2281 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2282 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2285 * Store reference to the cmd used to register for AEN. When an
2286 * application wants us to register for AEN, we have to abort this
2287 * cmd and re-register with a new EVENT LOCALE supplied by that app
2289 instance->aen_cmd = cmd;
2292 * Issue the aen registration frame
2294 instance->instancet->fire_cmd(cmd->frame_phys_addr ,0,instance->reg_set);
2296 return 0;
2300 * megasas_start_aen - Subscribes to AEN during driver load time
2301 * @instance: Adapter soft state
2303 static int megasas_start_aen(struct megasas_instance *instance)
2305 struct megasas_evt_log_info eli;
2306 union megasas_evt_class_locale class_locale;
2309 * Get the latest sequence number from FW
2311 memset(&eli, 0, sizeof(eli));
2313 if (megasas_get_seq_num(instance, &eli))
2314 return -1;
2317 * Register AEN with FW for latest sequence number plus 1
2319 class_locale.members.reserved = 0;
2320 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2321 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2323 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2324 class_locale.word);
2328 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2329 * @instance: Adapter soft state
2331 static int megasas_io_attach(struct megasas_instance *instance)
2333 struct Scsi_Host *host = instance->host;
2336 * Export parameters required by SCSI mid-layer
2338 host->irq = instance->pdev->irq;
2339 host->unique_id = instance->unique_id;
2340 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
2341 host->this_id = instance->init_id;
2342 host->sg_tablesize = instance->max_num_sge;
2343 host->max_sectors = instance->max_sectors_per_req;
2344 host->cmd_per_lun = 128;
2345 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
2346 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
2347 host->max_lun = MEGASAS_MAX_LUN;
2348 host->max_cmd_len = 16;
2351 * Notify the mid-layer about the new controller
2353 if (scsi_add_host(host, &instance->pdev->dev)) {
2354 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2355 return -ENODEV;
2359 * Trigger SCSI to scan our drives
2361 scsi_scan_host(host);
2362 return 0;
2365 static int
2366 megasas_set_dma_mask(struct pci_dev *pdev)
2369 * All our contollers are capable of performing 64-bit DMA
2371 if (IS_DMA64) {
2372 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2374 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2375 goto fail_set_dma_mask;
2377 } else {
2378 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2379 goto fail_set_dma_mask;
2381 return 0;
2383 fail_set_dma_mask:
2384 return 1;
2388 * megasas_probe_one - PCI hotplug entry point
2389 * @pdev: PCI device structure
2390 * @id: PCI ids of supported hotplugged adapter
2392 static int __devinit
2393 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2395 int rval;
2396 struct Scsi_Host *host;
2397 struct megasas_instance *instance;
2400 * Announce PCI information
2402 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2403 pdev->vendor, pdev->device, pdev->subsystem_vendor,
2404 pdev->subsystem_device);
2406 printk("bus %d:slot %d:func %d\n",
2407 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2410 * PCI prepping: enable device set bus mastering and dma mask
2412 rval = pci_enable_device(pdev);
2414 if (rval) {
2415 return rval;
2418 pci_set_master(pdev);
2420 if (megasas_set_dma_mask(pdev))
2421 goto fail_set_dma_mask;
2423 host = scsi_host_alloc(&megasas_template,
2424 sizeof(struct megasas_instance));
2426 if (!host) {
2427 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2428 goto fail_alloc_instance;
2431 instance = (struct megasas_instance *)host->hostdata;
2432 memset(instance, 0, sizeof(*instance));
2434 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2435 &instance->producer_h);
2436 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2437 &instance->consumer_h);
2439 if (!instance->producer || !instance->consumer) {
2440 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2441 "producer, consumer\n");
2442 goto fail_alloc_dma_buf;
2445 *instance->producer = 0;
2446 *instance->consumer = 0;
2448 instance->evt_detail = pci_alloc_consistent(pdev,
2449 sizeof(struct
2450 megasas_evt_detail),
2451 &instance->evt_detail_h);
2453 if (!instance->evt_detail) {
2454 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2455 "event detail structure\n");
2456 goto fail_alloc_dma_buf;
2460 * Initialize locks and queues
2462 INIT_LIST_HEAD(&instance->cmd_pool);
2464 atomic_set(&instance->fw_outstanding,0);
2466 init_waitqueue_head(&instance->int_cmd_wait_q);
2467 init_waitqueue_head(&instance->abort_cmd_wait_q);
2469 spin_lock_init(&instance->cmd_pool_lock);
2470 spin_lock_init(&instance->completion_lock);
2472 mutex_init(&instance->aen_mutex);
2473 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2476 * Initialize PCI related and misc parameters
2478 instance->pdev = pdev;
2479 instance->host = host;
2480 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2481 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2483 megasas_dbg_lvl = 0;
2484 instance->flag = 0;
2485 instance->last_time = 0;
2488 * Initialize MFI Firmware
2490 if (megasas_init_mfi(instance))
2491 goto fail_init_mfi;
2494 * Register IRQ
2496 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
2497 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2498 goto fail_irq;
2501 instance->instancet->enable_intr(instance->reg_set);
2504 * Store instance in PCI softstate
2506 pci_set_drvdata(pdev, instance);
2509 * Add this controller to megasas_mgmt_info structure so that it
2510 * can be exported to management applications
2512 megasas_mgmt_info.count++;
2513 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2514 megasas_mgmt_info.max_index++;
2517 * Initiate AEN (Asynchronous Event Notification)
2519 if (megasas_start_aen(instance)) {
2520 printk(KERN_DEBUG "megasas: start aen failed\n");
2521 goto fail_start_aen;
2525 * Register with SCSI mid-layer
2527 if (megasas_io_attach(instance))
2528 goto fail_io_attach;
2530 return 0;
2532 fail_start_aen:
2533 fail_io_attach:
2534 megasas_mgmt_info.count--;
2535 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2536 megasas_mgmt_info.max_index--;
2538 pci_set_drvdata(pdev, NULL);
2539 instance->instancet->disable_intr(instance->reg_set);
2540 free_irq(instance->pdev->irq, instance);
2542 megasas_release_mfi(instance);
2544 fail_irq:
2545 fail_init_mfi:
2546 fail_alloc_dma_buf:
2547 if (instance->evt_detail)
2548 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2549 instance->evt_detail,
2550 instance->evt_detail_h);
2552 if (instance->producer)
2553 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2554 instance->producer_h);
2555 if (instance->consumer)
2556 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2557 instance->consumer_h);
2558 scsi_host_put(host);
2560 fail_alloc_instance:
2561 fail_set_dma_mask:
2562 pci_disable_device(pdev);
2564 return -ENODEV;
2568 * megasas_flush_cache - Requests FW to flush all its caches
2569 * @instance: Adapter soft state
2571 static void megasas_flush_cache(struct megasas_instance *instance)
2573 struct megasas_cmd *cmd;
2574 struct megasas_dcmd_frame *dcmd;
2576 cmd = megasas_get_cmd(instance);
2578 if (!cmd)
2579 return;
2581 dcmd = &cmd->frame->dcmd;
2583 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2585 dcmd->cmd = MFI_CMD_DCMD;
2586 dcmd->cmd_status = 0x0;
2587 dcmd->sge_count = 0;
2588 dcmd->flags = MFI_FRAME_DIR_NONE;
2589 dcmd->timeout = 0;
2590 dcmd->data_xfer_len = 0;
2591 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2592 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2594 megasas_issue_blocked_cmd(instance, cmd);
2596 megasas_return_cmd(instance, cmd);
2598 return;
2602 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2603 * @instance: Adapter soft state
2604 * @opcode: Shutdown/Hibernate
2606 static void megasas_shutdown_controller(struct megasas_instance *instance,
2607 u32 opcode)
2609 struct megasas_cmd *cmd;
2610 struct megasas_dcmd_frame *dcmd;
2612 cmd = megasas_get_cmd(instance);
2614 if (!cmd)
2615 return;
2617 if (instance->aen_cmd)
2618 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2620 dcmd = &cmd->frame->dcmd;
2622 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2624 dcmd->cmd = MFI_CMD_DCMD;
2625 dcmd->cmd_status = 0x0;
2626 dcmd->sge_count = 0;
2627 dcmd->flags = MFI_FRAME_DIR_NONE;
2628 dcmd->timeout = 0;
2629 dcmd->data_xfer_len = 0;
2630 dcmd->opcode = opcode;
2632 megasas_issue_blocked_cmd(instance, cmd);
2634 megasas_return_cmd(instance, cmd);
2636 return;
2640 * megasas_suspend - driver suspend entry point
2641 * @pdev: PCI device structure
2642 * @state: PCI power state to suspend routine
2644 static int __devinit
2645 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
2647 struct Scsi_Host *host;
2648 struct megasas_instance *instance;
2650 instance = pci_get_drvdata(pdev);
2651 host = instance->host;
2653 if (poll_mode_io)
2654 del_timer_sync(&instance->io_completion_timer);
2656 megasas_flush_cache(instance);
2657 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
2658 tasklet_kill(&instance->isr_tasklet);
2660 pci_set_drvdata(instance->pdev, instance);
2661 instance->instancet->disable_intr(instance->reg_set);
2662 free_irq(instance->pdev->irq, instance);
2664 pci_save_state(pdev);
2665 pci_disable_device(pdev);
2667 pci_set_power_state(pdev, pci_choose_state(pdev, state));
2669 return 0;
2673 * megasas_resume- driver resume entry point
2674 * @pdev: PCI device structure
2676 static int __devinit
2677 megasas_resume(struct pci_dev *pdev)
2679 int rval;
2680 struct Scsi_Host *host;
2681 struct megasas_instance *instance;
2683 instance = pci_get_drvdata(pdev);
2684 host = instance->host;
2685 pci_set_power_state(pdev, PCI_D0);
2686 pci_enable_wake(pdev, PCI_D0, 0);
2687 pci_restore_state(pdev);
2690 * PCI prepping: enable device set bus mastering and dma mask
2692 rval = pci_enable_device(pdev);
2694 if (rval) {
2695 printk(KERN_ERR "megasas: Enable device failed\n");
2696 return rval;
2699 pci_set_master(pdev);
2701 if (megasas_set_dma_mask(pdev))
2702 goto fail_set_dma_mask;
2705 * Initialize MFI Firmware
2708 *instance->producer = 0;
2709 *instance->consumer = 0;
2711 atomic_set(&instance->fw_outstanding, 0);
2714 * We expect the FW state to be READY
2716 if (megasas_transition_to_ready(instance))
2717 goto fail_ready_state;
2719 if (megasas_issue_init_mfi(instance))
2720 goto fail_init_mfi;
2722 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2723 (unsigned long)instance);
2726 * Register IRQ
2728 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
2729 "megasas", instance)) {
2730 printk(KERN_ERR "megasas: Failed to register IRQ\n");
2731 goto fail_irq;
2734 instance->instancet->enable_intr(instance->reg_set);
2737 * Initiate AEN (Asynchronous Event Notification)
2739 if (megasas_start_aen(instance))
2740 printk(KERN_ERR "megasas: Start AEN failed\n");
2742 /* Initialize the cmd completion timer */
2743 if (poll_mode_io)
2744 megasas_start_timer(instance, &instance->io_completion_timer,
2745 megasas_io_completion_timer,
2746 MEGASAS_COMPLETION_TIMER_INTERVAL);
2747 return 0;
2749 fail_irq:
2750 fail_init_mfi:
2751 if (instance->evt_detail)
2752 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2753 instance->evt_detail,
2754 instance->evt_detail_h);
2756 if (instance->producer)
2757 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2758 instance->producer_h);
2759 if (instance->consumer)
2760 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2761 instance->consumer_h);
2762 scsi_host_put(host);
2764 fail_set_dma_mask:
2765 fail_ready_state:
2767 pci_disable_device(pdev);
2769 return -ENODEV;
2773 * megasas_detach_one - PCI hot"un"plug entry point
2774 * @pdev: PCI device structure
2776 static void megasas_detach_one(struct pci_dev *pdev)
2778 int i;
2779 struct Scsi_Host *host;
2780 struct megasas_instance *instance;
2782 instance = pci_get_drvdata(pdev);
2783 host = instance->host;
2785 if (poll_mode_io)
2786 del_timer_sync(&instance->io_completion_timer);
2788 scsi_remove_host(instance->host);
2789 megasas_flush_cache(instance);
2790 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
2791 tasklet_kill(&instance->isr_tasklet);
2794 * Take the instance off the instance array. Note that we will not
2795 * decrement the max_index. We let this array be sparse array
2797 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2798 if (megasas_mgmt_info.instance[i] == instance) {
2799 megasas_mgmt_info.count--;
2800 megasas_mgmt_info.instance[i] = NULL;
2802 break;
2806 pci_set_drvdata(instance->pdev, NULL);
2808 instance->instancet->disable_intr(instance->reg_set);
2810 free_irq(instance->pdev->irq, instance);
2812 megasas_release_mfi(instance);
2814 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2815 instance->evt_detail, instance->evt_detail_h);
2817 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2818 instance->producer_h);
2820 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2821 instance->consumer_h);
2823 scsi_host_put(host);
2825 pci_set_drvdata(pdev, NULL);
2827 pci_disable_device(pdev);
2829 return;
2833 * megasas_shutdown - Shutdown entry point
2834 * @device: Generic device structure
2836 static void megasas_shutdown(struct pci_dev *pdev)
2838 struct megasas_instance *instance = pci_get_drvdata(pdev);
2839 megasas_flush_cache(instance);
2843 * megasas_mgmt_open - char node "open" entry point
2845 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2848 * Allow only those users with admin rights
2850 if (!capable(CAP_SYS_ADMIN))
2851 return -EACCES;
2853 return 0;
2857 * megasas_mgmt_release - char node "release" entry point
2859 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2861 filep->private_data = NULL;
2862 fasync_helper(-1, filep, 0, &megasas_async_queue);
2864 return 0;
2868 * megasas_mgmt_fasync - Async notifier registration from applications
2870 * This function adds the calling process to a driver global queue. When an
2871 * event occurs, SIGIO will be sent to all processes in this queue.
2873 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2875 int rc;
2877 mutex_lock(&megasas_async_queue_mutex);
2879 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2881 mutex_unlock(&megasas_async_queue_mutex);
2883 if (rc >= 0) {
2884 /* For sanity check when we get ioctl */
2885 filep->private_data = filep;
2886 return 0;
2889 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2891 return rc;
2895 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2896 * @instance: Adapter soft state
2897 * @argp: User's ioctl packet
2899 static int
2900 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2901 struct megasas_iocpacket __user * user_ioc,
2902 struct megasas_iocpacket *ioc)
2904 struct megasas_sge32 *kern_sge32;
2905 struct megasas_cmd *cmd;
2906 void *kbuff_arr[MAX_IOCTL_SGE];
2907 dma_addr_t buf_handle = 0;
2908 int error = 0, i;
2909 void *sense = NULL;
2910 dma_addr_t sense_handle;
2911 u32 *sense_ptr;
2912 unsigned long *sense_buff;
2914 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2916 if (ioc->sge_count > MAX_IOCTL_SGE) {
2917 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2918 ioc->sge_count, MAX_IOCTL_SGE);
2919 return -EINVAL;
2922 cmd = megasas_get_cmd(instance);
2923 if (!cmd) {
2924 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2925 return -ENOMEM;
2929 * User's IOCTL packet has 2 frames (maximum). Copy those two
2930 * frames into our cmd's frames. cmd->frame's context will get
2931 * overwritten when we copy from user's frames. So set that value
2932 * alone separately
2934 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2935 cmd->frame->hdr.context = cmd->index;
2938 * The management interface between applications and the fw uses
2939 * MFI frames. E.g, RAID configuration changes, LD property changes
2940 * etc are accomplishes through different kinds of MFI frames. The
2941 * driver needs to care only about substituting user buffers with
2942 * kernel buffers in SGLs. The location of SGL is embedded in the
2943 * struct iocpacket itself.
2945 kern_sge32 = (struct megasas_sge32 *)
2946 ((unsigned long)cmd->frame + ioc->sgl_off);
2949 * For each user buffer, create a mirror buffer and copy in
2951 for (i = 0; i < ioc->sge_count; i++) {
2952 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
2953 ioc->sgl[i].iov_len,
2954 &buf_handle, GFP_KERNEL);
2955 if (!kbuff_arr[i]) {
2956 printk(KERN_DEBUG "megasas: Failed to alloc "
2957 "kernel SGL buffer for IOCTL \n");
2958 error = -ENOMEM;
2959 goto out;
2963 * We don't change the dma_coherent_mask, so
2964 * pci_alloc_consistent only returns 32bit addresses
2966 kern_sge32[i].phys_addr = (u32) buf_handle;
2967 kern_sge32[i].length = ioc->sgl[i].iov_len;
2970 * We created a kernel buffer corresponding to the
2971 * user buffer. Now copy in from the user buffer
2973 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2974 (u32) (ioc->sgl[i].iov_len))) {
2975 error = -EFAULT;
2976 goto out;
2980 if (ioc->sense_len) {
2981 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
2982 &sense_handle, GFP_KERNEL);
2983 if (!sense) {
2984 error = -ENOMEM;
2985 goto out;
2988 sense_ptr =
2989 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2990 *sense_ptr = sense_handle;
2994 * Set the sync_cmd flag so that the ISR knows not to complete this
2995 * cmd to the SCSI mid-layer
2997 cmd->sync_cmd = 1;
2998 megasas_issue_blocked_cmd(instance, cmd);
2999 cmd->sync_cmd = 0;
3002 * copy out the kernel buffers to user buffers
3004 for (i = 0; i < ioc->sge_count; i++) {
3005 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3006 ioc->sgl[i].iov_len)) {
3007 error = -EFAULT;
3008 goto out;
3013 * copy out the sense
3015 if (ioc->sense_len) {
3017 * sense_buff points to the location that has the user
3018 * sense buffer address
3020 sense_buff = (unsigned long *) ((unsigned long)ioc->frame.raw +
3021 ioc->sense_off);
3023 if (copy_to_user((void __user *)(unsigned long)(*sense_buff),
3024 sense, ioc->sense_len)) {
3025 printk(KERN_ERR "megasas: Failed to copy out to user "
3026 "sense data\n");
3027 error = -EFAULT;
3028 goto out;
3033 * copy the status codes returned by the fw
3035 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3036 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3037 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3038 error = -EFAULT;
3041 out:
3042 if (sense) {
3043 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
3044 sense, sense_handle);
3047 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
3048 dma_free_coherent(&instance->pdev->dev,
3049 kern_sge32[i].length,
3050 kbuff_arr[i], kern_sge32[i].phys_addr);
3053 megasas_return_cmd(instance, cmd);
3054 return error;
3057 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
3059 int i;
3061 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3063 if ((megasas_mgmt_info.instance[i]) &&
3064 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
3065 return megasas_mgmt_info.instance[i];
3068 return NULL;
3071 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3073 struct megasas_iocpacket __user *user_ioc =
3074 (struct megasas_iocpacket __user *)arg;
3075 struct megasas_iocpacket *ioc;
3076 struct megasas_instance *instance;
3077 int error;
3079 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3080 if (!ioc)
3081 return -ENOMEM;
3083 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3084 error = -EFAULT;
3085 goto out_kfree_ioc;
3088 instance = megasas_lookup_instance(ioc->host_no);
3089 if (!instance) {
3090 error = -ENODEV;
3091 goto out_kfree_ioc;
3095 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3097 if (down_interruptible(&instance->ioctl_sem)) {
3098 error = -ERESTARTSYS;
3099 goto out_kfree_ioc;
3101 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3102 up(&instance->ioctl_sem);
3104 out_kfree_ioc:
3105 kfree(ioc);
3106 return error;
3109 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3111 struct megasas_instance *instance;
3112 struct megasas_aen aen;
3113 int error;
3115 if (file->private_data != file) {
3116 printk(KERN_DEBUG "megasas: fasync_helper was not "
3117 "called first\n");
3118 return -EINVAL;
3121 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3122 return -EFAULT;
3124 instance = megasas_lookup_instance(aen.host_no);
3126 if (!instance)
3127 return -ENODEV;
3129 mutex_lock(&instance->aen_mutex);
3130 error = megasas_register_aen(instance, aen.seq_num,
3131 aen.class_locale_word);
3132 mutex_unlock(&instance->aen_mutex);
3133 return error;
3137 * megasas_mgmt_ioctl - char node ioctl entry point
3139 static long
3140 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3142 switch (cmd) {
3143 case MEGASAS_IOC_FIRMWARE:
3144 return megasas_mgmt_ioctl_fw(file, arg);
3146 case MEGASAS_IOC_GET_AEN:
3147 return megasas_mgmt_ioctl_aen(file, arg);
3150 return -ENOTTY;
3153 #ifdef CONFIG_COMPAT
3154 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3156 struct compat_megasas_iocpacket __user *cioc =
3157 (struct compat_megasas_iocpacket __user *)arg;
3158 struct megasas_iocpacket __user *ioc =
3159 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3160 int i;
3161 int error = 0;
3163 if (clear_user(ioc, sizeof(*ioc)))
3164 return -EFAULT;
3166 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3167 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3168 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3169 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3170 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3171 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3172 return -EFAULT;
3174 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3175 compat_uptr_t ptr;
3177 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3178 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3179 copy_in_user(&ioc->sgl[i].iov_len,
3180 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3181 return -EFAULT;
3184 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3186 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3187 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3188 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3189 return -EFAULT;
3191 return error;
3194 static long
3195 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3196 unsigned long arg)
3198 switch (cmd) {
3199 case MEGASAS_IOC_FIRMWARE32:
3200 return megasas_mgmt_compat_ioctl_fw(file, arg);
3201 case MEGASAS_IOC_GET_AEN:
3202 return megasas_mgmt_ioctl_aen(file, arg);
3205 return -ENOTTY;
3207 #endif
3210 * File operations structure for management interface
3212 static const struct file_operations megasas_mgmt_fops = {
3213 .owner = THIS_MODULE,
3214 .open = megasas_mgmt_open,
3215 .release = megasas_mgmt_release,
3216 .fasync = megasas_mgmt_fasync,
3217 .unlocked_ioctl = megasas_mgmt_ioctl,
3218 #ifdef CONFIG_COMPAT
3219 .compat_ioctl = megasas_mgmt_compat_ioctl,
3220 #endif
3224 * PCI hotplug support registration structure
3226 static struct pci_driver megasas_pci_driver = {
3228 .name = "megaraid_sas",
3229 .id_table = megasas_pci_table,
3230 .probe = megasas_probe_one,
3231 .remove = __devexit_p(megasas_detach_one),
3232 .suspend = megasas_suspend,
3233 .resume = megasas_resume,
3234 .shutdown = megasas_shutdown,
3238 * Sysfs driver attributes
3240 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3242 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3243 MEGASAS_VERSION);
3246 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3248 static ssize_t
3249 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3251 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3252 MEGASAS_RELDATE);
3255 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3256 NULL);
3258 static ssize_t
3259 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
3261 return sprintf(buf, "%u\n", megasas_dbg_lvl);
3264 static ssize_t
3265 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
3267 int retval = count;
3268 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
3269 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
3270 retval = -EINVAL;
3272 return retval;
3275 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUGO, megasas_sysfs_show_dbg_lvl,
3276 megasas_sysfs_set_dbg_lvl);
3278 static ssize_t
3279 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
3281 return sprintf(buf, "%u\n", poll_mode_io);
3284 static ssize_t
3285 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
3286 const char *buf, size_t count)
3288 int retval = count;
3289 int tmp = poll_mode_io;
3290 int i;
3291 struct megasas_instance *instance;
3293 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
3294 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
3295 retval = -EINVAL;
3299 * Check if poll_mode_io is already set or is same as previous value
3301 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
3302 goto out;
3304 if (poll_mode_io) {
3306 * Start timers for all adapters
3308 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3309 instance = megasas_mgmt_info.instance[i];
3310 if (instance) {
3311 megasas_start_timer(instance,
3312 &instance->io_completion_timer,
3313 megasas_io_completion_timer,
3314 MEGASAS_COMPLETION_TIMER_INTERVAL);
3317 } else {
3319 * Delete timers for all adapters
3321 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3322 instance = megasas_mgmt_info.instance[i];
3323 if (instance)
3324 del_timer_sync(&instance->io_completion_timer);
3328 out:
3329 return retval;
3332 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUGO,
3333 megasas_sysfs_show_poll_mode_io,
3334 megasas_sysfs_set_poll_mode_io);
3337 * megasas_init - Driver load entry point
3339 static int __init megasas_init(void)
3341 int rval;
3344 * Announce driver version and other information
3346 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
3347 MEGASAS_EXT_VERSION);
3349 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
3352 * Register character device node
3354 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
3356 if (rval < 0) {
3357 printk(KERN_DEBUG "megasas: failed to open device node\n");
3358 return rval;
3361 megasas_mgmt_majorno = rval;
3364 * Register ourselves as PCI hotplug module
3366 rval = pci_register_driver(&megasas_pci_driver);
3368 if (rval) {
3369 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
3370 goto err_pcidrv;
3373 rval = driver_create_file(&megasas_pci_driver.driver,
3374 &driver_attr_version);
3375 if (rval)
3376 goto err_dcf_attr_ver;
3377 rval = driver_create_file(&megasas_pci_driver.driver,
3378 &driver_attr_release_date);
3379 if (rval)
3380 goto err_dcf_rel_date;
3381 rval = driver_create_file(&megasas_pci_driver.driver,
3382 &driver_attr_dbg_lvl);
3383 if (rval)
3384 goto err_dcf_dbg_lvl;
3385 rval = driver_create_file(&megasas_pci_driver.driver,
3386 &driver_attr_poll_mode_io);
3387 if (rval)
3388 goto err_dcf_poll_mode_io;
3390 return rval;
3392 err_dcf_poll_mode_io:
3393 driver_remove_file(&megasas_pci_driver.driver,
3394 &driver_attr_dbg_lvl);
3395 err_dcf_dbg_lvl:
3396 driver_remove_file(&megasas_pci_driver.driver,
3397 &driver_attr_release_date);
3398 err_dcf_rel_date:
3399 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3400 err_dcf_attr_ver:
3401 pci_unregister_driver(&megasas_pci_driver);
3402 err_pcidrv:
3403 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3404 return rval;
3408 * megasas_exit - Driver unload entry point
3410 static void __exit megasas_exit(void)
3412 driver_remove_file(&megasas_pci_driver.driver,
3413 &driver_attr_poll_mode_io);
3414 driver_remove_file(&megasas_pci_driver.driver,
3415 &driver_attr_dbg_lvl);
3416 driver_remove_file(&megasas_pci_driver.driver,
3417 &driver_attr_release_date);
3418 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
3420 pci_unregister_driver(&megasas_pci_driver);
3421 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
3424 module_init(megasas_init);
3425 module_exit(megasas_exit);