Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_sas.c
blob99e4478c3f3ed4efc8d018ee2cc5f3b98d941514
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.04.17.1-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/smp_lock.h>
37 #include <linux/uio.h>
38 #include <linux/slab.h>
39 #include <asm/uaccess.h>
40 #include <linux/fs.h>
41 #include <linux/compat.h>
42 #include <linux/blkdev.h>
43 #include <linux/mutex.h>
44 #include <linux/poll.h>
46 #include <scsi/scsi.h>
47 #include <scsi/scsi_cmnd.h>
48 #include <scsi/scsi_device.h>
49 #include <scsi/scsi_host.h>
50 #include "megaraid_sas.h"
53 * poll_mode_io:1- schedule complete completion from q cmd
55 static unsigned int poll_mode_io;
56 module_param_named(poll_mode_io, poll_mode_io, int, 0);
57 MODULE_PARM_DESC(poll_mode_io,
58 "Complete cmds from IO path, (default=0)");
60 MODULE_LICENSE("GPL");
61 MODULE_VERSION(MEGASAS_VERSION);
62 MODULE_AUTHOR("megaraidlinux@lsi.com");
63 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
66 * PCI ID table for all supported controllers
68 static struct pci_device_id megasas_pci_table[] = {
70 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
71 /* xscale IOP */
72 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
73 /* ppc IOP */
74 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
75 /* ppc IOP */
76 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
77 /* gen2*/
78 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
79 /* gen2*/
80 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
81 /* skinny*/
82 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
83 /* skinny*/
84 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
85 /* xscale IOP, vega */
86 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
87 /* xscale IOP */
91 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
93 static int megasas_mgmt_majorno;
94 static struct megasas_mgmt_info megasas_mgmt_info;
95 static struct fasync_struct *megasas_async_queue;
96 static DEFINE_MUTEX(megasas_async_queue_mutex);
98 static int megasas_poll_wait_aen;
99 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
100 static u32 support_poll_for_event;
101 static u32 megasas_dbg_lvl;
103 /* define lock for aen poll */
104 spinlock_t poll_aen_lock;
106 static void
107 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
108 u8 alt_status);
111 * megasas_get_cmd - Get a command from the free pool
112 * @instance: Adapter soft state
114 * Returns a free command from the pool
116 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
117 *instance)
119 unsigned long flags;
120 struct megasas_cmd *cmd = NULL;
122 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
124 if (!list_empty(&instance->cmd_pool)) {
125 cmd = list_entry((&instance->cmd_pool)->next,
126 struct megasas_cmd, list);
127 list_del_init(&cmd->list);
128 } else {
129 printk(KERN_ERR "megasas: Command pool empty!\n");
132 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
133 return cmd;
137 * megasas_return_cmd - Return a cmd to free command pool
138 * @instance: Adapter soft state
139 * @cmd: Command packet to be returned to free command pool
141 static inline void
142 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
144 unsigned long flags;
146 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
148 cmd->scmd = NULL;
149 list_add_tail(&cmd->list, &instance->cmd_pool);
151 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
156 * The following functions are defined for xscale
157 * (deviceid : 1064R, PERC5) controllers
161 * megasas_enable_intr_xscale - Enables interrupts
162 * @regs: MFI register set
164 static inline void
165 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
167 writel(1, &(regs)->outbound_intr_mask);
169 /* Dummy readl to force pci flush */
170 readl(&regs->outbound_intr_mask);
174 * megasas_disable_intr_xscale -Disables interrupt
175 * @regs: MFI register set
177 static inline void
178 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
180 u32 mask = 0x1f;
181 writel(mask, &regs->outbound_intr_mask);
182 /* Dummy readl to force pci flush */
183 readl(&regs->outbound_intr_mask);
187 * megasas_read_fw_status_reg_xscale - returns the current FW status value
188 * @regs: MFI register set
190 static u32
191 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
193 return readl(&(regs)->outbound_msg_0);
196 * megasas_clear_interrupt_xscale - Check & clear interrupt
197 * @regs: MFI register set
199 static int
200 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
202 u32 status;
204 * Check if it is our interrupt
206 status = readl(&regs->outbound_intr_status);
208 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
209 return 1;
213 * Clear the interrupt by writing back the same value
215 writel(status, &regs->outbound_intr_status);
217 /* Dummy readl to force pci flush */
218 readl(&regs->outbound_intr_status);
220 return 0;
224 * megasas_fire_cmd_xscale - Sends command to the FW
225 * @frame_phys_addr : Physical address of cmd
226 * @frame_count : Number of frames for the command
227 * @regs : MFI register set
229 static inline void
230 megasas_fire_cmd_xscale(struct megasas_instance *instance,
231 dma_addr_t frame_phys_addr,
232 u32 frame_count,
233 struct megasas_register_set __iomem *regs)
235 writel((frame_phys_addr >> 3)|(frame_count),
236 &(regs)->inbound_queue_port);
239 static struct megasas_instance_template megasas_instance_template_xscale = {
241 .fire_cmd = megasas_fire_cmd_xscale,
242 .enable_intr = megasas_enable_intr_xscale,
243 .disable_intr = megasas_disable_intr_xscale,
244 .clear_intr = megasas_clear_intr_xscale,
245 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
249 * This is the end of set of functions & definitions specific
250 * to xscale (deviceid : 1064R, PERC5) controllers
254 * The following functions are defined for ppc (deviceid : 0x60)
255 * controllers
259 * megasas_enable_intr_ppc - Enables interrupts
260 * @regs: MFI register set
262 static inline void
263 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
265 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
267 writel(~0x80000004, &(regs)->outbound_intr_mask);
269 /* Dummy readl to force pci flush */
270 readl(&regs->outbound_intr_mask);
274 * megasas_disable_intr_ppc - Disable interrupt
275 * @regs: MFI register set
277 static inline void
278 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
280 u32 mask = 0xFFFFFFFF;
281 writel(mask, &regs->outbound_intr_mask);
282 /* Dummy readl to force pci flush */
283 readl(&regs->outbound_intr_mask);
287 * megasas_read_fw_status_reg_ppc - returns the current FW status value
288 * @regs: MFI register set
290 static u32
291 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
293 return readl(&(regs)->outbound_scratch_pad);
297 * megasas_clear_interrupt_ppc - Check & clear interrupt
298 * @regs: MFI register set
300 static int
301 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
303 u32 status;
305 * Check if it is our interrupt
307 status = readl(&regs->outbound_intr_status);
309 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
310 return 1;
314 * Clear the interrupt by writing back the same value
316 writel(status, &regs->outbound_doorbell_clear);
318 /* Dummy readl to force pci flush */
319 readl(&regs->outbound_doorbell_clear);
321 return 0;
324 * megasas_fire_cmd_ppc - Sends command to the FW
325 * @frame_phys_addr : Physical address of cmd
326 * @frame_count : Number of frames for the command
327 * @regs : MFI register set
329 static inline void
330 megasas_fire_cmd_ppc(struct megasas_instance *instance,
331 dma_addr_t frame_phys_addr,
332 u32 frame_count,
333 struct megasas_register_set __iomem *regs)
335 writel((frame_phys_addr | (frame_count<<1))|1,
336 &(regs)->inbound_queue_port);
339 static struct megasas_instance_template megasas_instance_template_ppc = {
341 .fire_cmd = megasas_fire_cmd_ppc,
342 .enable_intr = megasas_enable_intr_ppc,
343 .disable_intr = megasas_disable_intr_ppc,
344 .clear_intr = megasas_clear_intr_ppc,
345 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
349 * megasas_enable_intr_skinny - Enables interrupts
350 * @regs: MFI register set
352 static inline void
353 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
355 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
357 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
359 /* Dummy readl to force pci flush */
360 readl(&regs->outbound_intr_mask);
364 * megasas_disable_intr_skinny - Disables interrupt
365 * @regs: MFI register set
367 static inline void
368 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
370 u32 mask = 0xFFFFFFFF;
371 writel(mask, &regs->outbound_intr_mask);
372 /* Dummy readl to force pci flush */
373 readl(&regs->outbound_intr_mask);
377 * megasas_read_fw_status_reg_skinny - returns the current FW status value
378 * @regs: MFI register set
380 static u32
381 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
383 return readl(&(regs)->outbound_scratch_pad);
387 * megasas_clear_interrupt_skinny - Check & clear interrupt
388 * @regs: MFI register set
390 static int
391 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
393 u32 status;
395 * Check if it is our interrupt
397 status = readl(&regs->outbound_intr_status);
399 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
400 return 1;
404 * Clear the interrupt by writing back the same value
406 writel(status, &regs->outbound_intr_status);
409 * dummy read to flush PCI
411 readl(&regs->outbound_intr_status);
413 return 0;
417 * megasas_fire_cmd_skinny - Sends command to the FW
418 * @frame_phys_addr : Physical address of cmd
419 * @frame_count : Number of frames for the command
420 * @regs : MFI register set
422 static inline void
423 megasas_fire_cmd_skinny(struct megasas_instance *instance,
424 dma_addr_t frame_phys_addr,
425 u32 frame_count,
426 struct megasas_register_set __iomem *regs)
428 unsigned long flags;
429 spin_lock_irqsave(&instance->fire_lock, flags);
430 writel(0, &(regs)->inbound_high_queue_port);
431 writel((frame_phys_addr | (frame_count<<1))|1,
432 &(regs)->inbound_low_queue_port);
433 spin_unlock_irqrestore(&instance->fire_lock, flags);
436 static struct megasas_instance_template megasas_instance_template_skinny = {
438 .fire_cmd = megasas_fire_cmd_skinny,
439 .enable_intr = megasas_enable_intr_skinny,
440 .disable_intr = megasas_disable_intr_skinny,
441 .clear_intr = megasas_clear_intr_skinny,
442 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
447 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
448 * controllers
452 * megasas_enable_intr_gen2 - Enables interrupts
453 * @regs: MFI register set
455 static inline void
456 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
458 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
460 /* write ~0x00000005 (4 & 1) to the intr mask*/
461 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
463 /* Dummy readl to force pci flush */
464 readl(&regs->outbound_intr_mask);
468 * megasas_disable_intr_gen2 - Disables interrupt
469 * @regs: MFI register set
471 static inline void
472 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
474 u32 mask = 0xFFFFFFFF;
475 writel(mask, &regs->outbound_intr_mask);
476 /* Dummy readl to force pci flush */
477 readl(&regs->outbound_intr_mask);
481 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
482 * @regs: MFI register set
484 static u32
485 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
487 return readl(&(regs)->outbound_scratch_pad);
491 * megasas_clear_interrupt_gen2 - Check & clear interrupt
492 * @regs: MFI register set
494 static int
495 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
497 u32 status;
499 * Check if it is our interrupt
501 status = readl(&regs->outbound_intr_status);
503 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK))
504 return 1;
507 * Clear the interrupt by writing back the same value
509 writel(status, &regs->outbound_doorbell_clear);
511 /* Dummy readl to force pci flush */
512 readl(&regs->outbound_intr_status);
514 return 0;
517 * megasas_fire_cmd_gen2 - Sends command to the FW
518 * @frame_phys_addr : Physical address of cmd
519 * @frame_count : Number of frames for the command
520 * @regs : MFI register set
522 static inline void
523 megasas_fire_cmd_gen2(struct megasas_instance *instance,
524 dma_addr_t frame_phys_addr,
525 u32 frame_count,
526 struct megasas_register_set __iomem *regs)
528 writel((frame_phys_addr | (frame_count<<1))|1,
529 &(regs)->inbound_queue_port);
532 static struct megasas_instance_template megasas_instance_template_gen2 = {
534 .fire_cmd = megasas_fire_cmd_gen2,
535 .enable_intr = megasas_enable_intr_gen2,
536 .disable_intr = megasas_disable_intr_gen2,
537 .clear_intr = megasas_clear_intr_gen2,
538 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
542 * This is the end of set of functions & definitions
543 * specific to ppc (deviceid : 0x60) controllers
547 * megasas_issue_polled - Issues a polling command
548 * @instance: Adapter soft state
549 * @cmd: Command packet to be issued
551 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
553 static int
554 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
556 int i;
557 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
559 struct megasas_header *frame_hdr = &cmd->frame->hdr;
561 frame_hdr->cmd_status = 0xFF;
562 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
565 * Issue the frame using inbound queue port
567 instance->instancet->fire_cmd(instance,
568 cmd->frame_phys_addr, 0, instance->reg_set);
571 * Wait for cmd_status to change
573 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
574 rmb();
575 msleep(1);
578 if (frame_hdr->cmd_status == 0xff)
579 return -ETIME;
581 return 0;
585 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
586 * @instance: Adapter soft state
587 * @cmd: Command to be issued
589 * This function waits on an event for the command to be returned from ISR.
590 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
591 * Used to issue ioctl commands.
593 static int
594 megasas_issue_blocked_cmd(struct megasas_instance *instance,
595 struct megasas_cmd *cmd)
597 cmd->cmd_status = ENODATA;
599 instance->instancet->fire_cmd(instance,
600 cmd->frame_phys_addr, 0, instance->reg_set);
602 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA),
603 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
605 return 0;
609 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
610 * @instance: Adapter soft state
611 * @cmd_to_abort: Previously issued cmd to be aborted
613 * MFI firmware can abort previously issued AEN comamnd (automatic event
614 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
615 * cmd and waits for return status.
616 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
618 static int
619 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
620 struct megasas_cmd *cmd_to_abort)
622 struct megasas_cmd *cmd;
623 struct megasas_abort_frame *abort_fr;
625 cmd = megasas_get_cmd(instance);
627 if (!cmd)
628 return -1;
630 abort_fr = &cmd->frame->abort;
633 * Prepare and issue the abort frame
635 abort_fr->cmd = MFI_CMD_ABORT;
636 abort_fr->cmd_status = 0xFF;
637 abort_fr->flags = 0;
638 abort_fr->abort_context = cmd_to_abort->index;
639 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
640 abort_fr->abort_mfi_phys_addr_hi = 0;
642 cmd->sync_cmd = 1;
643 cmd->cmd_status = 0xFF;
645 instance->instancet->fire_cmd(instance,
646 cmd->frame_phys_addr, 0, instance->reg_set);
649 * Wait for this cmd to complete
651 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF),
652 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ);
654 megasas_return_cmd(instance, cmd);
655 return 0;
659 * megasas_make_sgl32 - Prepares 32-bit SGL
660 * @instance: Adapter soft state
661 * @scp: SCSI command from the mid-layer
662 * @mfi_sgl: SGL to be filled in
664 * If successful, this function returns the number of SG elements. Otherwise,
665 * it returnes -1.
667 static int
668 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
669 union megasas_sgl *mfi_sgl)
671 int i;
672 int sge_count;
673 struct scatterlist *os_sgl;
675 sge_count = scsi_dma_map(scp);
676 BUG_ON(sge_count < 0);
678 if (sge_count) {
679 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
680 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
681 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
684 return sge_count;
688 * megasas_make_sgl64 - Prepares 64-bit SGL
689 * @instance: Adapter soft state
690 * @scp: SCSI command from the mid-layer
691 * @mfi_sgl: SGL to be filled in
693 * If successful, this function returns the number of SG elements. Otherwise,
694 * it returnes -1.
696 static int
697 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
698 union megasas_sgl *mfi_sgl)
700 int i;
701 int sge_count;
702 struct scatterlist *os_sgl;
704 sge_count = scsi_dma_map(scp);
705 BUG_ON(sge_count < 0);
707 if (sge_count) {
708 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
709 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
710 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
713 return sge_count;
717 * megasas_make_sgl_skinny - Prepares IEEE SGL
718 * @instance: Adapter soft state
719 * @scp: SCSI command from the mid-layer
720 * @mfi_sgl: SGL to be filled in
722 * If successful, this function returns the number of SG elements. Otherwise,
723 * it returnes -1.
725 static int
726 megasas_make_sgl_skinny(struct megasas_instance *instance,
727 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
729 int i;
730 int sge_count;
731 struct scatterlist *os_sgl;
733 sge_count = scsi_dma_map(scp);
735 if (sge_count) {
736 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
737 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
738 mfi_sgl->sge_skinny[i].phys_addr =
739 sg_dma_address(os_sgl);
742 return sge_count;
746 * megasas_get_frame_count - Computes the number of frames
747 * @frame_type : type of frame- io or pthru frame
748 * @sge_count : number of sg elements
750 * Returns the number of frames required for numnber of sge's (sge_count)
753 static u32 megasas_get_frame_count(struct megasas_instance *instance,
754 u8 sge_count, u8 frame_type)
756 int num_cnt;
757 int sge_bytes;
758 u32 sge_sz;
759 u32 frame_count=0;
761 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
762 sizeof(struct megasas_sge32);
764 if (instance->flag_ieee) {
765 sge_sz = sizeof(struct megasas_sge_skinny);
769 * Main frame can contain 2 SGEs for 64-bit SGLs and
770 * 3 SGEs for 32-bit SGLs for ldio &
771 * 1 SGEs for 64-bit SGLs and
772 * 2 SGEs for 32-bit SGLs for pthru frame
774 if (unlikely(frame_type == PTHRU_FRAME)) {
775 if (instance->flag_ieee == 1) {
776 num_cnt = sge_count - 1;
777 } else if (IS_DMA64)
778 num_cnt = sge_count - 1;
779 else
780 num_cnt = sge_count - 2;
781 } else {
782 if (instance->flag_ieee == 1) {
783 num_cnt = sge_count - 1;
784 } else if (IS_DMA64)
785 num_cnt = sge_count - 2;
786 else
787 num_cnt = sge_count - 3;
790 if(num_cnt>0){
791 sge_bytes = sge_sz * num_cnt;
793 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
794 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
796 /* Main frame */
797 frame_count +=1;
799 if (frame_count > 7)
800 frame_count = 8;
801 return frame_count;
805 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
806 * @instance: Adapter soft state
807 * @scp: SCSI command
808 * @cmd: Command to be prepared in
810 * This function prepares CDB commands. These are typcially pass-through
811 * commands to the devices.
813 static int
814 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
815 struct megasas_cmd *cmd)
817 u32 is_logical;
818 u32 device_id;
819 u16 flags = 0;
820 struct megasas_pthru_frame *pthru;
822 is_logical = MEGASAS_IS_LOGICAL(scp);
823 device_id = MEGASAS_DEV_INDEX(instance, scp);
824 pthru = (struct megasas_pthru_frame *)cmd->frame;
826 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
827 flags = MFI_FRAME_DIR_WRITE;
828 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
829 flags = MFI_FRAME_DIR_READ;
830 else if (scp->sc_data_direction == PCI_DMA_NONE)
831 flags = MFI_FRAME_DIR_NONE;
833 if (instance->flag_ieee == 1) {
834 flags |= MFI_FRAME_IEEE;
838 * Prepare the DCDB frame
840 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
841 pthru->cmd_status = 0x0;
842 pthru->scsi_status = 0x0;
843 pthru->target_id = device_id;
844 pthru->lun = scp->device->lun;
845 pthru->cdb_len = scp->cmd_len;
846 pthru->timeout = 0;
847 pthru->pad_0 = 0;
848 pthru->flags = flags;
849 pthru->data_xfer_len = scsi_bufflen(scp);
851 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
854 * If the command is for the tape device, set the
855 * pthru timeout to the os layer timeout value.
857 if (scp->device->type == TYPE_TAPE) {
858 if ((scp->request->timeout / HZ) > 0xFFFF)
859 pthru->timeout = 0xFFFF;
860 else
861 pthru->timeout = scp->request->timeout / HZ;
865 * Construct SGL
867 if (instance->flag_ieee == 1) {
868 pthru->flags |= MFI_FRAME_SGL64;
869 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
870 &pthru->sgl);
871 } else if (IS_DMA64) {
872 pthru->flags |= MFI_FRAME_SGL64;
873 pthru->sge_count = megasas_make_sgl64(instance, scp,
874 &pthru->sgl);
875 } else
876 pthru->sge_count = megasas_make_sgl32(instance, scp,
877 &pthru->sgl);
879 if (pthru->sge_count > instance->max_num_sge) {
880 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
881 pthru->sge_count);
882 return 0;
886 * Sense info specific
888 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
889 pthru->sense_buf_phys_addr_hi = 0;
890 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
893 * Compute the total number of frames this command consumes. FW uses
894 * this number to pull sufficient number of frames from host memory.
896 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
897 PTHRU_FRAME);
899 return cmd->frame_count;
903 * megasas_build_ldio - Prepares IOs to logical devices
904 * @instance: Adapter soft state
905 * @scp: SCSI command
906 * @cmd: Command to be prepared
908 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
910 static int
911 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
912 struct megasas_cmd *cmd)
914 u32 device_id;
915 u8 sc = scp->cmnd[0];
916 u16 flags = 0;
917 struct megasas_io_frame *ldio;
919 device_id = MEGASAS_DEV_INDEX(instance, scp);
920 ldio = (struct megasas_io_frame *)cmd->frame;
922 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
923 flags = MFI_FRAME_DIR_WRITE;
924 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
925 flags = MFI_FRAME_DIR_READ;
927 if (instance->flag_ieee == 1) {
928 flags |= MFI_FRAME_IEEE;
932 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
934 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
935 ldio->cmd_status = 0x0;
936 ldio->scsi_status = 0x0;
937 ldio->target_id = device_id;
938 ldio->timeout = 0;
939 ldio->reserved_0 = 0;
940 ldio->pad_0 = 0;
941 ldio->flags = flags;
942 ldio->start_lba_hi = 0;
943 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
946 * 6-byte READ(0x08) or WRITE(0x0A) cdb
948 if (scp->cmd_len == 6) {
949 ldio->lba_count = (u32) scp->cmnd[4];
950 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
951 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
953 ldio->start_lba_lo &= 0x1FFFFF;
957 * 10-byte READ(0x28) or WRITE(0x2A) cdb
959 else if (scp->cmd_len == 10) {
960 ldio->lba_count = (u32) scp->cmnd[8] |
961 ((u32) scp->cmnd[7] << 8);
962 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
963 ((u32) scp->cmnd[3] << 16) |
964 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
968 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
970 else if (scp->cmd_len == 12) {
971 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
972 ((u32) scp->cmnd[7] << 16) |
973 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
975 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
976 ((u32) scp->cmnd[3] << 16) |
977 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
981 * 16-byte READ(0x88) or WRITE(0x8A) cdb
983 else if (scp->cmd_len == 16) {
984 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
985 ((u32) scp->cmnd[11] << 16) |
986 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
988 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
989 ((u32) scp->cmnd[7] << 16) |
990 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
992 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
993 ((u32) scp->cmnd[3] << 16) |
994 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
999 * Construct SGL
1001 if (instance->flag_ieee) {
1002 ldio->flags |= MFI_FRAME_SGL64;
1003 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1004 &ldio->sgl);
1005 } else if (IS_DMA64) {
1006 ldio->flags |= MFI_FRAME_SGL64;
1007 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1008 } else
1009 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1011 if (ldio->sge_count > instance->max_num_sge) {
1012 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1013 ldio->sge_count);
1014 return 0;
1018 * Sense info specific
1020 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1021 ldio->sense_buf_phys_addr_hi = 0;
1022 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1025 * Compute the total number of frames this command consumes. FW uses
1026 * this number to pull sufficient number of frames from host memory.
1028 cmd->frame_count = megasas_get_frame_count(instance,
1029 ldio->sge_count, IO_FRAME);
1031 return cmd->frame_count;
1035 * megasas_is_ldio - Checks if the cmd is for logical drive
1036 * @scmd: SCSI command
1038 * Called by megasas_queue_command to find out if the command to be queued
1039 * is a logical drive command
1041 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1043 if (!MEGASAS_IS_LOGICAL(cmd))
1044 return 0;
1045 switch (cmd->cmnd[0]) {
1046 case READ_10:
1047 case WRITE_10:
1048 case READ_12:
1049 case WRITE_12:
1050 case READ_6:
1051 case WRITE_6:
1052 case READ_16:
1053 case WRITE_16:
1054 return 1;
1055 default:
1056 return 0;
1061 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1062 * in FW
1063 * @instance: Adapter soft state
1065 static inline void
1066 megasas_dump_pending_frames(struct megasas_instance *instance)
1068 struct megasas_cmd *cmd;
1069 int i,n;
1070 union megasas_sgl *mfi_sgl;
1071 struct megasas_io_frame *ldio;
1072 struct megasas_pthru_frame *pthru;
1073 u32 sgcount;
1074 u32 max_cmd = instance->max_fw_cmds;
1076 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1077 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1078 if (IS_DMA64)
1079 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1080 else
1081 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1083 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1084 for (i = 0; i < max_cmd; i++) {
1085 cmd = instance->cmd_list[i];
1086 if(!cmd->scmd)
1087 continue;
1088 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1089 if (megasas_is_ldio(cmd->scmd)){
1090 ldio = (struct megasas_io_frame *)cmd->frame;
1091 mfi_sgl = &ldio->sgl;
1092 sgcount = ldio->sge_count;
1093 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);
1095 else {
1096 pthru = (struct megasas_pthru_frame *) cmd->frame;
1097 mfi_sgl = &pthru->sgl;
1098 sgcount = pthru->sge_count;
1099 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);
1101 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1102 for (n = 0; n < sgcount; n++){
1103 if (IS_DMA64)
1104 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) ;
1105 else
1106 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1109 printk(KERN_ERR "\n");
1110 } /*for max_cmd*/
1111 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1112 for (i = 0; i < max_cmd; i++) {
1114 cmd = instance->cmd_list[i];
1116 if(cmd->sync_cmd == 1){
1117 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1120 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1124 * megasas_queue_command - Queue entry point
1125 * @scmd: SCSI command to be queued
1126 * @done: Callback entry point
1128 static int
1129 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1131 u32 frame_count;
1132 struct megasas_cmd *cmd;
1133 struct megasas_instance *instance;
1135 instance = (struct megasas_instance *)
1136 scmd->device->host->hostdata;
1138 /* Don't process if we have already declared adapter dead */
1139 if (instance->hw_crit_error)
1140 return SCSI_MLQUEUE_HOST_BUSY;
1142 scmd->scsi_done = done;
1143 scmd->result = 0;
1145 if (MEGASAS_IS_LOGICAL(scmd) &&
1146 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1147 scmd->result = DID_BAD_TARGET << 16;
1148 goto out_done;
1151 switch (scmd->cmnd[0]) {
1152 case SYNCHRONIZE_CACHE:
1154 * FW takes care of flush cache on its own
1155 * No need to send it down
1157 scmd->result = DID_OK << 16;
1158 goto out_done;
1159 default:
1160 break;
1163 cmd = megasas_get_cmd(instance);
1164 if (!cmd)
1165 return SCSI_MLQUEUE_HOST_BUSY;
1168 * Logical drive command
1170 if (megasas_is_ldio(scmd))
1171 frame_count = megasas_build_ldio(instance, scmd, cmd);
1172 else
1173 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1175 if (!frame_count)
1176 goto out_return_cmd;
1178 cmd->scmd = scmd;
1179 scmd->SCp.ptr = (char *)cmd;
1182 * Issue the command to the FW
1184 atomic_inc(&instance->fw_outstanding);
1186 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1187 cmd->frame_count-1, instance->reg_set);
1189 * Check if we have pend cmds to be completed
1191 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1192 tasklet_schedule(&instance->isr_tasklet);
1195 return 0;
1197 out_return_cmd:
1198 megasas_return_cmd(instance, cmd);
1199 out_done:
1200 done(scmd);
1201 return 0;
1204 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1206 int i;
1208 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1210 if ((megasas_mgmt_info.instance[i]) &&
1211 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1212 return megasas_mgmt_info.instance[i];
1215 return NULL;
1218 static int megasas_slave_configure(struct scsi_device *sdev)
1220 u16 pd_index = 0;
1221 struct megasas_instance *instance ;
1223 instance = megasas_lookup_instance(sdev->host->host_no);
1226 * Don't export physical disk devices to the disk driver.
1228 * FIXME: Currently we don't export them to the midlayer at all.
1229 * That will be fixed once LSI engineers have audited the
1230 * firmware for possible issues.
1232 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1233 sdev->type == TYPE_DISK) {
1234 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1235 sdev->id;
1236 if (instance->pd_list[pd_index].driveState ==
1237 MR_PD_STATE_SYSTEM) {
1238 blk_queue_rq_timeout(sdev->request_queue,
1239 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1240 return 0;
1242 return -ENXIO;
1246 * The RAID firmware may require extended timeouts.
1248 blk_queue_rq_timeout(sdev->request_queue,
1249 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1250 return 0;
1253 static int megasas_slave_alloc(struct scsi_device *sdev)
1255 u16 pd_index = 0;
1256 struct megasas_instance *instance ;
1257 instance = megasas_lookup_instance(sdev->host->host_no);
1258 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1259 (sdev->type == TYPE_DISK)) {
1261 * Open the OS scan to the SYSTEM PD
1263 pd_index =
1264 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1265 sdev->id;
1266 if ((instance->pd_list[pd_index].driveState ==
1267 MR_PD_STATE_SYSTEM) &&
1268 (instance->pd_list[pd_index].driveType ==
1269 TYPE_DISK)) {
1270 return 0;
1272 return -ENXIO;
1274 return 0;
1278 * megasas_complete_cmd_dpc - Returns FW's controller structure
1279 * @instance_addr: Address of adapter soft state
1281 * Tasklet to complete cmds
1283 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1285 u32 producer;
1286 u32 consumer;
1287 u32 context;
1288 struct megasas_cmd *cmd;
1289 struct megasas_instance *instance =
1290 (struct megasas_instance *)instance_addr;
1291 unsigned long flags;
1293 /* If we have already declared adapter dead, donot complete cmds */
1294 if (instance->hw_crit_error)
1295 return;
1297 spin_lock_irqsave(&instance->completion_lock, flags);
1299 producer = *instance->producer;
1300 consumer = *instance->consumer;
1302 while (consumer != producer) {
1303 context = instance->reply_queue[consumer];
1305 cmd = instance->cmd_list[context];
1307 megasas_complete_cmd(instance, cmd, DID_OK);
1309 consumer++;
1310 if (consumer == (instance->max_fw_cmds + 1)) {
1311 consumer = 0;
1315 *instance->consumer = producer;
1317 spin_unlock_irqrestore(&instance->completion_lock, flags);
1320 * Check if we can restore can_queue
1322 if (instance->flag & MEGASAS_FW_BUSY
1323 && time_after(jiffies, instance->last_time + 5 * HZ)
1324 && atomic_read(&instance->fw_outstanding) < 17) {
1326 spin_lock_irqsave(instance->host->host_lock, flags);
1327 instance->flag &= ~MEGASAS_FW_BUSY;
1328 if ((instance->pdev->device ==
1329 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1330 (instance->pdev->device ==
1331 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1332 instance->host->can_queue =
1333 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1334 } else
1335 instance->host->can_queue =
1336 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1338 spin_unlock_irqrestore(instance->host->host_lock, flags);
1343 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1344 * @instance: Adapter soft state
1346 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1347 * complete all its outstanding commands. Returns error if one or more IOs
1348 * are pending after this time period. It also marks the controller dead.
1350 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1352 int i;
1353 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1355 for (i = 0; i < wait_time; i++) {
1357 int outstanding = atomic_read(&instance->fw_outstanding);
1359 if (!outstanding)
1360 break;
1362 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1363 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1364 "commands to complete\n",i,outstanding);
1366 * Call cmd completion routine. Cmd to be
1367 * be completed directly without depending on isr.
1369 megasas_complete_cmd_dpc((unsigned long)instance);
1372 msleep(1000);
1375 if (atomic_read(&instance->fw_outstanding)) {
1377 * Send signal to FW to stop processing any pending cmds.
1378 * The controller will be taken offline by the OS now.
1380 if ((instance->pdev->device ==
1381 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1382 (instance->pdev->device ==
1383 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1384 writel(MFI_STOP_ADP,
1385 &instance->reg_set->reserved_0[0]);
1386 } else {
1387 writel(MFI_STOP_ADP,
1388 &instance->reg_set->inbound_doorbell);
1390 megasas_dump_pending_frames(instance);
1391 instance->hw_crit_error = 1;
1392 return FAILED;
1395 return SUCCESS;
1399 * megasas_generic_reset - Generic reset routine
1400 * @scmd: Mid-layer SCSI command
1402 * This routine implements a generic reset handler for device, bus and host
1403 * reset requests. Device, bus and host specific reset handlers can use this
1404 * function after they do their specific tasks.
1406 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1408 int ret_val;
1409 struct megasas_instance *instance;
1411 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1413 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1414 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1416 if (instance->hw_crit_error) {
1417 printk(KERN_ERR "megasas: cannot recover from previous reset "
1418 "failures\n");
1419 return FAILED;
1422 ret_val = megasas_wait_for_outstanding(instance);
1423 if (ret_val == SUCCESS)
1424 printk(KERN_NOTICE "megasas: reset successful \n");
1425 else
1426 printk(KERN_ERR "megasas: failed to do reset\n");
1428 return ret_val;
1432 * megasas_reset_timer - quiesce the adapter if required
1433 * @scmd: scsi cmnd
1435 * Sets the FW busy flag and reduces the host->can_queue if the
1436 * cmd has not been completed within the timeout period.
1438 static enum
1439 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1441 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1442 struct megasas_instance *instance;
1443 unsigned long flags;
1445 if (time_after(jiffies, scmd->jiffies_at_alloc +
1446 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1447 return BLK_EH_NOT_HANDLED;
1450 instance = cmd->instance;
1451 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1452 /* FW is busy, throttle IO */
1453 spin_lock_irqsave(instance->host->host_lock, flags);
1455 instance->host->can_queue = 16;
1456 instance->last_time = jiffies;
1457 instance->flag |= MEGASAS_FW_BUSY;
1459 spin_unlock_irqrestore(instance->host->host_lock, flags);
1461 return BLK_EH_RESET_TIMER;
1465 * megasas_reset_device - Device reset handler entry point
1467 static int megasas_reset_device(struct scsi_cmnd *scmd)
1469 int ret;
1472 * First wait for all commands to complete
1474 ret = megasas_generic_reset(scmd);
1476 return ret;
1480 * megasas_reset_bus_host - Bus & host reset handler entry point
1482 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1484 int ret;
1487 * First wait for all commands to complete
1489 ret = megasas_generic_reset(scmd);
1491 return ret;
1495 * megasas_bios_param - Returns disk geometry for a disk
1496 * @sdev: device handle
1497 * @bdev: block device
1498 * @capacity: drive capacity
1499 * @geom: geometry parameters
1501 static int
1502 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1503 sector_t capacity, int geom[])
1505 int heads;
1506 int sectors;
1507 sector_t cylinders;
1508 unsigned long tmp;
1509 /* Default heads (64) & sectors (32) */
1510 heads = 64;
1511 sectors = 32;
1513 tmp = heads * sectors;
1514 cylinders = capacity;
1516 sector_div(cylinders, tmp);
1519 * Handle extended translation size for logical drives > 1Gb
1522 if (capacity >= 0x200000) {
1523 heads = 255;
1524 sectors = 63;
1525 tmp = heads*sectors;
1526 cylinders = capacity;
1527 sector_div(cylinders, tmp);
1530 geom[0] = heads;
1531 geom[1] = sectors;
1532 geom[2] = cylinders;
1534 return 0;
1537 static void megasas_aen_polling(struct work_struct *work);
1540 * megasas_service_aen - Processes an event notification
1541 * @instance: Adapter soft state
1542 * @cmd: AEN command completed by the ISR
1544 * For AEN, driver sends a command down to FW that is held by the FW till an
1545 * event occurs. When an event of interest occurs, FW completes the command
1546 * that it was previously holding.
1548 * This routines sends SIGIO signal to processes that have registered with the
1549 * driver for AEN.
1551 static void
1552 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1554 unsigned long flags;
1556 * Don't signal app if it is just an aborted previously registered aen
1558 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1559 spin_lock_irqsave(&poll_aen_lock, flags);
1560 megasas_poll_wait_aen = 1;
1561 spin_unlock_irqrestore(&poll_aen_lock, flags);
1562 wake_up(&megasas_poll_wait);
1563 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1565 else
1566 cmd->abort_aen = 0;
1568 instance->aen_cmd = NULL;
1569 megasas_return_cmd(instance, cmd);
1571 if (instance->unload == 0) {
1572 struct megasas_aen_event *ev;
1573 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1574 if (!ev) {
1575 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1576 } else {
1577 ev->instance = instance;
1578 instance->ev = ev;
1579 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1580 schedule_delayed_work(
1581 (struct delayed_work *)&ev->hotplug_work, 0);
1587 * Scsi host template for megaraid_sas driver
1589 static struct scsi_host_template megasas_template = {
1591 .module = THIS_MODULE,
1592 .name = "LSI SAS based MegaRAID driver",
1593 .proc_name = "megaraid_sas",
1594 .slave_configure = megasas_slave_configure,
1595 .slave_alloc = megasas_slave_alloc,
1596 .queuecommand = megasas_queue_command,
1597 .eh_device_reset_handler = megasas_reset_device,
1598 .eh_bus_reset_handler = megasas_reset_bus_host,
1599 .eh_host_reset_handler = megasas_reset_bus_host,
1600 .eh_timed_out = megasas_reset_timer,
1601 .bios_param = megasas_bios_param,
1602 .use_clustering = ENABLE_CLUSTERING,
1606 * megasas_complete_int_cmd - Completes an internal command
1607 * @instance: Adapter soft state
1608 * @cmd: Command to be completed
1610 * The megasas_issue_blocked_cmd() function waits for a command to complete
1611 * after it issues a command. This function wakes up that waiting routine by
1612 * calling wake_up() on the wait queue.
1614 static void
1615 megasas_complete_int_cmd(struct megasas_instance *instance,
1616 struct megasas_cmd *cmd)
1618 cmd->cmd_status = cmd->frame->io.cmd_status;
1620 if (cmd->cmd_status == ENODATA) {
1621 cmd->cmd_status = 0;
1623 wake_up(&instance->int_cmd_wait_q);
1627 * megasas_complete_abort - Completes aborting a command
1628 * @instance: Adapter soft state
1629 * @cmd: Cmd that was issued to abort another cmd
1631 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
1632 * after it issues an abort on a previously issued command. This function
1633 * wakes up all functions waiting on the same wait queue.
1635 static void
1636 megasas_complete_abort(struct megasas_instance *instance,
1637 struct megasas_cmd *cmd)
1639 if (cmd->sync_cmd) {
1640 cmd->sync_cmd = 0;
1641 cmd->cmd_status = 0;
1642 wake_up(&instance->abort_cmd_wait_q);
1645 return;
1649 * megasas_complete_cmd - Completes a command
1650 * @instance: Adapter soft state
1651 * @cmd: Command to be completed
1652 * @alt_status: If non-zero, use this value as status to
1653 * SCSI mid-layer instead of the value returned
1654 * by the FW. This should be used if caller wants
1655 * an alternate status (as in the case of aborted
1656 * commands)
1658 static void
1659 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
1660 u8 alt_status)
1662 int exception = 0;
1663 struct megasas_header *hdr = &cmd->frame->hdr;
1664 unsigned long flags;
1666 if (cmd->scmd)
1667 cmd->scmd->SCp.ptr = NULL;
1669 switch (hdr->cmd) {
1671 case MFI_CMD_PD_SCSI_IO:
1672 case MFI_CMD_LD_SCSI_IO:
1675 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
1676 * issued either through an IO path or an IOCTL path. If it
1677 * was via IOCTL, we will send it to internal completion.
1679 if (cmd->sync_cmd) {
1680 cmd->sync_cmd = 0;
1681 megasas_complete_int_cmd(instance, cmd);
1682 break;
1685 case MFI_CMD_LD_READ:
1686 case MFI_CMD_LD_WRITE:
1688 if (alt_status) {
1689 cmd->scmd->result = alt_status << 16;
1690 exception = 1;
1693 if (exception) {
1695 atomic_dec(&instance->fw_outstanding);
1697 scsi_dma_unmap(cmd->scmd);
1698 cmd->scmd->scsi_done(cmd->scmd);
1699 megasas_return_cmd(instance, cmd);
1701 break;
1704 switch (hdr->cmd_status) {
1706 case MFI_STAT_OK:
1707 cmd->scmd->result = DID_OK << 16;
1708 break;
1710 case MFI_STAT_SCSI_IO_FAILED:
1711 case MFI_STAT_LD_INIT_IN_PROGRESS:
1712 cmd->scmd->result =
1713 (DID_ERROR << 16) | hdr->scsi_status;
1714 break;
1716 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1718 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1720 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1721 memset(cmd->scmd->sense_buffer, 0,
1722 SCSI_SENSE_BUFFERSIZE);
1723 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1724 hdr->sense_len);
1726 cmd->scmd->result |= DRIVER_SENSE << 24;
1729 break;
1731 case MFI_STAT_LD_OFFLINE:
1732 case MFI_STAT_DEVICE_NOT_FOUND:
1733 cmd->scmd->result = DID_BAD_TARGET << 16;
1734 break;
1736 default:
1737 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1738 hdr->cmd_status);
1739 cmd->scmd->result = DID_ERROR << 16;
1740 break;
1743 atomic_dec(&instance->fw_outstanding);
1745 scsi_dma_unmap(cmd->scmd);
1746 cmd->scmd->scsi_done(cmd->scmd);
1747 megasas_return_cmd(instance, cmd);
1749 break;
1751 case MFI_CMD_SMP:
1752 case MFI_CMD_STP:
1753 case MFI_CMD_DCMD:
1754 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
1755 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
1756 spin_lock_irqsave(&poll_aen_lock, flags);
1757 megasas_poll_wait_aen = 0;
1758 spin_unlock_irqrestore(&poll_aen_lock, flags);
1762 * See if got an event notification
1764 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1765 megasas_service_aen(instance, cmd);
1766 else
1767 megasas_complete_int_cmd(instance, cmd);
1769 break;
1771 case MFI_CMD_ABORT:
1773 * Cmd issued to abort another cmd returned
1775 megasas_complete_abort(instance, cmd);
1776 break;
1778 default:
1779 printk("megasas: Unknown command completed! [0x%X]\n",
1780 hdr->cmd);
1781 break;
1786 * megasas_deplete_reply_queue - Processes all completed commands
1787 * @instance: Adapter soft state
1788 * @alt_status: Alternate status to be returned to
1789 * SCSI mid-layer instead of the status
1790 * returned by the FW
1792 static int
1793 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1796 * Check if it is our interrupt
1797 * Clear the interrupt
1799 if(instance->instancet->clear_intr(instance->reg_set))
1800 return IRQ_NONE;
1802 if (instance->hw_crit_error)
1803 goto out_done;
1805 * Schedule the tasklet for cmd completion
1807 tasklet_schedule(&instance->isr_tasklet);
1808 out_done:
1809 return IRQ_HANDLED;
1813 * megasas_isr - isr entry point
1815 static irqreturn_t megasas_isr(int irq, void *devp)
1817 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1818 DID_OK);
1822 * megasas_transition_to_ready - Move the FW to READY state
1823 * @instance: Adapter soft state
1825 * During the initialization, FW passes can potentially be in any one of
1826 * several possible states. If the FW in operational, waiting-for-handshake
1827 * states, driver must take steps to bring it to ready state. Otherwise, it
1828 * has to wait for the ready state.
1830 static int
1831 megasas_transition_to_ready(struct megasas_instance* instance)
1833 int i;
1834 u8 max_wait;
1835 u32 fw_state;
1836 u32 cur_state;
1837 u32 abs_state, curr_abs_state;
1839 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
1841 if (fw_state != MFI_STATE_READY)
1842 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1843 " state\n");
1845 while (fw_state != MFI_STATE_READY) {
1847 abs_state =
1848 instance->instancet->read_fw_status_reg(instance->reg_set);
1850 switch (fw_state) {
1852 case MFI_STATE_FAULT:
1854 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1855 return -ENODEV;
1857 case MFI_STATE_WAIT_HANDSHAKE:
1859 * Set the CLR bit in inbound doorbell
1861 if ((instance->pdev->device ==
1862 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1863 (instance->pdev->device ==
1864 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1866 writel(
1867 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1868 &instance->reg_set->reserved_0[0]);
1869 } else {
1870 writel(
1871 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
1872 &instance->reg_set->inbound_doorbell);
1875 max_wait = MEGASAS_RESET_WAIT_TIME;
1876 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1877 break;
1879 case MFI_STATE_BOOT_MESSAGE_PENDING:
1880 if ((instance->pdev->device ==
1881 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1882 (instance->pdev->device ==
1883 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1884 writel(MFI_INIT_HOTPLUG,
1885 &instance->reg_set->reserved_0[0]);
1886 } else
1887 writel(MFI_INIT_HOTPLUG,
1888 &instance->reg_set->inbound_doorbell);
1890 max_wait = MEGASAS_RESET_WAIT_TIME;
1891 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
1892 break;
1894 case MFI_STATE_OPERATIONAL:
1896 * Bring it to READY state; assuming max wait 10 secs
1898 instance->instancet->disable_intr(instance->reg_set);
1899 if ((instance->pdev->device ==
1900 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1901 (instance->pdev->device ==
1902 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1903 writel(MFI_RESET_FLAGS,
1904 &instance->reg_set->reserved_0[0]);
1905 } else
1906 writel(MFI_RESET_FLAGS,
1907 &instance->reg_set->inbound_doorbell);
1909 max_wait = MEGASAS_RESET_WAIT_TIME;
1910 cur_state = MFI_STATE_OPERATIONAL;
1911 break;
1913 case MFI_STATE_UNDEFINED:
1915 * This state should not last for more than 2 seconds
1917 max_wait = MEGASAS_RESET_WAIT_TIME;
1918 cur_state = MFI_STATE_UNDEFINED;
1919 break;
1921 case MFI_STATE_BB_INIT:
1922 max_wait = MEGASAS_RESET_WAIT_TIME;
1923 cur_state = MFI_STATE_BB_INIT;
1924 break;
1926 case MFI_STATE_FW_INIT:
1927 max_wait = MEGASAS_RESET_WAIT_TIME;
1928 cur_state = MFI_STATE_FW_INIT;
1929 break;
1931 case MFI_STATE_FW_INIT_2:
1932 max_wait = MEGASAS_RESET_WAIT_TIME;
1933 cur_state = MFI_STATE_FW_INIT_2;
1934 break;
1936 case MFI_STATE_DEVICE_SCAN:
1937 max_wait = MEGASAS_RESET_WAIT_TIME;
1938 cur_state = MFI_STATE_DEVICE_SCAN;
1939 break;
1941 case MFI_STATE_FLUSH_CACHE:
1942 max_wait = MEGASAS_RESET_WAIT_TIME;
1943 cur_state = MFI_STATE_FLUSH_CACHE;
1944 break;
1946 default:
1947 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1948 fw_state);
1949 return -ENODEV;
1953 * The cur_state should not last for more than max_wait secs
1955 for (i = 0; i < (max_wait * 1000); i++) {
1956 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1957 MFI_STATE_MASK ;
1958 curr_abs_state =
1959 instance->instancet->read_fw_status_reg(instance->reg_set);
1961 if (abs_state == curr_abs_state) {
1962 msleep(1);
1963 } else
1964 break;
1968 * Return error if fw_state hasn't changed after max_wait
1970 if (curr_abs_state == abs_state) {
1971 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1972 "in %d secs\n", fw_state, max_wait);
1973 return -ENODEV;
1976 printk(KERN_INFO "megasas: FW now in Ready state\n");
1978 return 0;
1982 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1983 * @instance: Adapter soft state
1985 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1987 int i;
1988 u32 max_cmd = instance->max_fw_cmds;
1989 struct megasas_cmd *cmd;
1991 if (!instance->frame_dma_pool)
1992 return;
1995 * Return all frames to pool
1997 for (i = 0; i < max_cmd; i++) {
1999 cmd = instance->cmd_list[i];
2001 if (cmd->frame)
2002 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2003 cmd->frame_phys_addr);
2005 if (cmd->sense)
2006 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2007 cmd->sense_phys_addr);
2011 * Now destroy the pool itself
2013 pci_pool_destroy(instance->frame_dma_pool);
2014 pci_pool_destroy(instance->sense_dma_pool);
2016 instance->frame_dma_pool = NULL;
2017 instance->sense_dma_pool = NULL;
2021 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2022 * @instance: Adapter soft state
2024 * Each command packet has an embedded DMA memory buffer that is used for
2025 * filling MFI frame and the SG list that immediately follows the frame. This
2026 * function creates those DMA memory buffers for each command packet by using
2027 * PCI pool facility.
2029 static int megasas_create_frame_pool(struct megasas_instance *instance)
2031 int i;
2032 u32 max_cmd;
2033 u32 sge_sz;
2034 u32 sgl_sz;
2035 u32 total_sz;
2036 u32 frame_count;
2037 struct megasas_cmd *cmd;
2039 max_cmd = instance->max_fw_cmds;
2042 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2043 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2045 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2046 sizeof(struct megasas_sge32);
2048 if (instance->flag_ieee) {
2049 sge_sz = sizeof(struct megasas_sge_skinny);
2053 * Calculated the number of 64byte frames required for SGL
2055 sgl_sz = sge_sz * instance->max_num_sge;
2056 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2059 * We need one extra frame for the MFI command
2061 frame_count++;
2063 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2065 * Use DMA pool facility provided by PCI layer
2067 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2068 instance->pdev, total_sz, 64,
2071 if (!instance->frame_dma_pool) {
2072 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2073 return -ENOMEM;
2076 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2077 instance->pdev, 128, 4, 0);
2079 if (!instance->sense_dma_pool) {
2080 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2082 pci_pool_destroy(instance->frame_dma_pool);
2083 instance->frame_dma_pool = NULL;
2085 return -ENOMEM;
2089 * Allocate and attach a frame to each of the commands in cmd_list.
2090 * By making cmd->index as the context instead of the &cmd, we can
2091 * always use 32bit context regardless of the architecture
2093 for (i = 0; i < max_cmd; i++) {
2095 cmd = instance->cmd_list[i];
2097 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2098 GFP_KERNEL, &cmd->frame_phys_addr);
2100 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2101 GFP_KERNEL, &cmd->sense_phys_addr);
2104 * megasas_teardown_frame_pool() takes care of freeing
2105 * whatever has been allocated
2107 if (!cmd->frame || !cmd->sense) {
2108 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2109 megasas_teardown_frame_pool(instance);
2110 return -ENOMEM;
2113 cmd->frame->io.context = cmd->index;
2114 cmd->frame->io.pad_0 = 0;
2117 return 0;
2121 * megasas_free_cmds - Free all the cmds in the free cmd pool
2122 * @instance: Adapter soft state
2124 static void megasas_free_cmds(struct megasas_instance *instance)
2126 int i;
2127 /* First free the MFI frame pool */
2128 megasas_teardown_frame_pool(instance);
2130 /* Free all the commands in the cmd_list */
2131 for (i = 0; i < instance->max_fw_cmds; i++)
2132 kfree(instance->cmd_list[i]);
2134 /* Free the cmd_list buffer itself */
2135 kfree(instance->cmd_list);
2136 instance->cmd_list = NULL;
2138 INIT_LIST_HEAD(&instance->cmd_pool);
2142 * megasas_alloc_cmds - Allocates the command packets
2143 * @instance: Adapter soft state
2145 * Each command that is issued to the FW, whether IO commands from the OS or
2146 * internal commands like IOCTLs, are wrapped in local data structure called
2147 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2148 * the FW.
2150 * Each frame has a 32-bit field called context (tag). This context is used
2151 * to get back the megasas_cmd from the frame when a frame gets completed in
2152 * the ISR. Typically the address of the megasas_cmd itself would be used as
2153 * the context. But we wanted to keep the differences between 32 and 64 bit
2154 * systems to the mininum. We always use 32 bit integers for the context. In
2155 * this driver, the 32 bit values are the indices into an array cmd_list.
2156 * This array is used only to look up the megasas_cmd given the context. The
2157 * free commands themselves are maintained in a linked list called cmd_pool.
2159 static int megasas_alloc_cmds(struct megasas_instance *instance)
2161 int i;
2162 int j;
2163 u32 max_cmd;
2164 struct megasas_cmd *cmd;
2166 max_cmd = instance->max_fw_cmds;
2169 * instance->cmd_list is an array of struct megasas_cmd pointers.
2170 * Allocate the dynamic array first and then allocate individual
2171 * commands.
2173 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2175 if (!instance->cmd_list) {
2176 printk(KERN_DEBUG "megasas: out of memory\n");
2177 return -ENOMEM;
2181 for (i = 0; i < max_cmd; i++) {
2182 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2183 GFP_KERNEL);
2185 if (!instance->cmd_list[i]) {
2187 for (j = 0; j < i; j++)
2188 kfree(instance->cmd_list[j]);
2190 kfree(instance->cmd_list);
2191 instance->cmd_list = NULL;
2193 return -ENOMEM;
2198 * Add all the commands to command pool (instance->cmd_pool)
2200 for (i = 0; i < max_cmd; i++) {
2201 cmd = instance->cmd_list[i];
2202 memset(cmd, 0, sizeof(struct megasas_cmd));
2203 cmd->index = i;
2204 cmd->instance = instance;
2206 list_add_tail(&cmd->list, &instance->cmd_pool);
2210 * Create a frame pool and assign one frame to each cmd
2212 if (megasas_create_frame_pool(instance)) {
2213 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2214 megasas_free_cmds(instance);
2217 return 0;
2221 * megasas_get_pd_list_info - Returns FW's pd_list structure
2222 * @instance: Adapter soft state
2223 * @pd_list: pd_list structure
2225 * Issues an internal command (DCMD) to get the FW's controller PD
2226 * list structure. This information is mainly used to find out SYSTEM
2227 * supported by the FW.
2229 static int
2230 megasas_get_pd_list(struct megasas_instance *instance)
2232 int ret = 0, pd_index = 0;
2233 struct megasas_cmd *cmd;
2234 struct megasas_dcmd_frame *dcmd;
2235 struct MR_PD_LIST *ci;
2236 struct MR_PD_ADDRESS *pd_addr;
2237 dma_addr_t ci_h = 0;
2239 cmd = megasas_get_cmd(instance);
2241 if (!cmd) {
2242 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2243 return -ENOMEM;
2246 dcmd = &cmd->frame->dcmd;
2248 ci = pci_alloc_consistent(instance->pdev,
2249 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2251 if (!ci) {
2252 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2253 megasas_return_cmd(instance, cmd);
2254 return -ENOMEM;
2257 memset(ci, 0, sizeof(*ci));
2258 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2260 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2261 dcmd->mbox.b[1] = 0;
2262 dcmd->cmd = MFI_CMD_DCMD;
2263 dcmd->cmd_status = 0xFF;
2264 dcmd->sge_count = 1;
2265 dcmd->flags = MFI_FRAME_DIR_READ;
2266 dcmd->timeout = 0;
2267 dcmd->pad_0 = 0;
2268 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2269 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2270 dcmd->sgl.sge32[0].phys_addr = ci_h;
2271 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2273 if (!megasas_issue_polled(instance, cmd)) {
2274 ret = 0;
2275 } else {
2276 ret = -1;
2280 * the following function will get the instance PD LIST.
2283 pd_addr = ci->addr;
2285 if ( ret == 0 &&
2286 (ci->count <
2287 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2289 memset(instance->pd_list, 0,
2290 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2292 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2294 instance->pd_list[pd_addr->deviceId].tid =
2295 pd_addr->deviceId;
2296 instance->pd_list[pd_addr->deviceId].driveType =
2297 pd_addr->scsiDevType;
2298 instance->pd_list[pd_addr->deviceId].driveState =
2299 MR_PD_STATE_SYSTEM;
2300 pd_addr++;
2304 pci_free_consistent(instance->pdev,
2305 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2306 ci, ci_h);
2307 megasas_return_cmd(instance, cmd);
2309 return ret;
2313 * megasas_get_ld_list_info - Returns FW's ld_list structure
2314 * @instance: Adapter soft state
2315 * @ld_list: ld_list structure
2317 * Issues an internal command (DCMD) to get the FW's controller PD
2318 * list structure. This information is mainly used to find out SYSTEM
2319 * supported by the FW.
2321 static int
2322 megasas_get_ld_list(struct megasas_instance *instance)
2324 int ret = 0, ld_index = 0, ids = 0;
2325 struct megasas_cmd *cmd;
2326 struct megasas_dcmd_frame *dcmd;
2327 struct MR_LD_LIST *ci;
2328 dma_addr_t ci_h = 0;
2330 cmd = megasas_get_cmd(instance);
2332 if (!cmd) {
2333 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2334 return -ENOMEM;
2337 dcmd = &cmd->frame->dcmd;
2339 ci = pci_alloc_consistent(instance->pdev,
2340 sizeof(struct MR_LD_LIST),
2341 &ci_h);
2343 if (!ci) {
2344 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2345 megasas_return_cmd(instance, cmd);
2346 return -ENOMEM;
2349 memset(ci, 0, sizeof(*ci));
2350 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2352 dcmd->cmd = MFI_CMD_DCMD;
2353 dcmd->cmd_status = 0xFF;
2354 dcmd->sge_count = 1;
2355 dcmd->flags = MFI_FRAME_DIR_READ;
2356 dcmd->timeout = 0;
2357 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2358 dcmd->opcode = MR_DCMD_LD_GET_LIST;
2359 dcmd->sgl.sge32[0].phys_addr = ci_h;
2360 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
2361 dcmd->pad_0 = 0;
2363 if (!megasas_issue_polled(instance, cmd)) {
2364 ret = 0;
2365 } else {
2366 ret = -1;
2369 /* the following function will get the instance PD LIST */
2371 if ((ret == 0) && (ci->ldCount < MAX_LOGICAL_DRIVES)) {
2372 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2374 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
2375 if (ci->ldList[ld_index].state != 0) {
2376 ids = ci->ldList[ld_index].ref.targetId;
2377 instance->ld_ids[ids] =
2378 ci->ldList[ld_index].ref.targetId;
2383 pci_free_consistent(instance->pdev,
2384 sizeof(struct MR_LD_LIST),
2386 ci_h);
2388 megasas_return_cmd(instance, cmd);
2389 return ret;
2393 * megasas_get_controller_info - Returns FW's controller structure
2394 * @instance: Adapter soft state
2395 * @ctrl_info: Controller information structure
2397 * Issues an internal command (DCMD) to get the FW's controller structure.
2398 * This information is mainly used to find out the maximum IO transfer per
2399 * command supported by the FW.
2401 static int
2402 megasas_get_ctrl_info(struct megasas_instance *instance,
2403 struct megasas_ctrl_info *ctrl_info)
2405 int ret = 0;
2406 struct megasas_cmd *cmd;
2407 struct megasas_dcmd_frame *dcmd;
2408 struct megasas_ctrl_info *ci;
2409 dma_addr_t ci_h = 0;
2411 cmd = megasas_get_cmd(instance);
2413 if (!cmd) {
2414 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
2415 return -ENOMEM;
2418 dcmd = &cmd->frame->dcmd;
2420 ci = pci_alloc_consistent(instance->pdev,
2421 sizeof(struct megasas_ctrl_info), &ci_h);
2423 if (!ci) {
2424 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
2425 megasas_return_cmd(instance, cmd);
2426 return -ENOMEM;
2429 memset(ci, 0, sizeof(*ci));
2430 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2432 dcmd->cmd = MFI_CMD_DCMD;
2433 dcmd->cmd_status = 0xFF;
2434 dcmd->sge_count = 1;
2435 dcmd->flags = MFI_FRAME_DIR_READ;
2436 dcmd->timeout = 0;
2437 dcmd->pad_0 = 0;
2438 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
2439 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
2440 dcmd->sgl.sge32[0].phys_addr = ci_h;
2441 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
2443 if (!megasas_issue_polled(instance, cmd)) {
2444 ret = 0;
2445 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
2446 } else {
2447 ret = -1;
2450 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
2451 ci, ci_h);
2453 megasas_return_cmd(instance, cmd);
2454 return ret;
2458 * megasas_issue_init_mfi - Initializes the FW
2459 * @instance: Adapter soft state
2461 * Issues the INIT MFI cmd
2463 static int
2464 megasas_issue_init_mfi(struct megasas_instance *instance)
2466 u32 context;
2468 struct megasas_cmd *cmd;
2470 struct megasas_init_frame *init_frame;
2471 struct megasas_init_queue_info *initq_info;
2472 dma_addr_t init_frame_h;
2473 dma_addr_t initq_info_h;
2476 * Prepare a init frame. Note the init frame points to queue info
2477 * structure. Each frame has SGL allocated after first 64 bytes. For
2478 * this frame - since we don't need any SGL - we use SGL's space as
2479 * queue info structure
2481 * We will not get a NULL command below. We just created the pool.
2483 cmd = megasas_get_cmd(instance);
2485 init_frame = (struct megasas_init_frame *)cmd->frame;
2486 initq_info = (struct megasas_init_queue_info *)
2487 ((unsigned long)init_frame + 64);
2489 init_frame_h = cmd->frame_phys_addr;
2490 initq_info_h = init_frame_h + 64;
2492 context = init_frame->context;
2493 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
2494 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
2495 init_frame->context = context;
2497 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
2498 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
2500 initq_info->producer_index_phys_addr_lo = instance->producer_h;
2501 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
2503 init_frame->cmd = MFI_CMD_INIT;
2504 init_frame->cmd_status = 0xFF;
2505 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
2507 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
2510 * disable the intr before firing the init frame to FW
2512 instance->instancet->disable_intr(instance->reg_set);
2515 * Issue the init frame in polled mode
2518 if (megasas_issue_polled(instance, cmd)) {
2519 printk(KERN_ERR "megasas: Failed to init firmware\n");
2520 megasas_return_cmd(instance, cmd);
2521 goto fail_fw_init;
2524 megasas_return_cmd(instance, cmd);
2526 return 0;
2528 fail_fw_init:
2529 return -EINVAL;
2533 * megasas_start_timer - Initializes a timer object
2534 * @instance: Adapter soft state
2535 * @timer: timer object to be initialized
2536 * @fn: timer function
2537 * @interval: time interval between timer function call
2539 static inline void
2540 megasas_start_timer(struct megasas_instance *instance,
2541 struct timer_list *timer,
2542 void *fn, unsigned long interval)
2544 init_timer(timer);
2545 timer->expires = jiffies + interval;
2546 timer->data = (unsigned long)instance;
2547 timer->function = fn;
2548 add_timer(timer);
2552 * megasas_io_completion_timer - Timer fn
2553 * @instance_addr: Address of adapter soft state
2555 * Schedules tasklet for cmd completion
2556 * if poll_mode_io is set
2558 static void
2559 megasas_io_completion_timer(unsigned long instance_addr)
2561 struct megasas_instance *instance =
2562 (struct megasas_instance *)instance_addr;
2564 if (atomic_read(&instance->fw_outstanding))
2565 tasklet_schedule(&instance->isr_tasklet);
2567 /* Restart timer */
2568 if (poll_mode_io)
2569 mod_timer(&instance->io_completion_timer,
2570 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
2574 * megasas_init_mfi - Initializes the FW
2575 * @instance: Adapter soft state
2577 * This is the main function for initializing MFI firmware.
2579 static int megasas_init_mfi(struct megasas_instance *instance)
2581 u32 context_sz;
2582 u32 reply_q_sz;
2583 u32 max_sectors_1;
2584 u32 max_sectors_2;
2585 u32 tmp_sectors;
2586 struct megasas_register_set __iomem *reg_set;
2587 struct megasas_ctrl_info *ctrl_info;
2589 * Map the message registers
2591 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
2592 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2593 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2594 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
2595 instance->base_addr = pci_resource_start(instance->pdev, 1);
2596 } else {
2597 instance->base_addr = pci_resource_start(instance->pdev, 0);
2600 if (pci_request_selected_regions(instance->pdev,
2601 pci_select_bars(instance->pdev, IORESOURCE_MEM),
2602 "megasas: LSI")) {
2603 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
2604 return -EBUSY;
2607 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
2609 if (!instance->reg_set) {
2610 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
2611 goto fail_ioremap;
2614 reg_set = instance->reg_set;
2616 switch(instance->pdev->device)
2618 case PCI_DEVICE_ID_LSI_SAS1078R:
2619 case PCI_DEVICE_ID_LSI_SAS1078DE:
2620 instance->instancet = &megasas_instance_template_ppc;
2621 break;
2622 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
2623 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
2624 instance->instancet = &megasas_instance_template_gen2;
2625 break;
2626 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
2627 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
2628 instance->instancet = &megasas_instance_template_skinny;
2629 break;
2630 case PCI_DEVICE_ID_LSI_SAS1064R:
2631 case PCI_DEVICE_ID_DELL_PERC5:
2632 default:
2633 instance->instancet = &megasas_instance_template_xscale;
2634 break;
2638 * We expect the FW state to be READY
2640 if (megasas_transition_to_ready(instance))
2641 goto fail_ready_state;
2644 * Get various operational parameters from status register
2646 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
2648 * Reduce the max supported cmds by 1. This is to ensure that the
2649 * reply_q_sz (1 more than the max cmd that driver may send)
2650 * does not exceed max cmds that the FW can support
2652 instance->max_fw_cmds = instance->max_fw_cmds-1;
2653 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
2654 0x10;
2656 * Create a pool of commands
2658 if (megasas_alloc_cmds(instance))
2659 goto fail_alloc_cmds;
2662 * Allocate memory for reply queue. Length of reply queue should
2663 * be _one_ more than the maximum commands handled by the firmware.
2665 * Note: When FW completes commands, it places corresponding contex
2666 * values in this circular reply queue. This circular queue is a fairly
2667 * typical producer-consumer queue. FW is the producer (of completed
2668 * commands) and the driver is the consumer.
2670 context_sz = sizeof(u32);
2671 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
2673 instance->reply_queue = pci_alloc_consistent(instance->pdev,
2674 reply_q_sz,
2675 &instance->reply_queue_h);
2677 if (!instance->reply_queue) {
2678 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
2679 goto fail_reply_queue;
2682 if (megasas_issue_init_mfi(instance))
2683 goto fail_fw_init;
2685 memset(instance->pd_list, 0 ,
2686 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
2687 megasas_get_pd_list(instance);
2689 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2690 megasas_get_ld_list(instance);
2692 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
2695 * Compute the max allowed sectors per IO: The controller info has two
2696 * limits on max sectors. Driver should use the minimum of these two.
2698 * 1 << stripe_sz_ops.min = max sectors per strip
2700 * Note that older firmwares ( < FW ver 30) didn't report information
2701 * to calculate max_sectors_1. So the number ended up as zero always.
2703 tmp_sectors = 0;
2704 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
2706 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
2707 ctrl_info->max_strips_per_io;
2708 max_sectors_2 = ctrl_info->max_request_size;
2710 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
2713 instance->max_sectors_per_req = instance->max_num_sge *
2714 PAGE_SIZE / 512;
2715 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
2716 instance->max_sectors_per_req = tmp_sectors;
2718 kfree(ctrl_info);
2721 * Setup tasklet for cmd completion
2724 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
2725 (unsigned long)instance);
2727 /* Initialize the cmd completion timer */
2728 if (poll_mode_io)
2729 megasas_start_timer(instance, &instance->io_completion_timer,
2730 megasas_io_completion_timer,
2731 MEGASAS_COMPLETION_TIMER_INTERVAL);
2732 return 0;
2734 fail_fw_init:
2736 pci_free_consistent(instance->pdev, reply_q_sz,
2737 instance->reply_queue, instance->reply_queue_h);
2738 fail_reply_queue:
2739 megasas_free_cmds(instance);
2741 fail_alloc_cmds:
2742 fail_ready_state:
2743 iounmap(instance->reg_set);
2745 fail_ioremap:
2746 pci_release_selected_regions(instance->pdev,
2747 pci_select_bars(instance->pdev, IORESOURCE_MEM));
2749 return -EINVAL;
2753 * megasas_release_mfi - Reverses the FW initialization
2754 * @intance: Adapter soft state
2756 static void megasas_release_mfi(struct megasas_instance *instance)
2758 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
2760 pci_free_consistent(instance->pdev, reply_q_sz,
2761 instance->reply_queue, instance->reply_queue_h);
2763 megasas_free_cmds(instance);
2765 iounmap(instance->reg_set);
2767 pci_release_selected_regions(instance->pdev,
2768 pci_select_bars(instance->pdev, IORESOURCE_MEM));
2772 * megasas_get_seq_num - Gets latest event sequence numbers
2773 * @instance: Adapter soft state
2774 * @eli: FW event log sequence numbers information
2776 * FW maintains a log of all events in a non-volatile area. Upper layers would
2777 * usually find out the latest sequence number of the events, the seq number at
2778 * the boot etc. They would "read" all the events below the latest seq number
2779 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
2780 * number), they would subsribe to AEN (asynchronous event notification) and
2781 * wait for the events to happen.
2783 static int
2784 megasas_get_seq_num(struct megasas_instance *instance,
2785 struct megasas_evt_log_info *eli)
2787 struct megasas_cmd *cmd;
2788 struct megasas_dcmd_frame *dcmd;
2789 struct megasas_evt_log_info *el_info;
2790 dma_addr_t el_info_h = 0;
2792 cmd = megasas_get_cmd(instance);
2794 if (!cmd) {
2795 return -ENOMEM;
2798 dcmd = &cmd->frame->dcmd;
2799 el_info = pci_alloc_consistent(instance->pdev,
2800 sizeof(struct megasas_evt_log_info),
2801 &el_info_h);
2803 if (!el_info) {
2804 megasas_return_cmd(instance, cmd);
2805 return -ENOMEM;
2808 memset(el_info, 0, sizeof(*el_info));
2809 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2811 dcmd->cmd = MFI_CMD_DCMD;
2812 dcmd->cmd_status = 0x0;
2813 dcmd->sge_count = 1;
2814 dcmd->flags = MFI_FRAME_DIR_READ;
2815 dcmd->timeout = 0;
2816 dcmd->pad_0 = 0;
2817 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
2818 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
2819 dcmd->sgl.sge32[0].phys_addr = el_info_h;
2820 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
2822 megasas_issue_blocked_cmd(instance, cmd);
2825 * Copy the data back into callers buffer
2827 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
2829 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
2830 el_info, el_info_h);
2832 megasas_return_cmd(instance, cmd);
2834 return 0;
2838 * megasas_register_aen - Registers for asynchronous event notification
2839 * @instance: Adapter soft state
2840 * @seq_num: The starting sequence number
2841 * @class_locale: Class of the event
2843 * This function subscribes for AEN for events beyond the @seq_num. It requests
2844 * to be notified if and only if the event is of type @class_locale
2846 static int
2847 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
2848 u32 class_locale_word)
2850 int ret_val;
2851 struct megasas_cmd *cmd;
2852 struct megasas_dcmd_frame *dcmd;
2853 union megasas_evt_class_locale curr_aen;
2854 union megasas_evt_class_locale prev_aen;
2857 * If there an AEN pending already (aen_cmd), check if the
2858 * class_locale of that pending AEN is inclusive of the new
2859 * AEN request we currently have. If it is, then we don't have
2860 * to do anything. In other words, whichever events the current
2861 * AEN request is subscribing to, have already been subscribed
2862 * to.
2864 * If the old_cmd is _not_ inclusive, then we have to abort
2865 * that command, form a class_locale that is superset of both
2866 * old and current and re-issue to the FW
2869 curr_aen.word = class_locale_word;
2871 if (instance->aen_cmd) {
2873 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
2876 * A class whose enum value is smaller is inclusive of all
2877 * higher values. If a PROGRESS (= -1) was previously
2878 * registered, then a new registration requests for higher
2879 * classes need not be sent to FW. They are automatically
2880 * included.
2882 * Locale numbers don't have such hierarchy. They are bitmap
2883 * values
2885 if ((prev_aen.members.class <= curr_aen.members.class) &&
2886 !((prev_aen.members.locale & curr_aen.members.locale) ^
2887 curr_aen.members.locale)) {
2889 * Previously issued event registration includes
2890 * current request. Nothing to do.
2892 return 0;
2893 } else {
2894 curr_aen.members.locale |= prev_aen.members.locale;
2896 if (prev_aen.members.class < curr_aen.members.class)
2897 curr_aen.members.class = prev_aen.members.class;
2899 instance->aen_cmd->abort_aen = 1;
2900 ret_val = megasas_issue_blocked_abort_cmd(instance,
2901 instance->
2902 aen_cmd);
2904 if (ret_val) {
2905 printk(KERN_DEBUG "megasas: Failed to abort "
2906 "previous AEN command\n");
2907 return ret_val;
2912 cmd = megasas_get_cmd(instance);
2914 if (!cmd)
2915 return -ENOMEM;
2917 dcmd = &cmd->frame->dcmd;
2919 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
2922 * Prepare DCMD for aen registration
2924 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2926 dcmd->cmd = MFI_CMD_DCMD;
2927 dcmd->cmd_status = 0x0;
2928 dcmd->sge_count = 1;
2929 dcmd->flags = MFI_FRAME_DIR_READ;
2930 dcmd->timeout = 0;
2931 dcmd->pad_0 = 0;
2932 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
2933 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
2934 dcmd->mbox.w[0] = seq_num;
2935 dcmd->mbox.w[1] = curr_aen.word;
2936 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
2937 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
2939 if (instance->aen_cmd != NULL) {
2940 megasas_return_cmd(instance, cmd);
2941 return 0;
2945 * Store reference to the cmd used to register for AEN. When an
2946 * application wants us to register for AEN, we have to abort this
2947 * cmd and re-register with a new EVENT LOCALE supplied by that app
2949 instance->aen_cmd = cmd;
2952 * Issue the aen registration frame
2954 instance->instancet->fire_cmd(instance,
2955 cmd->frame_phys_addr, 0, instance->reg_set);
2957 return 0;
2961 * megasas_start_aen - Subscribes to AEN during driver load time
2962 * @instance: Adapter soft state
2964 static int megasas_start_aen(struct megasas_instance *instance)
2966 struct megasas_evt_log_info eli;
2967 union megasas_evt_class_locale class_locale;
2970 * Get the latest sequence number from FW
2972 memset(&eli, 0, sizeof(eli));
2974 if (megasas_get_seq_num(instance, &eli))
2975 return -1;
2978 * Register AEN with FW for latest sequence number plus 1
2980 class_locale.members.reserved = 0;
2981 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2982 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2984 return megasas_register_aen(instance, eli.newest_seq_num + 1,
2985 class_locale.word);
2989 * megasas_io_attach - Attaches this driver to SCSI mid-layer
2990 * @instance: Adapter soft state
2992 static int megasas_io_attach(struct megasas_instance *instance)
2994 struct Scsi_Host *host = instance->host;
2997 * Export parameters required by SCSI mid-layer
2999 host->irq = instance->pdev->irq;
3000 host->unique_id = instance->unique_id;
3001 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3002 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3003 host->can_queue =
3004 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3005 } else
3006 host->can_queue =
3007 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3008 host->this_id = instance->init_id;
3009 host->sg_tablesize = instance->max_num_sge;
3010 host->max_sectors = instance->max_sectors_per_req;
3011 host->cmd_per_lun = 128;
3012 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3013 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3014 host->max_lun = MEGASAS_MAX_LUN;
3015 host->max_cmd_len = 16;
3018 * Notify the mid-layer about the new controller
3020 if (scsi_add_host(host, &instance->pdev->dev)) {
3021 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3022 return -ENODEV;
3026 * Trigger SCSI to scan our drives
3028 scsi_scan_host(host);
3029 return 0;
3032 static int
3033 megasas_set_dma_mask(struct pci_dev *pdev)
3036 * All our contollers are capable of performing 64-bit DMA
3038 if (IS_DMA64) {
3039 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3041 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3042 goto fail_set_dma_mask;
3044 } else {
3045 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3046 goto fail_set_dma_mask;
3048 return 0;
3050 fail_set_dma_mask:
3051 return 1;
3055 * megasas_probe_one - PCI hotplug entry point
3056 * @pdev: PCI device structure
3057 * @id: PCI ids of supported hotplugged adapter
3059 static int __devinit
3060 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3062 int rval;
3063 struct Scsi_Host *host;
3064 struct megasas_instance *instance;
3067 * Announce PCI information
3069 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3070 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3071 pdev->subsystem_device);
3073 printk("bus %d:slot %d:func %d\n",
3074 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3077 * PCI prepping: enable device set bus mastering and dma mask
3079 rval = pci_enable_device_mem(pdev);
3081 if (rval) {
3082 return rval;
3085 pci_set_master(pdev);
3087 if (megasas_set_dma_mask(pdev))
3088 goto fail_set_dma_mask;
3090 host = scsi_host_alloc(&megasas_template,
3091 sizeof(struct megasas_instance));
3093 if (!host) {
3094 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3095 goto fail_alloc_instance;
3098 instance = (struct megasas_instance *)host->hostdata;
3099 memset(instance, 0, sizeof(*instance));
3101 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3102 &instance->producer_h);
3103 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3104 &instance->consumer_h);
3106 if (!instance->producer || !instance->consumer) {
3107 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3108 "producer, consumer\n");
3109 goto fail_alloc_dma_buf;
3112 *instance->producer = 0;
3113 *instance->consumer = 0;
3114 megasas_poll_wait_aen = 0;
3115 instance->flag_ieee = 0;
3116 instance->ev = NULL;
3118 instance->evt_detail = pci_alloc_consistent(pdev,
3119 sizeof(struct
3120 megasas_evt_detail),
3121 &instance->evt_detail_h);
3123 if (!instance->evt_detail) {
3124 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3125 "event detail structure\n");
3126 goto fail_alloc_dma_buf;
3130 * Initialize locks and queues
3132 INIT_LIST_HEAD(&instance->cmd_pool);
3134 atomic_set(&instance->fw_outstanding,0);
3136 init_waitqueue_head(&instance->int_cmd_wait_q);
3137 init_waitqueue_head(&instance->abort_cmd_wait_q);
3139 spin_lock_init(&instance->cmd_pool_lock);
3140 spin_lock_init(&instance->fire_lock);
3141 spin_lock_init(&instance->completion_lock);
3142 spin_lock_init(&poll_aen_lock);
3144 mutex_init(&instance->aen_mutex);
3147 * Initialize PCI related and misc parameters
3149 instance->pdev = pdev;
3150 instance->host = host;
3151 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3152 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3154 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3155 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3156 instance->flag_ieee = 1;
3157 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3158 } else
3159 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3161 megasas_dbg_lvl = 0;
3162 instance->flag = 0;
3163 instance->unload = 1;
3164 instance->last_time = 0;
3167 * Initialize MFI Firmware
3169 if (megasas_init_mfi(instance))
3170 goto fail_init_mfi;
3173 * Register IRQ
3175 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
3176 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3177 goto fail_irq;
3180 instance->instancet->enable_intr(instance->reg_set);
3183 * Store instance in PCI softstate
3185 pci_set_drvdata(pdev, instance);
3188 * Add this controller to megasas_mgmt_info structure so that it
3189 * can be exported to management applications
3191 megasas_mgmt_info.count++;
3192 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3193 megasas_mgmt_info.max_index++;
3196 * Initiate AEN (Asynchronous Event Notification)
3198 if (megasas_start_aen(instance)) {
3199 printk(KERN_DEBUG "megasas: start aen failed\n");
3200 goto fail_start_aen;
3204 * Register with SCSI mid-layer
3206 if (megasas_io_attach(instance))
3207 goto fail_io_attach;
3209 instance->unload = 0;
3210 return 0;
3212 fail_start_aen:
3213 fail_io_attach:
3214 megasas_mgmt_info.count--;
3215 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3216 megasas_mgmt_info.max_index--;
3218 pci_set_drvdata(pdev, NULL);
3219 instance->instancet->disable_intr(instance->reg_set);
3220 free_irq(instance->pdev->irq, instance);
3222 megasas_release_mfi(instance);
3224 fail_irq:
3225 fail_init_mfi:
3226 fail_alloc_dma_buf:
3227 if (instance->evt_detail)
3228 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3229 instance->evt_detail,
3230 instance->evt_detail_h);
3232 if (instance->producer)
3233 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3234 instance->producer_h);
3235 if (instance->consumer)
3236 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3237 instance->consumer_h);
3238 scsi_host_put(host);
3240 fail_alloc_instance:
3241 fail_set_dma_mask:
3242 pci_disable_device(pdev);
3244 return -ENODEV;
3248 * megasas_flush_cache - Requests FW to flush all its caches
3249 * @instance: Adapter soft state
3251 static void megasas_flush_cache(struct megasas_instance *instance)
3253 struct megasas_cmd *cmd;
3254 struct megasas_dcmd_frame *dcmd;
3256 cmd = megasas_get_cmd(instance);
3258 if (!cmd)
3259 return;
3261 dcmd = &cmd->frame->dcmd;
3263 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3265 dcmd->cmd = MFI_CMD_DCMD;
3266 dcmd->cmd_status = 0x0;
3267 dcmd->sge_count = 0;
3268 dcmd->flags = MFI_FRAME_DIR_NONE;
3269 dcmd->timeout = 0;
3270 dcmd->pad_0 = 0;
3271 dcmd->data_xfer_len = 0;
3272 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3273 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3275 megasas_issue_blocked_cmd(instance, cmd);
3277 megasas_return_cmd(instance, cmd);
3279 return;
3283 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3284 * @instance: Adapter soft state
3285 * @opcode: Shutdown/Hibernate
3287 static void megasas_shutdown_controller(struct megasas_instance *instance,
3288 u32 opcode)
3290 struct megasas_cmd *cmd;
3291 struct megasas_dcmd_frame *dcmd;
3293 cmd = megasas_get_cmd(instance);
3295 if (!cmd)
3296 return;
3298 if (instance->aen_cmd)
3299 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3301 dcmd = &cmd->frame->dcmd;
3303 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3305 dcmd->cmd = MFI_CMD_DCMD;
3306 dcmd->cmd_status = 0x0;
3307 dcmd->sge_count = 0;
3308 dcmd->flags = MFI_FRAME_DIR_NONE;
3309 dcmd->timeout = 0;
3310 dcmd->pad_0 = 0;
3311 dcmd->data_xfer_len = 0;
3312 dcmd->opcode = opcode;
3314 megasas_issue_blocked_cmd(instance, cmd);
3316 megasas_return_cmd(instance, cmd);
3318 return;
3321 #ifdef CONFIG_PM
3323 * megasas_suspend - driver suspend entry point
3324 * @pdev: PCI device structure
3325 * @state: PCI power state to suspend routine
3327 static int
3328 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
3330 struct Scsi_Host *host;
3331 struct megasas_instance *instance;
3333 instance = pci_get_drvdata(pdev);
3334 host = instance->host;
3335 instance->unload = 1;
3337 if (poll_mode_io)
3338 del_timer_sync(&instance->io_completion_timer);
3340 megasas_flush_cache(instance);
3341 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
3343 /* cancel the delayed work if this work still in queue */
3344 if (instance->ev != NULL) {
3345 struct megasas_aen_event *ev = instance->ev;
3346 cancel_delayed_work(
3347 (struct delayed_work *)&ev->hotplug_work);
3348 flush_scheduled_work();
3349 instance->ev = NULL;
3352 tasklet_kill(&instance->isr_tasklet);
3354 pci_set_drvdata(instance->pdev, instance);
3355 instance->instancet->disable_intr(instance->reg_set);
3356 free_irq(instance->pdev->irq, instance);
3358 pci_save_state(pdev);
3359 pci_disable_device(pdev);
3361 pci_set_power_state(pdev, pci_choose_state(pdev, state));
3363 return 0;
3367 * megasas_resume- driver resume entry point
3368 * @pdev: PCI device structure
3370 static int
3371 megasas_resume(struct pci_dev *pdev)
3373 int rval;
3374 struct Scsi_Host *host;
3375 struct megasas_instance *instance;
3377 instance = pci_get_drvdata(pdev);
3378 host = instance->host;
3379 pci_set_power_state(pdev, PCI_D0);
3380 pci_enable_wake(pdev, PCI_D0, 0);
3381 pci_restore_state(pdev);
3384 * PCI prepping: enable device set bus mastering and dma mask
3386 rval = pci_enable_device_mem(pdev);
3388 if (rval) {
3389 printk(KERN_ERR "megasas: Enable device failed\n");
3390 return rval;
3393 pci_set_master(pdev);
3395 if (megasas_set_dma_mask(pdev))
3396 goto fail_set_dma_mask;
3399 * Initialize MFI Firmware
3402 *instance->producer = 0;
3403 *instance->consumer = 0;
3405 atomic_set(&instance->fw_outstanding, 0);
3408 * We expect the FW state to be READY
3410 if (megasas_transition_to_ready(instance))
3411 goto fail_ready_state;
3413 if (megasas_issue_init_mfi(instance))
3414 goto fail_init_mfi;
3416 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3417 (unsigned long)instance);
3420 * Register IRQ
3422 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
3423 "megasas", instance)) {
3424 printk(KERN_ERR "megasas: Failed to register IRQ\n");
3425 goto fail_irq;
3428 instance->instancet->enable_intr(instance->reg_set);
3431 * Initiate AEN (Asynchronous Event Notification)
3433 if (megasas_start_aen(instance))
3434 printk(KERN_ERR "megasas: Start AEN failed\n");
3436 /* Initialize the cmd completion timer */
3437 if (poll_mode_io)
3438 megasas_start_timer(instance, &instance->io_completion_timer,
3439 megasas_io_completion_timer,
3440 MEGASAS_COMPLETION_TIMER_INTERVAL);
3441 instance->unload = 0;
3443 return 0;
3445 fail_irq:
3446 fail_init_mfi:
3447 if (instance->evt_detail)
3448 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3449 instance->evt_detail,
3450 instance->evt_detail_h);
3452 if (instance->producer)
3453 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3454 instance->producer_h);
3455 if (instance->consumer)
3456 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3457 instance->consumer_h);
3458 scsi_host_put(host);
3460 fail_set_dma_mask:
3461 fail_ready_state:
3463 pci_disable_device(pdev);
3465 return -ENODEV;
3467 #else
3468 #define megasas_suspend NULL
3469 #define megasas_resume NULL
3470 #endif
3473 * megasas_detach_one - PCI hot"un"plug entry point
3474 * @pdev: PCI device structure
3476 static void __devexit megasas_detach_one(struct pci_dev *pdev)
3478 int i;
3479 struct Scsi_Host *host;
3480 struct megasas_instance *instance;
3482 instance = pci_get_drvdata(pdev);
3483 instance->unload = 1;
3484 host = instance->host;
3486 if (poll_mode_io)
3487 del_timer_sync(&instance->io_completion_timer);
3489 scsi_remove_host(instance->host);
3490 megasas_flush_cache(instance);
3491 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
3493 /* cancel the delayed work if this work still in queue*/
3494 if (instance->ev != NULL) {
3495 struct megasas_aen_event *ev = instance->ev;
3496 cancel_delayed_work(
3497 (struct delayed_work *)&ev->hotplug_work);
3498 flush_scheduled_work();
3499 instance->ev = NULL;
3502 tasklet_kill(&instance->isr_tasklet);
3505 * Take the instance off the instance array. Note that we will not
3506 * decrement the max_index. We let this array be sparse array
3508 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
3509 if (megasas_mgmt_info.instance[i] == instance) {
3510 megasas_mgmt_info.count--;
3511 megasas_mgmt_info.instance[i] = NULL;
3513 break;
3517 pci_set_drvdata(instance->pdev, NULL);
3519 instance->instancet->disable_intr(instance->reg_set);
3521 free_irq(instance->pdev->irq, instance);
3523 megasas_release_mfi(instance);
3525 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3526 instance->evt_detail, instance->evt_detail_h);
3528 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3529 instance->producer_h);
3531 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3532 instance->consumer_h);
3534 scsi_host_put(host);
3536 pci_set_drvdata(pdev, NULL);
3538 pci_disable_device(pdev);
3540 return;
3544 * megasas_shutdown - Shutdown entry point
3545 * @device: Generic device structure
3547 static void megasas_shutdown(struct pci_dev *pdev)
3549 struct megasas_instance *instance = pci_get_drvdata(pdev);
3550 instance->unload = 1;
3551 megasas_flush_cache(instance);
3552 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
3556 * megasas_mgmt_open - char node "open" entry point
3558 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
3560 cycle_kernel_lock();
3562 * Allow only those users with admin rights
3564 if (!capable(CAP_SYS_ADMIN))
3565 return -EACCES;
3567 return 0;
3571 * megasas_mgmt_fasync - Async notifier registration from applications
3573 * This function adds the calling process to a driver global queue. When an
3574 * event occurs, SIGIO will be sent to all processes in this queue.
3576 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
3578 int rc;
3580 mutex_lock(&megasas_async_queue_mutex);
3582 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
3584 mutex_unlock(&megasas_async_queue_mutex);
3586 if (rc >= 0) {
3587 /* For sanity check when we get ioctl */
3588 filep->private_data = filep;
3589 return 0;
3592 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
3594 return rc;
3598 * megasas_mgmt_poll - char node "poll" entry point
3599 * */
3600 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
3602 unsigned int mask;
3603 unsigned long flags;
3604 poll_wait(file, &megasas_poll_wait, wait);
3605 spin_lock_irqsave(&poll_aen_lock, flags);
3606 if (megasas_poll_wait_aen)
3607 mask = (POLLIN | POLLRDNORM);
3608 else
3609 mask = 0;
3610 spin_unlock_irqrestore(&poll_aen_lock, flags);
3611 return mask;
3615 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
3616 * @instance: Adapter soft state
3617 * @argp: User's ioctl packet
3619 static int
3620 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
3621 struct megasas_iocpacket __user * user_ioc,
3622 struct megasas_iocpacket *ioc)
3624 struct megasas_sge32 *kern_sge32;
3625 struct megasas_cmd *cmd;
3626 void *kbuff_arr[MAX_IOCTL_SGE];
3627 dma_addr_t buf_handle = 0;
3628 int error = 0, i;
3629 void *sense = NULL;
3630 dma_addr_t sense_handle;
3631 unsigned long *sense_ptr;
3633 memset(kbuff_arr, 0, sizeof(kbuff_arr));
3635 if (ioc->sge_count > MAX_IOCTL_SGE) {
3636 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
3637 ioc->sge_count, MAX_IOCTL_SGE);
3638 return -EINVAL;
3641 cmd = megasas_get_cmd(instance);
3642 if (!cmd) {
3643 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
3644 return -ENOMEM;
3648 * User's IOCTL packet has 2 frames (maximum). Copy those two
3649 * frames into our cmd's frames. cmd->frame's context will get
3650 * overwritten when we copy from user's frames. So set that value
3651 * alone separately
3653 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
3654 cmd->frame->hdr.context = cmd->index;
3655 cmd->frame->hdr.pad_0 = 0;
3658 * The management interface between applications and the fw uses
3659 * MFI frames. E.g, RAID configuration changes, LD property changes
3660 * etc are accomplishes through different kinds of MFI frames. The
3661 * driver needs to care only about substituting user buffers with
3662 * kernel buffers in SGLs. The location of SGL is embedded in the
3663 * struct iocpacket itself.
3665 kern_sge32 = (struct megasas_sge32 *)
3666 ((unsigned long)cmd->frame + ioc->sgl_off);
3669 * For each user buffer, create a mirror buffer and copy in
3671 for (i = 0; i < ioc->sge_count; i++) {
3672 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
3673 ioc->sgl[i].iov_len,
3674 &buf_handle, GFP_KERNEL);
3675 if (!kbuff_arr[i]) {
3676 printk(KERN_DEBUG "megasas: Failed to alloc "
3677 "kernel SGL buffer for IOCTL \n");
3678 error = -ENOMEM;
3679 goto out;
3683 * We don't change the dma_coherent_mask, so
3684 * pci_alloc_consistent only returns 32bit addresses
3686 kern_sge32[i].phys_addr = (u32) buf_handle;
3687 kern_sge32[i].length = ioc->sgl[i].iov_len;
3690 * We created a kernel buffer corresponding to the
3691 * user buffer. Now copy in from the user buffer
3693 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
3694 (u32) (ioc->sgl[i].iov_len))) {
3695 error = -EFAULT;
3696 goto out;
3700 if (ioc->sense_len) {
3701 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
3702 &sense_handle, GFP_KERNEL);
3703 if (!sense) {
3704 error = -ENOMEM;
3705 goto out;
3708 sense_ptr =
3709 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
3710 *sense_ptr = sense_handle;
3714 * Set the sync_cmd flag so that the ISR knows not to complete this
3715 * cmd to the SCSI mid-layer
3717 cmd->sync_cmd = 1;
3718 megasas_issue_blocked_cmd(instance, cmd);
3719 cmd->sync_cmd = 0;
3722 * copy out the kernel buffers to user buffers
3724 for (i = 0; i < ioc->sge_count; i++) {
3725 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
3726 ioc->sgl[i].iov_len)) {
3727 error = -EFAULT;
3728 goto out;
3733 * copy out the sense
3735 if (ioc->sense_len) {
3737 * sense_ptr points to the location that has the user
3738 * sense buffer address
3740 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
3741 ioc->sense_off);
3743 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
3744 sense, ioc->sense_len)) {
3745 printk(KERN_ERR "megasas: Failed to copy out to user "
3746 "sense data\n");
3747 error = -EFAULT;
3748 goto out;
3753 * copy the status codes returned by the fw
3755 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
3756 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
3757 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
3758 error = -EFAULT;
3761 out:
3762 if (sense) {
3763 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
3764 sense, sense_handle);
3767 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
3768 dma_free_coherent(&instance->pdev->dev,
3769 kern_sge32[i].length,
3770 kbuff_arr[i], kern_sge32[i].phys_addr);
3773 megasas_return_cmd(instance, cmd);
3774 return error;
3777 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
3779 struct megasas_iocpacket __user *user_ioc =
3780 (struct megasas_iocpacket __user *)arg;
3781 struct megasas_iocpacket *ioc;
3782 struct megasas_instance *instance;
3783 int error;
3785 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
3786 if (!ioc)
3787 return -ENOMEM;
3789 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
3790 error = -EFAULT;
3791 goto out_kfree_ioc;
3794 instance = megasas_lookup_instance(ioc->host_no);
3795 if (!instance) {
3796 error = -ENODEV;
3797 goto out_kfree_ioc;
3800 if (instance->hw_crit_error == 1) {
3801 printk(KERN_DEBUG "Controller in Crit ERROR\n");
3802 error = -ENODEV;
3803 goto out_kfree_ioc;
3806 if (instance->unload == 1) {
3807 error = -ENODEV;
3808 goto out_kfree_ioc;
3812 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
3814 if (down_interruptible(&instance->ioctl_sem)) {
3815 error = -ERESTARTSYS;
3816 goto out_kfree_ioc;
3818 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
3819 up(&instance->ioctl_sem);
3821 out_kfree_ioc:
3822 kfree(ioc);
3823 return error;
3826 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
3828 struct megasas_instance *instance;
3829 struct megasas_aen aen;
3830 int error;
3832 if (file->private_data != file) {
3833 printk(KERN_DEBUG "megasas: fasync_helper was not "
3834 "called first\n");
3835 return -EINVAL;
3838 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
3839 return -EFAULT;
3841 instance = megasas_lookup_instance(aen.host_no);
3843 if (!instance)
3844 return -ENODEV;
3846 if (instance->hw_crit_error == 1) {
3847 error = -ENODEV;
3850 if (instance->unload == 1) {
3851 return -ENODEV;
3854 mutex_lock(&instance->aen_mutex);
3855 error = megasas_register_aen(instance, aen.seq_num,
3856 aen.class_locale_word);
3857 mutex_unlock(&instance->aen_mutex);
3858 return error;
3862 * megasas_mgmt_ioctl - char node ioctl entry point
3864 static long
3865 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3867 switch (cmd) {
3868 case MEGASAS_IOC_FIRMWARE:
3869 return megasas_mgmt_ioctl_fw(file, arg);
3871 case MEGASAS_IOC_GET_AEN:
3872 return megasas_mgmt_ioctl_aen(file, arg);
3875 return -ENOTTY;
3878 #ifdef CONFIG_COMPAT
3879 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
3881 struct compat_megasas_iocpacket __user *cioc =
3882 (struct compat_megasas_iocpacket __user *)arg;
3883 struct megasas_iocpacket __user *ioc =
3884 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
3885 int i;
3886 int error = 0;
3887 compat_uptr_t ptr;
3889 if (clear_user(ioc, sizeof(*ioc)))
3890 return -EFAULT;
3892 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
3893 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
3894 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
3895 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
3896 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
3897 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
3898 return -EFAULT;
3901 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
3902 * sense_len is not null, so prepare the 64bit value under
3903 * the same condition.
3905 if (ioc->sense_len) {
3906 void __user **sense_ioc_ptr =
3907 (void __user **)(ioc->frame.raw + ioc->sense_off);
3908 compat_uptr_t *sense_cioc_ptr =
3909 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
3910 if (get_user(ptr, sense_cioc_ptr) ||
3911 put_user(compat_ptr(ptr), sense_ioc_ptr))
3912 return -EFAULT;
3915 for (i = 0; i < MAX_IOCTL_SGE; i++) {
3916 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
3917 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
3918 copy_in_user(&ioc->sgl[i].iov_len,
3919 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
3920 return -EFAULT;
3923 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
3925 if (copy_in_user(&cioc->frame.hdr.cmd_status,
3926 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
3927 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
3928 return -EFAULT;
3930 return error;
3933 static long
3934 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
3935 unsigned long arg)
3937 switch (cmd) {
3938 case MEGASAS_IOC_FIRMWARE32:
3939 return megasas_mgmt_compat_ioctl_fw(file, arg);
3940 case MEGASAS_IOC_GET_AEN:
3941 return megasas_mgmt_ioctl_aen(file, arg);
3944 return -ENOTTY;
3946 #endif
3949 * File operations structure for management interface
3951 static const struct file_operations megasas_mgmt_fops = {
3952 .owner = THIS_MODULE,
3953 .open = megasas_mgmt_open,
3954 .fasync = megasas_mgmt_fasync,
3955 .unlocked_ioctl = megasas_mgmt_ioctl,
3956 .poll = megasas_mgmt_poll,
3957 #ifdef CONFIG_COMPAT
3958 .compat_ioctl = megasas_mgmt_compat_ioctl,
3959 #endif
3963 * PCI hotplug support registration structure
3965 static struct pci_driver megasas_pci_driver = {
3967 .name = "megaraid_sas",
3968 .id_table = megasas_pci_table,
3969 .probe = megasas_probe_one,
3970 .remove = __devexit_p(megasas_detach_one),
3971 .suspend = megasas_suspend,
3972 .resume = megasas_resume,
3973 .shutdown = megasas_shutdown,
3977 * Sysfs driver attributes
3979 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
3981 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
3982 MEGASAS_VERSION);
3985 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
3987 static ssize_t
3988 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
3990 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
3991 MEGASAS_RELDATE);
3994 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
3995 NULL);
3997 static ssize_t
3998 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4000 return sprintf(buf, "%u\n", support_poll_for_event);
4003 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4004 megasas_sysfs_show_support_poll_for_event, NULL);
4006 static ssize_t
4007 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4009 return sprintf(buf, "%u\n", megasas_dbg_lvl);
4012 static ssize_t
4013 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4015 int retval = count;
4016 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4017 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4018 retval = -EINVAL;
4020 return retval;
4023 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
4024 megasas_sysfs_set_dbg_lvl);
4026 static ssize_t
4027 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4029 return sprintf(buf, "%u\n", poll_mode_io);
4032 static ssize_t
4033 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4034 const char *buf, size_t count)
4036 int retval = count;
4037 int tmp = poll_mode_io;
4038 int i;
4039 struct megasas_instance *instance;
4041 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4042 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4043 retval = -EINVAL;
4047 * Check if poll_mode_io is already set or is same as previous value
4049 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4050 goto out;
4052 if (poll_mode_io) {
4054 * Start timers for all adapters
4056 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4057 instance = megasas_mgmt_info.instance[i];
4058 if (instance) {
4059 megasas_start_timer(instance,
4060 &instance->io_completion_timer,
4061 megasas_io_completion_timer,
4062 MEGASAS_COMPLETION_TIMER_INTERVAL);
4065 } else {
4067 * Delete timers for all adapters
4069 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4070 instance = megasas_mgmt_info.instance[i];
4071 if (instance)
4072 del_timer_sync(&instance->io_completion_timer);
4076 out:
4077 return retval;
4080 static void
4081 megasas_aen_polling(struct work_struct *work)
4083 struct megasas_aen_event *ev =
4084 container_of(work, struct megasas_aen_event, hotplug_work);
4085 struct megasas_instance *instance = ev->instance;
4086 union megasas_evt_class_locale class_locale;
4087 struct Scsi_Host *host;
4088 struct scsi_device *sdev1;
4089 u16 pd_index = 0;
4090 u16 ld_index = 0;
4091 int i, j, doscan = 0;
4092 u32 seq_num;
4093 int error;
4095 if (!instance) {
4096 printk(KERN_ERR "invalid instance!\n");
4097 kfree(ev);
4098 return;
4100 instance->ev = NULL;
4101 host = instance->host;
4102 if (instance->evt_detail) {
4104 switch (instance->evt_detail->code) {
4105 case MR_EVT_PD_INSERTED:
4106 if (megasas_get_pd_list(instance) == 0) {
4107 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4108 for (j = 0;
4109 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4110 j++) {
4112 pd_index =
4113 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4115 sdev1 =
4116 scsi_device_lookup(host, i, j, 0);
4118 if (instance->pd_list[pd_index].driveState
4119 == MR_PD_STATE_SYSTEM) {
4120 if (!sdev1) {
4121 scsi_add_device(host, i, j, 0);
4124 if (sdev1)
4125 scsi_device_put(sdev1);
4130 doscan = 0;
4131 break;
4133 case MR_EVT_PD_REMOVED:
4134 if (megasas_get_pd_list(instance) == 0) {
4135 megasas_get_pd_list(instance);
4136 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4137 for (j = 0;
4138 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4139 j++) {
4141 pd_index =
4142 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4144 sdev1 =
4145 scsi_device_lookup(host, i, j, 0);
4147 if (instance->pd_list[pd_index].driveState
4148 == MR_PD_STATE_SYSTEM) {
4149 if (sdev1) {
4150 scsi_device_put(sdev1);
4152 } else {
4153 if (sdev1) {
4154 scsi_remove_device(sdev1);
4155 scsi_device_put(sdev1);
4161 doscan = 0;
4162 break;
4164 case MR_EVT_LD_OFFLINE:
4165 case MR_EVT_LD_DELETED:
4166 megasas_get_ld_list(instance);
4167 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4168 for (j = 0;
4169 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4170 j++) {
4172 ld_index =
4173 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4175 sdev1 = scsi_device_lookup(host,
4176 i + MEGASAS_MAX_LD_CHANNELS,
4180 if (instance->ld_ids[ld_index] != 0xff) {
4181 if (sdev1) {
4182 scsi_device_put(sdev1);
4184 } else {
4185 if (sdev1) {
4186 scsi_remove_device(sdev1);
4187 scsi_device_put(sdev1);
4192 doscan = 0;
4193 break;
4194 case MR_EVT_LD_CREATED:
4195 megasas_get_ld_list(instance);
4196 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4197 for (j = 0;
4198 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4199 j++) {
4200 ld_index =
4201 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4203 sdev1 = scsi_device_lookup(host,
4204 i+MEGASAS_MAX_LD_CHANNELS,
4205 j, 0);
4207 if (instance->ld_ids[ld_index] !=
4208 0xff) {
4209 if (!sdev1) {
4210 scsi_add_device(host,
4211 i + 2,
4212 j, 0);
4215 if (sdev1) {
4216 scsi_device_put(sdev1);
4220 doscan = 0;
4221 break;
4222 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4223 case MR_EVT_FOREIGN_CFG_IMPORTED:
4224 doscan = 1;
4225 break;
4226 default:
4227 doscan = 0;
4228 break;
4230 } else {
4231 printk(KERN_ERR "invalid evt_detail!\n");
4232 kfree(ev);
4233 return;
4236 if (doscan) {
4237 printk(KERN_INFO "scanning ...\n");
4238 megasas_get_pd_list(instance);
4239 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4240 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4241 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
4242 sdev1 = scsi_device_lookup(host, i, j, 0);
4243 if (instance->pd_list[pd_index].driveState ==
4244 MR_PD_STATE_SYSTEM) {
4245 if (!sdev1) {
4246 scsi_add_device(host, i, j, 0);
4248 if (sdev1)
4249 scsi_device_put(sdev1);
4250 } else {
4251 if (sdev1) {
4252 scsi_remove_device(sdev1);
4253 scsi_device_put(sdev1);
4259 megasas_get_ld_list(instance);
4260 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4261 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
4262 ld_index =
4263 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4265 sdev1 = scsi_device_lookup(host,
4266 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
4267 if (instance->ld_ids[ld_index] != 0xff) {
4268 if (!sdev1) {
4269 scsi_add_device(host,
4270 i+2,
4271 j, 0);
4272 } else {
4273 scsi_device_put(sdev1);
4275 } else {
4276 if (sdev1) {
4277 scsi_remove_device(sdev1);
4278 scsi_device_put(sdev1);
4285 if ( instance->aen_cmd != NULL ) {
4286 kfree(ev);
4287 return ;
4290 seq_num = instance->evt_detail->seq_num + 1;
4292 /* Register AEN with FW for latest sequence number plus 1 */
4293 class_locale.members.reserved = 0;
4294 class_locale.members.locale = MR_EVT_LOCALE_ALL;
4295 class_locale.members.class = MR_EVT_CLASS_DEBUG;
4296 mutex_lock(&instance->aen_mutex);
4297 error = megasas_register_aen(instance, seq_num,
4298 class_locale.word);
4299 mutex_unlock(&instance->aen_mutex);
4301 if (error)
4302 printk(KERN_ERR "register aen failed error %x\n", error);
4304 kfree(ev);
4308 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
4309 megasas_sysfs_show_poll_mode_io,
4310 megasas_sysfs_set_poll_mode_io);
4313 * megasas_init - Driver load entry point
4315 static int __init megasas_init(void)
4317 int rval;
4320 * Announce driver version and other information
4322 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
4323 MEGASAS_EXT_VERSION);
4325 support_poll_for_event = 2;
4327 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
4330 * Register character device node
4332 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
4334 if (rval < 0) {
4335 printk(KERN_DEBUG "megasas: failed to open device node\n");
4336 return rval;
4339 megasas_mgmt_majorno = rval;
4342 * Register ourselves as PCI hotplug module
4344 rval = pci_register_driver(&megasas_pci_driver);
4346 if (rval) {
4347 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
4348 goto err_pcidrv;
4351 rval = driver_create_file(&megasas_pci_driver.driver,
4352 &driver_attr_version);
4353 if (rval)
4354 goto err_dcf_attr_ver;
4355 rval = driver_create_file(&megasas_pci_driver.driver,
4356 &driver_attr_release_date);
4357 if (rval)
4358 goto err_dcf_rel_date;
4360 rval = driver_create_file(&megasas_pci_driver.driver,
4361 &driver_attr_support_poll_for_event);
4362 if (rval)
4363 goto err_dcf_support_poll_for_event;
4365 rval = driver_create_file(&megasas_pci_driver.driver,
4366 &driver_attr_dbg_lvl);
4367 if (rval)
4368 goto err_dcf_dbg_lvl;
4369 rval = driver_create_file(&megasas_pci_driver.driver,
4370 &driver_attr_poll_mode_io);
4371 if (rval)
4372 goto err_dcf_poll_mode_io;
4374 return rval;
4376 err_dcf_poll_mode_io:
4377 driver_remove_file(&megasas_pci_driver.driver,
4378 &driver_attr_dbg_lvl);
4379 err_dcf_dbg_lvl:
4380 driver_remove_file(&megasas_pci_driver.driver,
4381 &driver_attr_support_poll_for_event);
4383 err_dcf_support_poll_for_event:
4384 driver_remove_file(&megasas_pci_driver.driver,
4385 &driver_attr_release_date);
4387 err_dcf_rel_date:
4388 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
4389 err_dcf_attr_ver:
4390 pci_unregister_driver(&megasas_pci_driver);
4391 err_pcidrv:
4392 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4393 return rval;
4397 * megasas_exit - Driver unload entry point
4399 static void __exit megasas_exit(void)
4401 driver_remove_file(&megasas_pci_driver.driver,
4402 &driver_attr_poll_mode_io);
4403 driver_remove_file(&megasas_pci_driver.driver,
4404 &driver_attr_dbg_lvl);
4405 driver_remove_file(&megasas_pci_driver.driver,
4406 &driver_attr_release_date);
4407 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
4409 pci_unregister_driver(&megasas_pci_driver);
4410 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
4413 module_init(megasas_init);
4414 module_exit(megasas_exit);