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.31-rc1
16 * (email-id : megaraidlinux@lsi.com)
21 * List of supported controllers
23 * OEM Product Name VID DID SSVID SSID
24 * --- ------------ --- --- ---- ----
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/pci.h>
30 #include <linux/list.h>
31 #include <linux/moduleparam.h>
32 #include <linux/module.h>
33 #include <linux/spinlock.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/uio.h>
37 #include <linux/slab.h>
38 #include <asm/uaccess.h>
40 #include <linux/compat.h>
41 #include <linux/blkdev.h>
42 #include <linux/mutex.h>
43 #include <linux/poll.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_host.h>
49 #include "megaraid_sas.h"
52 * poll_mode_io:1- schedule complete completion from q cmd
54 static unsigned int poll_mode_io;
55 module_param_named(poll_mode_io, poll_mode_io, int, 0);
56 MODULE_PARM_DESC(poll_mode_io,
57 "Complete cmds from IO path, (default=0)");
60 * Number of sectors per IO command
61 * Will be set in megasas_init_mfi if user does not provide
63 static unsigned int max_sectors;
64 module_param_named(max_sectors, max_sectors, int, 0);
65 MODULE_PARM_DESC(max_sectors,
66 "Maximum number of sectors per IO command");
68 MODULE_LICENSE("GPL");
69 MODULE_VERSION(MEGASAS_VERSION);
70 MODULE_AUTHOR("megaraidlinux@lsi.com");
71 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
73 static int megasas_transition_to_ready(struct megasas_instance *instance);
74 static int megasas_get_pd_list(struct megasas_instance *instance);
75 static int megasas_issue_init_mfi(struct megasas_instance *instance);
76 static int megasas_register_aen(struct megasas_instance *instance,
77 u32 seq_num, u32 class_locale_word);
79 * PCI ID table for all supported controllers
81 static struct pci_device_id megasas_pci_table[] = {
83 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
85 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
87 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
89 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
91 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
93 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
95 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
97 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
98 /* xscale IOP, vega */
99 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
104 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
106 static int megasas_mgmt_majorno;
107 static struct megasas_mgmt_info megasas_mgmt_info;
108 static struct fasync_struct *megasas_async_queue;
109 static DEFINE_MUTEX(megasas_async_queue_mutex);
111 static int megasas_poll_wait_aen;
112 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
113 static u32 support_poll_for_event;
114 static u32 megasas_dbg_lvl;
115 static u32 support_device_change;
117 /* define lock for aen poll */
118 spinlock_t poll_aen_lock;
121 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
125 * megasas_get_cmd - Get a command from the free pool
126 * @instance: Adapter soft state
128 * Returns a free command from the pool
130 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
134 struct megasas_cmd *cmd = NULL;
136 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
138 if (!list_empty(&instance->cmd_pool)) {
139 cmd = list_entry((&instance->cmd_pool)->next,
140 struct megasas_cmd, list);
141 list_del_init(&cmd->list);
143 printk(KERN_ERR "megasas: Command pool empty!\n");
146 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
151 * megasas_return_cmd - Return a cmd to free command pool
152 * @instance: Adapter soft state
153 * @cmd: Command packet to be returned to free command pool
156 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
160 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
163 list_add_tail(&cmd->list, &instance->cmd_pool);
165 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
170 * The following functions are defined for xscale
171 * (deviceid : 1064R, PERC5) controllers
175 * megasas_enable_intr_xscale - Enables interrupts
176 * @regs: MFI register set
179 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
181 writel(0, &(regs)->outbound_intr_mask);
183 /* Dummy readl to force pci flush */
184 readl(®s->outbound_intr_mask);
188 * megasas_disable_intr_xscale -Disables interrupt
189 * @regs: MFI register set
192 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
195 writel(mask, ®s->outbound_intr_mask);
196 /* Dummy readl to force pci flush */
197 readl(®s->outbound_intr_mask);
201 * megasas_read_fw_status_reg_xscale - returns the current FW status value
202 * @regs: MFI register set
205 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
207 return readl(&(regs)->outbound_msg_0);
210 * megasas_clear_interrupt_xscale - Check & clear interrupt
211 * @regs: MFI register set
214 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
219 * Check if it is our interrupt
221 status = readl(®s->outbound_intr_status);
223 if (status & MFI_OB_INTR_STATUS_MASK)
224 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
225 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
226 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
229 * Clear the interrupt by writing back the same value
232 writel(status, ®s->outbound_intr_status);
234 /* Dummy readl to force pci flush */
235 readl(®s->outbound_intr_status);
241 * megasas_fire_cmd_xscale - Sends command to the FW
242 * @frame_phys_addr : Physical address of cmd
243 * @frame_count : Number of frames for the command
244 * @regs : MFI register set
247 megasas_fire_cmd_xscale(struct megasas_instance *instance,
248 dma_addr_t frame_phys_addr,
250 struct megasas_register_set __iomem *regs)
253 spin_lock_irqsave(&instance->hba_lock, flags);
254 writel((frame_phys_addr >> 3)|(frame_count),
255 &(regs)->inbound_queue_port);
256 spin_unlock_irqrestore(&instance->hba_lock, flags);
260 * megasas_adp_reset_xscale - For controller reset
261 * @regs: MFI register set
264 megasas_adp_reset_xscale(struct megasas_instance *instance,
265 struct megasas_register_set __iomem *regs)
269 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
271 for (i = 0; i < 3; i++)
272 msleep(1000); /* sleep for 3 secs */
274 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
275 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
277 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
279 pci_write_config_dword(instance->pdev,
280 MFI_1068_PCSR_OFFSET, pcidata);
282 for (i = 0; i < 2; i++)
283 msleep(1000); /* need to wait 2 secs again */
286 pci_read_config_dword(instance->pdev,
287 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
288 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
289 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
290 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
292 pci_write_config_dword(instance->pdev,
293 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
300 * megasas_check_reset_xscale - For controller reset check
301 * @regs: MFI register set
304 megasas_check_reset_xscale(struct megasas_instance *instance,
305 struct megasas_register_set __iomem *regs)
308 consumer = *instance->consumer;
310 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
311 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
317 static struct megasas_instance_template megasas_instance_template_xscale = {
319 .fire_cmd = megasas_fire_cmd_xscale,
320 .enable_intr = megasas_enable_intr_xscale,
321 .disable_intr = megasas_disable_intr_xscale,
322 .clear_intr = megasas_clear_intr_xscale,
323 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
324 .adp_reset = megasas_adp_reset_xscale,
325 .check_reset = megasas_check_reset_xscale,
329 * This is the end of set of functions & definitions specific
330 * to xscale (deviceid : 1064R, PERC5) controllers
334 * The following functions are defined for ppc (deviceid : 0x60)
339 * megasas_enable_intr_ppc - Enables interrupts
340 * @regs: MFI register set
343 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
345 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
347 writel(~0x80000000, &(regs)->outbound_intr_mask);
349 /* Dummy readl to force pci flush */
350 readl(®s->outbound_intr_mask);
354 * megasas_disable_intr_ppc - Disable interrupt
355 * @regs: MFI register set
358 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
360 u32 mask = 0xFFFFFFFF;
361 writel(mask, ®s->outbound_intr_mask);
362 /* Dummy readl to force pci flush */
363 readl(®s->outbound_intr_mask);
367 * megasas_read_fw_status_reg_ppc - returns the current FW status value
368 * @regs: MFI register set
371 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
373 return readl(&(regs)->outbound_scratch_pad);
377 * megasas_clear_interrupt_ppc - Check & clear interrupt
378 * @regs: MFI register set
381 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
385 * Check if it is our interrupt
387 status = readl(®s->outbound_intr_status);
389 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
394 * Clear the interrupt by writing back the same value
396 writel(status, ®s->outbound_doorbell_clear);
398 /* Dummy readl to force pci flush */
399 readl(®s->outbound_doorbell_clear);
404 * megasas_fire_cmd_ppc - Sends command to the FW
405 * @frame_phys_addr : Physical address of cmd
406 * @frame_count : Number of frames for the command
407 * @regs : MFI register set
410 megasas_fire_cmd_ppc(struct megasas_instance *instance,
411 dma_addr_t frame_phys_addr,
413 struct megasas_register_set __iomem *regs)
416 spin_lock_irqsave(&instance->hba_lock, flags);
417 writel((frame_phys_addr | (frame_count<<1))|1,
418 &(regs)->inbound_queue_port);
419 spin_unlock_irqrestore(&instance->hba_lock, flags);
423 * megasas_adp_reset_ppc - For controller reset
424 * @regs: MFI register set
427 megasas_adp_reset_ppc(struct megasas_instance *instance,
428 struct megasas_register_set __iomem *regs)
434 * megasas_check_reset_ppc - For controller reset check
435 * @regs: MFI register set
438 megasas_check_reset_ppc(struct megasas_instance *instance,
439 struct megasas_register_set __iomem *regs)
443 static struct megasas_instance_template megasas_instance_template_ppc = {
445 .fire_cmd = megasas_fire_cmd_ppc,
446 .enable_intr = megasas_enable_intr_ppc,
447 .disable_intr = megasas_disable_intr_ppc,
448 .clear_intr = megasas_clear_intr_ppc,
449 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
450 .adp_reset = megasas_adp_reset_ppc,
451 .check_reset = megasas_check_reset_ppc,
455 * megasas_enable_intr_skinny - Enables interrupts
456 * @regs: MFI register set
459 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
461 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
463 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
465 /* Dummy readl to force pci flush */
466 readl(®s->outbound_intr_mask);
470 * megasas_disable_intr_skinny - Disables interrupt
471 * @regs: MFI register set
474 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
476 u32 mask = 0xFFFFFFFF;
477 writel(mask, ®s->outbound_intr_mask);
478 /* Dummy readl to force pci flush */
479 readl(®s->outbound_intr_mask);
483 * megasas_read_fw_status_reg_skinny - returns the current FW status value
484 * @regs: MFI register set
487 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
489 return readl(&(regs)->outbound_scratch_pad);
493 * megasas_clear_interrupt_skinny - Check & clear interrupt
494 * @regs: MFI register set
497 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
501 * Check if it is our interrupt
503 status = readl(®s->outbound_intr_status);
505 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
510 * Clear the interrupt by writing back the same value
512 writel(status, ®s->outbound_intr_status);
515 * dummy read to flush PCI
517 readl(®s->outbound_intr_status);
523 * megasas_fire_cmd_skinny - Sends command to the FW
524 * @frame_phys_addr : Physical address of cmd
525 * @frame_count : Number of frames for the command
526 * @regs : MFI register set
529 megasas_fire_cmd_skinny(struct megasas_instance *instance,
530 dma_addr_t frame_phys_addr,
532 struct megasas_register_set __iomem *regs)
535 spin_lock_irqsave(&instance->hba_lock, flags);
536 writel(0, &(regs)->inbound_high_queue_port);
537 writel((frame_phys_addr | (frame_count<<1))|1,
538 &(regs)->inbound_low_queue_port);
539 spin_unlock_irqrestore(&instance->hba_lock, flags);
543 * megasas_adp_reset_skinny - For controller reset
544 * @regs: MFI register set
547 megasas_adp_reset_skinny(struct megasas_instance *instance,
548 struct megasas_register_set __iomem *regs)
554 * megasas_check_reset_skinny - For controller reset check
555 * @regs: MFI register set
558 megasas_check_reset_skinny(struct megasas_instance *instance,
559 struct megasas_register_set __iomem *regs)
564 static struct megasas_instance_template megasas_instance_template_skinny = {
566 .fire_cmd = megasas_fire_cmd_skinny,
567 .enable_intr = megasas_enable_intr_skinny,
568 .disable_intr = megasas_disable_intr_skinny,
569 .clear_intr = megasas_clear_intr_skinny,
570 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
571 .adp_reset = megasas_adp_reset_skinny,
572 .check_reset = megasas_check_reset_skinny,
577 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
582 * megasas_enable_intr_gen2 - Enables interrupts
583 * @regs: MFI register set
586 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
588 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
590 /* write ~0x00000005 (4 & 1) to the intr mask*/
591 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
593 /* Dummy readl to force pci flush */
594 readl(®s->outbound_intr_mask);
598 * megasas_disable_intr_gen2 - Disables interrupt
599 * @regs: MFI register set
602 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
604 u32 mask = 0xFFFFFFFF;
605 writel(mask, ®s->outbound_intr_mask);
606 /* Dummy readl to force pci flush */
607 readl(®s->outbound_intr_mask);
611 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
612 * @regs: MFI register set
615 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
617 return readl(&(regs)->outbound_scratch_pad);
621 * megasas_clear_interrupt_gen2 - Check & clear interrupt
622 * @regs: MFI register set
625 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
630 * Check if it is our interrupt
632 status = readl(®s->outbound_intr_status);
634 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
635 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
637 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
638 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
642 * Clear the interrupt by writing back the same value
645 writel(status, ®s->outbound_doorbell_clear);
647 /* Dummy readl to force pci flush */
648 readl(®s->outbound_intr_status);
653 * megasas_fire_cmd_gen2 - Sends command to the FW
654 * @frame_phys_addr : Physical address of cmd
655 * @frame_count : Number of frames for the command
656 * @regs : MFI register set
659 megasas_fire_cmd_gen2(struct megasas_instance *instance,
660 dma_addr_t frame_phys_addr,
662 struct megasas_register_set __iomem *regs)
665 spin_lock_irqsave(&instance->hba_lock, flags);
666 writel((frame_phys_addr | (frame_count<<1))|1,
667 &(regs)->inbound_queue_port);
668 spin_unlock_irqrestore(&instance->hba_lock, flags);
672 * megasas_adp_reset_gen2 - For controller reset
673 * @regs: MFI register set
676 megasas_adp_reset_gen2(struct megasas_instance *instance,
677 struct megasas_register_set __iomem *reg_set)
682 writel(0, ®_set->seq_offset);
683 writel(4, ®_set->seq_offset);
684 writel(0xb, ®_set->seq_offset);
685 writel(2, ®_set->seq_offset);
686 writel(7, ®_set->seq_offset);
687 writel(0xd, ®_set->seq_offset);
690 HostDiag = (u32)readl(®_set->host_diag);
692 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
694 HostDiag = (u32)readl(®_set->host_diag);
695 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
703 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
705 writel((HostDiag | DIAG_RESET_ADAPTER), ®_set->host_diag);
709 HostDiag = (u32)readl(®_set->host_diag);
710 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
712 HostDiag = (u32)readl(®_set->host_diag);
713 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
724 * megasas_check_reset_gen2 - For controller reset check
725 * @regs: MFI register set
728 megasas_check_reset_gen2(struct megasas_instance *instance,
729 struct megasas_register_set __iomem *regs)
731 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
738 static struct megasas_instance_template megasas_instance_template_gen2 = {
740 .fire_cmd = megasas_fire_cmd_gen2,
741 .enable_intr = megasas_enable_intr_gen2,
742 .disable_intr = megasas_disable_intr_gen2,
743 .clear_intr = megasas_clear_intr_gen2,
744 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
745 .adp_reset = megasas_adp_reset_gen2,
746 .check_reset = megasas_check_reset_gen2,
750 * This is the end of set of functions & definitions
751 * specific to gen2 (deviceid : 0x78, 0x79) controllers
755 * megasas_issue_polled - Issues a polling command
756 * @instance: Adapter soft state
757 * @cmd: Command packet to be issued
759 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
762 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
765 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
767 struct megasas_header *frame_hdr = &cmd->frame->hdr;
769 frame_hdr->cmd_status = 0xFF;
770 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
773 * Issue the frame using inbound queue port
775 instance->instancet->fire_cmd(instance,
776 cmd->frame_phys_addr, 0, instance->reg_set);
779 * Wait for cmd_status to change
781 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
786 if (frame_hdr->cmd_status == 0xff)
793 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
794 * @instance: Adapter soft state
795 * @cmd: Command to be issued
797 * This function waits on an event for the command to be returned from ISR.
798 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
799 * Used to issue ioctl commands.
802 megasas_issue_blocked_cmd(struct megasas_instance *instance,
803 struct megasas_cmd *cmd)
805 cmd->cmd_status = ENODATA;
807 instance->instancet->fire_cmd(instance,
808 cmd->frame_phys_addr, 0, instance->reg_set);
810 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
816 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
817 * @instance: Adapter soft state
818 * @cmd_to_abort: Previously issued cmd to be aborted
820 * MFI firmware can abort previously issued AEN comamnd (automatic event
821 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
822 * cmd and waits for return status.
823 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
826 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
827 struct megasas_cmd *cmd_to_abort)
829 struct megasas_cmd *cmd;
830 struct megasas_abort_frame *abort_fr;
832 cmd = megasas_get_cmd(instance);
837 abort_fr = &cmd->frame->abort;
840 * Prepare and issue the abort frame
842 abort_fr->cmd = MFI_CMD_ABORT;
843 abort_fr->cmd_status = 0xFF;
845 abort_fr->abort_context = cmd_to_abort->index;
846 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
847 abort_fr->abort_mfi_phys_addr_hi = 0;
850 cmd->cmd_status = 0xFF;
852 instance->instancet->fire_cmd(instance,
853 cmd->frame_phys_addr, 0, instance->reg_set);
856 * Wait for this cmd to complete
858 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
861 megasas_return_cmd(instance, cmd);
866 * megasas_make_sgl32 - Prepares 32-bit SGL
867 * @instance: Adapter soft state
868 * @scp: SCSI command from the mid-layer
869 * @mfi_sgl: SGL to be filled in
871 * If successful, this function returns the number of SG elements. Otherwise,
875 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
876 union megasas_sgl *mfi_sgl)
880 struct scatterlist *os_sgl;
882 sge_count = scsi_dma_map(scp);
883 BUG_ON(sge_count < 0);
886 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
887 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
888 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
895 * megasas_make_sgl64 - Prepares 64-bit SGL
896 * @instance: Adapter soft state
897 * @scp: SCSI command from the mid-layer
898 * @mfi_sgl: SGL to be filled in
900 * If successful, this function returns the number of SG elements. Otherwise,
904 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
905 union megasas_sgl *mfi_sgl)
909 struct scatterlist *os_sgl;
911 sge_count = scsi_dma_map(scp);
912 BUG_ON(sge_count < 0);
915 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
916 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
917 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
924 * megasas_make_sgl_skinny - Prepares IEEE SGL
925 * @instance: Adapter soft state
926 * @scp: SCSI command from the mid-layer
927 * @mfi_sgl: SGL to be filled in
929 * If successful, this function returns the number of SG elements. Otherwise,
933 megasas_make_sgl_skinny(struct megasas_instance *instance,
934 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
938 struct scatterlist *os_sgl;
940 sge_count = scsi_dma_map(scp);
943 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
944 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
945 mfi_sgl->sge_skinny[i].phys_addr =
946 sg_dma_address(os_sgl);
947 mfi_sgl->sge_skinny[i].flag = 0;
954 * megasas_get_frame_count - Computes the number of frames
955 * @frame_type : type of frame- io or pthru frame
956 * @sge_count : number of sg elements
958 * Returns the number of frames required for numnber of sge's (sge_count)
961 static u32 megasas_get_frame_count(struct megasas_instance *instance,
962 u8 sge_count, u8 frame_type)
969 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
970 sizeof(struct megasas_sge32);
972 if (instance->flag_ieee) {
973 sge_sz = sizeof(struct megasas_sge_skinny);
977 * Main frame can contain 2 SGEs for 64-bit SGLs and
978 * 3 SGEs for 32-bit SGLs for ldio &
979 * 1 SGEs for 64-bit SGLs and
980 * 2 SGEs for 32-bit SGLs for pthru frame
982 if (unlikely(frame_type == PTHRU_FRAME)) {
983 if (instance->flag_ieee == 1) {
984 num_cnt = sge_count - 1;
986 num_cnt = sge_count - 1;
988 num_cnt = sge_count - 2;
990 if (instance->flag_ieee == 1) {
991 num_cnt = sge_count - 1;
993 num_cnt = sge_count - 2;
995 num_cnt = sge_count - 3;
999 sge_bytes = sge_sz * num_cnt;
1001 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1002 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1007 if (frame_count > 7)
1013 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1014 * @instance: Adapter soft state
1015 * @scp: SCSI command
1016 * @cmd: Command to be prepared in
1018 * This function prepares CDB commands. These are typcially pass-through
1019 * commands to the devices.
1022 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1023 struct megasas_cmd *cmd)
1028 struct megasas_pthru_frame *pthru;
1030 is_logical = MEGASAS_IS_LOGICAL(scp);
1031 device_id = MEGASAS_DEV_INDEX(instance, scp);
1032 pthru = (struct megasas_pthru_frame *)cmd->frame;
1034 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1035 flags = MFI_FRAME_DIR_WRITE;
1036 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1037 flags = MFI_FRAME_DIR_READ;
1038 else if (scp->sc_data_direction == PCI_DMA_NONE)
1039 flags = MFI_FRAME_DIR_NONE;
1041 if (instance->flag_ieee == 1) {
1042 flags |= MFI_FRAME_IEEE;
1046 * Prepare the DCDB frame
1048 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1049 pthru->cmd_status = 0x0;
1050 pthru->scsi_status = 0x0;
1051 pthru->target_id = device_id;
1052 pthru->lun = scp->device->lun;
1053 pthru->cdb_len = scp->cmd_len;
1056 pthru->flags = flags;
1057 pthru->data_xfer_len = scsi_bufflen(scp);
1059 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1062 * If the command is for the tape device, set the
1063 * pthru timeout to the os layer timeout value.
1065 if (scp->device->type == TYPE_TAPE) {
1066 if ((scp->request->timeout / HZ) > 0xFFFF)
1067 pthru->timeout = 0xFFFF;
1069 pthru->timeout = scp->request->timeout / HZ;
1075 if (instance->flag_ieee == 1) {
1076 pthru->flags |= MFI_FRAME_SGL64;
1077 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1079 } else if (IS_DMA64) {
1080 pthru->flags |= MFI_FRAME_SGL64;
1081 pthru->sge_count = megasas_make_sgl64(instance, scp,
1084 pthru->sge_count = megasas_make_sgl32(instance, scp,
1087 if (pthru->sge_count > instance->max_num_sge) {
1088 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1094 * Sense info specific
1096 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1097 pthru->sense_buf_phys_addr_hi = 0;
1098 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1101 * Compute the total number of frames this command consumes. FW uses
1102 * this number to pull sufficient number of frames from host memory.
1104 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1107 return cmd->frame_count;
1111 * megasas_build_ldio - Prepares IOs to logical devices
1112 * @instance: Adapter soft state
1113 * @scp: SCSI command
1114 * @cmd: Command to be prepared
1116 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1119 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1120 struct megasas_cmd *cmd)
1123 u8 sc = scp->cmnd[0];
1125 struct megasas_io_frame *ldio;
1127 device_id = MEGASAS_DEV_INDEX(instance, scp);
1128 ldio = (struct megasas_io_frame *)cmd->frame;
1130 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1131 flags = MFI_FRAME_DIR_WRITE;
1132 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1133 flags = MFI_FRAME_DIR_READ;
1135 if (instance->flag_ieee == 1) {
1136 flags |= MFI_FRAME_IEEE;
1140 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1142 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1143 ldio->cmd_status = 0x0;
1144 ldio->scsi_status = 0x0;
1145 ldio->target_id = device_id;
1147 ldio->reserved_0 = 0;
1149 ldio->flags = flags;
1150 ldio->start_lba_hi = 0;
1151 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1154 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1156 if (scp->cmd_len == 6) {
1157 ldio->lba_count = (u32) scp->cmnd[4];
1158 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1159 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1161 ldio->start_lba_lo &= 0x1FFFFF;
1165 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1167 else if (scp->cmd_len == 10) {
1168 ldio->lba_count = (u32) scp->cmnd[8] |
1169 ((u32) scp->cmnd[7] << 8);
1170 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1171 ((u32) scp->cmnd[3] << 16) |
1172 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1176 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1178 else if (scp->cmd_len == 12) {
1179 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1180 ((u32) scp->cmnd[7] << 16) |
1181 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1183 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1184 ((u32) scp->cmnd[3] << 16) |
1185 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1189 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1191 else if (scp->cmd_len == 16) {
1192 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1193 ((u32) scp->cmnd[11] << 16) |
1194 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1196 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1197 ((u32) scp->cmnd[7] << 16) |
1198 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1200 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1201 ((u32) scp->cmnd[3] << 16) |
1202 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1209 if (instance->flag_ieee) {
1210 ldio->flags |= MFI_FRAME_SGL64;
1211 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1213 } else if (IS_DMA64) {
1214 ldio->flags |= MFI_FRAME_SGL64;
1215 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1217 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1219 if (ldio->sge_count > instance->max_num_sge) {
1220 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1226 * Sense info specific
1228 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1229 ldio->sense_buf_phys_addr_hi = 0;
1230 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1233 * Compute the total number of frames this command consumes. FW uses
1234 * this number to pull sufficient number of frames from host memory.
1236 cmd->frame_count = megasas_get_frame_count(instance,
1237 ldio->sge_count, IO_FRAME);
1239 return cmd->frame_count;
1243 * megasas_is_ldio - Checks if the cmd is for logical drive
1244 * @scmd: SCSI command
1246 * Called by megasas_queue_command to find out if the command to be queued
1247 * is a logical drive command
1249 static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1251 if (!MEGASAS_IS_LOGICAL(cmd))
1253 switch (cmd->cmnd[0]) {
1269 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1271 * @instance: Adapter soft state
1274 megasas_dump_pending_frames(struct megasas_instance *instance)
1276 struct megasas_cmd *cmd;
1278 union megasas_sgl *mfi_sgl;
1279 struct megasas_io_frame *ldio;
1280 struct megasas_pthru_frame *pthru;
1282 u32 max_cmd = instance->max_fw_cmds;
1284 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1285 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1287 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1289 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1291 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1292 for (i = 0; i < max_cmd; i++) {
1293 cmd = instance->cmd_list[i];
1296 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1297 if (megasas_is_ldio(cmd->scmd)){
1298 ldio = (struct megasas_io_frame *)cmd->frame;
1299 mfi_sgl = &ldio->sgl;
1300 sgcount = ldio->sge_count;
1301 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);
1304 pthru = (struct megasas_pthru_frame *) cmd->frame;
1305 mfi_sgl = &pthru->sgl;
1306 sgcount = pthru->sge_count;
1307 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);
1309 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1310 for (n = 0; n < sgcount; n++){
1312 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) ;
1314 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1317 printk(KERN_ERR "\n");
1319 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1320 for (i = 0; i < max_cmd; i++) {
1322 cmd = instance->cmd_list[i];
1324 if(cmd->sync_cmd == 1){
1325 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1328 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1332 * megasas_queue_command - Queue entry point
1333 * @scmd: SCSI command to be queued
1334 * @done: Callback entry point
1337 megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1340 struct megasas_cmd *cmd;
1341 struct megasas_instance *instance;
1342 unsigned long flags;
1344 instance = (struct megasas_instance *)
1345 scmd->device->host->hostdata;
1347 if (instance->issuepend_done == 0)
1348 return SCSI_MLQUEUE_HOST_BUSY;
1350 spin_lock_irqsave(&instance->hba_lock, flags);
1351 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1352 spin_unlock_irqrestore(&instance->hba_lock, flags);
1353 return SCSI_MLQUEUE_HOST_BUSY;
1356 spin_unlock_irqrestore(&instance->hba_lock, flags);
1358 scmd->scsi_done = done;
1361 if (MEGASAS_IS_LOGICAL(scmd) &&
1362 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1363 scmd->result = DID_BAD_TARGET << 16;
1367 switch (scmd->cmnd[0]) {
1368 case SYNCHRONIZE_CACHE:
1370 * FW takes care of flush cache on its own
1371 * No need to send it down
1373 scmd->result = DID_OK << 16;
1379 cmd = megasas_get_cmd(instance);
1381 return SCSI_MLQUEUE_HOST_BUSY;
1384 * Logical drive command
1386 if (megasas_is_ldio(scmd))
1387 frame_count = megasas_build_ldio(instance, scmd, cmd);
1389 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1392 goto out_return_cmd;
1395 scmd->SCp.ptr = (char *)cmd;
1398 * Issue the command to the FW
1400 atomic_inc(&instance->fw_outstanding);
1402 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1403 cmd->frame_count-1, instance->reg_set);
1405 * Check if we have pend cmds to be completed
1407 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1408 tasklet_schedule(&instance->isr_tasklet);
1414 megasas_return_cmd(instance, cmd);
1420 static DEF_SCSI_QCMD(megasas_queue_command)
1422 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1426 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1428 if ((megasas_mgmt_info.instance[i]) &&
1429 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1430 return megasas_mgmt_info.instance[i];
1436 static int megasas_slave_configure(struct scsi_device *sdev)
1439 struct megasas_instance *instance ;
1441 instance = megasas_lookup_instance(sdev->host->host_no);
1444 * Don't export physical disk devices to the disk driver.
1446 * FIXME: Currently we don't export them to the midlayer at all.
1447 * That will be fixed once LSI engineers have audited the
1448 * firmware for possible issues.
1450 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1451 sdev->type == TYPE_DISK) {
1452 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1454 if (instance->pd_list[pd_index].driveState ==
1455 MR_PD_STATE_SYSTEM) {
1456 blk_queue_rq_timeout(sdev->request_queue,
1457 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1464 * The RAID firmware may require extended timeouts.
1466 blk_queue_rq_timeout(sdev->request_queue,
1467 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1471 static int megasas_slave_alloc(struct scsi_device *sdev)
1474 struct megasas_instance *instance ;
1475 instance = megasas_lookup_instance(sdev->host->host_no);
1476 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1477 (sdev->type == TYPE_DISK)) {
1479 * Open the OS scan to the SYSTEM PD
1482 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1484 if ((instance->pd_list[pd_index].driveState ==
1485 MR_PD_STATE_SYSTEM) &&
1486 (instance->pd_list[pd_index].driveType ==
1495 static void megaraid_sas_kill_hba(struct megasas_instance *instance)
1497 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1498 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1499 writel(MFI_STOP_ADP,
1500 &instance->reg_set->reserved_0[0]);
1502 writel(MFI_STOP_ADP,
1503 &instance->reg_set->inbound_doorbell);
1508 * megasas_complete_cmd_dpc - Returns FW's controller structure
1509 * @instance_addr: Address of adapter soft state
1511 * Tasklet to complete cmds
1513 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1518 struct megasas_cmd *cmd;
1519 struct megasas_instance *instance =
1520 (struct megasas_instance *)instance_addr;
1521 unsigned long flags;
1523 /* If we have already declared adapter dead, donot complete cmds */
1524 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1527 spin_lock_irqsave(&instance->completion_lock, flags);
1529 producer = *instance->producer;
1530 consumer = *instance->consumer;
1532 while (consumer != producer) {
1533 context = instance->reply_queue[consumer];
1534 if (context >= instance->max_fw_cmds) {
1535 printk(KERN_ERR "Unexpected context value %x\n",
1540 cmd = instance->cmd_list[context];
1542 megasas_complete_cmd(instance, cmd, DID_OK);
1545 if (consumer == (instance->max_fw_cmds + 1)) {
1550 *instance->consumer = producer;
1552 spin_unlock_irqrestore(&instance->completion_lock, flags);
1555 * Check if we can restore can_queue
1557 if (instance->flag & MEGASAS_FW_BUSY
1558 && time_after(jiffies, instance->last_time + 5 * HZ)
1559 && atomic_read(&instance->fw_outstanding) < 17) {
1561 spin_lock_irqsave(instance->host->host_lock, flags);
1562 instance->flag &= ~MEGASAS_FW_BUSY;
1563 if ((instance->pdev->device ==
1564 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1565 (instance->pdev->device ==
1566 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1567 instance->host->can_queue =
1568 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1570 instance->host->can_queue =
1571 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1573 spin_unlock_irqrestore(instance->host->host_lock, flags);
1578 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1581 process_fw_state_change_wq(struct work_struct *work);
1583 void megasas_do_ocr(struct megasas_instance *instance)
1585 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1586 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1587 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1588 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1590 instance->instancet->disable_intr(instance->reg_set);
1591 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1592 instance->issuepend_done = 0;
1594 atomic_set(&instance->fw_outstanding, 0);
1595 megasas_internal_reset_defer_cmds(instance);
1596 process_fw_state_change_wq(&instance->work_init);
1600 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1601 * @instance: Adapter soft state
1603 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1604 * complete all its outstanding commands. Returns error if one or more IOs
1605 * are pending after this time period. It also marks the controller dead.
1607 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1611 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1613 unsigned long flags;
1614 struct list_head clist_local;
1615 struct megasas_cmd *reset_cmd;
1617 u8 kill_adapter_flag;
1619 spin_lock_irqsave(&instance->hba_lock, flags);
1620 adprecovery = instance->adprecovery;
1621 spin_unlock_irqrestore(&instance->hba_lock, flags);
1623 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1625 INIT_LIST_HEAD(&clist_local);
1626 spin_lock_irqsave(&instance->hba_lock, flags);
1627 list_splice_init(&instance->internal_reset_pending_q,
1629 spin_unlock_irqrestore(&instance->hba_lock, flags);
1631 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1632 for (i = 0; i < wait_time; i++) {
1634 spin_lock_irqsave(&instance->hba_lock, flags);
1635 adprecovery = instance->adprecovery;
1636 spin_unlock_irqrestore(&instance->hba_lock, flags);
1637 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1641 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1642 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1643 spin_lock_irqsave(&instance->hba_lock, flags);
1644 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1645 spin_unlock_irqrestore(&instance->hba_lock, flags);
1650 while (!list_empty(&clist_local)) {
1651 reset_cmd = list_entry((&clist_local)->next,
1652 struct megasas_cmd, list);
1653 list_del_init(&reset_cmd->list);
1654 if (reset_cmd->scmd) {
1655 reset_cmd->scmd->result = DID_RESET << 16;
1656 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1657 reset_index, reset_cmd,
1658 reset_cmd->scmd->cmnd[0],
1659 reset_cmd->scmd->serial_number);
1661 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1662 megasas_return_cmd(instance, reset_cmd);
1663 } else if (reset_cmd->sync_cmd) {
1664 printk(KERN_NOTICE "megasas:%p synch cmds"
1668 reset_cmd->cmd_status = ENODATA;
1669 instance->instancet->fire_cmd(instance,
1670 reset_cmd->frame_phys_addr,
1671 0, instance->reg_set);
1673 printk(KERN_NOTICE "megasas: %p unexpected"
1683 for (i = 0; i < wait_time; i++) {
1685 int outstanding = atomic_read(&instance->fw_outstanding);
1690 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1691 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1692 "commands to complete\n",i,outstanding);
1694 * Call cmd completion routine. Cmd to be
1695 * be completed directly without depending on isr.
1697 megasas_complete_cmd_dpc((unsigned long)instance);
1704 kill_adapter_flag = 0;
1706 fw_state = instance->instancet->read_fw_status_reg(
1707 instance->reg_set) & MFI_STATE_MASK;
1708 if ((fw_state == MFI_STATE_FAULT) &&
1709 (instance->disableOnlineCtrlReset == 0)) {
1711 kill_adapter_flag = 2;
1714 megasas_do_ocr(instance);
1715 kill_adapter_flag = 1;
1717 /* wait for 1 secs to let FW finish the pending cmds */
1723 if (atomic_read(&instance->fw_outstanding) &&
1724 !kill_adapter_flag) {
1725 if (instance->disableOnlineCtrlReset == 0) {
1727 megasas_do_ocr(instance);
1729 /* wait for 5 secs to let FW finish the pending cmds */
1730 for (i = 0; i < wait_time; i++) {
1732 atomic_read(&instance->fw_outstanding);
1740 if (atomic_read(&instance->fw_outstanding) ||
1741 (kill_adapter_flag == 2)) {
1742 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1744 * Send signal to FW to stop processing any pending cmds.
1745 * The controller will be taken offline by the OS now.
1747 if ((instance->pdev->device ==
1748 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1749 (instance->pdev->device ==
1750 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1751 writel(MFI_STOP_ADP,
1752 &instance->reg_set->reserved_0[0]);
1754 writel(MFI_STOP_ADP,
1755 &instance->reg_set->inbound_doorbell);
1757 megasas_dump_pending_frames(instance);
1758 spin_lock_irqsave(&instance->hba_lock, flags);
1759 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1760 spin_unlock_irqrestore(&instance->hba_lock, flags);
1764 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1770 * megasas_generic_reset - Generic reset routine
1771 * @scmd: Mid-layer SCSI command
1773 * This routine implements a generic reset handler for device, bus and host
1774 * reset requests. Device, bus and host specific reset handlers can use this
1775 * function after they do their specific tasks.
1777 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1780 struct megasas_instance *instance;
1782 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1784 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1785 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1787 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1788 printk(KERN_ERR "megasas: cannot recover from previous reset "
1793 ret_val = megasas_wait_for_outstanding(instance);
1794 if (ret_val == SUCCESS)
1795 printk(KERN_NOTICE "megasas: reset successful \n");
1797 printk(KERN_ERR "megasas: failed to do reset\n");
1803 * megasas_reset_timer - quiesce the adapter if required
1806 * Sets the FW busy flag and reduces the host->can_queue if the
1807 * cmd has not been completed within the timeout period.
1810 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1812 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1813 struct megasas_instance *instance;
1814 unsigned long flags;
1816 if (time_after(jiffies, scmd->jiffies_at_alloc +
1817 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1818 return BLK_EH_NOT_HANDLED;
1821 instance = cmd->instance;
1822 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1823 /* FW is busy, throttle IO */
1824 spin_lock_irqsave(instance->host->host_lock, flags);
1826 instance->host->can_queue = 16;
1827 instance->last_time = jiffies;
1828 instance->flag |= MEGASAS_FW_BUSY;
1830 spin_unlock_irqrestore(instance->host->host_lock, flags);
1832 return BLK_EH_RESET_TIMER;
1836 * megasas_reset_device - Device reset handler entry point
1838 static int megasas_reset_device(struct scsi_cmnd *scmd)
1843 * First wait for all commands to complete
1845 ret = megasas_generic_reset(scmd);
1851 * megasas_reset_bus_host - Bus & host reset handler entry point
1853 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1858 * First wait for all commands to complete
1860 ret = megasas_generic_reset(scmd);
1866 * megasas_bios_param - Returns disk geometry for a disk
1867 * @sdev: device handle
1868 * @bdev: block device
1869 * @capacity: drive capacity
1870 * @geom: geometry parameters
1873 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1874 sector_t capacity, int geom[])
1880 /* Default heads (64) & sectors (32) */
1884 tmp = heads * sectors;
1885 cylinders = capacity;
1887 sector_div(cylinders, tmp);
1890 * Handle extended translation size for logical drives > 1Gb
1893 if (capacity >= 0x200000) {
1896 tmp = heads*sectors;
1897 cylinders = capacity;
1898 sector_div(cylinders, tmp);
1903 geom[2] = cylinders;
1908 static void megasas_aen_polling(struct work_struct *work);
1911 * megasas_service_aen - Processes an event notification
1912 * @instance: Adapter soft state
1913 * @cmd: AEN command completed by the ISR
1915 * For AEN, driver sends a command down to FW that is held by the FW till an
1916 * event occurs. When an event of interest occurs, FW completes the command
1917 * that it was previously holding.
1919 * This routines sends SIGIO signal to processes that have registered with the
1923 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1925 unsigned long flags;
1927 * Don't signal app if it is just an aborted previously registered aen
1929 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1930 spin_lock_irqsave(&poll_aen_lock, flags);
1931 megasas_poll_wait_aen = 1;
1932 spin_unlock_irqrestore(&poll_aen_lock, flags);
1933 wake_up(&megasas_poll_wait);
1934 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1939 instance->aen_cmd = NULL;
1940 megasas_return_cmd(instance, cmd);
1942 if ((instance->unload == 0) &&
1943 ((instance->issuepend_done == 1))) {
1944 struct megasas_aen_event *ev;
1945 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1947 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1949 ev->instance = instance;
1951 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1952 schedule_delayed_work(
1953 (struct delayed_work *)&ev->hotplug_work, 0);
1959 * Scsi host template for megaraid_sas driver
1961 static struct scsi_host_template megasas_template = {
1963 .module = THIS_MODULE,
1964 .name = "LSI SAS based MegaRAID driver",
1965 .proc_name = "megaraid_sas",
1966 .slave_configure = megasas_slave_configure,
1967 .slave_alloc = megasas_slave_alloc,
1968 .queuecommand = megasas_queue_command,
1969 .eh_device_reset_handler = megasas_reset_device,
1970 .eh_bus_reset_handler = megasas_reset_bus_host,
1971 .eh_host_reset_handler = megasas_reset_bus_host,
1972 .eh_timed_out = megasas_reset_timer,
1973 .bios_param = megasas_bios_param,
1974 .use_clustering = ENABLE_CLUSTERING,
1978 * megasas_complete_int_cmd - Completes an internal command
1979 * @instance: Adapter soft state
1980 * @cmd: Command to be completed
1982 * The megasas_issue_blocked_cmd() function waits for a command to complete
1983 * after it issues a command. This function wakes up that waiting routine by
1984 * calling wake_up() on the wait queue.
1987 megasas_complete_int_cmd(struct megasas_instance *instance,
1988 struct megasas_cmd *cmd)
1990 cmd->cmd_status = cmd->frame->io.cmd_status;
1992 if (cmd->cmd_status == ENODATA) {
1993 cmd->cmd_status = 0;
1995 wake_up(&instance->int_cmd_wait_q);
1999 * megasas_complete_abort - Completes aborting a command
2000 * @instance: Adapter soft state
2001 * @cmd: Cmd that was issued to abort another cmd
2003 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2004 * after it issues an abort on a previously issued command. This function
2005 * wakes up all functions waiting on the same wait queue.
2008 megasas_complete_abort(struct megasas_instance *instance,
2009 struct megasas_cmd *cmd)
2011 if (cmd->sync_cmd) {
2013 cmd->cmd_status = 0;
2014 wake_up(&instance->abort_cmd_wait_q);
2021 * megasas_complete_cmd - Completes a command
2022 * @instance: Adapter soft state
2023 * @cmd: Command to be completed
2024 * @alt_status: If non-zero, use this value as status to
2025 * SCSI mid-layer instead of the value returned
2026 * by the FW. This should be used if caller wants
2027 * an alternate status (as in the case of aborted
2031 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2035 struct megasas_header *hdr = &cmd->frame->hdr;
2036 unsigned long flags;
2038 /* flag for the retry reset */
2039 cmd->retry_for_fw_reset = 0;
2042 cmd->scmd->SCp.ptr = NULL;
2046 case MFI_CMD_PD_SCSI_IO:
2047 case MFI_CMD_LD_SCSI_IO:
2050 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2051 * issued either through an IO path or an IOCTL path. If it
2052 * was via IOCTL, we will send it to internal completion.
2054 if (cmd->sync_cmd) {
2056 megasas_complete_int_cmd(instance, cmd);
2060 case MFI_CMD_LD_READ:
2061 case MFI_CMD_LD_WRITE:
2064 cmd->scmd->result = alt_status << 16;
2070 atomic_dec(&instance->fw_outstanding);
2072 scsi_dma_unmap(cmd->scmd);
2073 cmd->scmd->scsi_done(cmd->scmd);
2074 megasas_return_cmd(instance, cmd);
2079 switch (hdr->cmd_status) {
2082 cmd->scmd->result = DID_OK << 16;
2085 case MFI_STAT_SCSI_IO_FAILED:
2086 case MFI_STAT_LD_INIT_IN_PROGRESS:
2088 (DID_ERROR << 16) | hdr->scsi_status;
2091 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2093 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2095 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2096 memset(cmd->scmd->sense_buffer, 0,
2097 SCSI_SENSE_BUFFERSIZE);
2098 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2101 cmd->scmd->result |= DRIVER_SENSE << 24;
2106 case MFI_STAT_LD_OFFLINE:
2107 case MFI_STAT_DEVICE_NOT_FOUND:
2108 cmd->scmd->result = DID_BAD_TARGET << 16;
2112 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2114 cmd->scmd->result = DID_ERROR << 16;
2118 atomic_dec(&instance->fw_outstanding);
2120 scsi_dma_unmap(cmd->scmd);
2121 cmd->scmd->scsi_done(cmd->scmd);
2122 megasas_return_cmd(instance, cmd);
2129 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2130 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2131 spin_lock_irqsave(&poll_aen_lock, flags);
2132 megasas_poll_wait_aen = 0;
2133 spin_unlock_irqrestore(&poll_aen_lock, flags);
2137 * See if got an event notification
2139 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2140 megasas_service_aen(instance, cmd);
2142 megasas_complete_int_cmd(instance, cmd);
2148 * Cmd issued to abort another cmd returned
2150 megasas_complete_abort(instance, cmd);
2154 printk("megasas: Unknown command completed! [0x%X]\n",
2161 * megasas_issue_pending_cmds_again - issue all pending cmds
2162 * in FW again because of the fw reset
2163 * @instance: Adapter soft state
2166 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2168 struct megasas_cmd *cmd;
2169 struct list_head clist_local;
2170 union megasas_evt_class_locale class_locale;
2171 unsigned long flags;
2174 INIT_LIST_HEAD(&clist_local);
2175 spin_lock_irqsave(&instance->hba_lock, flags);
2176 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2177 spin_unlock_irqrestore(&instance->hba_lock, flags);
2179 while (!list_empty(&clist_local)) {
2180 cmd = list_entry((&clist_local)->next,
2181 struct megasas_cmd, list);
2182 list_del_init(&cmd->list);
2184 if (cmd->sync_cmd || cmd->scmd) {
2185 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2186 "detected to be pending while HBA reset.\n",
2187 cmd, cmd->scmd, cmd->sync_cmd);
2189 cmd->retry_for_fw_reset++;
2191 if (cmd->retry_for_fw_reset == 3) {
2192 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2193 "was tried multiple times during reset."
2194 "Shutting down the HBA\n",
2195 cmd, cmd->scmd, cmd->sync_cmd);
2196 megaraid_sas_kill_hba(instance);
2198 instance->adprecovery =
2199 MEGASAS_HW_CRITICAL_ERROR;
2204 if (cmd->sync_cmd == 1) {
2206 printk(KERN_NOTICE "megaraid_sas: unexpected"
2207 "cmd attached to internal command!\n");
2209 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2210 "on the internal reset queue,"
2211 "issue it again.\n", cmd);
2212 cmd->cmd_status = ENODATA;
2213 instance->instancet->fire_cmd(instance,
2214 cmd->frame_phys_addr ,
2215 0, instance->reg_set);
2216 } else if (cmd->scmd) {
2217 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2218 "detected on the internal queue, issue again.\n",
2219 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2221 atomic_inc(&instance->fw_outstanding);
2222 instance->instancet->fire_cmd(instance,
2223 cmd->frame_phys_addr,
2224 cmd->frame_count-1, instance->reg_set);
2226 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2227 "internal reset defer list while re-issue!!\n",
2232 if (instance->aen_cmd) {
2233 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2234 megasas_return_cmd(instance, instance->aen_cmd);
2236 instance->aen_cmd = NULL;
2240 * Initiate AEN (Asynchronous Event Notification)
2242 seq_num = instance->last_seq_num;
2243 class_locale.members.reserved = 0;
2244 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2245 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2247 megasas_register_aen(instance, seq_num, class_locale.word);
2251 * Move the internal reset pending commands to a deferred queue.
2253 * We move the commands pending at internal reset time to a
2254 * pending queue. This queue would be flushed after successful
2255 * completion of the internal reset sequence. if the internal reset
2256 * did not complete in time, the kernel reset handler would flush
2260 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2262 struct megasas_cmd *cmd;
2264 u32 max_cmd = instance->max_fw_cmds;
2266 unsigned long flags;
2269 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2270 for (i = 0; i < max_cmd; i++) {
2271 cmd = instance->cmd_list[i];
2272 if (cmd->sync_cmd == 1 || cmd->scmd) {
2273 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2274 "on the defer queue as internal\n",
2275 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2277 if (!list_empty(&cmd->list)) {
2278 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2279 " moving this cmd:%p, %d %p, it was"
2280 "discovered on some list?\n",
2281 cmd, cmd->sync_cmd, cmd->scmd);
2283 list_del_init(&cmd->list);
2286 list_add_tail(&cmd->list,
2287 &instance->internal_reset_pending_q);
2290 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2295 process_fw_state_change_wq(struct work_struct *work)
2297 struct megasas_instance *instance =
2298 container_of(work, struct megasas_instance, work_init);
2300 unsigned long flags;
2302 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2303 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2304 instance->adprecovery);
2308 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2309 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2310 "state, restarting it...\n");
2312 instance->instancet->disable_intr(instance->reg_set);
2313 atomic_set(&instance->fw_outstanding, 0);
2315 atomic_set(&instance->fw_reset_no_pci_access, 1);
2316 instance->instancet->adp_reset(instance, instance->reg_set);
2317 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2319 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2320 "initiating next stage...\n");
2322 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2323 "state 2 starting...\n");
2325 /*waitting for about 20 second before start the second init*/
2326 for (wait = 0; wait < 30; wait++) {
2330 if (megasas_transition_to_ready(instance)) {
2331 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2333 megaraid_sas_kill_hba(instance);
2334 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2338 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2339 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2340 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2342 *instance->consumer = *instance->producer;
2344 *instance->consumer = 0;
2345 *instance->producer = 0;
2348 megasas_issue_init_mfi(instance);
2350 spin_lock_irqsave(&instance->hba_lock, flags);
2351 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2352 spin_unlock_irqrestore(&instance->hba_lock, flags);
2353 instance->instancet->enable_intr(instance->reg_set);
2355 megasas_issue_pending_cmds_again(instance);
2356 instance->issuepend_done = 1;
2362 * megasas_deplete_reply_queue - Processes all completed commands
2363 * @instance: Adapter soft state
2364 * @alt_status: Alternate status to be returned to
2365 * SCSI mid-layer instead of the status
2366 * returned by the FW
2367 * Note: this must be called with hba lock held
2370 megasas_deplete_reply_queue(struct megasas_instance *instance,
2376 if ((mfiStatus = instance->instancet->check_reset(instance,
2377 instance->reg_set)) == 1) {
2381 if ((mfiStatus = instance->instancet->clear_intr(
2387 instance->mfiStatus = mfiStatus;
2389 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2390 fw_state = instance->instancet->read_fw_status_reg(
2391 instance->reg_set) & MFI_STATE_MASK;
2393 if (fw_state != MFI_STATE_FAULT) {
2394 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2398 if ((fw_state == MFI_STATE_FAULT) &&
2399 (instance->disableOnlineCtrlReset == 0)) {
2400 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2402 if ((instance->pdev->device ==
2403 PCI_DEVICE_ID_LSI_SAS1064R) ||
2404 (instance->pdev->device ==
2405 PCI_DEVICE_ID_DELL_PERC5) ||
2406 (instance->pdev->device ==
2407 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2409 *instance->consumer =
2410 MEGASAS_ADPRESET_INPROG_SIGN;
2414 instance->instancet->disable_intr(instance->reg_set);
2415 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2416 instance->issuepend_done = 0;
2418 atomic_set(&instance->fw_outstanding, 0);
2419 megasas_internal_reset_defer_cmds(instance);
2421 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2422 fw_state, instance->adprecovery);
2424 schedule_work(&instance->work_init);
2428 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2429 fw_state, instance->disableOnlineCtrlReset);
2433 tasklet_schedule(&instance->isr_tasklet);
2437 * megasas_isr - isr entry point
2439 static irqreturn_t megasas_isr(int irq, void *devp)
2441 struct megasas_instance *instance;
2442 unsigned long flags;
2446 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2449 instance = (struct megasas_instance *)devp;
2451 spin_lock_irqsave(&instance->hba_lock, flags);
2452 rc = megasas_deplete_reply_queue(instance, DID_OK);
2453 spin_unlock_irqrestore(&instance->hba_lock, flags);
2459 * megasas_transition_to_ready - Move the FW to READY state
2460 * @instance: Adapter soft state
2462 * During the initialization, FW passes can potentially be in any one of
2463 * several possible states. If the FW in operational, waiting-for-handshake
2464 * states, driver must take steps to bring it to ready state. Otherwise, it
2465 * has to wait for the ready state.
2468 megasas_transition_to_ready(struct megasas_instance* instance)
2474 u32 abs_state, curr_abs_state;
2476 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2478 if (fw_state != MFI_STATE_READY)
2479 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2482 while (fw_state != MFI_STATE_READY) {
2485 instance->instancet->read_fw_status_reg(instance->reg_set);
2489 case MFI_STATE_FAULT:
2491 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2494 case MFI_STATE_WAIT_HANDSHAKE:
2496 * Set the CLR bit in inbound doorbell
2498 if ((instance->pdev->device ==
2499 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2500 (instance->pdev->device ==
2501 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2504 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2505 &instance->reg_set->reserved_0[0]);
2508 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2509 &instance->reg_set->inbound_doorbell);
2512 max_wait = MEGASAS_RESET_WAIT_TIME;
2513 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2516 case MFI_STATE_BOOT_MESSAGE_PENDING:
2517 if ((instance->pdev->device ==
2518 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2519 (instance->pdev->device ==
2520 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2521 writel(MFI_INIT_HOTPLUG,
2522 &instance->reg_set->reserved_0[0]);
2524 writel(MFI_INIT_HOTPLUG,
2525 &instance->reg_set->inbound_doorbell);
2527 max_wait = MEGASAS_RESET_WAIT_TIME;
2528 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2531 case MFI_STATE_OPERATIONAL:
2533 * Bring it to READY state; assuming max wait 10 secs
2535 instance->instancet->disable_intr(instance->reg_set);
2536 if ((instance->pdev->device ==
2537 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2538 (instance->pdev->device ==
2539 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2540 writel(MFI_RESET_FLAGS,
2541 &instance->reg_set->reserved_0[0]);
2543 writel(MFI_RESET_FLAGS,
2544 &instance->reg_set->inbound_doorbell);
2546 max_wait = MEGASAS_RESET_WAIT_TIME;
2547 cur_state = MFI_STATE_OPERATIONAL;
2550 case MFI_STATE_UNDEFINED:
2552 * This state should not last for more than 2 seconds
2554 max_wait = MEGASAS_RESET_WAIT_TIME;
2555 cur_state = MFI_STATE_UNDEFINED;
2558 case MFI_STATE_BB_INIT:
2559 max_wait = MEGASAS_RESET_WAIT_TIME;
2560 cur_state = MFI_STATE_BB_INIT;
2563 case MFI_STATE_FW_INIT:
2564 max_wait = MEGASAS_RESET_WAIT_TIME;
2565 cur_state = MFI_STATE_FW_INIT;
2568 case MFI_STATE_FW_INIT_2:
2569 max_wait = MEGASAS_RESET_WAIT_TIME;
2570 cur_state = MFI_STATE_FW_INIT_2;
2573 case MFI_STATE_DEVICE_SCAN:
2574 max_wait = MEGASAS_RESET_WAIT_TIME;
2575 cur_state = MFI_STATE_DEVICE_SCAN;
2578 case MFI_STATE_FLUSH_CACHE:
2579 max_wait = MEGASAS_RESET_WAIT_TIME;
2580 cur_state = MFI_STATE_FLUSH_CACHE;
2584 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2590 * The cur_state should not last for more than max_wait secs
2592 for (i = 0; i < (max_wait * 1000); i++) {
2593 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2596 instance->instancet->read_fw_status_reg(instance->reg_set);
2598 if (abs_state == curr_abs_state) {
2605 * Return error if fw_state hasn't changed after max_wait
2607 if (curr_abs_state == abs_state) {
2608 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2609 "in %d secs\n", fw_state, max_wait);
2613 printk(KERN_INFO "megasas: FW now in Ready state\n");
2619 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2620 * @instance: Adapter soft state
2622 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2625 u32 max_cmd = instance->max_fw_cmds;
2626 struct megasas_cmd *cmd;
2628 if (!instance->frame_dma_pool)
2632 * Return all frames to pool
2634 for (i = 0; i < max_cmd; i++) {
2636 cmd = instance->cmd_list[i];
2639 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2640 cmd->frame_phys_addr);
2643 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2644 cmd->sense_phys_addr);
2648 * Now destroy the pool itself
2650 pci_pool_destroy(instance->frame_dma_pool);
2651 pci_pool_destroy(instance->sense_dma_pool);
2653 instance->frame_dma_pool = NULL;
2654 instance->sense_dma_pool = NULL;
2658 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2659 * @instance: Adapter soft state
2661 * Each command packet has an embedded DMA memory buffer that is used for
2662 * filling MFI frame and the SG list that immediately follows the frame. This
2663 * function creates those DMA memory buffers for each command packet by using
2664 * PCI pool facility.
2666 static int megasas_create_frame_pool(struct megasas_instance *instance)
2674 struct megasas_cmd *cmd;
2676 max_cmd = instance->max_fw_cmds;
2679 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2680 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2682 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2683 sizeof(struct megasas_sge32);
2685 if (instance->flag_ieee) {
2686 sge_sz = sizeof(struct megasas_sge_skinny);
2690 * Calculated the number of 64byte frames required for SGL
2692 sgl_sz = sge_sz * instance->max_num_sge;
2693 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2697 * We need one extra frame for the MFI command
2701 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2703 * Use DMA pool facility provided by PCI layer
2705 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2706 instance->pdev, total_sz, 64,
2709 if (!instance->frame_dma_pool) {
2710 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2714 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2715 instance->pdev, 128, 4, 0);
2717 if (!instance->sense_dma_pool) {
2718 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2720 pci_pool_destroy(instance->frame_dma_pool);
2721 instance->frame_dma_pool = NULL;
2727 * Allocate and attach a frame to each of the commands in cmd_list.
2728 * By making cmd->index as the context instead of the &cmd, we can
2729 * always use 32bit context regardless of the architecture
2731 for (i = 0; i < max_cmd; i++) {
2733 cmd = instance->cmd_list[i];
2735 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2736 GFP_KERNEL, &cmd->frame_phys_addr);
2738 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2739 GFP_KERNEL, &cmd->sense_phys_addr);
2742 * megasas_teardown_frame_pool() takes care of freeing
2743 * whatever has been allocated
2745 if (!cmd->frame || !cmd->sense) {
2746 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2747 megasas_teardown_frame_pool(instance);
2751 memset(cmd->frame, 0, total_sz);
2752 cmd->frame->io.context = cmd->index;
2753 cmd->frame->io.pad_0 = 0;
2760 * megasas_free_cmds - Free all the cmds in the free cmd pool
2761 * @instance: Adapter soft state
2763 static void megasas_free_cmds(struct megasas_instance *instance)
2766 /* First free the MFI frame pool */
2767 megasas_teardown_frame_pool(instance);
2769 /* Free all the commands in the cmd_list */
2770 for (i = 0; i < instance->max_fw_cmds; i++)
2771 kfree(instance->cmd_list[i]);
2773 /* Free the cmd_list buffer itself */
2774 kfree(instance->cmd_list);
2775 instance->cmd_list = NULL;
2777 INIT_LIST_HEAD(&instance->cmd_pool);
2781 * megasas_alloc_cmds - Allocates the command packets
2782 * @instance: Adapter soft state
2784 * Each command that is issued to the FW, whether IO commands from the OS or
2785 * internal commands like IOCTLs, are wrapped in local data structure called
2786 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2789 * Each frame has a 32-bit field called context (tag). This context is used
2790 * to get back the megasas_cmd from the frame when a frame gets completed in
2791 * the ISR. Typically the address of the megasas_cmd itself would be used as
2792 * the context. But we wanted to keep the differences between 32 and 64 bit
2793 * systems to the mininum. We always use 32 bit integers for the context. In
2794 * this driver, the 32 bit values are the indices into an array cmd_list.
2795 * This array is used only to look up the megasas_cmd given the context. The
2796 * free commands themselves are maintained in a linked list called cmd_pool.
2798 static int megasas_alloc_cmds(struct megasas_instance *instance)
2803 struct megasas_cmd *cmd;
2805 max_cmd = instance->max_fw_cmds;
2808 * instance->cmd_list is an array of struct megasas_cmd pointers.
2809 * Allocate the dynamic array first and then allocate individual
2812 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2814 if (!instance->cmd_list) {
2815 printk(KERN_DEBUG "megasas: out of memory\n");
2820 for (i = 0; i < max_cmd; i++) {
2821 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2824 if (!instance->cmd_list[i]) {
2826 for (j = 0; j < i; j++)
2827 kfree(instance->cmd_list[j]);
2829 kfree(instance->cmd_list);
2830 instance->cmd_list = NULL;
2837 * Add all the commands to command pool (instance->cmd_pool)
2839 for (i = 0; i < max_cmd; i++) {
2840 cmd = instance->cmd_list[i];
2841 memset(cmd, 0, sizeof(struct megasas_cmd));
2844 cmd->instance = instance;
2846 list_add_tail(&cmd->list, &instance->cmd_pool);
2850 * Create a frame pool and assign one frame to each cmd
2852 if (megasas_create_frame_pool(instance)) {
2853 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2854 megasas_free_cmds(instance);
2861 * megasas_get_pd_list_info - Returns FW's pd_list structure
2862 * @instance: Adapter soft state
2863 * @pd_list: pd_list structure
2865 * Issues an internal command (DCMD) to get the FW's controller PD
2866 * list structure. This information is mainly used to find out SYSTEM
2867 * supported by the FW.
2870 megasas_get_pd_list(struct megasas_instance *instance)
2872 int ret = 0, pd_index = 0;
2873 struct megasas_cmd *cmd;
2874 struct megasas_dcmd_frame *dcmd;
2875 struct MR_PD_LIST *ci;
2876 struct MR_PD_ADDRESS *pd_addr;
2877 dma_addr_t ci_h = 0;
2879 cmd = megasas_get_cmd(instance);
2882 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2886 dcmd = &cmd->frame->dcmd;
2888 ci = pci_alloc_consistent(instance->pdev,
2889 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2892 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2893 megasas_return_cmd(instance, cmd);
2897 memset(ci, 0, sizeof(*ci));
2898 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2900 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2901 dcmd->mbox.b[1] = 0;
2902 dcmd->cmd = MFI_CMD_DCMD;
2903 dcmd->cmd_status = 0xFF;
2904 dcmd->sge_count = 1;
2905 dcmd->flags = MFI_FRAME_DIR_READ;
2908 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2909 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2910 dcmd->sgl.sge32[0].phys_addr = ci_h;
2911 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2913 if (!megasas_issue_polled(instance, cmd)) {
2920 * the following function will get the instance PD LIST.
2927 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2929 memset(instance->pd_list, 0,
2930 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2932 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2934 instance->pd_list[pd_addr->deviceId].tid =
2936 instance->pd_list[pd_addr->deviceId].driveType =
2937 pd_addr->scsiDevType;
2938 instance->pd_list[pd_addr->deviceId].driveState =
2944 pci_free_consistent(instance->pdev,
2945 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2947 megasas_return_cmd(instance, cmd);
2953 * megasas_get_ld_list_info - Returns FW's ld_list structure
2954 * @instance: Adapter soft state
2955 * @ld_list: ld_list structure
2957 * Issues an internal command (DCMD) to get the FW's controller PD
2958 * list structure. This information is mainly used to find out SYSTEM
2959 * supported by the FW.
2962 megasas_get_ld_list(struct megasas_instance *instance)
2964 int ret = 0, ld_index = 0, ids = 0;
2965 struct megasas_cmd *cmd;
2966 struct megasas_dcmd_frame *dcmd;
2967 struct MR_LD_LIST *ci;
2968 dma_addr_t ci_h = 0;
2970 cmd = megasas_get_cmd(instance);
2973 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2977 dcmd = &cmd->frame->dcmd;
2979 ci = pci_alloc_consistent(instance->pdev,
2980 sizeof(struct MR_LD_LIST),
2984 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2985 megasas_return_cmd(instance, cmd);
2989 memset(ci, 0, sizeof(*ci));
2990 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2992 dcmd->cmd = MFI_CMD_DCMD;
2993 dcmd->cmd_status = 0xFF;
2994 dcmd->sge_count = 1;
2995 dcmd->flags = MFI_FRAME_DIR_READ;
2997 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2998 dcmd->opcode = MR_DCMD_LD_GET_LIST;
2999 dcmd->sgl.sge32[0].phys_addr = ci_h;
3000 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3003 if (!megasas_issue_polled(instance, cmd)) {
3009 /* the following function will get the instance PD LIST */
3011 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3012 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3014 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3015 if (ci->ldList[ld_index].state != 0) {
3016 ids = ci->ldList[ld_index].ref.targetId;
3017 instance->ld_ids[ids] =
3018 ci->ldList[ld_index].ref.targetId;
3023 pci_free_consistent(instance->pdev,
3024 sizeof(struct MR_LD_LIST),
3028 megasas_return_cmd(instance, cmd);
3033 * megasas_get_controller_info - Returns FW's controller structure
3034 * @instance: Adapter soft state
3035 * @ctrl_info: Controller information structure
3037 * Issues an internal command (DCMD) to get the FW's controller structure.
3038 * This information is mainly used to find out the maximum IO transfer per
3039 * command supported by the FW.
3042 megasas_get_ctrl_info(struct megasas_instance *instance,
3043 struct megasas_ctrl_info *ctrl_info)
3046 struct megasas_cmd *cmd;
3047 struct megasas_dcmd_frame *dcmd;
3048 struct megasas_ctrl_info *ci;
3049 dma_addr_t ci_h = 0;
3051 cmd = megasas_get_cmd(instance);
3054 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3058 dcmd = &cmd->frame->dcmd;
3060 ci = pci_alloc_consistent(instance->pdev,
3061 sizeof(struct megasas_ctrl_info), &ci_h);
3064 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3065 megasas_return_cmd(instance, cmd);
3069 memset(ci, 0, sizeof(*ci));
3070 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3072 dcmd->cmd = MFI_CMD_DCMD;
3073 dcmd->cmd_status = 0xFF;
3074 dcmd->sge_count = 1;
3075 dcmd->flags = MFI_FRAME_DIR_READ;
3078 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3079 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3080 dcmd->sgl.sge32[0].phys_addr = ci_h;
3081 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3083 if (!megasas_issue_polled(instance, cmd)) {
3085 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3090 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3093 megasas_return_cmd(instance, cmd);
3098 * megasas_issue_init_mfi - Initializes the FW
3099 * @instance: Adapter soft state
3101 * Issues the INIT MFI cmd
3104 megasas_issue_init_mfi(struct megasas_instance *instance)
3108 struct megasas_cmd *cmd;
3110 struct megasas_init_frame *init_frame;
3111 struct megasas_init_queue_info *initq_info;
3112 dma_addr_t init_frame_h;
3113 dma_addr_t initq_info_h;
3116 * Prepare a init frame. Note the init frame points to queue info
3117 * structure. Each frame has SGL allocated after first 64 bytes. For
3118 * this frame - since we don't need any SGL - we use SGL's space as
3119 * queue info structure
3121 * We will not get a NULL command below. We just created the pool.
3123 cmd = megasas_get_cmd(instance);
3125 init_frame = (struct megasas_init_frame *)cmd->frame;
3126 initq_info = (struct megasas_init_queue_info *)
3127 ((unsigned long)init_frame + 64);
3129 init_frame_h = cmd->frame_phys_addr;
3130 initq_info_h = init_frame_h + 64;
3132 context = init_frame->context;
3133 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3134 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3135 init_frame->context = context;
3137 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3138 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3140 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3141 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3143 init_frame->cmd = MFI_CMD_INIT;
3144 init_frame->cmd_status = 0xFF;
3145 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3147 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3150 * disable the intr before firing the init frame to FW
3152 instance->instancet->disable_intr(instance->reg_set);
3155 * Issue the init frame in polled mode
3158 if (megasas_issue_polled(instance, cmd)) {
3159 printk(KERN_ERR "megasas: Failed to init firmware\n");
3160 megasas_return_cmd(instance, cmd);
3164 megasas_return_cmd(instance, cmd);
3173 * megasas_start_timer - Initializes a timer object
3174 * @instance: Adapter soft state
3175 * @timer: timer object to be initialized
3176 * @fn: timer function
3177 * @interval: time interval between timer function call
3180 megasas_start_timer(struct megasas_instance *instance,
3181 struct timer_list *timer,
3182 void *fn, unsigned long interval)
3185 timer->expires = jiffies + interval;
3186 timer->data = (unsigned long)instance;
3187 timer->function = fn;
3192 * megasas_io_completion_timer - Timer fn
3193 * @instance_addr: Address of adapter soft state
3195 * Schedules tasklet for cmd completion
3196 * if poll_mode_io is set
3199 megasas_io_completion_timer(unsigned long instance_addr)
3201 struct megasas_instance *instance =
3202 (struct megasas_instance *)instance_addr;
3204 if (atomic_read(&instance->fw_outstanding))
3205 tasklet_schedule(&instance->isr_tasklet);
3209 mod_timer(&instance->io_completion_timer,
3210 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3214 * megasas_init_mfi - Initializes the FW
3215 * @instance: Adapter soft state
3217 * This is the main function for initializing MFI firmware.
3219 static int megasas_init_mfi(struct megasas_instance *instance)
3226 struct megasas_register_set __iomem *reg_set;
3227 struct megasas_ctrl_info *ctrl_info;
3229 * Map the message registers
3231 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3232 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3233 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3234 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
3235 instance->base_addr = pci_resource_start(instance->pdev, 1);
3237 instance->base_addr = pci_resource_start(instance->pdev, 0);
3240 if (pci_request_selected_regions(instance->pdev,
3241 pci_select_bars(instance->pdev, IORESOURCE_MEM),
3243 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3247 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3249 if (!instance->reg_set) {
3250 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3254 reg_set = instance->reg_set;
3256 switch(instance->pdev->device)
3258 case PCI_DEVICE_ID_LSI_SAS1078R:
3259 case PCI_DEVICE_ID_LSI_SAS1078DE:
3260 instance->instancet = &megasas_instance_template_ppc;
3262 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3263 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3264 instance->instancet = &megasas_instance_template_gen2;
3266 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3267 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3268 instance->instancet = &megasas_instance_template_skinny;
3270 case PCI_DEVICE_ID_LSI_SAS1064R:
3271 case PCI_DEVICE_ID_DELL_PERC5:
3273 instance->instancet = &megasas_instance_template_xscale;
3278 * We expect the FW state to be READY
3280 if (megasas_transition_to_ready(instance))
3281 goto fail_ready_state;
3284 * Get various operational parameters from status register
3286 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3288 * Reduce the max supported cmds by 1. This is to ensure that the
3289 * reply_q_sz (1 more than the max cmd that driver may send)
3290 * does not exceed max cmds that the FW can support
3292 instance->max_fw_cmds = instance->max_fw_cmds-1;
3293 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3296 * Create a pool of commands
3298 if (megasas_alloc_cmds(instance))
3299 goto fail_alloc_cmds;
3302 * Allocate memory for reply queue. Length of reply queue should
3303 * be _one_ more than the maximum commands handled by the firmware.
3305 * Note: When FW completes commands, it places corresponding contex
3306 * values in this circular reply queue. This circular queue is a fairly
3307 * typical producer-consumer queue. FW is the producer (of completed
3308 * commands) and the driver is the consumer.
3310 context_sz = sizeof(u32);
3311 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3313 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3315 &instance->reply_queue_h);
3317 if (!instance->reply_queue) {
3318 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3319 goto fail_reply_queue;
3322 if (megasas_issue_init_mfi(instance))
3325 instance->fw_support_ieee = 0;
3326 instance->fw_support_ieee =
3327 (instance->instancet->read_fw_status_reg(reg_set) &
3330 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3331 instance->fw_support_ieee);
3333 if (instance->fw_support_ieee)
3334 instance->flag_ieee = 1;
3337 * the following function will get the PD LIST.
3340 memset(instance->pd_list, 0 ,
3341 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3342 megasas_get_pd_list(instance);
3344 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3345 megasas_get_ld_list(instance);
3347 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3350 * Compute the max allowed sectors per IO: The controller info has two
3351 * limits on max sectors. Driver should use the minimum of these two.
3353 * 1 << stripe_sz_ops.min = max sectors per strip
3355 * Note that older firmwares ( < FW ver 30) didn't report information
3356 * to calculate max_sectors_1. So the number ended up as zero always.
3359 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3361 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3362 ctrl_info->max_strips_per_io;
3363 max_sectors_2 = ctrl_info->max_request_size;
3365 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3366 instance->disableOnlineCtrlReset =
3367 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3370 instance->max_sectors_per_req = instance->max_num_sge *
3372 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3373 instance->max_sectors_per_req = tmp_sectors;
3378 * Setup tasklet for cmd completion
3381 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3382 (unsigned long)instance);
3384 /* Initialize the cmd completion timer */
3386 megasas_start_timer(instance, &instance->io_completion_timer,
3387 megasas_io_completion_timer,
3388 MEGASAS_COMPLETION_TIMER_INTERVAL);
3393 pci_free_consistent(instance->pdev, reply_q_sz,
3394 instance->reply_queue, instance->reply_queue_h);
3396 megasas_free_cmds(instance);
3400 iounmap(instance->reg_set);
3403 pci_release_selected_regions(instance->pdev,
3404 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3410 * megasas_release_mfi - Reverses the FW initialization
3411 * @intance: Adapter soft state
3413 static void megasas_release_mfi(struct megasas_instance *instance)
3415 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3417 pci_free_consistent(instance->pdev, reply_q_sz,
3418 instance->reply_queue, instance->reply_queue_h);
3420 megasas_free_cmds(instance);
3422 iounmap(instance->reg_set);
3424 pci_release_selected_regions(instance->pdev,
3425 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3429 * megasas_get_seq_num - Gets latest event sequence numbers
3430 * @instance: Adapter soft state
3431 * @eli: FW event log sequence numbers information
3433 * FW maintains a log of all events in a non-volatile area. Upper layers would
3434 * usually find out the latest sequence number of the events, the seq number at
3435 * the boot etc. They would "read" all the events below the latest seq number
3436 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3437 * number), they would subsribe to AEN (asynchronous event notification) and
3438 * wait for the events to happen.
3441 megasas_get_seq_num(struct megasas_instance *instance,
3442 struct megasas_evt_log_info *eli)
3444 struct megasas_cmd *cmd;
3445 struct megasas_dcmd_frame *dcmd;
3446 struct megasas_evt_log_info *el_info;
3447 dma_addr_t el_info_h = 0;
3449 cmd = megasas_get_cmd(instance);
3455 dcmd = &cmd->frame->dcmd;
3456 el_info = pci_alloc_consistent(instance->pdev,
3457 sizeof(struct megasas_evt_log_info),
3461 megasas_return_cmd(instance, cmd);
3465 memset(el_info, 0, sizeof(*el_info));
3466 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3468 dcmd->cmd = MFI_CMD_DCMD;
3469 dcmd->cmd_status = 0x0;
3470 dcmd->sge_count = 1;
3471 dcmd->flags = MFI_FRAME_DIR_READ;
3474 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3475 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3476 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3477 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3479 megasas_issue_blocked_cmd(instance, cmd);
3482 * Copy the data back into callers buffer
3484 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3486 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3487 el_info, el_info_h);
3489 megasas_return_cmd(instance, cmd);
3495 * megasas_register_aen - Registers for asynchronous event notification
3496 * @instance: Adapter soft state
3497 * @seq_num: The starting sequence number
3498 * @class_locale: Class of the event
3500 * This function subscribes for AEN for events beyond the @seq_num. It requests
3501 * to be notified if and only if the event is of type @class_locale
3504 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3505 u32 class_locale_word)
3508 struct megasas_cmd *cmd;
3509 struct megasas_dcmd_frame *dcmd;
3510 union megasas_evt_class_locale curr_aen;
3511 union megasas_evt_class_locale prev_aen;
3514 * If there an AEN pending already (aen_cmd), check if the
3515 * class_locale of that pending AEN is inclusive of the new
3516 * AEN request we currently have. If it is, then we don't have
3517 * to do anything. In other words, whichever events the current
3518 * AEN request is subscribing to, have already been subscribed
3521 * If the old_cmd is _not_ inclusive, then we have to abort
3522 * that command, form a class_locale that is superset of both
3523 * old and current and re-issue to the FW
3526 curr_aen.word = class_locale_word;
3528 if (instance->aen_cmd) {
3530 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3533 * A class whose enum value is smaller is inclusive of all
3534 * higher values. If a PROGRESS (= -1) was previously
3535 * registered, then a new registration requests for higher
3536 * classes need not be sent to FW. They are automatically
3539 * Locale numbers don't have such hierarchy. They are bitmap
3542 if ((prev_aen.members.class <= curr_aen.members.class) &&
3543 !((prev_aen.members.locale & curr_aen.members.locale) ^
3544 curr_aen.members.locale)) {
3546 * Previously issued event registration includes
3547 * current request. Nothing to do.
3551 curr_aen.members.locale |= prev_aen.members.locale;
3553 if (prev_aen.members.class < curr_aen.members.class)
3554 curr_aen.members.class = prev_aen.members.class;
3556 instance->aen_cmd->abort_aen = 1;
3557 ret_val = megasas_issue_blocked_abort_cmd(instance,
3562 printk(KERN_DEBUG "megasas: Failed to abort "
3563 "previous AEN command\n");
3569 cmd = megasas_get_cmd(instance);
3574 dcmd = &cmd->frame->dcmd;
3576 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3579 * Prepare DCMD for aen registration
3581 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3583 dcmd->cmd = MFI_CMD_DCMD;
3584 dcmd->cmd_status = 0x0;
3585 dcmd->sge_count = 1;
3586 dcmd->flags = MFI_FRAME_DIR_READ;
3589 instance->last_seq_num = seq_num;
3590 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3591 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3592 dcmd->mbox.w[0] = seq_num;
3593 dcmd->mbox.w[1] = curr_aen.word;
3594 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3595 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3597 if (instance->aen_cmd != NULL) {
3598 megasas_return_cmd(instance, cmd);
3603 * Store reference to the cmd used to register for AEN. When an
3604 * application wants us to register for AEN, we have to abort this
3605 * cmd and re-register with a new EVENT LOCALE supplied by that app
3607 instance->aen_cmd = cmd;
3610 * Issue the aen registration frame
3612 instance->instancet->fire_cmd(instance,
3613 cmd->frame_phys_addr, 0, instance->reg_set);
3619 * megasas_start_aen - Subscribes to AEN during driver load time
3620 * @instance: Adapter soft state
3622 static int megasas_start_aen(struct megasas_instance *instance)
3624 struct megasas_evt_log_info eli;
3625 union megasas_evt_class_locale class_locale;
3628 * Get the latest sequence number from FW
3630 memset(&eli, 0, sizeof(eli));
3632 if (megasas_get_seq_num(instance, &eli))
3636 * Register AEN with FW for latest sequence number plus 1
3638 class_locale.members.reserved = 0;
3639 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3640 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3642 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3647 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3648 * @instance: Adapter soft state
3650 static int megasas_io_attach(struct megasas_instance *instance)
3652 struct Scsi_Host *host = instance->host;
3655 * Export parameters required by SCSI mid-layer
3657 host->irq = instance->pdev->irq;
3658 host->unique_id = instance->unique_id;
3659 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3660 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3662 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3665 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3666 host->this_id = instance->init_id;
3667 host->sg_tablesize = instance->max_num_sge;
3669 * Check if the module parameter value for max_sectors can be used
3671 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3672 instance->max_sectors_per_req = max_sectors;
3675 if (((instance->pdev->device ==
3676 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3677 (instance->pdev->device ==
3678 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3679 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3680 instance->max_sectors_per_req = max_sectors;
3682 printk(KERN_INFO "megasas: max_sectors should be > 0"
3683 "and <= %d (or < 1MB for GEN2 controller)\n",
3684 instance->max_sectors_per_req);
3689 host->max_sectors = instance->max_sectors_per_req;
3690 host->cmd_per_lun = 128;
3691 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3692 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3693 host->max_lun = MEGASAS_MAX_LUN;
3694 host->max_cmd_len = 16;
3697 * Notify the mid-layer about the new controller
3699 if (scsi_add_host(host, &instance->pdev->dev)) {
3700 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3705 * Trigger SCSI to scan our drives
3707 scsi_scan_host(host);
3712 megasas_set_dma_mask(struct pci_dev *pdev)
3715 * All our contollers are capable of performing 64-bit DMA
3718 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3720 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3721 goto fail_set_dma_mask;
3724 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3725 goto fail_set_dma_mask;
3734 * megasas_probe_one - PCI hotplug entry point
3735 * @pdev: PCI device structure
3736 * @id: PCI ids of supported hotplugged adapter
3738 static int __devinit
3739 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3742 struct Scsi_Host *host;
3743 struct megasas_instance *instance;
3746 * Announce PCI information
3748 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3749 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3750 pdev->subsystem_device);
3752 printk("bus %d:slot %d:func %d\n",
3753 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3756 * PCI prepping: enable device set bus mastering and dma mask
3758 rval = pci_enable_device_mem(pdev);
3764 pci_set_master(pdev);
3766 if (megasas_set_dma_mask(pdev))
3767 goto fail_set_dma_mask;
3769 host = scsi_host_alloc(&megasas_template,
3770 sizeof(struct megasas_instance));
3773 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3774 goto fail_alloc_instance;
3777 instance = (struct megasas_instance *)host->hostdata;
3778 memset(instance, 0, sizeof(*instance));
3779 atomic_set( &instance->fw_reset_no_pci_access, 0 );
3781 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3782 &instance->producer_h);
3783 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3784 &instance->consumer_h);
3786 if (!instance->producer || !instance->consumer) {
3787 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3788 "producer, consumer\n");
3789 goto fail_alloc_dma_buf;
3792 *instance->producer = 0;
3793 *instance->consumer = 0;
3794 megasas_poll_wait_aen = 0;
3795 instance->flag_ieee = 0;
3796 instance->ev = NULL;
3797 instance->issuepend_done = 1;
3798 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3799 megasas_poll_wait_aen = 0;
3801 instance->evt_detail = pci_alloc_consistent(pdev,
3803 megasas_evt_detail),
3804 &instance->evt_detail_h);
3806 if (!instance->evt_detail) {
3807 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3808 "event detail structure\n");
3809 goto fail_alloc_dma_buf;
3813 * Initialize locks and queues
3815 INIT_LIST_HEAD(&instance->cmd_pool);
3816 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
3818 atomic_set(&instance->fw_outstanding,0);
3820 init_waitqueue_head(&instance->int_cmd_wait_q);
3821 init_waitqueue_head(&instance->abort_cmd_wait_q);
3823 spin_lock_init(&instance->cmd_pool_lock);
3824 spin_lock_init(&instance->hba_lock);
3825 spin_lock_init(&instance->completion_lock);
3826 spin_lock_init(&poll_aen_lock);
3828 mutex_init(&instance->aen_mutex);
3831 * Initialize PCI related and misc parameters
3833 instance->pdev = pdev;
3834 instance->host = host;
3835 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3836 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3838 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3839 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3840 instance->flag_ieee = 1;
3841 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3843 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3845 megasas_dbg_lvl = 0;
3847 instance->unload = 1;
3848 instance->last_time = 0;
3849 instance->disableOnlineCtrlReset = 1;
3851 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
3854 * Initialize MFI Firmware
3856 if (megasas_init_mfi(instance))
3862 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
3863 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3867 instance->instancet->enable_intr(instance->reg_set);
3870 * Store instance in PCI softstate
3872 pci_set_drvdata(pdev, instance);
3875 * Add this controller to megasas_mgmt_info structure so that it
3876 * can be exported to management applications
3878 megasas_mgmt_info.count++;
3879 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3880 megasas_mgmt_info.max_index++;
3883 * Initiate AEN (Asynchronous Event Notification)
3885 if (megasas_start_aen(instance)) {
3886 printk(KERN_DEBUG "megasas: start aen failed\n");
3887 goto fail_start_aen;
3891 * Register with SCSI mid-layer
3893 if (megasas_io_attach(instance))
3894 goto fail_io_attach;
3896 instance->unload = 0;
3901 megasas_mgmt_info.count--;
3902 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3903 megasas_mgmt_info.max_index--;
3905 pci_set_drvdata(pdev, NULL);
3906 instance->instancet->disable_intr(instance->reg_set);
3907 free_irq(instance->pdev->irq, instance);
3909 megasas_release_mfi(instance);
3914 if (instance->evt_detail)
3915 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3916 instance->evt_detail,
3917 instance->evt_detail_h);
3919 if (instance->producer)
3920 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3921 instance->producer_h);
3922 if (instance->consumer)
3923 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3924 instance->consumer_h);
3925 scsi_host_put(host);
3927 fail_alloc_instance:
3929 pci_disable_device(pdev);
3935 * megasas_flush_cache - Requests FW to flush all its caches
3936 * @instance: Adapter soft state
3938 static void megasas_flush_cache(struct megasas_instance *instance)
3940 struct megasas_cmd *cmd;
3941 struct megasas_dcmd_frame *dcmd;
3943 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3946 cmd = megasas_get_cmd(instance);
3951 dcmd = &cmd->frame->dcmd;
3953 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3955 dcmd->cmd = MFI_CMD_DCMD;
3956 dcmd->cmd_status = 0x0;
3957 dcmd->sge_count = 0;
3958 dcmd->flags = MFI_FRAME_DIR_NONE;
3961 dcmd->data_xfer_len = 0;
3962 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3963 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3965 megasas_issue_blocked_cmd(instance, cmd);
3967 megasas_return_cmd(instance, cmd);
3973 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3974 * @instance: Adapter soft state
3975 * @opcode: Shutdown/Hibernate
3977 static void megasas_shutdown_controller(struct megasas_instance *instance,
3980 struct megasas_cmd *cmd;
3981 struct megasas_dcmd_frame *dcmd;
3983 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3986 cmd = megasas_get_cmd(instance);
3991 if (instance->aen_cmd)
3992 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3994 dcmd = &cmd->frame->dcmd;
3996 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3998 dcmd->cmd = MFI_CMD_DCMD;
3999 dcmd->cmd_status = 0x0;
4000 dcmd->sge_count = 0;
4001 dcmd->flags = MFI_FRAME_DIR_NONE;
4004 dcmd->data_xfer_len = 0;
4005 dcmd->opcode = opcode;
4007 megasas_issue_blocked_cmd(instance, cmd);
4009 megasas_return_cmd(instance, cmd);
4016 * megasas_suspend - driver suspend entry point
4017 * @pdev: PCI device structure
4018 * @state: PCI power state to suspend routine
4021 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4023 struct Scsi_Host *host;
4024 struct megasas_instance *instance;
4026 instance = pci_get_drvdata(pdev);
4027 host = instance->host;
4028 instance->unload = 1;
4031 del_timer_sync(&instance->io_completion_timer);
4033 megasas_flush_cache(instance);
4034 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4036 /* cancel the delayed work if this work still in queue */
4037 if (instance->ev != NULL) {
4038 struct megasas_aen_event *ev = instance->ev;
4039 cancel_delayed_work(
4040 (struct delayed_work *)&ev->hotplug_work);
4041 flush_scheduled_work();
4042 instance->ev = NULL;
4045 tasklet_kill(&instance->isr_tasklet);
4047 pci_set_drvdata(instance->pdev, instance);
4048 instance->instancet->disable_intr(instance->reg_set);
4049 free_irq(instance->pdev->irq, instance);
4051 pci_save_state(pdev);
4052 pci_disable_device(pdev);
4054 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4060 * megasas_resume- driver resume entry point
4061 * @pdev: PCI device structure
4064 megasas_resume(struct pci_dev *pdev)
4067 struct Scsi_Host *host;
4068 struct megasas_instance *instance;
4070 instance = pci_get_drvdata(pdev);
4071 host = instance->host;
4072 pci_set_power_state(pdev, PCI_D0);
4073 pci_enable_wake(pdev, PCI_D0, 0);
4074 pci_restore_state(pdev);
4077 * PCI prepping: enable device set bus mastering and dma mask
4079 rval = pci_enable_device_mem(pdev);
4082 printk(KERN_ERR "megasas: Enable device failed\n");
4086 pci_set_master(pdev);
4088 if (megasas_set_dma_mask(pdev))
4089 goto fail_set_dma_mask;
4092 * Initialize MFI Firmware
4095 *instance->producer = 0;
4096 *instance->consumer = 0;
4098 atomic_set(&instance->fw_outstanding, 0);
4101 * We expect the FW state to be READY
4103 if (megasas_transition_to_ready(instance))
4104 goto fail_ready_state;
4106 if (megasas_issue_init_mfi(instance))
4109 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4110 (unsigned long)instance);
4115 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
4116 "megasas", instance)) {
4117 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4121 instance->instancet->enable_intr(instance->reg_set);
4124 * Initiate AEN (Asynchronous Event Notification)
4126 if (megasas_start_aen(instance))
4127 printk(KERN_ERR "megasas: Start AEN failed\n");
4129 /* Initialize the cmd completion timer */
4131 megasas_start_timer(instance, &instance->io_completion_timer,
4132 megasas_io_completion_timer,
4133 MEGASAS_COMPLETION_TIMER_INTERVAL);
4134 instance->unload = 0;
4140 if (instance->evt_detail)
4141 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4142 instance->evt_detail,
4143 instance->evt_detail_h);
4145 if (instance->producer)
4146 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4147 instance->producer_h);
4148 if (instance->consumer)
4149 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4150 instance->consumer_h);
4151 scsi_host_put(host);
4156 pci_disable_device(pdev);
4161 #define megasas_suspend NULL
4162 #define megasas_resume NULL
4166 * megasas_detach_one - PCI hot"un"plug entry point
4167 * @pdev: PCI device structure
4169 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4172 struct Scsi_Host *host;
4173 struct megasas_instance *instance;
4175 instance = pci_get_drvdata(pdev);
4176 instance->unload = 1;
4177 host = instance->host;
4180 del_timer_sync(&instance->io_completion_timer);
4182 scsi_remove_host(instance->host);
4183 megasas_flush_cache(instance);
4184 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4186 /* cancel the delayed work if this work still in queue*/
4187 if (instance->ev != NULL) {
4188 struct megasas_aen_event *ev = instance->ev;
4189 cancel_delayed_work(
4190 (struct delayed_work *)&ev->hotplug_work);
4191 flush_scheduled_work();
4192 instance->ev = NULL;
4195 tasklet_kill(&instance->isr_tasklet);
4198 * Take the instance off the instance array. Note that we will not
4199 * decrement the max_index. We let this array be sparse array
4201 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4202 if (megasas_mgmt_info.instance[i] == instance) {
4203 megasas_mgmt_info.count--;
4204 megasas_mgmt_info.instance[i] = NULL;
4210 pci_set_drvdata(instance->pdev, NULL);
4212 instance->instancet->disable_intr(instance->reg_set);
4214 free_irq(instance->pdev->irq, instance);
4216 megasas_release_mfi(instance);
4218 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4219 instance->evt_detail, instance->evt_detail_h);
4221 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4222 instance->producer_h);
4224 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4225 instance->consumer_h);
4227 scsi_host_put(host);
4229 pci_set_drvdata(pdev, NULL);
4231 pci_disable_device(pdev);
4237 * megasas_shutdown - Shutdown entry point
4238 * @device: Generic device structure
4240 static void megasas_shutdown(struct pci_dev *pdev)
4242 struct megasas_instance *instance = pci_get_drvdata(pdev);
4243 instance->unload = 1;
4244 megasas_flush_cache(instance);
4245 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4249 * megasas_mgmt_open - char node "open" entry point
4251 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4254 * Allow only those users with admin rights
4256 if (!capable(CAP_SYS_ADMIN))
4263 * megasas_mgmt_fasync - Async notifier registration from applications
4265 * This function adds the calling process to a driver global queue. When an
4266 * event occurs, SIGIO will be sent to all processes in this queue.
4268 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4272 mutex_lock(&megasas_async_queue_mutex);
4274 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4276 mutex_unlock(&megasas_async_queue_mutex);
4279 /* For sanity check when we get ioctl */
4280 filep->private_data = filep;
4284 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4290 * megasas_mgmt_poll - char node "poll" entry point
4292 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4295 unsigned long flags;
4296 poll_wait(file, &megasas_poll_wait, wait);
4297 spin_lock_irqsave(&poll_aen_lock, flags);
4298 if (megasas_poll_wait_aen)
4299 mask = (POLLIN | POLLRDNORM);
4302 spin_unlock_irqrestore(&poll_aen_lock, flags);
4307 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4308 * @instance: Adapter soft state
4309 * @argp: User's ioctl packet
4312 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4313 struct megasas_iocpacket __user * user_ioc,
4314 struct megasas_iocpacket *ioc)
4316 struct megasas_sge32 *kern_sge32;
4317 struct megasas_cmd *cmd;
4318 void *kbuff_arr[MAX_IOCTL_SGE];
4319 dma_addr_t buf_handle = 0;
4322 dma_addr_t sense_handle;
4323 unsigned long *sense_ptr;
4325 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4327 if (ioc->sge_count > MAX_IOCTL_SGE) {
4328 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4329 ioc->sge_count, MAX_IOCTL_SGE);
4333 cmd = megasas_get_cmd(instance);
4335 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4340 * User's IOCTL packet has 2 frames (maximum). Copy those two
4341 * frames into our cmd's frames. cmd->frame's context will get
4342 * overwritten when we copy from user's frames. So set that value
4345 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4346 cmd->frame->hdr.context = cmd->index;
4347 cmd->frame->hdr.pad_0 = 0;
4350 * The management interface between applications and the fw uses
4351 * MFI frames. E.g, RAID configuration changes, LD property changes
4352 * etc are accomplishes through different kinds of MFI frames. The
4353 * driver needs to care only about substituting user buffers with
4354 * kernel buffers in SGLs. The location of SGL is embedded in the
4355 * struct iocpacket itself.
4357 kern_sge32 = (struct megasas_sge32 *)
4358 ((unsigned long)cmd->frame + ioc->sgl_off);
4361 * For each user buffer, create a mirror buffer and copy in
4363 for (i = 0; i < ioc->sge_count; i++) {
4364 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4365 ioc->sgl[i].iov_len,
4366 &buf_handle, GFP_KERNEL);
4367 if (!kbuff_arr[i]) {
4368 printk(KERN_DEBUG "megasas: Failed to alloc "
4369 "kernel SGL buffer for IOCTL \n");
4375 * We don't change the dma_coherent_mask, so
4376 * pci_alloc_consistent only returns 32bit addresses
4378 kern_sge32[i].phys_addr = (u32) buf_handle;
4379 kern_sge32[i].length = ioc->sgl[i].iov_len;
4382 * We created a kernel buffer corresponding to the
4383 * user buffer. Now copy in from the user buffer
4385 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4386 (u32) (ioc->sgl[i].iov_len))) {
4392 if (ioc->sense_len) {
4393 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4394 &sense_handle, GFP_KERNEL);
4401 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4402 *sense_ptr = sense_handle;
4406 * Set the sync_cmd flag so that the ISR knows not to complete this
4407 * cmd to the SCSI mid-layer
4410 megasas_issue_blocked_cmd(instance, cmd);
4414 * copy out the kernel buffers to user buffers
4416 for (i = 0; i < ioc->sge_count; i++) {
4417 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4418 ioc->sgl[i].iov_len)) {
4425 * copy out the sense
4427 if (ioc->sense_len) {
4429 * sense_ptr points to the location that has the user
4430 * sense buffer address
4432 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4435 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4436 sense, ioc->sense_len)) {
4437 printk(KERN_ERR "megasas: Failed to copy out to user "
4445 * copy the status codes returned by the fw
4447 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4448 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4449 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4455 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4456 sense, sense_handle);
4459 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4460 dma_free_coherent(&instance->pdev->dev,
4461 kern_sge32[i].length,
4462 kbuff_arr[i], kern_sge32[i].phys_addr);
4465 megasas_return_cmd(instance, cmd);
4469 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4471 struct megasas_iocpacket __user *user_ioc =
4472 (struct megasas_iocpacket __user *)arg;
4473 struct megasas_iocpacket *ioc;
4474 struct megasas_instance *instance;
4477 unsigned long flags;
4478 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4480 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4484 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4489 instance = megasas_lookup_instance(ioc->host_no);
4495 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4496 printk(KERN_ERR "Controller in crit error\n");
4501 if (instance->unload == 1) {
4507 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4509 if (down_interruptible(&instance->ioctl_sem)) {
4510 error = -ERESTARTSYS;
4514 for (i = 0; i < wait_time; i++) {
4516 spin_lock_irqsave(&instance->hba_lock, flags);
4517 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4518 spin_unlock_irqrestore(&instance->hba_lock, flags);
4521 spin_unlock_irqrestore(&instance->hba_lock, flags);
4523 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4524 printk(KERN_NOTICE "megasas: waiting"
4525 "for controller reset to finish\n");
4531 spin_lock_irqsave(&instance->hba_lock, flags);
4532 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4533 spin_unlock_irqrestore(&instance->hba_lock, flags);
4535 printk(KERN_ERR "megaraid_sas: timed out while"
4536 "waiting for HBA to recover\n");
4540 spin_unlock_irqrestore(&instance->hba_lock, flags);
4542 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4543 up(&instance->ioctl_sem);
4550 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4552 struct megasas_instance *instance;
4553 struct megasas_aen aen;
4556 unsigned long flags;
4557 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4559 if (file->private_data != file) {
4560 printk(KERN_DEBUG "megasas: fasync_helper was not "
4565 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4568 instance = megasas_lookup_instance(aen.host_no);
4573 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4577 if (instance->unload == 1) {
4581 for (i = 0; i < wait_time; i++) {
4583 spin_lock_irqsave(&instance->hba_lock, flags);
4584 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4585 spin_unlock_irqrestore(&instance->hba_lock,
4590 spin_unlock_irqrestore(&instance->hba_lock, flags);
4592 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4593 printk(KERN_NOTICE "megasas: waiting for"
4594 "controller reset to finish\n");
4600 spin_lock_irqsave(&instance->hba_lock, flags);
4601 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4602 spin_unlock_irqrestore(&instance->hba_lock, flags);
4603 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4604 "for HBA to recover.\n");
4607 spin_unlock_irqrestore(&instance->hba_lock, flags);
4609 mutex_lock(&instance->aen_mutex);
4610 error = megasas_register_aen(instance, aen.seq_num,
4611 aen.class_locale_word);
4612 mutex_unlock(&instance->aen_mutex);
4617 * megasas_mgmt_ioctl - char node ioctl entry point
4620 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4623 case MEGASAS_IOC_FIRMWARE:
4624 return megasas_mgmt_ioctl_fw(file, arg);
4626 case MEGASAS_IOC_GET_AEN:
4627 return megasas_mgmt_ioctl_aen(file, arg);
4633 #ifdef CONFIG_COMPAT
4634 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4636 struct compat_megasas_iocpacket __user *cioc =
4637 (struct compat_megasas_iocpacket __user *)arg;
4638 struct megasas_iocpacket __user *ioc =
4639 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4644 if (clear_user(ioc, sizeof(*ioc)))
4647 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4648 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4649 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4650 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4651 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4652 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4656 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4657 * sense_len is not null, so prepare the 64bit value under
4658 * the same condition.
4660 if (ioc->sense_len) {
4661 void __user **sense_ioc_ptr =
4662 (void __user **)(ioc->frame.raw + ioc->sense_off);
4663 compat_uptr_t *sense_cioc_ptr =
4664 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4665 if (get_user(ptr, sense_cioc_ptr) ||
4666 put_user(compat_ptr(ptr), sense_ioc_ptr))
4670 for (i = 0; i < MAX_IOCTL_SGE; i++) {
4671 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4672 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4673 copy_in_user(&ioc->sgl[i].iov_len,
4674 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4678 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4680 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4681 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4682 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4689 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4693 case MEGASAS_IOC_FIRMWARE32:
4694 return megasas_mgmt_compat_ioctl_fw(file, arg);
4695 case MEGASAS_IOC_GET_AEN:
4696 return megasas_mgmt_ioctl_aen(file, arg);
4704 * File operations structure for management interface
4706 static const struct file_operations megasas_mgmt_fops = {
4707 .owner = THIS_MODULE,
4708 .open = megasas_mgmt_open,
4709 .fasync = megasas_mgmt_fasync,
4710 .unlocked_ioctl = megasas_mgmt_ioctl,
4711 .poll = megasas_mgmt_poll,
4712 #ifdef CONFIG_COMPAT
4713 .compat_ioctl = megasas_mgmt_compat_ioctl,
4715 .llseek = noop_llseek,
4719 * PCI hotplug support registration structure
4721 static struct pci_driver megasas_pci_driver = {
4723 .name = "megaraid_sas",
4724 .id_table = megasas_pci_table,
4725 .probe = megasas_probe_one,
4726 .remove = __devexit_p(megasas_detach_one),
4727 .suspend = megasas_suspend,
4728 .resume = megasas_resume,
4729 .shutdown = megasas_shutdown,
4733 * Sysfs driver attributes
4735 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4737 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4741 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4744 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4746 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4750 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4754 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4756 return sprintf(buf, "%u\n", support_poll_for_event);
4759 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4760 megasas_sysfs_show_support_poll_for_event, NULL);
4763 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
4765 return sprintf(buf, "%u\n", support_device_change);
4768 static DRIVER_ATTR(support_device_change, S_IRUGO,
4769 megasas_sysfs_show_support_device_change, NULL);
4772 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4774 return sprintf(buf, "%u\n", megasas_dbg_lvl);
4778 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4781 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4782 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4788 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
4789 megasas_sysfs_set_dbg_lvl);
4792 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4794 return sprintf(buf, "%u\n", poll_mode_io);
4798 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4799 const char *buf, size_t count)
4802 int tmp = poll_mode_io;
4804 struct megasas_instance *instance;
4806 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4807 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4812 * Check if poll_mode_io is already set or is same as previous value
4814 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4819 * Start timers for all adapters
4821 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4822 instance = megasas_mgmt_info.instance[i];
4824 megasas_start_timer(instance,
4825 &instance->io_completion_timer,
4826 megasas_io_completion_timer,
4827 MEGASAS_COMPLETION_TIMER_INTERVAL);
4832 * Delete timers for all adapters
4834 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4835 instance = megasas_mgmt_info.instance[i];
4837 del_timer_sync(&instance->io_completion_timer);
4846 megasas_aen_polling(struct work_struct *work)
4848 struct megasas_aen_event *ev =
4849 container_of(work, struct megasas_aen_event, hotplug_work);
4850 struct megasas_instance *instance = ev->instance;
4851 union megasas_evt_class_locale class_locale;
4852 struct Scsi_Host *host;
4853 struct scsi_device *sdev1;
4856 int i, j, doscan = 0;
4861 printk(KERN_ERR "invalid instance!\n");
4865 instance->ev = NULL;
4866 host = instance->host;
4867 if (instance->evt_detail) {
4869 switch (instance->evt_detail->code) {
4870 case MR_EVT_PD_INSERTED:
4871 if (megasas_get_pd_list(instance) == 0) {
4872 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4874 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4878 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4881 scsi_device_lookup(host, i, j, 0);
4883 if (instance->pd_list[pd_index].driveState
4884 == MR_PD_STATE_SYSTEM) {
4886 scsi_add_device(host, i, j, 0);
4890 scsi_device_put(sdev1);
4898 case MR_EVT_PD_REMOVED:
4899 if (megasas_get_pd_list(instance) == 0) {
4900 megasas_get_pd_list(instance);
4901 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4903 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4907 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4910 scsi_device_lookup(host, i, j, 0);
4912 if (instance->pd_list[pd_index].driveState
4913 == MR_PD_STATE_SYSTEM) {
4915 scsi_device_put(sdev1);
4919 scsi_remove_device(sdev1);
4920 scsi_device_put(sdev1);
4929 case MR_EVT_LD_OFFLINE:
4930 case MR_EVT_LD_DELETED:
4931 megasas_get_ld_list(instance);
4932 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4934 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4938 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4940 sdev1 = scsi_device_lookup(host,
4941 i + MEGASAS_MAX_LD_CHANNELS,
4945 if (instance->ld_ids[ld_index] != 0xff) {
4947 scsi_device_put(sdev1);
4951 scsi_remove_device(sdev1);
4952 scsi_device_put(sdev1);
4959 case MR_EVT_LD_CREATED:
4960 megasas_get_ld_list(instance);
4961 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4963 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4966 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4968 sdev1 = scsi_device_lookup(host,
4969 i+MEGASAS_MAX_LD_CHANNELS,
4972 if (instance->ld_ids[ld_index] !=
4975 scsi_add_device(host,
4981 scsi_device_put(sdev1);
4987 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4988 case MR_EVT_FOREIGN_CFG_IMPORTED:
4996 printk(KERN_ERR "invalid evt_detail!\n");
5002 printk(KERN_INFO "scanning ...\n");
5003 megasas_get_pd_list(instance);
5004 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5005 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5006 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5007 sdev1 = scsi_device_lookup(host, i, j, 0);
5008 if (instance->pd_list[pd_index].driveState ==
5009 MR_PD_STATE_SYSTEM) {
5011 scsi_add_device(host, i, j, 0);
5014 scsi_device_put(sdev1);
5017 scsi_remove_device(sdev1);
5018 scsi_device_put(sdev1);
5024 megasas_get_ld_list(instance);
5025 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5026 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5028 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5030 sdev1 = scsi_device_lookup(host,
5031 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5032 if (instance->ld_ids[ld_index] != 0xff) {
5034 scsi_add_device(host,
5038 scsi_device_put(sdev1);
5042 scsi_remove_device(sdev1);
5043 scsi_device_put(sdev1);
5050 if ( instance->aen_cmd != NULL ) {
5055 seq_num = instance->evt_detail->seq_num + 1;
5057 /* Register AEN with FW for latest sequence number plus 1 */
5058 class_locale.members.reserved = 0;
5059 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5060 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5061 mutex_lock(&instance->aen_mutex);
5062 error = megasas_register_aen(instance, seq_num,
5064 mutex_unlock(&instance->aen_mutex);
5067 printk(KERN_ERR "register aen failed error %x\n", error);
5073 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
5074 megasas_sysfs_show_poll_mode_io,
5075 megasas_sysfs_set_poll_mode_io);
5078 * megasas_init - Driver load entry point
5080 static int __init megasas_init(void)
5085 * Announce driver version and other information
5087 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5088 MEGASAS_EXT_VERSION);
5090 support_poll_for_event = 2;
5091 support_device_change = 1;
5093 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5096 * Register character device node
5098 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5101 printk(KERN_DEBUG "megasas: failed to open device node\n");
5105 megasas_mgmt_majorno = rval;
5108 * Register ourselves as PCI hotplug module
5110 rval = pci_register_driver(&megasas_pci_driver);
5113 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5117 rval = driver_create_file(&megasas_pci_driver.driver,
5118 &driver_attr_version);
5120 goto err_dcf_attr_ver;
5121 rval = driver_create_file(&megasas_pci_driver.driver,
5122 &driver_attr_release_date);
5124 goto err_dcf_rel_date;
5126 rval = driver_create_file(&megasas_pci_driver.driver,
5127 &driver_attr_support_poll_for_event);
5129 goto err_dcf_support_poll_for_event;
5131 rval = driver_create_file(&megasas_pci_driver.driver,
5132 &driver_attr_dbg_lvl);
5134 goto err_dcf_dbg_lvl;
5135 rval = driver_create_file(&megasas_pci_driver.driver,
5136 &driver_attr_poll_mode_io);
5138 goto err_dcf_poll_mode_io;
5140 rval = driver_create_file(&megasas_pci_driver.driver,
5141 &driver_attr_support_device_change);
5143 goto err_dcf_support_device_change;
5147 err_dcf_support_device_change:
5148 driver_remove_file(&megasas_pci_driver.driver,
5149 &driver_attr_poll_mode_io);
5151 err_dcf_poll_mode_io:
5152 driver_remove_file(&megasas_pci_driver.driver,
5153 &driver_attr_dbg_lvl);
5155 driver_remove_file(&megasas_pci_driver.driver,
5156 &driver_attr_support_poll_for_event);
5158 err_dcf_support_poll_for_event:
5159 driver_remove_file(&megasas_pci_driver.driver,
5160 &driver_attr_release_date);
5163 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5165 pci_unregister_driver(&megasas_pci_driver);
5167 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5172 * megasas_exit - Driver unload entry point
5174 static void __exit megasas_exit(void)
5176 driver_remove_file(&megasas_pci_driver.driver,
5177 &driver_attr_poll_mode_io);
5178 driver_remove_file(&megasas_pci_driver.driver,
5179 &driver_attr_dbg_lvl);
5180 driver_remove_file(&megasas_pci_driver.driver,
5181 &driver_attr_support_poll_for_event);
5182 driver_remove_file(&megasas_pci_driver.driver,
5183 &driver_attr_support_device_change);
5184 driver_remove_file(&megasas_pci_driver.driver,
5185 &driver_attr_release_date);
5186 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5188 pci_unregister_driver(&megasas_pci_driver);
5189 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5192 module_init(megasas_init);
5193 module_exit(megasas_exit);