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(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 struct megasas_instance *megasas_lookup_instance(u16 host_no)
1424 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1426 if ((megasas_mgmt_info.instance[i]) &&
1427 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1428 return megasas_mgmt_info.instance[i];
1434 static int megasas_slave_configure(struct scsi_device *sdev)
1437 struct megasas_instance *instance ;
1439 instance = megasas_lookup_instance(sdev->host->host_no);
1442 * Don't export physical disk devices to the disk driver.
1444 * FIXME: Currently we don't export them to the midlayer at all.
1445 * That will be fixed once LSI engineers have audited the
1446 * firmware for possible issues.
1448 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1449 sdev->type == TYPE_DISK) {
1450 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1452 if (instance->pd_list[pd_index].driveState ==
1453 MR_PD_STATE_SYSTEM) {
1454 blk_queue_rq_timeout(sdev->request_queue,
1455 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1462 * The RAID firmware may require extended timeouts.
1464 blk_queue_rq_timeout(sdev->request_queue,
1465 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1469 static int megasas_slave_alloc(struct scsi_device *sdev)
1472 struct megasas_instance *instance ;
1473 instance = megasas_lookup_instance(sdev->host->host_no);
1474 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1475 (sdev->type == TYPE_DISK)) {
1477 * Open the OS scan to the SYSTEM PD
1480 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1482 if ((instance->pd_list[pd_index].driveState ==
1483 MR_PD_STATE_SYSTEM) &&
1484 (instance->pd_list[pd_index].driveType ==
1493 static void megaraid_sas_kill_hba(struct megasas_instance *instance)
1495 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1496 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1497 writel(MFI_STOP_ADP,
1498 &instance->reg_set->reserved_0[0]);
1500 writel(MFI_STOP_ADP,
1501 &instance->reg_set->inbound_doorbell);
1506 * megasas_complete_cmd_dpc - Returns FW's controller structure
1507 * @instance_addr: Address of adapter soft state
1509 * Tasklet to complete cmds
1511 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1516 struct megasas_cmd *cmd;
1517 struct megasas_instance *instance =
1518 (struct megasas_instance *)instance_addr;
1519 unsigned long flags;
1521 /* If we have already declared adapter dead, donot complete cmds */
1522 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1525 spin_lock_irqsave(&instance->completion_lock, flags);
1527 producer = *instance->producer;
1528 consumer = *instance->consumer;
1530 while (consumer != producer) {
1531 context = instance->reply_queue[consumer];
1532 if (context >= instance->max_fw_cmds) {
1533 printk(KERN_ERR "Unexpected context value %x\n",
1538 cmd = instance->cmd_list[context];
1540 megasas_complete_cmd(instance, cmd, DID_OK);
1543 if (consumer == (instance->max_fw_cmds + 1)) {
1548 *instance->consumer = producer;
1550 spin_unlock_irqrestore(&instance->completion_lock, flags);
1553 * Check if we can restore can_queue
1555 if (instance->flag & MEGASAS_FW_BUSY
1556 && time_after(jiffies, instance->last_time + 5 * HZ)
1557 && atomic_read(&instance->fw_outstanding) < 17) {
1559 spin_lock_irqsave(instance->host->host_lock, flags);
1560 instance->flag &= ~MEGASAS_FW_BUSY;
1561 if ((instance->pdev->device ==
1562 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1563 (instance->pdev->device ==
1564 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1565 instance->host->can_queue =
1566 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1568 instance->host->can_queue =
1569 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1571 spin_unlock_irqrestore(instance->host->host_lock, flags);
1576 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1579 process_fw_state_change_wq(struct work_struct *work);
1581 void megasas_do_ocr(struct megasas_instance *instance)
1583 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1584 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1585 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1586 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1588 instance->instancet->disable_intr(instance->reg_set);
1589 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1590 instance->issuepend_done = 0;
1592 atomic_set(&instance->fw_outstanding, 0);
1593 megasas_internal_reset_defer_cmds(instance);
1594 process_fw_state_change_wq(&instance->work_init);
1598 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1599 * @instance: Adapter soft state
1601 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1602 * complete all its outstanding commands. Returns error if one or more IOs
1603 * are pending after this time period. It also marks the controller dead.
1605 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1609 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1611 unsigned long flags;
1612 struct list_head clist_local;
1613 struct megasas_cmd *reset_cmd;
1615 u8 kill_adapter_flag;
1617 spin_lock_irqsave(&instance->hba_lock, flags);
1618 adprecovery = instance->adprecovery;
1619 spin_unlock_irqrestore(&instance->hba_lock, flags);
1621 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1623 INIT_LIST_HEAD(&clist_local);
1624 spin_lock_irqsave(&instance->hba_lock, flags);
1625 list_splice_init(&instance->internal_reset_pending_q,
1627 spin_unlock_irqrestore(&instance->hba_lock, flags);
1629 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1630 for (i = 0; i < wait_time; i++) {
1632 spin_lock_irqsave(&instance->hba_lock, flags);
1633 adprecovery = instance->adprecovery;
1634 spin_unlock_irqrestore(&instance->hba_lock, flags);
1635 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1639 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1640 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1641 spin_lock_irqsave(&instance->hba_lock, flags);
1642 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1643 spin_unlock_irqrestore(&instance->hba_lock, flags);
1648 while (!list_empty(&clist_local)) {
1649 reset_cmd = list_entry((&clist_local)->next,
1650 struct megasas_cmd, list);
1651 list_del_init(&reset_cmd->list);
1652 if (reset_cmd->scmd) {
1653 reset_cmd->scmd->result = DID_RESET << 16;
1654 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1655 reset_index, reset_cmd,
1656 reset_cmd->scmd->cmnd[0],
1657 reset_cmd->scmd->serial_number);
1659 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1660 megasas_return_cmd(instance, reset_cmd);
1661 } else if (reset_cmd->sync_cmd) {
1662 printk(KERN_NOTICE "megasas:%p synch cmds"
1666 reset_cmd->cmd_status = ENODATA;
1667 instance->instancet->fire_cmd(instance,
1668 reset_cmd->frame_phys_addr,
1669 0, instance->reg_set);
1671 printk(KERN_NOTICE "megasas: %p unexpected"
1681 for (i = 0; i < wait_time; i++) {
1683 int outstanding = atomic_read(&instance->fw_outstanding);
1688 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1689 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1690 "commands to complete\n",i,outstanding);
1692 * Call cmd completion routine. Cmd to be
1693 * be completed directly without depending on isr.
1695 megasas_complete_cmd_dpc((unsigned long)instance);
1702 kill_adapter_flag = 0;
1704 fw_state = instance->instancet->read_fw_status_reg(
1705 instance->reg_set) & MFI_STATE_MASK;
1706 if ((fw_state == MFI_STATE_FAULT) &&
1707 (instance->disableOnlineCtrlReset == 0)) {
1709 kill_adapter_flag = 2;
1712 megasas_do_ocr(instance);
1713 kill_adapter_flag = 1;
1715 /* wait for 1 secs to let FW finish the pending cmds */
1721 if (atomic_read(&instance->fw_outstanding) &&
1722 !kill_adapter_flag) {
1723 if (instance->disableOnlineCtrlReset == 0) {
1725 megasas_do_ocr(instance);
1727 /* wait for 5 secs to let FW finish the pending cmds */
1728 for (i = 0; i < wait_time; i++) {
1730 atomic_read(&instance->fw_outstanding);
1738 if (atomic_read(&instance->fw_outstanding) ||
1739 (kill_adapter_flag == 2)) {
1740 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1742 * Send signal to FW to stop processing any pending cmds.
1743 * The controller will be taken offline by the OS now.
1745 if ((instance->pdev->device ==
1746 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1747 (instance->pdev->device ==
1748 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1749 writel(MFI_STOP_ADP,
1750 &instance->reg_set->reserved_0[0]);
1752 writel(MFI_STOP_ADP,
1753 &instance->reg_set->inbound_doorbell);
1755 megasas_dump_pending_frames(instance);
1756 spin_lock_irqsave(&instance->hba_lock, flags);
1757 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1758 spin_unlock_irqrestore(&instance->hba_lock, flags);
1762 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1768 * megasas_generic_reset - Generic reset routine
1769 * @scmd: Mid-layer SCSI command
1771 * This routine implements a generic reset handler for device, bus and host
1772 * reset requests. Device, bus and host specific reset handlers can use this
1773 * function after they do their specific tasks.
1775 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1778 struct megasas_instance *instance;
1780 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1782 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1783 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1785 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1786 printk(KERN_ERR "megasas: cannot recover from previous reset "
1791 ret_val = megasas_wait_for_outstanding(instance);
1792 if (ret_val == SUCCESS)
1793 printk(KERN_NOTICE "megasas: reset successful \n");
1795 printk(KERN_ERR "megasas: failed to do reset\n");
1801 * megasas_reset_timer - quiesce the adapter if required
1804 * Sets the FW busy flag and reduces the host->can_queue if the
1805 * cmd has not been completed within the timeout period.
1808 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1810 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1811 struct megasas_instance *instance;
1812 unsigned long flags;
1814 if (time_after(jiffies, scmd->jiffies_at_alloc +
1815 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1816 return BLK_EH_NOT_HANDLED;
1819 instance = cmd->instance;
1820 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1821 /* FW is busy, throttle IO */
1822 spin_lock_irqsave(instance->host->host_lock, flags);
1824 instance->host->can_queue = 16;
1825 instance->last_time = jiffies;
1826 instance->flag |= MEGASAS_FW_BUSY;
1828 spin_unlock_irqrestore(instance->host->host_lock, flags);
1830 return BLK_EH_RESET_TIMER;
1834 * megasas_reset_device - Device reset handler entry point
1836 static int megasas_reset_device(struct scsi_cmnd *scmd)
1841 * First wait for all commands to complete
1843 ret = megasas_generic_reset(scmd);
1849 * megasas_reset_bus_host - Bus & host reset handler entry point
1851 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1856 * First wait for all commands to complete
1858 ret = megasas_generic_reset(scmd);
1864 * megasas_bios_param - Returns disk geometry for a disk
1865 * @sdev: device handle
1866 * @bdev: block device
1867 * @capacity: drive capacity
1868 * @geom: geometry parameters
1871 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1872 sector_t capacity, int geom[])
1878 /* Default heads (64) & sectors (32) */
1882 tmp = heads * sectors;
1883 cylinders = capacity;
1885 sector_div(cylinders, tmp);
1888 * Handle extended translation size for logical drives > 1Gb
1891 if (capacity >= 0x200000) {
1894 tmp = heads*sectors;
1895 cylinders = capacity;
1896 sector_div(cylinders, tmp);
1901 geom[2] = cylinders;
1906 static void megasas_aen_polling(struct work_struct *work);
1909 * megasas_service_aen - Processes an event notification
1910 * @instance: Adapter soft state
1911 * @cmd: AEN command completed by the ISR
1913 * For AEN, driver sends a command down to FW that is held by the FW till an
1914 * event occurs. When an event of interest occurs, FW completes the command
1915 * that it was previously holding.
1917 * This routines sends SIGIO signal to processes that have registered with the
1921 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1923 unsigned long flags;
1925 * Don't signal app if it is just an aborted previously registered aen
1927 if ((!cmd->abort_aen) && (instance->unload == 0)) {
1928 spin_lock_irqsave(&poll_aen_lock, flags);
1929 megasas_poll_wait_aen = 1;
1930 spin_unlock_irqrestore(&poll_aen_lock, flags);
1931 wake_up(&megasas_poll_wait);
1932 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1937 instance->aen_cmd = NULL;
1938 megasas_return_cmd(instance, cmd);
1940 if ((instance->unload == 0) &&
1941 ((instance->issuepend_done == 1))) {
1942 struct megasas_aen_event *ev;
1943 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1945 printk(KERN_ERR "megasas_service_aen: out of memory\n");
1947 ev->instance = instance;
1949 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1950 schedule_delayed_work(
1951 (struct delayed_work *)&ev->hotplug_work, 0);
1957 * Scsi host template for megaraid_sas driver
1959 static struct scsi_host_template megasas_template = {
1961 .module = THIS_MODULE,
1962 .name = "LSI SAS based MegaRAID driver",
1963 .proc_name = "megaraid_sas",
1964 .slave_configure = megasas_slave_configure,
1965 .slave_alloc = megasas_slave_alloc,
1966 .queuecommand = megasas_queue_command,
1967 .eh_device_reset_handler = megasas_reset_device,
1968 .eh_bus_reset_handler = megasas_reset_bus_host,
1969 .eh_host_reset_handler = megasas_reset_bus_host,
1970 .eh_timed_out = megasas_reset_timer,
1971 .bios_param = megasas_bios_param,
1972 .use_clustering = ENABLE_CLUSTERING,
1976 * megasas_complete_int_cmd - Completes an internal command
1977 * @instance: Adapter soft state
1978 * @cmd: Command to be completed
1980 * The megasas_issue_blocked_cmd() function waits for a command to complete
1981 * after it issues a command. This function wakes up that waiting routine by
1982 * calling wake_up() on the wait queue.
1985 megasas_complete_int_cmd(struct megasas_instance *instance,
1986 struct megasas_cmd *cmd)
1988 cmd->cmd_status = cmd->frame->io.cmd_status;
1990 if (cmd->cmd_status == ENODATA) {
1991 cmd->cmd_status = 0;
1993 wake_up(&instance->int_cmd_wait_q);
1997 * megasas_complete_abort - Completes aborting a command
1998 * @instance: Adapter soft state
1999 * @cmd: Cmd that was issued to abort another cmd
2001 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2002 * after it issues an abort on a previously issued command. This function
2003 * wakes up all functions waiting on the same wait queue.
2006 megasas_complete_abort(struct megasas_instance *instance,
2007 struct megasas_cmd *cmd)
2009 if (cmd->sync_cmd) {
2011 cmd->cmd_status = 0;
2012 wake_up(&instance->abort_cmd_wait_q);
2019 * megasas_complete_cmd - Completes a command
2020 * @instance: Adapter soft state
2021 * @cmd: Command to be completed
2022 * @alt_status: If non-zero, use this value as status to
2023 * SCSI mid-layer instead of the value returned
2024 * by the FW. This should be used if caller wants
2025 * an alternate status (as in the case of aborted
2029 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2033 struct megasas_header *hdr = &cmd->frame->hdr;
2034 unsigned long flags;
2036 /* flag for the retry reset */
2037 cmd->retry_for_fw_reset = 0;
2040 cmd->scmd->SCp.ptr = NULL;
2044 case MFI_CMD_PD_SCSI_IO:
2045 case MFI_CMD_LD_SCSI_IO:
2048 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2049 * issued either through an IO path or an IOCTL path. If it
2050 * was via IOCTL, we will send it to internal completion.
2052 if (cmd->sync_cmd) {
2054 megasas_complete_int_cmd(instance, cmd);
2058 case MFI_CMD_LD_READ:
2059 case MFI_CMD_LD_WRITE:
2062 cmd->scmd->result = alt_status << 16;
2068 atomic_dec(&instance->fw_outstanding);
2070 scsi_dma_unmap(cmd->scmd);
2071 cmd->scmd->scsi_done(cmd->scmd);
2072 megasas_return_cmd(instance, cmd);
2077 switch (hdr->cmd_status) {
2080 cmd->scmd->result = DID_OK << 16;
2083 case MFI_STAT_SCSI_IO_FAILED:
2084 case MFI_STAT_LD_INIT_IN_PROGRESS:
2086 (DID_ERROR << 16) | hdr->scsi_status;
2089 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2091 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2093 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2094 memset(cmd->scmd->sense_buffer, 0,
2095 SCSI_SENSE_BUFFERSIZE);
2096 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2099 cmd->scmd->result |= DRIVER_SENSE << 24;
2104 case MFI_STAT_LD_OFFLINE:
2105 case MFI_STAT_DEVICE_NOT_FOUND:
2106 cmd->scmd->result = DID_BAD_TARGET << 16;
2110 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2112 cmd->scmd->result = DID_ERROR << 16;
2116 atomic_dec(&instance->fw_outstanding);
2118 scsi_dma_unmap(cmd->scmd);
2119 cmd->scmd->scsi_done(cmd->scmd);
2120 megasas_return_cmd(instance, cmd);
2127 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2128 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2129 spin_lock_irqsave(&poll_aen_lock, flags);
2130 megasas_poll_wait_aen = 0;
2131 spin_unlock_irqrestore(&poll_aen_lock, flags);
2135 * See if got an event notification
2137 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2138 megasas_service_aen(instance, cmd);
2140 megasas_complete_int_cmd(instance, cmd);
2146 * Cmd issued to abort another cmd returned
2148 megasas_complete_abort(instance, cmd);
2152 printk("megasas: Unknown command completed! [0x%X]\n",
2159 * megasas_issue_pending_cmds_again - issue all pending cmds
2160 * in FW again because of the fw reset
2161 * @instance: Adapter soft state
2164 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2166 struct megasas_cmd *cmd;
2167 struct list_head clist_local;
2168 union megasas_evt_class_locale class_locale;
2169 unsigned long flags;
2172 INIT_LIST_HEAD(&clist_local);
2173 spin_lock_irqsave(&instance->hba_lock, flags);
2174 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2175 spin_unlock_irqrestore(&instance->hba_lock, flags);
2177 while (!list_empty(&clist_local)) {
2178 cmd = list_entry((&clist_local)->next,
2179 struct megasas_cmd, list);
2180 list_del_init(&cmd->list);
2182 if (cmd->sync_cmd || cmd->scmd) {
2183 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2184 "detected to be pending while HBA reset.\n",
2185 cmd, cmd->scmd, cmd->sync_cmd);
2187 cmd->retry_for_fw_reset++;
2189 if (cmd->retry_for_fw_reset == 3) {
2190 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2191 "was tried multiple times during reset."
2192 "Shutting down the HBA\n",
2193 cmd, cmd->scmd, cmd->sync_cmd);
2194 megaraid_sas_kill_hba(instance);
2196 instance->adprecovery =
2197 MEGASAS_HW_CRITICAL_ERROR;
2202 if (cmd->sync_cmd == 1) {
2204 printk(KERN_NOTICE "megaraid_sas: unexpected"
2205 "cmd attached to internal command!\n");
2207 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2208 "on the internal reset queue,"
2209 "issue it again.\n", cmd);
2210 cmd->cmd_status = ENODATA;
2211 instance->instancet->fire_cmd(instance,
2212 cmd->frame_phys_addr ,
2213 0, instance->reg_set);
2214 } else if (cmd->scmd) {
2215 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2216 "detected on the internal queue, issue again.\n",
2217 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2219 atomic_inc(&instance->fw_outstanding);
2220 instance->instancet->fire_cmd(instance,
2221 cmd->frame_phys_addr,
2222 cmd->frame_count-1, instance->reg_set);
2224 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2225 "internal reset defer list while re-issue!!\n",
2230 if (instance->aen_cmd) {
2231 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2232 megasas_return_cmd(instance, instance->aen_cmd);
2234 instance->aen_cmd = NULL;
2238 * Initiate AEN (Asynchronous Event Notification)
2240 seq_num = instance->last_seq_num;
2241 class_locale.members.reserved = 0;
2242 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2243 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2245 megasas_register_aen(instance, seq_num, class_locale.word);
2249 * Move the internal reset pending commands to a deferred queue.
2251 * We move the commands pending at internal reset time to a
2252 * pending queue. This queue would be flushed after successful
2253 * completion of the internal reset sequence. if the internal reset
2254 * did not complete in time, the kernel reset handler would flush
2258 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2260 struct megasas_cmd *cmd;
2262 u32 max_cmd = instance->max_fw_cmds;
2264 unsigned long flags;
2267 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2268 for (i = 0; i < max_cmd; i++) {
2269 cmd = instance->cmd_list[i];
2270 if (cmd->sync_cmd == 1 || cmd->scmd) {
2271 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2272 "on the defer queue as internal\n",
2273 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2275 if (!list_empty(&cmd->list)) {
2276 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2277 " moving this cmd:%p, %d %p, it was"
2278 "discovered on some list?\n",
2279 cmd, cmd->sync_cmd, cmd->scmd);
2281 list_del_init(&cmd->list);
2284 list_add_tail(&cmd->list,
2285 &instance->internal_reset_pending_q);
2288 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2293 process_fw_state_change_wq(struct work_struct *work)
2295 struct megasas_instance *instance =
2296 container_of(work, struct megasas_instance, work_init);
2298 unsigned long flags;
2300 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2301 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2302 instance->adprecovery);
2306 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2307 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2308 "state, restarting it...\n");
2310 instance->instancet->disable_intr(instance->reg_set);
2311 atomic_set(&instance->fw_outstanding, 0);
2313 atomic_set(&instance->fw_reset_no_pci_access, 1);
2314 instance->instancet->adp_reset(instance, instance->reg_set);
2315 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2317 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2318 "initiating next stage...\n");
2320 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2321 "state 2 starting...\n");
2323 /*waitting for about 20 second before start the second init*/
2324 for (wait = 0; wait < 30; wait++) {
2328 if (megasas_transition_to_ready(instance)) {
2329 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2331 megaraid_sas_kill_hba(instance);
2332 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2336 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2337 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2338 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2340 *instance->consumer = *instance->producer;
2342 *instance->consumer = 0;
2343 *instance->producer = 0;
2346 megasas_issue_init_mfi(instance);
2348 spin_lock_irqsave(&instance->hba_lock, flags);
2349 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2350 spin_unlock_irqrestore(&instance->hba_lock, flags);
2351 instance->instancet->enable_intr(instance->reg_set);
2353 megasas_issue_pending_cmds_again(instance);
2354 instance->issuepend_done = 1;
2360 * megasas_deplete_reply_queue - Processes all completed commands
2361 * @instance: Adapter soft state
2362 * @alt_status: Alternate status to be returned to
2363 * SCSI mid-layer instead of the status
2364 * returned by the FW
2365 * Note: this must be called with hba lock held
2368 megasas_deplete_reply_queue(struct megasas_instance *instance,
2374 if ((mfiStatus = instance->instancet->check_reset(instance,
2375 instance->reg_set)) == 1) {
2379 if ((mfiStatus = instance->instancet->clear_intr(
2385 instance->mfiStatus = mfiStatus;
2387 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2388 fw_state = instance->instancet->read_fw_status_reg(
2389 instance->reg_set) & MFI_STATE_MASK;
2391 if (fw_state != MFI_STATE_FAULT) {
2392 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2396 if ((fw_state == MFI_STATE_FAULT) &&
2397 (instance->disableOnlineCtrlReset == 0)) {
2398 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2400 if ((instance->pdev->device ==
2401 PCI_DEVICE_ID_LSI_SAS1064R) ||
2402 (instance->pdev->device ==
2403 PCI_DEVICE_ID_DELL_PERC5) ||
2404 (instance->pdev->device ==
2405 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2407 *instance->consumer =
2408 MEGASAS_ADPRESET_INPROG_SIGN;
2412 instance->instancet->disable_intr(instance->reg_set);
2413 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2414 instance->issuepend_done = 0;
2416 atomic_set(&instance->fw_outstanding, 0);
2417 megasas_internal_reset_defer_cmds(instance);
2419 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2420 fw_state, instance->adprecovery);
2422 schedule_work(&instance->work_init);
2426 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2427 fw_state, instance->disableOnlineCtrlReset);
2431 tasklet_schedule(&instance->isr_tasklet);
2435 * megasas_isr - isr entry point
2437 static irqreturn_t megasas_isr(int irq, void *devp)
2439 struct megasas_instance *instance;
2440 unsigned long flags;
2444 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2447 instance = (struct megasas_instance *)devp;
2449 spin_lock_irqsave(&instance->hba_lock, flags);
2450 rc = megasas_deplete_reply_queue(instance, DID_OK);
2451 spin_unlock_irqrestore(&instance->hba_lock, flags);
2457 * megasas_transition_to_ready - Move the FW to READY state
2458 * @instance: Adapter soft state
2460 * During the initialization, FW passes can potentially be in any one of
2461 * several possible states. If the FW in operational, waiting-for-handshake
2462 * states, driver must take steps to bring it to ready state. Otherwise, it
2463 * has to wait for the ready state.
2466 megasas_transition_to_ready(struct megasas_instance* instance)
2472 u32 abs_state, curr_abs_state;
2474 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2476 if (fw_state != MFI_STATE_READY)
2477 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2480 while (fw_state != MFI_STATE_READY) {
2483 instance->instancet->read_fw_status_reg(instance->reg_set);
2487 case MFI_STATE_FAULT:
2489 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2492 case MFI_STATE_WAIT_HANDSHAKE:
2494 * Set the CLR bit in inbound doorbell
2496 if ((instance->pdev->device ==
2497 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2498 (instance->pdev->device ==
2499 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2502 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2503 &instance->reg_set->reserved_0[0]);
2506 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2507 &instance->reg_set->inbound_doorbell);
2510 max_wait = MEGASAS_RESET_WAIT_TIME;
2511 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2514 case MFI_STATE_BOOT_MESSAGE_PENDING:
2515 if ((instance->pdev->device ==
2516 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2517 (instance->pdev->device ==
2518 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2519 writel(MFI_INIT_HOTPLUG,
2520 &instance->reg_set->reserved_0[0]);
2522 writel(MFI_INIT_HOTPLUG,
2523 &instance->reg_set->inbound_doorbell);
2525 max_wait = MEGASAS_RESET_WAIT_TIME;
2526 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2529 case MFI_STATE_OPERATIONAL:
2531 * Bring it to READY state; assuming max wait 10 secs
2533 instance->instancet->disable_intr(instance->reg_set);
2534 if ((instance->pdev->device ==
2535 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2536 (instance->pdev->device ==
2537 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2538 writel(MFI_RESET_FLAGS,
2539 &instance->reg_set->reserved_0[0]);
2541 writel(MFI_RESET_FLAGS,
2542 &instance->reg_set->inbound_doorbell);
2544 max_wait = MEGASAS_RESET_WAIT_TIME;
2545 cur_state = MFI_STATE_OPERATIONAL;
2548 case MFI_STATE_UNDEFINED:
2550 * This state should not last for more than 2 seconds
2552 max_wait = MEGASAS_RESET_WAIT_TIME;
2553 cur_state = MFI_STATE_UNDEFINED;
2556 case MFI_STATE_BB_INIT:
2557 max_wait = MEGASAS_RESET_WAIT_TIME;
2558 cur_state = MFI_STATE_BB_INIT;
2561 case MFI_STATE_FW_INIT:
2562 max_wait = MEGASAS_RESET_WAIT_TIME;
2563 cur_state = MFI_STATE_FW_INIT;
2566 case MFI_STATE_FW_INIT_2:
2567 max_wait = MEGASAS_RESET_WAIT_TIME;
2568 cur_state = MFI_STATE_FW_INIT_2;
2571 case MFI_STATE_DEVICE_SCAN:
2572 max_wait = MEGASAS_RESET_WAIT_TIME;
2573 cur_state = MFI_STATE_DEVICE_SCAN;
2576 case MFI_STATE_FLUSH_CACHE:
2577 max_wait = MEGASAS_RESET_WAIT_TIME;
2578 cur_state = MFI_STATE_FLUSH_CACHE;
2582 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2588 * The cur_state should not last for more than max_wait secs
2590 for (i = 0; i < (max_wait * 1000); i++) {
2591 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2594 instance->instancet->read_fw_status_reg(instance->reg_set);
2596 if (abs_state == curr_abs_state) {
2603 * Return error if fw_state hasn't changed after max_wait
2605 if (curr_abs_state == abs_state) {
2606 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2607 "in %d secs\n", fw_state, max_wait);
2611 printk(KERN_INFO "megasas: FW now in Ready state\n");
2617 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2618 * @instance: Adapter soft state
2620 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2623 u32 max_cmd = instance->max_fw_cmds;
2624 struct megasas_cmd *cmd;
2626 if (!instance->frame_dma_pool)
2630 * Return all frames to pool
2632 for (i = 0; i < max_cmd; i++) {
2634 cmd = instance->cmd_list[i];
2637 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2638 cmd->frame_phys_addr);
2641 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2642 cmd->sense_phys_addr);
2646 * Now destroy the pool itself
2648 pci_pool_destroy(instance->frame_dma_pool);
2649 pci_pool_destroy(instance->sense_dma_pool);
2651 instance->frame_dma_pool = NULL;
2652 instance->sense_dma_pool = NULL;
2656 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2657 * @instance: Adapter soft state
2659 * Each command packet has an embedded DMA memory buffer that is used for
2660 * filling MFI frame and the SG list that immediately follows the frame. This
2661 * function creates those DMA memory buffers for each command packet by using
2662 * PCI pool facility.
2664 static int megasas_create_frame_pool(struct megasas_instance *instance)
2672 struct megasas_cmd *cmd;
2674 max_cmd = instance->max_fw_cmds;
2677 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2678 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2680 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2681 sizeof(struct megasas_sge32);
2683 if (instance->flag_ieee) {
2684 sge_sz = sizeof(struct megasas_sge_skinny);
2688 * Calculated the number of 64byte frames required for SGL
2690 sgl_sz = sge_sz * instance->max_num_sge;
2691 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2695 * We need one extra frame for the MFI command
2699 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2701 * Use DMA pool facility provided by PCI layer
2703 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2704 instance->pdev, total_sz, 64,
2707 if (!instance->frame_dma_pool) {
2708 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2712 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2713 instance->pdev, 128, 4, 0);
2715 if (!instance->sense_dma_pool) {
2716 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2718 pci_pool_destroy(instance->frame_dma_pool);
2719 instance->frame_dma_pool = NULL;
2725 * Allocate and attach a frame to each of the commands in cmd_list.
2726 * By making cmd->index as the context instead of the &cmd, we can
2727 * always use 32bit context regardless of the architecture
2729 for (i = 0; i < max_cmd; i++) {
2731 cmd = instance->cmd_list[i];
2733 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2734 GFP_KERNEL, &cmd->frame_phys_addr);
2736 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2737 GFP_KERNEL, &cmd->sense_phys_addr);
2740 * megasas_teardown_frame_pool() takes care of freeing
2741 * whatever has been allocated
2743 if (!cmd->frame || !cmd->sense) {
2744 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2745 megasas_teardown_frame_pool(instance);
2749 memset(cmd->frame, 0, total_sz);
2750 cmd->frame->io.context = cmd->index;
2751 cmd->frame->io.pad_0 = 0;
2758 * megasas_free_cmds - Free all the cmds in the free cmd pool
2759 * @instance: Adapter soft state
2761 static void megasas_free_cmds(struct megasas_instance *instance)
2764 /* First free the MFI frame pool */
2765 megasas_teardown_frame_pool(instance);
2767 /* Free all the commands in the cmd_list */
2768 for (i = 0; i < instance->max_fw_cmds; i++)
2769 kfree(instance->cmd_list[i]);
2771 /* Free the cmd_list buffer itself */
2772 kfree(instance->cmd_list);
2773 instance->cmd_list = NULL;
2775 INIT_LIST_HEAD(&instance->cmd_pool);
2779 * megasas_alloc_cmds - Allocates the command packets
2780 * @instance: Adapter soft state
2782 * Each command that is issued to the FW, whether IO commands from the OS or
2783 * internal commands like IOCTLs, are wrapped in local data structure called
2784 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2787 * Each frame has a 32-bit field called context (tag). This context is used
2788 * to get back the megasas_cmd from the frame when a frame gets completed in
2789 * the ISR. Typically the address of the megasas_cmd itself would be used as
2790 * the context. But we wanted to keep the differences between 32 and 64 bit
2791 * systems to the mininum. We always use 32 bit integers for the context. In
2792 * this driver, the 32 bit values are the indices into an array cmd_list.
2793 * This array is used only to look up the megasas_cmd given the context. The
2794 * free commands themselves are maintained in a linked list called cmd_pool.
2796 static int megasas_alloc_cmds(struct megasas_instance *instance)
2801 struct megasas_cmd *cmd;
2803 max_cmd = instance->max_fw_cmds;
2806 * instance->cmd_list is an array of struct megasas_cmd pointers.
2807 * Allocate the dynamic array first and then allocate individual
2810 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2812 if (!instance->cmd_list) {
2813 printk(KERN_DEBUG "megasas: out of memory\n");
2818 for (i = 0; i < max_cmd; i++) {
2819 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2822 if (!instance->cmd_list[i]) {
2824 for (j = 0; j < i; j++)
2825 kfree(instance->cmd_list[j]);
2827 kfree(instance->cmd_list);
2828 instance->cmd_list = NULL;
2835 * Add all the commands to command pool (instance->cmd_pool)
2837 for (i = 0; i < max_cmd; i++) {
2838 cmd = instance->cmd_list[i];
2839 memset(cmd, 0, sizeof(struct megasas_cmd));
2842 cmd->instance = instance;
2844 list_add_tail(&cmd->list, &instance->cmd_pool);
2848 * Create a frame pool and assign one frame to each cmd
2850 if (megasas_create_frame_pool(instance)) {
2851 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2852 megasas_free_cmds(instance);
2859 * megasas_get_pd_list_info - Returns FW's pd_list structure
2860 * @instance: Adapter soft state
2861 * @pd_list: pd_list structure
2863 * Issues an internal command (DCMD) to get the FW's controller PD
2864 * list structure. This information is mainly used to find out SYSTEM
2865 * supported by the FW.
2868 megasas_get_pd_list(struct megasas_instance *instance)
2870 int ret = 0, pd_index = 0;
2871 struct megasas_cmd *cmd;
2872 struct megasas_dcmd_frame *dcmd;
2873 struct MR_PD_LIST *ci;
2874 struct MR_PD_ADDRESS *pd_addr;
2875 dma_addr_t ci_h = 0;
2877 cmd = megasas_get_cmd(instance);
2880 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2884 dcmd = &cmd->frame->dcmd;
2886 ci = pci_alloc_consistent(instance->pdev,
2887 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2890 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2891 megasas_return_cmd(instance, cmd);
2895 memset(ci, 0, sizeof(*ci));
2896 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2898 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2899 dcmd->mbox.b[1] = 0;
2900 dcmd->cmd = MFI_CMD_DCMD;
2901 dcmd->cmd_status = 0xFF;
2902 dcmd->sge_count = 1;
2903 dcmd->flags = MFI_FRAME_DIR_READ;
2906 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2907 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2908 dcmd->sgl.sge32[0].phys_addr = ci_h;
2909 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2911 if (!megasas_issue_polled(instance, cmd)) {
2918 * the following function will get the instance PD LIST.
2925 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2927 memset(instance->pd_list, 0,
2928 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2930 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2932 instance->pd_list[pd_addr->deviceId].tid =
2934 instance->pd_list[pd_addr->deviceId].driveType =
2935 pd_addr->scsiDevType;
2936 instance->pd_list[pd_addr->deviceId].driveState =
2942 pci_free_consistent(instance->pdev,
2943 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2945 megasas_return_cmd(instance, cmd);
2951 * megasas_get_ld_list_info - Returns FW's ld_list structure
2952 * @instance: Adapter soft state
2953 * @ld_list: ld_list structure
2955 * Issues an internal command (DCMD) to get the FW's controller PD
2956 * list structure. This information is mainly used to find out SYSTEM
2957 * supported by the FW.
2960 megasas_get_ld_list(struct megasas_instance *instance)
2962 int ret = 0, ld_index = 0, ids = 0;
2963 struct megasas_cmd *cmd;
2964 struct megasas_dcmd_frame *dcmd;
2965 struct MR_LD_LIST *ci;
2966 dma_addr_t ci_h = 0;
2968 cmd = megasas_get_cmd(instance);
2971 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2975 dcmd = &cmd->frame->dcmd;
2977 ci = pci_alloc_consistent(instance->pdev,
2978 sizeof(struct MR_LD_LIST),
2982 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2983 megasas_return_cmd(instance, cmd);
2987 memset(ci, 0, sizeof(*ci));
2988 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2990 dcmd->cmd = MFI_CMD_DCMD;
2991 dcmd->cmd_status = 0xFF;
2992 dcmd->sge_count = 1;
2993 dcmd->flags = MFI_FRAME_DIR_READ;
2995 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2996 dcmd->opcode = MR_DCMD_LD_GET_LIST;
2997 dcmd->sgl.sge32[0].phys_addr = ci_h;
2998 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3001 if (!megasas_issue_polled(instance, cmd)) {
3007 /* the following function will get the instance PD LIST */
3009 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3010 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3012 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3013 if (ci->ldList[ld_index].state != 0) {
3014 ids = ci->ldList[ld_index].ref.targetId;
3015 instance->ld_ids[ids] =
3016 ci->ldList[ld_index].ref.targetId;
3021 pci_free_consistent(instance->pdev,
3022 sizeof(struct MR_LD_LIST),
3026 megasas_return_cmd(instance, cmd);
3031 * megasas_get_controller_info - Returns FW's controller structure
3032 * @instance: Adapter soft state
3033 * @ctrl_info: Controller information structure
3035 * Issues an internal command (DCMD) to get the FW's controller structure.
3036 * This information is mainly used to find out the maximum IO transfer per
3037 * command supported by the FW.
3040 megasas_get_ctrl_info(struct megasas_instance *instance,
3041 struct megasas_ctrl_info *ctrl_info)
3044 struct megasas_cmd *cmd;
3045 struct megasas_dcmd_frame *dcmd;
3046 struct megasas_ctrl_info *ci;
3047 dma_addr_t ci_h = 0;
3049 cmd = megasas_get_cmd(instance);
3052 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3056 dcmd = &cmd->frame->dcmd;
3058 ci = pci_alloc_consistent(instance->pdev,
3059 sizeof(struct megasas_ctrl_info), &ci_h);
3062 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3063 megasas_return_cmd(instance, cmd);
3067 memset(ci, 0, sizeof(*ci));
3068 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3070 dcmd->cmd = MFI_CMD_DCMD;
3071 dcmd->cmd_status = 0xFF;
3072 dcmd->sge_count = 1;
3073 dcmd->flags = MFI_FRAME_DIR_READ;
3076 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3077 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3078 dcmd->sgl.sge32[0].phys_addr = ci_h;
3079 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3081 if (!megasas_issue_polled(instance, cmd)) {
3083 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3088 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3091 megasas_return_cmd(instance, cmd);
3096 * megasas_issue_init_mfi - Initializes the FW
3097 * @instance: Adapter soft state
3099 * Issues the INIT MFI cmd
3102 megasas_issue_init_mfi(struct megasas_instance *instance)
3106 struct megasas_cmd *cmd;
3108 struct megasas_init_frame *init_frame;
3109 struct megasas_init_queue_info *initq_info;
3110 dma_addr_t init_frame_h;
3111 dma_addr_t initq_info_h;
3114 * Prepare a init frame. Note the init frame points to queue info
3115 * structure. Each frame has SGL allocated after first 64 bytes. For
3116 * this frame - since we don't need any SGL - we use SGL's space as
3117 * queue info structure
3119 * We will not get a NULL command below. We just created the pool.
3121 cmd = megasas_get_cmd(instance);
3123 init_frame = (struct megasas_init_frame *)cmd->frame;
3124 initq_info = (struct megasas_init_queue_info *)
3125 ((unsigned long)init_frame + 64);
3127 init_frame_h = cmd->frame_phys_addr;
3128 initq_info_h = init_frame_h + 64;
3130 context = init_frame->context;
3131 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3132 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3133 init_frame->context = context;
3135 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3136 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3138 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3139 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3141 init_frame->cmd = MFI_CMD_INIT;
3142 init_frame->cmd_status = 0xFF;
3143 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3145 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3148 * disable the intr before firing the init frame to FW
3150 instance->instancet->disable_intr(instance->reg_set);
3153 * Issue the init frame in polled mode
3156 if (megasas_issue_polled(instance, cmd)) {
3157 printk(KERN_ERR "megasas: Failed to init firmware\n");
3158 megasas_return_cmd(instance, cmd);
3162 megasas_return_cmd(instance, cmd);
3171 * megasas_start_timer - Initializes a timer object
3172 * @instance: Adapter soft state
3173 * @timer: timer object to be initialized
3174 * @fn: timer function
3175 * @interval: time interval between timer function call
3178 megasas_start_timer(struct megasas_instance *instance,
3179 struct timer_list *timer,
3180 void *fn, unsigned long interval)
3183 timer->expires = jiffies + interval;
3184 timer->data = (unsigned long)instance;
3185 timer->function = fn;
3190 * megasas_io_completion_timer - Timer fn
3191 * @instance_addr: Address of adapter soft state
3193 * Schedules tasklet for cmd completion
3194 * if poll_mode_io is set
3197 megasas_io_completion_timer(unsigned long instance_addr)
3199 struct megasas_instance *instance =
3200 (struct megasas_instance *)instance_addr;
3202 if (atomic_read(&instance->fw_outstanding))
3203 tasklet_schedule(&instance->isr_tasklet);
3207 mod_timer(&instance->io_completion_timer,
3208 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3212 * megasas_init_mfi - Initializes the FW
3213 * @instance: Adapter soft state
3215 * This is the main function for initializing MFI firmware.
3217 static int megasas_init_mfi(struct megasas_instance *instance)
3224 struct megasas_register_set __iomem *reg_set;
3225 struct megasas_ctrl_info *ctrl_info;
3227 * Map the message registers
3229 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3230 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3231 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3232 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
3233 instance->base_addr = pci_resource_start(instance->pdev, 1);
3235 instance->base_addr = pci_resource_start(instance->pdev, 0);
3238 if (pci_request_selected_regions(instance->pdev,
3239 pci_select_bars(instance->pdev, IORESOURCE_MEM),
3241 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3245 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3247 if (!instance->reg_set) {
3248 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3252 reg_set = instance->reg_set;
3254 switch(instance->pdev->device)
3256 case PCI_DEVICE_ID_LSI_SAS1078R:
3257 case PCI_DEVICE_ID_LSI_SAS1078DE:
3258 instance->instancet = &megasas_instance_template_ppc;
3260 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3261 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3262 instance->instancet = &megasas_instance_template_gen2;
3264 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3265 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3266 instance->instancet = &megasas_instance_template_skinny;
3268 case PCI_DEVICE_ID_LSI_SAS1064R:
3269 case PCI_DEVICE_ID_DELL_PERC5:
3271 instance->instancet = &megasas_instance_template_xscale;
3276 * We expect the FW state to be READY
3278 if (megasas_transition_to_ready(instance))
3279 goto fail_ready_state;
3282 * Get various operational parameters from status register
3284 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3286 * Reduce the max supported cmds by 1. This is to ensure that the
3287 * reply_q_sz (1 more than the max cmd that driver may send)
3288 * does not exceed max cmds that the FW can support
3290 instance->max_fw_cmds = instance->max_fw_cmds-1;
3291 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3294 * Create a pool of commands
3296 if (megasas_alloc_cmds(instance))
3297 goto fail_alloc_cmds;
3300 * Allocate memory for reply queue. Length of reply queue should
3301 * be _one_ more than the maximum commands handled by the firmware.
3303 * Note: When FW completes commands, it places corresponding contex
3304 * values in this circular reply queue. This circular queue is a fairly
3305 * typical producer-consumer queue. FW is the producer (of completed
3306 * commands) and the driver is the consumer.
3308 context_sz = sizeof(u32);
3309 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3311 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3313 &instance->reply_queue_h);
3315 if (!instance->reply_queue) {
3316 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3317 goto fail_reply_queue;
3320 if (megasas_issue_init_mfi(instance))
3323 instance->fw_support_ieee = 0;
3324 instance->fw_support_ieee =
3325 (instance->instancet->read_fw_status_reg(reg_set) &
3328 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3329 instance->fw_support_ieee);
3331 if (instance->fw_support_ieee)
3332 instance->flag_ieee = 1;
3335 * the following function will get the PD LIST.
3338 memset(instance->pd_list, 0 ,
3339 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3340 megasas_get_pd_list(instance);
3342 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3343 megasas_get_ld_list(instance);
3345 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3348 * Compute the max allowed sectors per IO: The controller info has two
3349 * limits on max sectors. Driver should use the minimum of these two.
3351 * 1 << stripe_sz_ops.min = max sectors per strip
3353 * Note that older firmwares ( < FW ver 30) didn't report information
3354 * to calculate max_sectors_1. So the number ended up as zero always.
3357 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3359 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3360 ctrl_info->max_strips_per_io;
3361 max_sectors_2 = ctrl_info->max_request_size;
3363 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3364 instance->disableOnlineCtrlReset =
3365 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3368 instance->max_sectors_per_req = instance->max_num_sge *
3370 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3371 instance->max_sectors_per_req = tmp_sectors;
3376 * Setup tasklet for cmd completion
3379 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3380 (unsigned long)instance);
3382 /* Initialize the cmd completion timer */
3384 megasas_start_timer(instance, &instance->io_completion_timer,
3385 megasas_io_completion_timer,
3386 MEGASAS_COMPLETION_TIMER_INTERVAL);
3391 pci_free_consistent(instance->pdev, reply_q_sz,
3392 instance->reply_queue, instance->reply_queue_h);
3394 megasas_free_cmds(instance);
3398 iounmap(instance->reg_set);
3401 pci_release_selected_regions(instance->pdev,
3402 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3408 * megasas_release_mfi - Reverses the FW initialization
3409 * @intance: Adapter soft state
3411 static void megasas_release_mfi(struct megasas_instance *instance)
3413 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3415 pci_free_consistent(instance->pdev, reply_q_sz,
3416 instance->reply_queue, instance->reply_queue_h);
3418 megasas_free_cmds(instance);
3420 iounmap(instance->reg_set);
3422 pci_release_selected_regions(instance->pdev,
3423 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3427 * megasas_get_seq_num - Gets latest event sequence numbers
3428 * @instance: Adapter soft state
3429 * @eli: FW event log sequence numbers information
3431 * FW maintains a log of all events in a non-volatile area. Upper layers would
3432 * usually find out the latest sequence number of the events, the seq number at
3433 * the boot etc. They would "read" all the events below the latest seq number
3434 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3435 * number), they would subsribe to AEN (asynchronous event notification) and
3436 * wait for the events to happen.
3439 megasas_get_seq_num(struct megasas_instance *instance,
3440 struct megasas_evt_log_info *eli)
3442 struct megasas_cmd *cmd;
3443 struct megasas_dcmd_frame *dcmd;
3444 struct megasas_evt_log_info *el_info;
3445 dma_addr_t el_info_h = 0;
3447 cmd = megasas_get_cmd(instance);
3453 dcmd = &cmd->frame->dcmd;
3454 el_info = pci_alloc_consistent(instance->pdev,
3455 sizeof(struct megasas_evt_log_info),
3459 megasas_return_cmd(instance, cmd);
3463 memset(el_info, 0, sizeof(*el_info));
3464 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3466 dcmd->cmd = MFI_CMD_DCMD;
3467 dcmd->cmd_status = 0x0;
3468 dcmd->sge_count = 1;
3469 dcmd->flags = MFI_FRAME_DIR_READ;
3472 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3473 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3474 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3475 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3477 megasas_issue_blocked_cmd(instance, cmd);
3480 * Copy the data back into callers buffer
3482 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3484 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3485 el_info, el_info_h);
3487 megasas_return_cmd(instance, cmd);
3493 * megasas_register_aen - Registers for asynchronous event notification
3494 * @instance: Adapter soft state
3495 * @seq_num: The starting sequence number
3496 * @class_locale: Class of the event
3498 * This function subscribes for AEN for events beyond the @seq_num. It requests
3499 * to be notified if and only if the event is of type @class_locale
3502 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3503 u32 class_locale_word)
3506 struct megasas_cmd *cmd;
3507 struct megasas_dcmd_frame *dcmd;
3508 union megasas_evt_class_locale curr_aen;
3509 union megasas_evt_class_locale prev_aen;
3512 * If there an AEN pending already (aen_cmd), check if the
3513 * class_locale of that pending AEN is inclusive of the new
3514 * AEN request we currently have. If it is, then we don't have
3515 * to do anything. In other words, whichever events the current
3516 * AEN request is subscribing to, have already been subscribed
3519 * If the old_cmd is _not_ inclusive, then we have to abort
3520 * that command, form a class_locale that is superset of both
3521 * old and current and re-issue to the FW
3524 curr_aen.word = class_locale_word;
3526 if (instance->aen_cmd) {
3528 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3531 * A class whose enum value is smaller is inclusive of all
3532 * higher values. If a PROGRESS (= -1) was previously
3533 * registered, then a new registration requests for higher
3534 * classes need not be sent to FW. They are automatically
3537 * Locale numbers don't have such hierarchy. They are bitmap
3540 if ((prev_aen.members.class <= curr_aen.members.class) &&
3541 !((prev_aen.members.locale & curr_aen.members.locale) ^
3542 curr_aen.members.locale)) {
3544 * Previously issued event registration includes
3545 * current request. Nothing to do.
3549 curr_aen.members.locale |= prev_aen.members.locale;
3551 if (prev_aen.members.class < curr_aen.members.class)
3552 curr_aen.members.class = prev_aen.members.class;
3554 instance->aen_cmd->abort_aen = 1;
3555 ret_val = megasas_issue_blocked_abort_cmd(instance,
3560 printk(KERN_DEBUG "megasas: Failed to abort "
3561 "previous AEN command\n");
3567 cmd = megasas_get_cmd(instance);
3572 dcmd = &cmd->frame->dcmd;
3574 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3577 * Prepare DCMD for aen registration
3579 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3581 dcmd->cmd = MFI_CMD_DCMD;
3582 dcmd->cmd_status = 0x0;
3583 dcmd->sge_count = 1;
3584 dcmd->flags = MFI_FRAME_DIR_READ;
3587 instance->last_seq_num = seq_num;
3588 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3589 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3590 dcmd->mbox.w[0] = seq_num;
3591 dcmd->mbox.w[1] = curr_aen.word;
3592 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3593 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3595 if (instance->aen_cmd != NULL) {
3596 megasas_return_cmd(instance, cmd);
3601 * Store reference to the cmd used to register for AEN. When an
3602 * application wants us to register for AEN, we have to abort this
3603 * cmd and re-register with a new EVENT LOCALE supplied by that app
3605 instance->aen_cmd = cmd;
3608 * Issue the aen registration frame
3610 instance->instancet->fire_cmd(instance,
3611 cmd->frame_phys_addr, 0, instance->reg_set);
3617 * megasas_start_aen - Subscribes to AEN during driver load time
3618 * @instance: Adapter soft state
3620 static int megasas_start_aen(struct megasas_instance *instance)
3622 struct megasas_evt_log_info eli;
3623 union megasas_evt_class_locale class_locale;
3626 * Get the latest sequence number from FW
3628 memset(&eli, 0, sizeof(eli));
3630 if (megasas_get_seq_num(instance, &eli))
3634 * Register AEN with FW for latest sequence number plus 1
3636 class_locale.members.reserved = 0;
3637 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3638 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3640 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3645 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3646 * @instance: Adapter soft state
3648 static int megasas_io_attach(struct megasas_instance *instance)
3650 struct Scsi_Host *host = instance->host;
3653 * Export parameters required by SCSI mid-layer
3655 host->irq = instance->pdev->irq;
3656 host->unique_id = instance->unique_id;
3657 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3658 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3660 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3663 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3664 host->this_id = instance->init_id;
3665 host->sg_tablesize = instance->max_num_sge;
3667 * Check if the module parameter value for max_sectors can be used
3669 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3670 instance->max_sectors_per_req = max_sectors;
3673 if (((instance->pdev->device ==
3674 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3675 (instance->pdev->device ==
3676 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3677 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3678 instance->max_sectors_per_req = max_sectors;
3680 printk(KERN_INFO "megasas: max_sectors should be > 0"
3681 "and <= %d (or < 1MB for GEN2 controller)\n",
3682 instance->max_sectors_per_req);
3687 host->max_sectors = instance->max_sectors_per_req;
3688 host->cmd_per_lun = 128;
3689 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3690 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3691 host->max_lun = MEGASAS_MAX_LUN;
3692 host->max_cmd_len = 16;
3695 * Notify the mid-layer about the new controller
3697 if (scsi_add_host(host, &instance->pdev->dev)) {
3698 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3703 * Trigger SCSI to scan our drives
3705 scsi_scan_host(host);
3710 megasas_set_dma_mask(struct pci_dev *pdev)
3713 * All our contollers are capable of performing 64-bit DMA
3716 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3718 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3719 goto fail_set_dma_mask;
3722 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3723 goto fail_set_dma_mask;
3732 * megasas_probe_one - PCI hotplug entry point
3733 * @pdev: PCI device structure
3734 * @id: PCI ids of supported hotplugged adapter
3736 static int __devinit
3737 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3740 struct Scsi_Host *host;
3741 struct megasas_instance *instance;
3744 * Announce PCI information
3746 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3747 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3748 pdev->subsystem_device);
3750 printk("bus %d:slot %d:func %d\n",
3751 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3754 * PCI prepping: enable device set bus mastering and dma mask
3756 rval = pci_enable_device_mem(pdev);
3762 pci_set_master(pdev);
3764 if (megasas_set_dma_mask(pdev))
3765 goto fail_set_dma_mask;
3767 host = scsi_host_alloc(&megasas_template,
3768 sizeof(struct megasas_instance));
3771 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3772 goto fail_alloc_instance;
3775 instance = (struct megasas_instance *)host->hostdata;
3776 memset(instance, 0, sizeof(*instance));
3777 atomic_set( &instance->fw_reset_no_pci_access, 0 );
3779 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3780 &instance->producer_h);
3781 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3782 &instance->consumer_h);
3784 if (!instance->producer || !instance->consumer) {
3785 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3786 "producer, consumer\n");
3787 goto fail_alloc_dma_buf;
3790 *instance->producer = 0;
3791 *instance->consumer = 0;
3792 megasas_poll_wait_aen = 0;
3793 instance->flag_ieee = 0;
3794 instance->ev = NULL;
3795 instance->issuepend_done = 1;
3796 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3797 megasas_poll_wait_aen = 0;
3799 instance->evt_detail = pci_alloc_consistent(pdev,
3801 megasas_evt_detail),
3802 &instance->evt_detail_h);
3804 if (!instance->evt_detail) {
3805 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3806 "event detail structure\n");
3807 goto fail_alloc_dma_buf;
3811 * Initialize locks and queues
3813 INIT_LIST_HEAD(&instance->cmd_pool);
3814 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
3816 atomic_set(&instance->fw_outstanding,0);
3818 init_waitqueue_head(&instance->int_cmd_wait_q);
3819 init_waitqueue_head(&instance->abort_cmd_wait_q);
3821 spin_lock_init(&instance->cmd_pool_lock);
3822 spin_lock_init(&instance->hba_lock);
3823 spin_lock_init(&instance->completion_lock);
3824 spin_lock_init(&poll_aen_lock);
3826 mutex_init(&instance->aen_mutex);
3829 * Initialize PCI related and misc parameters
3831 instance->pdev = pdev;
3832 instance->host = host;
3833 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3834 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3836 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3837 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3838 instance->flag_ieee = 1;
3839 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3841 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3843 megasas_dbg_lvl = 0;
3845 instance->unload = 1;
3846 instance->last_time = 0;
3847 instance->disableOnlineCtrlReset = 1;
3849 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
3852 * Initialize MFI Firmware
3854 if (megasas_init_mfi(instance))
3860 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
3861 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3865 instance->instancet->enable_intr(instance->reg_set);
3868 * Store instance in PCI softstate
3870 pci_set_drvdata(pdev, instance);
3873 * Add this controller to megasas_mgmt_info structure so that it
3874 * can be exported to management applications
3876 megasas_mgmt_info.count++;
3877 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3878 megasas_mgmt_info.max_index++;
3881 * Initiate AEN (Asynchronous Event Notification)
3883 if (megasas_start_aen(instance)) {
3884 printk(KERN_DEBUG "megasas: start aen failed\n");
3885 goto fail_start_aen;
3889 * Register with SCSI mid-layer
3891 if (megasas_io_attach(instance))
3892 goto fail_io_attach;
3894 instance->unload = 0;
3899 megasas_mgmt_info.count--;
3900 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3901 megasas_mgmt_info.max_index--;
3903 pci_set_drvdata(pdev, NULL);
3904 instance->instancet->disable_intr(instance->reg_set);
3905 free_irq(instance->pdev->irq, instance);
3907 megasas_release_mfi(instance);
3912 if (instance->evt_detail)
3913 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3914 instance->evt_detail,
3915 instance->evt_detail_h);
3917 if (instance->producer)
3918 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3919 instance->producer_h);
3920 if (instance->consumer)
3921 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3922 instance->consumer_h);
3923 scsi_host_put(host);
3925 fail_alloc_instance:
3927 pci_disable_device(pdev);
3933 * megasas_flush_cache - Requests FW to flush all its caches
3934 * @instance: Adapter soft state
3936 static void megasas_flush_cache(struct megasas_instance *instance)
3938 struct megasas_cmd *cmd;
3939 struct megasas_dcmd_frame *dcmd;
3941 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3944 cmd = megasas_get_cmd(instance);
3949 dcmd = &cmd->frame->dcmd;
3951 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3953 dcmd->cmd = MFI_CMD_DCMD;
3954 dcmd->cmd_status = 0x0;
3955 dcmd->sge_count = 0;
3956 dcmd->flags = MFI_FRAME_DIR_NONE;
3959 dcmd->data_xfer_len = 0;
3960 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3961 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3963 megasas_issue_blocked_cmd(instance, cmd);
3965 megasas_return_cmd(instance, cmd);
3971 * megasas_shutdown_controller - Instructs FW to shutdown the controller
3972 * @instance: Adapter soft state
3973 * @opcode: Shutdown/Hibernate
3975 static void megasas_shutdown_controller(struct megasas_instance *instance,
3978 struct megasas_cmd *cmd;
3979 struct megasas_dcmd_frame *dcmd;
3981 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3984 cmd = megasas_get_cmd(instance);
3989 if (instance->aen_cmd)
3990 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3992 dcmd = &cmd->frame->dcmd;
3994 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3996 dcmd->cmd = MFI_CMD_DCMD;
3997 dcmd->cmd_status = 0x0;
3998 dcmd->sge_count = 0;
3999 dcmd->flags = MFI_FRAME_DIR_NONE;
4002 dcmd->data_xfer_len = 0;
4003 dcmd->opcode = opcode;
4005 megasas_issue_blocked_cmd(instance, cmd);
4007 megasas_return_cmd(instance, cmd);
4014 * megasas_suspend - driver suspend entry point
4015 * @pdev: PCI device structure
4016 * @state: PCI power state to suspend routine
4019 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4021 struct Scsi_Host *host;
4022 struct megasas_instance *instance;
4024 instance = pci_get_drvdata(pdev);
4025 host = instance->host;
4026 instance->unload = 1;
4029 del_timer_sync(&instance->io_completion_timer);
4031 megasas_flush_cache(instance);
4032 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4034 /* cancel the delayed work if this work still in queue */
4035 if (instance->ev != NULL) {
4036 struct megasas_aen_event *ev = instance->ev;
4037 cancel_delayed_work(
4038 (struct delayed_work *)&ev->hotplug_work);
4039 flush_scheduled_work();
4040 instance->ev = NULL;
4043 tasklet_kill(&instance->isr_tasklet);
4045 pci_set_drvdata(instance->pdev, instance);
4046 instance->instancet->disable_intr(instance->reg_set);
4047 free_irq(instance->pdev->irq, instance);
4049 pci_save_state(pdev);
4050 pci_disable_device(pdev);
4052 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4058 * megasas_resume- driver resume entry point
4059 * @pdev: PCI device structure
4062 megasas_resume(struct pci_dev *pdev)
4065 struct Scsi_Host *host;
4066 struct megasas_instance *instance;
4068 instance = pci_get_drvdata(pdev);
4069 host = instance->host;
4070 pci_set_power_state(pdev, PCI_D0);
4071 pci_enable_wake(pdev, PCI_D0, 0);
4072 pci_restore_state(pdev);
4075 * PCI prepping: enable device set bus mastering and dma mask
4077 rval = pci_enable_device_mem(pdev);
4080 printk(KERN_ERR "megasas: Enable device failed\n");
4084 pci_set_master(pdev);
4086 if (megasas_set_dma_mask(pdev))
4087 goto fail_set_dma_mask;
4090 * Initialize MFI Firmware
4093 *instance->producer = 0;
4094 *instance->consumer = 0;
4096 atomic_set(&instance->fw_outstanding, 0);
4099 * We expect the FW state to be READY
4101 if (megasas_transition_to_ready(instance))
4102 goto fail_ready_state;
4104 if (megasas_issue_init_mfi(instance))
4107 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4108 (unsigned long)instance);
4113 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
4114 "megasas", instance)) {
4115 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4119 instance->instancet->enable_intr(instance->reg_set);
4122 * Initiate AEN (Asynchronous Event Notification)
4124 if (megasas_start_aen(instance))
4125 printk(KERN_ERR "megasas: Start AEN failed\n");
4127 /* Initialize the cmd completion timer */
4129 megasas_start_timer(instance, &instance->io_completion_timer,
4130 megasas_io_completion_timer,
4131 MEGASAS_COMPLETION_TIMER_INTERVAL);
4132 instance->unload = 0;
4138 if (instance->evt_detail)
4139 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4140 instance->evt_detail,
4141 instance->evt_detail_h);
4143 if (instance->producer)
4144 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4145 instance->producer_h);
4146 if (instance->consumer)
4147 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4148 instance->consumer_h);
4149 scsi_host_put(host);
4154 pci_disable_device(pdev);
4159 #define megasas_suspend NULL
4160 #define megasas_resume NULL
4164 * megasas_detach_one - PCI hot"un"plug entry point
4165 * @pdev: PCI device structure
4167 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4170 struct Scsi_Host *host;
4171 struct megasas_instance *instance;
4173 instance = pci_get_drvdata(pdev);
4174 instance->unload = 1;
4175 host = instance->host;
4178 del_timer_sync(&instance->io_completion_timer);
4180 scsi_remove_host(instance->host);
4181 megasas_flush_cache(instance);
4182 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4184 /* cancel the delayed work if this work still in queue*/
4185 if (instance->ev != NULL) {
4186 struct megasas_aen_event *ev = instance->ev;
4187 cancel_delayed_work(
4188 (struct delayed_work *)&ev->hotplug_work);
4189 flush_scheduled_work();
4190 instance->ev = NULL;
4193 tasklet_kill(&instance->isr_tasklet);
4196 * Take the instance off the instance array. Note that we will not
4197 * decrement the max_index. We let this array be sparse array
4199 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4200 if (megasas_mgmt_info.instance[i] == instance) {
4201 megasas_mgmt_info.count--;
4202 megasas_mgmt_info.instance[i] = NULL;
4208 pci_set_drvdata(instance->pdev, NULL);
4210 instance->instancet->disable_intr(instance->reg_set);
4212 free_irq(instance->pdev->irq, instance);
4214 megasas_release_mfi(instance);
4216 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4217 instance->evt_detail, instance->evt_detail_h);
4219 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4220 instance->producer_h);
4222 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4223 instance->consumer_h);
4225 scsi_host_put(host);
4227 pci_set_drvdata(pdev, NULL);
4229 pci_disable_device(pdev);
4235 * megasas_shutdown - Shutdown entry point
4236 * @device: Generic device structure
4238 static void megasas_shutdown(struct pci_dev *pdev)
4240 struct megasas_instance *instance = pci_get_drvdata(pdev);
4241 instance->unload = 1;
4242 megasas_flush_cache(instance);
4243 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4247 * megasas_mgmt_open - char node "open" entry point
4249 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4252 * Allow only those users with admin rights
4254 if (!capable(CAP_SYS_ADMIN))
4261 * megasas_mgmt_fasync - Async notifier registration from applications
4263 * This function adds the calling process to a driver global queue. When an
4264 * event occurs, SIGIO will be sent to all processes in this queue.
4266 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4270 mutex_lock(&megasas_async_queue_mutex);
4272 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4274 mutex_unlock(&megasas_async_queue_mutex);
4277 /* For sanity check when we get ioctl */
4278 filep->private_data = filep;
4282 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4288 * megasas_mgmt_poll - char node "poll" entry point
4290 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4293 unsigned long flags;
4294 poll_wait(file, &megasas_poll_wait, wait);
4295 spin_lock_irqsave(&poll_aen_lock, flags);
4296 if (megasas_poll_wait_aen)
4297 mask = (POLLIN | POLLRDNORM);
4300 spin_unlock_irqrestore(&poll_aen_lock, flags);
4305 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4306 * @instance: Adapter soft state
4307 * @argp: User's ioctl packet
4310 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4311 struct megasas_iocpacket __user * user_ioc,
4312 struct megasas_iocpacket *ioc)
4314 struct megasas_sge32 *kern_sge32;
4315 struct megasas_cmd *cmd;
4316 void *kbuff_arr[MAX_IOCTL_SGE];
4317 dma_addr_t buf_handle = 0;
4320 dma_addr_t sense_handle;
4321 unsigned long *sense_ptr;
4323 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4325 if (ioc->sge_count > MAX_IOCTL_SGE) {
4326 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4327 ioc->sge_count, MAX_IOCTL_SGE);
4331 cmd = megasas_get_cmd(instance);
4333 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4338 * User's IOCTL packet has 2 frames (maximum). Copy those two
4339 * frames into our cmd's frames. cmd->frame's context will get
4340 * overwritten when we copy from user's frames. So set that value
4343 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4344 cmd->frame->hdr.context = cmd->index;
4345 cmd->frame->hdr.pad_0 = 0;
4348 * The management interface between applications and the fw uses
4349 * MFI frames. E.g, RAID configuration changes, LD property changes
4350 * etc are accomplishes through different kinds of MFI frames. The
4351 * driver needs to care only about substituting user buffers with
4352 * kernel buffers in SGLs. The location of SGL is embedded in the
4353 * struct iocpacket itself.
4355 kern_sge32 = (struct megasas_sge32 *)
4356 ((unsigned long)cmd->frame + ioc->sgl_off);
4359 * For each user buffer, create a mirror buffer and copy in
4361 for (i = 0; i < ioc->sge_count; i++) {
4362 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4363 ioc->sgl[i].iov_len,
4364 &buf_handle, GFP_KERNEL);
4365 if (!kbuff_arr[i]) {
4366 printk(KERN_DEBUG "megasas: Failed to alloc "
4367 "kernel SGL buffer for IOCTL \n");
4373 * We don't change the dma_coherent_mask, so
4374 * pci_alloc_consistent only returns 32bit addresses
4376 kern_sge32[i].phys_addr = (u32) buf_handle;
4377 kern_sge32[i].length = ioc->sgl[i].iov_len;
4380 * We created a kernel buffer corresponding to the
4381 * user buffer. Now copy in from the user buffer
4383 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4384 (u32) (ioc->sgl[i].iov_len))) {
4390 if (ioc->sense_len) {
4391 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4392 &sense_handle, GFP_KERNEL);
4399 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4400 *sense_ptr = sense_handle;
4404 * Set the sync_cmd flag so that the ISR knows not to complete this
4405 * cmd to the SCSI mid-layer
4408 megasas_issue_blocked_cmd(instance, cmd);
4412 * copy out the kernel buffers to user buffers
4414 for (i = 0; i < ioc->sge_count; i++) {
4415 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4416 ioc->sgl[i].iov_len)) {
4423 * copy out the sense
4425 if (ioc->sense_len) {
4427 * sense_ptr points to the location that has the user
4428 * sense buffer address
4430 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4433 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4434 sense, ioc->sense_len)) {
4435 printk(KERN_ERR "megasas: Failed to copy out to user "
4443 * copy the status codes returned by the fw
4445 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4446 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4447 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4453 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4454 sense, sense_handle);
4457 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4458 dma_free_coherent(&instance->pdev->dev,
4459 kern_sge32[i].length,
4460 kbuff_arr[i], kern_sge32[i].phys_addr);
4463 megasas_return_cmd(instance, cmd);
4467 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4469 struct megasas_iocpacket __user *user_ioc =
4470 (struct megasas_iocpacket __user *)arg;
4471 struct megasas_iocpacket *ioc;
4472 struct megasas_instance *instance;
4475 unsigned long flags;
4476 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4478 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4482 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4487 instance = megasas_lookup_instance(ioc->host_no);
4493 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4494 printk(KERN_ERR "Controller in crit error\n");
4499 if (instance->unload == 1) {
4505 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4507 if (down_interruptible(&instance->ioctl_sem)) {
4508 error = -ERESTARTSYS;
4512 for (i = 0; i < wait_time; i++) {
4514 spin_lock_irqsave(&instance->hba_lock, flags);
4515 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4516 spin_unlock_irqrestore(&instance->hba_lock, flags);
4519 spin_unlock_irqrestore(&instance->hba_lock, flags);
4521 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4522 printk(KERN_NOTICE "megasas: waiting"
4523 "for controller reset to finish\n");
4529 spin_lock_irqsave(&instance->hba_lock, flags);
4530 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4531 spin_unlock_irqrestore(&instance->hba_lock, flags);
4533 printk(KERN_ERR "megaraid_sas: timed out while"
4534 "waiting for HBA to recover\n");
4538 spin_unlock_irqrestore(&instance->hba_lock, flags);
4540 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4541 up(&instance->ioctl_sem);
4548 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4550 struct megasas_instance *instance;
4551 struct megasas_aen aen;
4554 unsigned long flags;
4555 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4557 if (file->private_data != file) {
4558 printk(KERN_DEBUG "megasas: fasync_helper was not "
4563 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4566 instance = megasas_lookup_instance(aen.host_no);
4571 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4575 if (instance->unload == 1) {
4579 for (i = 0; i < wait_time; i++) {
4581 spin_lock_irqsave(&instance->hba_lock, flags);
4582 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4583 spin_unlock_irqrestore(&instance->hba_lock,
4588 spin_unlock_irqrestore(&instance->hba_lock, flags);
4590 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4591 printk(KERN_NOTICE "megasas: waiting for"
4592 "controller reset to finish\n");
4598 spin_lock_irqsave(&instance->hba_lock, flags);
4599 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4600 spin_unlock_irqrestore(&instance->hba_lock, flags);
4601 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4602 "for HBA to recover.\n");
4605 spin_unlock_irqrestore(&instance->hba_lock, flags);
4607 mutex_lock(&instance->aen_mutex);
4608 error = megasas_register_aen(instance, aen.seq_num,
4609 aen.class_locale_word);
4610 mutex_unlock(&instance->aen_mutex);
4615 * megasas_mgmt_ioctl - char node ioctl entry point
4618 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4621 case MEGASAS_IOC_FIRMWARE:
4622 return megasas_mgmt_ioctl_fw(file, arg);
4624 case MEGASAS_IOC_GET_AEN:
4625 return megasas_mgmt_ioctl_aen(file, arg);
4631 #ifdef CONFIG_COMPAT
4632 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4634 struct compat_megasas_iocpacket __user *cioc =
4635 (struct compat_megasas_iocpacket __user *)arg;
4636 struct megasas_iocpacket __user *ioc =
4637 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4642 if (clear_user(ioc, sizeof(*ioc)))
4645 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4646 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4647 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4648 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4649 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4650 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4654 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4655 * sense_len is not null, so prepare the 64bit value under
4656 * the same condition.
4658 if (ioc->sense_len) {
4659 void __user **sense_ioc_ptr =
4660 (void __user **)(ioc->frame.raw + ioc->sense_off);
4661 compat_uptr_t *sense_cioc_ptr =
4662 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4663 if (get_user(ptr, sense_cioc_ptr) ||
4664 put_user(compat_ptr(ptr), sense_ioc_ptr))
4668 for (i = 0; i < MAX_IOCTL_SGE; i++) {
4669 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4670 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4671 copy_in_user(&ioc->sgl[i].iov_len,
4672 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4676 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4678 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4679 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4680 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4687 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4691 case MEGASAS_IOC_FIRMWARE32:
4692 return megasas_mgmt_compat_ioctl_fw(file, arg);
4693 case MEGASAS_IOC_GET_AEN:
4694 return megasas_mgmt_ioctl_aen(file, arg);
4702 * File operations structure for management interface
4704 static const struct file_operations megasas_mgmt_fops = {
4705 .owner = THIS_MODULE,
4706 .open = megasas_mgmt_open,
4707 .fasync = megasas_mgmt_fasync,
4708 .unlocked_ioctl = megasas_mgmt_ioctl,
4709 .poll = megasas_mgmt_poll,
4710 #ifdef CONFIG_COMPAT
4711 .compat_ioctl = megasas_mgmt_compat_ioctl,
4713 .llseek = noop_llseek,
4717 * PCI hotplug support registration structure
4719 static struct pci_driver megasas_pci_driver = {
4721 .name = "megaraid_sas",
4722 .id_table = megasas_pci_table,
4723 .probe = megasas_probe_one,
4724 .remove = __devexit_p(megasas_detach_one),
4725 .suspend = megasas_suspend,
4726 .resume = megasas_resume,
4727 .shutdown = megasas_shutdown,
4731 * Sysfs driver attributes
4733 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4735 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4739 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4742 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4744 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4748 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4752 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4754 return sprintf(buf, "%u\n", support_poll_for_event);
4757 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4758 megasas_sysfs_show_support_poll_for_event, NULL);
4761 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
4763 return sprintf(buf, "%u\n", support_device_change);
4766 static DRIVER_ATTR(support_device_change, S_IRUGO,
4767 megasas_sysfs_show_support_device_change, NULL);
4770 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4772 return sprintf(buf, "%u\n", megasas_dbg_lvl);
4776 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4779 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4780 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4786 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
4787 megasas_sysfs_set_dbg_lvl);
4790 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4792 return sprintf(buf, "%u\n", poll_mode_io);
4796 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4797 const char *buf, size_t count)
4800 int tmp = poll_mode_io;
4802 struct megasas_instance *instance;
4804 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4805 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4810 * Check if poll_mode_io is already set or is same as previous value
4812 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4817 * Start timers for all adapters
4819 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4820 instance = megasas_mgmt_info.instance[i];
4822 megasas_start_timer(instance,
4823 &instance->io_completion_timer,
4824 megasas_io_completion_timer,
4825 MEGASAS_COMPLETION_TIMER_INTERVAL);
4830 * Delete timers for all adapters
4832 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4833 instance = megasas_mgmt_info.instance[i];
4835 del_timer_sync(&instance->io_completion_timer);
4844 megasas_aen_polling(struct work_struct *work)
4846 struct megasas_aen_event *ev =
4847 container_of(work, struct megasas_aen_event, hotplug_work);
4848 struct megasas_instance *instance = ev->instance;
4849 union megasas_evt_class_locale class_locale;
4850 struct Scsi_Host *host;
4851 struct scsi_device *sdev1;
4854 int i, j, doscan = 0;
4859 printk(KERN_ERR "invalid instance!\n");
4863 instance->ev = NULL;
4864 host = instance->host;
4865 if (instance->evt_detail) {
4867 switch (instance->evt_detail->code) {
4868 case MR_EVT_PD_INSERTED:
4869 if (megasas_get_pd_list(instance) == 0) {
4870 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4872 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4876 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4879 scsi_device_lookup(host, i, j, 0);
4881 if (instance->pd_list[pd_index].driveState
4882 == MR_PD_STATE_SYSTEM) {
4884 scsi_add_device(host, i, j, 0);
4888 scsi_device_put(sdev1);
4896 case MR_EVT_PD_REMOVED:
4897 if (megasas_get_pd_list(instance) == 0) {
4898 megasas_get_pd_list(instance);
4899 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4901 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4905 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4908 scsi_device_lookup(host, i, j, 0);
4910 if (instance->pd_list[pd_index].driveState
4911 == MR_PD_STATE_SYSTEM) {
4913 scsi_device_put(sdev1);
4917 scsi_remove_device(sdev1);
4918 scsi_device_put(sdev1);
4927 case MR_EVT_LD_OFFLINE:
4928 case MR_EVT_LD_DELETED:
4929 megasas_get_ld_list(instance);
4930 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4932 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4936 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4938 sdev1 = scsi_device_lookup(host,
4939 i + MEGASAS_MAX_LD_CHANNELS,
4943 if (instance->ld_ids[ld_index] != 0xff) {
4945 scsi_device_put(sdev1);
4949 scsi_remove_device(sdev1);
4950 scsi_device_put(sdev1);
4957 case MR_EVT_LD_CREATED:
4958 megasas_get_ld_list(instance);
4959 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4961 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4964 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4966 sdev1 = scsi_device_lookup(host,
4967 i+MEGASAS_MAX_LD_CHANNELS,
4970 if (instance->ld_ids[ld_index] !=
4973 scsi_add_device(host,
4979 scsi_device_put(sdev1);
4985 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4986 case MR_EVT_FOREIGN_CFG_IMPORTED:
4994 printk(KERN_ERR "invalid evt_detail!\n");
5000 printk(KERN_INFO "scanning ...\n");
5001 megasas_get_pd_list(instance);
5002 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5003 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5004 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5005 sdev1 = scsi_device_lookup(host, i, j, 0);
5006 if (instance->pd_list[pd_index].driveState ==
5007 MR_PD_STATE_SYSTEM) {
5009 scsi_add_device(host, i, j, 0);
5012 scsi_device_put(sdev1);
5015 scsi_remove_device(sdev1);
5016 scsi_device_put(sdev1);
5022 megasas_get_ld_list(instance);
5023 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5024 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5026 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5028 sdev1 = scsi_device_lookup(host,
5029 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5030 if (instance->ld_ids[ld_index] != 0xff) {
5032 scsi_add_device(host,
5036 scsi_device_put(sdev1);
5040 scsi_remove_device(sdev1);
5041 scsi_device_put(sdev1);
5048 if ( instance->aen_cmd != NULL ) {
5053 seq_num = instance->evt_detail->seq_num + 1;
5055 /* Register AEN with FW for latest sequence number plus 1 */
5056 class_locale.members.reserved = 0;
5057 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5058 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5059 mutex_lock(&instance->aen_mutex);
5060 error = megasas_register_aen(instance, seq_num,
5062 mutex_unlock(&instance->aen_mutex);
5065 printk(KERN_ERR "register aen failed error %x\n", error);
5071 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
5072 megasas_sysfs_show_poll_mode_io,
5073 megasas_sysfs_set_poll_mode_io);
5076 * megasas_init - Driver load entry point
5078 static int __init megasas_init(void)
5083 * Announce driver version and other information
5085 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5086 MEGASAS_EXT_VERSION);
5088 support_poll_for_event = 2;
5089 support_device_change = 1;
5091 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5094 * Register character device node
5096 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5099 printk(KERN_DEBUG "megasas: failed to open device node\n");
5103 megasas_mgmt_majorno = rval;
5106 * Register ourselves as PCI hotplug module
5108 rval = pci_register_driver(&megasas_pci_driver);
5111 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5115 rval = driver_create_file(&megasas_pci_driver.driver,
5116 &driver_attr_version);
5118 goto err_dcf_attr_ver;
5119 rval = driver_create_file(&megasas_pci_driver.driver,
5120 &driver_attr_release_date);
5122 goto err_dcf_rel_date;
5124 rval = driver_create_file(&megasas_pci_driver.driver,
5125 &driver_attr_support_poll_for_event);
5127 goto err_dcf_support_poll_for_event;
5129 rval = driver_create_file(&megasas_pci_driver.driver,
5130 &driver_attr_dbg_lvl);
5132 goto err_dcf_dbg_lvl;
5133 rval = driver_create_file(&megasas_pci_driver.driver,
5134 &driver_attr_poll_mode_io);
5136 goto err_dcf_poll_mode_io;
5138 rval = driver_create_file(&megasas_pci_driver.driver,
5139 &driver_attr_support_device_change);
5141 goto err_dcf_support_device_change;
5145 err_dcf_support_device_change:
5146 driver_remove_file(&megasas_pci_driver.driver,
5147 &driver_attr_poll_mode_io);
5149 err_dcf_poll_mode_io:
5150 driver_remove_file(&megasas_pci_driver.driver,
5151 &driver_attr_dbg_lvl);
5153 driver_remove_file(&megasas_pci_driver.driver,
5154 &driver_attr_support_poll_for_event);
5156 err_dcf_support_poll_for_event:
5157 driver_remove_file(&megasas_pci_driver.driver,
5158 &driver_attr_release_date);
5161 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5163 pci_unregister_driver(&megasas_pci_driver);
5165 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5170 * megasas_exit - Driver unload entry point
5172 static void __exit megasas_exit(void)
5174 driver_remove_file(&megasas_pci_driver.driver,
5175 &driver_attr_poll_mode_io);
5176 driver_remove_file(&megasas_pci_driver.driver,
5177 &driver_attr_dbg_lvl);
5178 driver_remove_file(&megasas_pci_driver.driver,
5179 &driver_attr_support_poll_for_event);
5180 driver_remove_file(&megasas_pci_driver.driver,
5181 &driver_attr_support_device_change);
5182 driver_remove_file(&megasas_pci_driver.driver,
5183 &driver_attr_release_date);
5184 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5186 pci_unregister_driver(&megasas_pci_driver);
5187 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5190 module_init(megasas_init);
5191 module_exit(megasas_exit);