2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2011 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_base.c
21 * Version : v00.00.06.18-rc1
23 * Authors: LSI Corporation
27 * Adam Radford <linuxraid@lsi.com>
29 * Send feedback to: <megaraidlinux@lsi.com>
31 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/list.h>
39 #include <linux/moduleparam.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
42 #include <linux/interrupt.h>
43 #include <linux/delay.h>
44 #include <linux/uio.h>
45 #include <linux/slab.h>
46 #include <asm/uaccess.h>
48 #include <linux/compat.h>
49 #include <linux/blkdev.h>
50 #include <linux/mutex.h>
51 #include <linux/poll.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
62 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
65 static unsigned int max_sectors;
66 module_param_named(max_sectors, max_sectors, int, 0);
67 MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
70 static int msix_disable;
71 module_param(msix_disable, int, S_IRUGO);
72 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74 static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
75 module_param(throttlequeuedepth, int, S_IRUGO);
76 MODULE_PARM_DESC(throttlequeuedepth,
77 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
79 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
80 module_param(resetwaittime, int, S_IRUGO);
81 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
82 "before resetting adapter. Default: 180");
84 MODULE_LICENSE("GPL");
85 MODULE_VERSION(MEGASAS_VERSION);
86 MODULE_AUTHOR("megaraidlinux@lsi.com");
87 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
89 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
90 static int megasas_get_pd_list(struct megasas_instance *instance);
91 static int megasas_issue_init_mfi(struct megasas_instance *instance);
92 static int megasas_register_aen(struct megasas_instance *instance,
93 u32 seq_num, u32 class_locale_word);
95 * PCI ID table for all supported controllers
97 static struct pci_device_id megasas_pci_table[] = {
99 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
101 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
114 /* xscale IOP, vega */
115 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
119 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
126 static int megasas_mgmt_majorno;
127 static struct megasas_mgmt_info megasas_mgmt_info;
128 static struct fasync_struct *megasas_async_queue;
129 static DEFINE_MUTEX(megasas_async_queue_mutex);
131 static int megasas_poll_wait_aen;
132 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
133 static u32 support_poll_for_event;
135 static u32 support_device_change;
137 /* define lock for aen poll */
138 spinlock_t poll_aen_lock;
141 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
144 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
146 megasas_adp_reset_gen2(struct megasas_instance *instance,
147 struct megasas_register_set __iomem *reg_set);
148 static irqreturn_t megasas_isr(int irq, void *devp);
150 megasas_init_adapter_mfi(struct megasas_instance *instance);
152 megasas_build_and_issue_cmd(struct megasas_instance *instance,
153 struct scsi_cmnd *scmd);
154 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
156 megasas_release_fusion(struct megasas_instance *instance);
158 megasas_ioc_init_fusion(struct megasas_instance *instance);
160 megasas_free_cmds_fusion(struct megasas_instance *instance);
162 megasas_get_map_info(struct megasas_instance *instance);
164 megasas_sync_map_info(struct megasas_instance *instance);
166 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
167 void megasas_reset_reply_desc(struct megasas_instance *instance);
168 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
169 struct LD_LOAD_BALANCE_INFO *lbInfo);
170 int megasas_reset_fusion(struct Scsi_Host *shost);
171 void megasas_fusion_ocr_wq(struct work_struct *work);
174 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
176 instance->instancet->fire_cmd(instance,
177 cmd->frame_phys_addr, 0, instance->reg_set);
181 * megasas_get_cmd - Get a command from the free pool
182 * @instance: Adapter soft state
184 * Returns a free command from the pool
186 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
190 struct megasas_cmd *cmd = NULL;
192 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
194 if (!list_empty(&instance->cmd_pool)) {
195 cmd = list_entry((&instance->cmd_pool)->next,
196 struct megasas_cmd, list);
197 list_del_init(&cmd->list);
199 printk(KERN_ERR "megasas: Command pool empty!\n");
202 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
207 * megasas_return_cmd - Return a cmd to free command pool
208 * @instance: Adapter soft state
209 * @cmd: Command packet to be returned to free command pool
212 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
216 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
219 cmd->frame_count = 0;
220 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
221 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
223 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
224 list_add_tail(&cmd->list, &instance->cmd_pool);
226 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
231 * The following functions are defined for xscale
232 * (deviceid : 1064R, PERC5) controllers
236 * megasas_enable_intr_xscale - Enables interrupts
237 * @regs: MFI register set
240 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
242 writel(0, &(regs)->outbound_intr_mask);
244 /* Dummy readl to force pci flush */
245 readl(®s->outbound_intr_mask);
249 * megasas_disable_intr_xscale -Disables interrupt
250 * @regs: MFI register set
253 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
256 writel(mask, ®s->outbound_intr_mask);
257 /* Dummy readl to force pci flush */
258 readl(®s->outbound_intr_mask);
262 * megasas_read_fw_status_reg_xscale - returns the current FW status value
263 * @regs: MFI register set
266 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
268 return readl(&(regs)->outbound_msg_0);
271 * megasas_clear_interrupt_xscale - Check & clear interrupt
272 * @regs: MFI register set
275 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
280 * Check if it is our interrupt
282 status = readl(®s->outbound_intr_status);
284 if (status & MFI_OB_INTR_STATUS_MASK)
285 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
286 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
287 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
290 * Clear the interrupt by writing back the same value
293 writel(status, ®s->outbound_intr_status);
295 /* Dummy readl to force pci flush */
296 readl(®s->outbound_intr_status);
302 * megasas_fire_cmd_xscale - Sends command to the FW
303 * @frame_phys_addr : Physical address of cmd
304 * @frame_count : Number of frames for the command
305 * @regs : MFI register set
308 megasas_fire_cmd_xscale(struct megasas_instance *instance,
309 dma_addr_t frame_phys_addr,
311 struct megasas_register_set __iomem *regs)
314 spin_lock_irqsave(&instance->hba_lock, flags);
315 writel((frame_phys_addr >> 3)|(frame_count),
316 &(regs)->inbound_queue_port);
317 spin_unlock_irqrestore(&instance->hba_lock, flags);
321 * megasas_adp_reset_xscale - For controller reset
322 * @regs: MFI register set
325 megasas_adp_reset_xscale(struct megasas_instance *instance,
326 struct megasas_register_set __iomem *regs)
330 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
332 for (i = 0; i < 3; i++)
333 msleep(1000); /* sleep for 3 secs */
335 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
336 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
338 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
340 pci_write_config_dword(instance->pdev,
341 MFI_1068_PCSR_OFFSET, pcidata);
343 for (i = 0; i < 2; i++)
344 msleep(1000); /* need to wait 2 secs again */
347 pci_read_config_dword(instance->pdev,
348 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
349 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
350 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
351 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
353 pci_write_config_dword(instance->pdev,
354 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
361 * megasas_check_reset_xscale - For controller reset check
362 * @regs: MFI register set
365 megasas_check_reset_xscale(struct megasas_instance *instance,
366 struct megasas_register_set __iomem *regs)
369 consumer = *instance->consumer;
371 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
372 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
378 static struct megasas_instance_template megasas_instance_template_xscale = {
380 .fire_cmd = megasas_fire_cmd_xscale,
381 .enable_intr = megasas_enable_intr_xscale,
382 .disable_intr = megasas_disable_intr_xscale,
383 .clear_intr = megasas_clear_intr_xscale,
384 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
385 .adp_reset = megasas_adp_reset_xscale,
386 .check_reset = megasas_check_reset_xscale,
387 .service_isr = megasas_isr,
388 .tasklet = megasas_complete_cmd_dpc,
389 .init_adapter = megasas_init_adapter_mfi,
390 .build_and_issue_cmd = megasas_build_and_issue_cmd,
391 .issue_dcmd = megasas_issue_dcmd,
395 * This is the end of set of functions & definitions specific
396 * to xscale (deviceid : 1064R, PERC5) controllers
400 * The following functions are defined for ppc (deviceid : 0x60)
405 * megasas_enable_intr_ppc - Enables interrupts
406 * @regs: MFI register set
409 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
411 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
413 writel(~0x80000000, &(regs)->outbound_intr_mask);
415 /* Dummy readl to force pci flush */
416 readl(®s->outbound_intr_mask);
420 * megasas_disable_intr_ppc - Disable interrupt
421 * @regs: MFI register set
424 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
426 u32 mask = 0xFFFFFFFF;
427 writel(mask, ®s->outbound_intr_mask);
428 /* Dummy readl to force pci flush */
429 readl(®s->outbound_intr_mask);
433 * megasas_read_fw_status_reg_ppc - returns the current FW status value
434 * @regs: MFI register set
437 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
439 return readl(&(regs)->outbound_scratch_pad);
443 * megasas_clear_interrupt_ppc - Check & clear interrupt
444 * @regs: MFI register set
447 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
449 u32 status, mfiStatus = 0;
452 * Check if it is our interrupt
454 status = readl(®s->outbound_intr_status);
456 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
457 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
459 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
460 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
463 * Clear the interrupt by writing back the same value
465 writel(status, ®s->outbound_doorbell_clear);
467 /* Dummy readl to force pci flush */
468 readl(®s->outbound_doorbell_clear);
474 * megasas_fire_cmd_ppc - Sends command to the FW
475 * @frame_phys_addr : Physical address of cmd
476 * @frame_count : Number of frames for the command
477 * @regs : MFI register set
480 megasas_fire_cmd_ppc(struct megasas_instance *instance,
481 dma_addr_t frame_phys_addr,
483 struct megasas_register_set __iomem *regs)
486 spin_lock_irqsave(&instance->hba_lock, flags);
487 writel((frame_phys_addr | (frame_count<<1))|1,
488 &(regs)->inbound_queue_port);
489 spin_unlock_irqrestore(&instance->hba_lock, flags);
493 * megasas_check_reset_ppc - For controller reset check
494 * @regs: MFI register set
497 megasas_check_reset_ppc(struct megasas_instance *instance,
498 struct megasas_register_set __iomem *regs)
500 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
506 static struct megasas_instance_template megasas_instance_template_ppc = {
508 .fire_cmd = megasas_fire_cmd_ppc,
509 .enable_intr = megasas_enable_intr_ppc,
510 .disable_intr = megasas_disable_intr_ppc,
511 .clear_intr = megasas_clear_intr_ppc,
512 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
513 .adp_reset = megasas_adp_reset_xscale,
514 .check_reset = megasas_check_reset_ppc,
515 .service_isr = megasas_isr,
516 .tasklet = megasas_complete_cmd_dpc,
517 .init_adapter = megasas_init_adapter_mfi,
518 .build_and_issue_cmd = megasas_build_and_issue_cmd,
519 .issue_dcmd = megasas_issue_dcmd,
523 * megasas_enable_intr_skinny - Enables interrupts
524 * @regs: MFI register set
527 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
529 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
531 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
533 /* Dummy readl to force pci flush */
534 readl(®s->outbound_intr_mask);
538 * megasas_disable_intr_skinny - Disables interrupt
539 * @regs: MFI register set
542 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
544 u32 mask = 0xFFFFFFFF;
545 writel(mask, ®s->outbound_intr_mask);
546 /* Dummy readl to force pci flush */
547 readl(®s->outbound_intr_mask);
551 * megasas_read_fw_status_reg_skinny - returns the current FW status value
552 * @regs: MFI register set
555 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
557 return readl(&(regs)->outbound_scratch_pad);
561 * megasas_clear_interrupt_skinny - Check & clear interrupt
562 * @regs: MFI register set
565 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
571 * Check if it is our interrupt
573 status = readl(®s->outbound_intr_status);
575 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
580 * Check if it is our interrupt
582 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
584 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
586 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
589 * Clear the interrupt by writing back the same value
591 writel(status, ®s->outbound_intr_status);
594 * dummy read to flush PCI
596 readl(®s->outbound_intr_status);
602 * megasas_fire_cmd_skinny - Sends command to the FW
603 * @frame_phys_addr : Physical address of cmd
604 * @frame_count : Number of frames for the command
605 * @regs : MFI register set
608 megasas_fire_cmd_skinny(struct megasas_instance *instance,
609 dma_addr_t frame_phys_addr,
611 struct megasas_register_set __iomem *regs)
614 spin_lock_irqsave(&instance->hba_lock, flags);
615 writel(0, &(regs)->inbound_high_queue_port);
616 writel((frame_phys_addr | (frame_count<<1))|1,
617 &(regs)->inbound_low_queue_port);
618 spin_unlock_irqrestore(&instance->hba_lock, flags);
622 * megasas_check_reset_skinny - For controller reset check
623 * @regs: MFI register set
626 megasas_check_reset_skinny(struct megasas_instance *instance,
627 struct megasas_register_set __iomem *regs)
629 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
635 static struct megasas_instance_template megasas_instance_template_skinny = {
637 .fire_cmd = megasas_fire_cmd_skinny,
638 .enable_intr = megasas_enable_intr_skinny,
639 .disable_intr = megasas_disable_intr_skinny,
640 .clear_intr = megasas_clear_intr_skinny,
641 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
642 .adp_reset = megasas_adp_reset_gen2,
643 .check_reset = megasas_check_reset_skinny,
644 .service_isr = megasas_isr,
645 .tasklet = megasas_complete_cmd_dpc,
646 .init_adapter = megasas_init_adapter_mfi,
647 .build_and_issue_cmd = megasas_build_and_issue_cmd,
648 .issue_dcmd = megasas_issue_dcmd,
653 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
658 * megasas_enable_intr_gen2 - Enables interrupts
659 * @regs: MFI register set
662 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
664 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
666 /* write ~0x00000005 (4 & 1) to the intr mask*/
667 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
669 /* Dummy readl to force pci flush */
670 readl(®s->outbound_intr_mask);
674 * megasas_disable_intr_gen2 - Disables interrupt
675 * @regs: MFI register set
678 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
680 u32 mask = 0xFFFFFFFF;
681 writel(mask, ®s->outbound_intr_mask);
682 /* Dummy readl to force pci flush */
683 readl(®s->outbound_intr_mask);
687 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
688 * @regs: MFI register set
691 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
693 return readl(&(regs)->outbound_scratch_pad);
697 * megasas_clear_interrupt_gen2 - Check & clear interrupt
698 * @regs: MFI register set
701 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
706 * Check if it is our interrupt
708 status = readl(®s->outbound_intr_status);
710 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
711 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
713 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
714 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
718 * Clear the interrupt by writing back the same value
721 writel(status, ®s->outbound_doorbell_clear);
723 /* Dummy readl to force pci flush */
724 readl(®s->outbound_intr_status);
729 * megasas_fire_cmd_gen2 - Sends command to the FW
730 * @frame_phys_addr : Physical address of cmd
731 * @frame_count : Number of frames for the command
732 * @regs : MFI register set
735 megasas_fire_cmd_gen2(struct megasas_instance *instance,
736 dma_addr_t frame_phys_addr,
738 struct megasas_register_set __iomem *regs)
741 spin_lock_irqsave(&instance->hba_lock, flags);
742 writel((frame_phys_addr | (frame_count<<1))|1,
743 &(regs)->inbound_queue_port);
744 spin_unlock_irqrestore(&instance->hba_lock, flags);
748 * megasas_adp_reset_gen2 - For controller reset
749 * @regs: MFI register set
752 megasas_adp_reset_gen2(struct megasas_instance *instance,
753 struct megasas_register_set __iomem *reg_set)
757 u32 *seq_offset = ®_set->seq_offset;
758 u32 *hostdiag_offset = ®_set->host_diag;
760 if (instance->instancet == &megasas_instance_template_skinny) {
761 seq_offset = ®_set->fusion_seq_offset;
762 hostdiag_offset = ®_set->fusion_host_diag;
765 writel(0, seq_offset);
766 writel(4, seq_offset);
767 writel(0xb, seq_offset);
768 writel(2, seq_offset);
769 writel(7, seq_offset);
770 writel(0xd, seq_offset);
774 HostDiag = (u32)readl(hostdiag_offset);
776 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
778 HostDiag = (u32)readl(hostdiag_offset);
779 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
787 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
789 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
793 HostDiag = (u32)readl(hostdiag_offset);
794 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
796 HostDiag = (u32)readl(hostdiag_offset);
797 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
808 * megasas_check_reset_gen2 - For controller reset check
809 * @regs: MFI register set
812 megasas_check_reset_gen2(struct megasas_instance *instance,
813 struct megasas_register_set __iomem *regs)
815 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
822 static struct megasas_instance_template megasas_instance_template_gen2 = {
824 .fire_cmd = megasas_fire_cmd_gen2,
825 .enable_intr = megasas_enable_intr_gen2,
826 .disable_intr = megasas_disable_intr_gen2,
827 .clear_intr = megasas_clear_intr_gen2,
828 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
829 .adp_reset = megasas_adp_reset_gen2,
830 .check_reset = megasas_check_reset_gen2,
831 .service_isr = megasas_isr,
832 .tasklet = megasas_complete_cmd_dpc,
833 .init_adapter = megasas_init_adapter_mfi,
834 .build_and_issue_cmd = megasas_build_and_issue_cmd,
835 .issue_dcmd = megasas_issue_dcmd,
839 * This is the end of set of functions & definitions
840 * specific to gen2 (deviceid : 0x78, 0x79) controllers
844 * Template added for TB (Fusion)
846 extern struct megasas_instance_template megasas_instance_template_fusion;
849 * megasas_issue_polled - Issues a polling command
850 * @instance: Adapter soft state
851 * @cmd: Command packet to be issued
853 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
856 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
859 struct megasas_header *frame_hdr = &cmd->frame->hdr;
861 frame_hdr->cmd_status = 0xFF;
862 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
865 * Issue the frame using inbound queue port
867 instance->instancet->issue_dcmd(instance, cmd);
870 * Wait for cmd_status to change
872 return wait_and_poll(instance, cmd);
876 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
877 * @instance: Adapter soft state
878 * @cmd: Command to be issued
880 * This function waits on an event for the command to be returned from ISR.
881 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
882 * Used to issue ioctl commands.
885 megasas_issue_blocked_cmd(struct megasas_instance *instance,
886 struct megasas_cmd *cmd)
888 cmd->cmd_status = ENODATA;
890 instance->instancet->issue_dcmd(instance, cmd);
892 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
898 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
899 * @instance: Adapter soft state
900 * @cmd_to_abort: Previously issued cmd to be aborted
902 * MFI firmware can abort previously issued AEN command (automatic event
903 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
904 * cmd and waits for return status.
905 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
908 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
909 struct megasas_cmd *cmd_to_abort)
911 struct megasas_cmd *cmd;
912 struct megasas_abort_frame *abort_fr;
914 cmd = megasas_get_cmd(instance);
919 abort_fr = &cmd->frame->abort;
922 * Prepare and issue the abort frame
924 abort_fr->cmd = MFI_CMD_ABORT;
925 abort_fr->cmd_status = 0xFF;
927 abort_fr->abort_context = cmd_to_abort->index;
928 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
929 abort_fr->abort_mfi_phys_addr_hi = 0;
932 cmd->cmd_status = 0xFF;
934 instance->instancet->issue_dcmd(instance, cmd);
937 * Wait for this cmd to complete
939 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
942 megasas_return_cmd(instance, cmd);
947 * megasas_make_sgl32 - Prepares 32-bit SGL
948 * @instance: Adapter soft state
949 * @scp: SCSI command from the mid-layer
950 * @mfi_sgl: SGL to be filled in
952 * If successful, this function returns the number of SG elements. Otherwise,
956 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
957 union megasas_sgl *mfi_sgl)
961 struct scatterlist *os_sgl;
963 sge_count = scsi_dma_map(scp);
964 BUG_ON(sge_count < 0);
967 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
968 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
969 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
976 * megasas_make_sgl64 - Prepares 64-bit SGL
977 * @instance: Adapter soft state
978 * @scp: SCSI command from the mid-layer
979 * @mfi_sgl: SGL to be filled in
981 * If successful, this function returns the number of SG elements. Otherwise,
985 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
986 union megasas_sgl *mfi_sgl)
990 struct scatterlist *os_sgl;
992 sge_count = scsi_dma_map(scp);
993 BUG_ON(sge_count < 0);
996 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
997 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
998 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1005 * megasas_make_sgl_skinny - Prepares IEEE SGL
1006 * @instance: Adapter soft state
1007 * @scp: SCSI command from the mid-layer
1008 * @mfi_sgl: SGL to be filled in
1010 * If successful, this function returns the number of SG elements. Otherwise,
1014 megasas_make_sgl_skinny(struct megasas_instance *instance,
1015 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1019 struct scatterlist *os_sgl;
1021 sge_count = scsi_dma_map(scp);
1024 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1025 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1026 mfi_sgl->sge_skinny[i].phys_addr =
1027 sg_dma_address(os_sgl);
1028 mfi_sgl->sge_skinny[i].flag = 0;
1035 * megasas_get_frame_count - Computes the number of frames
1036 * @frame_type : type of frame- io or pthru frame
1037 * @sge_count : number of sg elements
1039 * Returns the number of frames required for numnber of sge's (sge_count)
1042 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1043 u8 sge_count, u8 frame_type)
1050 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1051 sizeof(struct megasas_sge32);
1053 if (instance->flag_ieee) {
1054 sge_sz = sizeof(struct megasas_sge_skinny);
1058 * Main frame can contain 2 SGEs for 64-bit SGLs and
1059 * 3 SGEs for 32-bit SGLs for ldio &
1060 * 1 SGEs for 64-bit SGLs and
1061 * 2 SGEs for 32-bit SGLs for pthru frame
1063 if (unlikely(frame_type == PTHRU_FRAME)) {
1064 if (instance->flag_ieee == 1) {
1065 num_cnt = sge_count - 1;
1066 } else if (IS_DMA64)
1067 num_cnt = sge_count - 1;
1069 num_cnt = sge_count - 2;
1071 if (instance->flag_ieee == 1) {
1072 num_cnt = sge_count - 1;
1073 } else if (IS_DMA64)
1074 num_cnt = sge_count - 2;
1076 num_cnt = sge_count - 3;
1080 sge_bytes = sge_sz * num_cnt;
1082 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1083 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1088 if (frame_count > 7)
1094 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1095 * @instance: Adapter soft state
1096 * @scp: SCSI command
1097 * @cmd: Command to be prepared in
1099 * This function prepares CDB commands. These are typcially pass-through
1100 * commands to the devices.
1103 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1104 struct megasas_cmd *cmd)
1109 struct megasas_pthru_frame *pthru;
1111 is_logical = MEGASAS_IS_LOGICAL(scp);
1112 device_id = MEGASAS_DEV_INDEX(instance, scp);
1113 pthru = (struct megasas_pthru_frame *)cmd->frame;
1115 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1116 flags = MFI_FRAME_DIR_WRITE;
1117 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1118 flags = MFI_FRAME_DIR_READ;
1119 else if (scp->sc_data_direction == PCI_DMA_NONE)
1120 flags = MFI_FRAME_DIR_NONE;
1122 if (instance->flag_ieee == 1) {
1123 flags |= MFI_FRAME_IEEE;
1127 * Prepare the DCDB frame
1129 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1130 pthru->cmd_status = 0x0;
1131 pthru->scsi_status = 0x0;
1132 pthru->target_id = device_id;
1133 pthru->lun = scp->device->lun;
1134 pthru->cdb_len = scp->cmd_len;
1137 pthru->flags = flags;
1138 pthru->data_xfer_len = scsi_bufflen(scp);
1140 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1143 * If the command is for the tape device, set the
1144 * pthru timeout to the os layer timeout value.
1146 if (scp->device->type == TYPE_TAPE) {
1147 if ((scp->request->timeout / HZ) > 0xFFFF)
1148 pthru->timeout = 0xFFFF;
1150 pthru->timeout = scp->request->timeout / HZ;
1156 if (instance->flag_ieee == 1) {
1157 pthru->flags |= MFI_FRAME_SGL64;
1158 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1160 } else if (IS_DMA64) {
1161 pthru->flags |= MFI_FRAME_SGL64;
1162 pthru->sge_count = megasas_make_sgl64(instance, scp,
1165 pthru->sge_count = megasas_make_sgl32(instance, scp,
1168 if (pthru->sge_count > instance->max_num_sge) {
1169 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1175 * Sense info specific
1177 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1178 pthru->sense_buf_phys_addr_hi = 0;
1179 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1182 * Compute the total number of frames this command consumes. FW uses
1183 * this number to pull sufficient number of frames from host memory.
1185 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1188 return cmd->frame_count;
1192 * megasas_build_ldio - Prepares IOs to logical devices
1193 * @instance: Adapter soft state
1194 * @scp: SCSI command
1195 * @cmd: Command to be prepared
1197 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1200 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1201 struct megasas_cmd *cmd)
1204 u8 sc = scp->cmnd[0];
1206 struct megasas_io_frame *ldio;
1208 device_id = MEGASAS_DEV_INDEX(instance, scp);
1209 ldio = (struct megasas_io_frame *)cmd->frame;
1211 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1212 flags = MFI_FRAME_DIR_WRITE;
1213 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1214 flags = MFI_FRAME_DIR_READ;
1216 if (instance->flag_ieee == 1) {
1217 flags |= MFI_FRAME_IEEE;
1221 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1223 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1224 ldio->cmd_status = 0x0;
1225 ldio->scsi_status = 0x0;
1226 ldio->target_id = device_id;
1228 ldio->reserved_0 = 0;
1230 ldio->flags = flags;
1231 ldio->start_lba_hi = 0;
1232 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1235 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1237 if (scp->cmd_len == 6) {
1238 ldio->lba_count = (u32) scp->cmnd[4];
1239 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1240 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1242 ldio->start_lba_lo &= 0x1FFFFF;
1246 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1248 else if (scp->cmd_len == 10) {
1249 ldio->lba_count = (u32) scp->cmnd[8] |
1250 ((u32) scp->cmnd[7] << 8);
1251 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1252 ((u32) scp->cmnd[3] << 16) |
1253 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1257 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1259 else if (scp->cmd_len == 12) {
1260 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1261 ((u32) scp->cmnd[7] << 16) |
1262 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1264 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1265 ((u32) scp->cmnd[3] << 16) |
1266 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1270 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1272 else if (scp->cmd_len == 16) {
1273 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1274 ((u32) scp->cmnd[11] << 16) |
1275 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1277 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1278 ((u32) scp->cmnd[7] << 16) |
1279 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1281 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1282 ((u32) scp->cmnd[3] << 16) |
1283 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1290 if (instance->flag_ieee) {
1291 ldio->flags |= MFI_FRAME_SGL64;
1292 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1294 } else if (IS_DMA64) {
1295 ldio->flags |= MFI_FRAME_SGL64;
1296 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1298 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1300 if (ldio->sge_count > instance->max_num_sge) {
1301 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1307 * Sense info specific
1309 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1310 ldio->sense_buf_phys_addr_hi = 0;
1311 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1314 * Compute the total number of frames this command consumes. FW uses
1315 * this number to pull sufficient number of frames from host memory.
1317 cmd->frame_count = megasas_get_frame_count(instance,
1318 ldio->sge_count, IO_FRAME);
1320 return cmd->frame_count;
1324 * megasas_is_ldio - Checks if the cmd is for logical drive
1325 * @scmd: SCSI command
1327 * Called by megasas_queue_command to find out if the command to be queued
1328 * is a logical drive command
1330 inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1332 if (!MEGASAS_IS_LOGICAL(cmd))
1334 switch (cmd->cmnd[0]) {
1350 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1352 * @instance: Adapter soft state
1355 megasas_dump_pending_frames(struct megasas_instance *instance)
1357 struct megasas_cmd *cmd;
1359 union megasas_sgl *mfi_sgl;
1360 struct megasas_io_frame *ldio;
1361 struct megasas_pthru_frame *pthru;
1363 u32 max_cmd = instance->max_fw_cmds;
1365 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1366 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1368 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1370 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1372 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1373 for (i = 0; i < max_cmd; i++) {
1374 cmd = instance->cmd_list[i];
1377 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1378 if (megasas_is_ldio(cmd->scmd)){
1379 ldio = (struct megasas_io_frame *)cmd->frame;
1380 mfi_sgl = &ldio->sgl;
1381 sgcount = ldio->sge_count;
1382 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);
1385 pthru = (struct megasas_pthru_frame *) cmd->frame;
1386 mfi_sgl = &pthru->sgl;
1387 sgcount = pthru->sge_count;
1388 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);
1390 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1391 for (n = 0; n < sgcount; n++){
1393 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) ;
1395 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1398 printk(KERN_ERR "\n");
1400 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1401 for (i = 0; i < max_cmd; i++) {
1403 cmd = instance->cmd_list[i];
1405 if(cmd->sync_cmd == 1){
1406 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1409 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1413 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1414 struct scsi_cmnd *scmd)
1416 struct megasas_cmd *cmd;
1419 cmd = megasas_get_cmd(instance);
1421 return SCSI_MLQUEUE_HOST_BUSY;
1424 * Logical drive command
1426 if (megasas_is_ldio(scmd))
1427 frame_count = megasas_build_ldio(instance, scmd, cmd);
1429 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1432 goto out_return_cmd;
1435 scmd->SCp.ptr = (char *)cmd;
1438 * Issue the command to the FW
1440 atomic_inc(&instance->fw_outstanding);
1442 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1443 cmd->frame_count-1, instance->reg_set);
1447 megasas_return_cmd(instance, cmd);
1453 * megasas_queue_command - Queue entry point
1454 * @scmd: SCSI command to be queued
1455 * @done: Callback entry point
1458 megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1460 struct megasas_instance *instance;
1461 unsigned long flags;
1463 instance = (struct megasas_instance *)
1464 scmd->device->host->hostdata;
1466 if (instance->issuepend_done == 0)
1467 return SCSI_MLQUEUE_HOST_BUSY;
1469 spin_lock_irqsave(&instance->hba_lock, flags);
1470 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1471 spin_unlock_irqrestore(&instance->hba_lock, flags);
1472 return SCSI_MLQUEUE_HOST_BUSY;
1475 spin_unlock_irqrestore(&instance->hba_lock, flags);
1477 scmd->scsi_done = done;
1480 if (MEGASAS_IS_LOGICAL(scmd) &&
1481 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1482 scmd->result = DID_BAD_TARGET << 16;
1486 switch (scmd->cmnd[0]) {
1487 case SYNCHRONIZE_CACHE:
1489 * FW takes care of flush cache on its own
1490 * No need to send it down
1492 scmd->result = DID_OK << 16;
1498 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1499 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1500 return SCSI_MLQUEUE_HOST_BUSY;
1510 static DEF_SCSI_QCMD(megasas_queue_command)
1512 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1516 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1518 if ((megasas_mgmt_info.instance[i]) &&
1519 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1520 return megasas_mgmt_info.instance[i];
1526 static int megasas_slave_configure(struct scsi_device *sdev)
1529 struct megasas_instance *instance ;
1531 instance = megasas_lookup_instance(sdev->host->host_no);
1534 * Don't export physical disk devices to the disk driver.
1536 * FIXME: Currently we don't export them to the midlayer at all.
1537 * That will be fixed once LSI engineers have audited the
1538 * firmware for possible issues.
1540 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1541 sdev->type == TYPE_DISK) {
1542 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1544 if (instance->pd_list[pd_index].driveState ==
1545 MR_PD_STATE_SYSTEM) {
1546 blk_queue_rq_timeout(sdev->request_queue,
1547 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1554 * The RAID firmware may require extended timeouts.
1556 blk_queue_rq_timeout(sdev->request_queue,
1557 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1561 static int megasas_slave_alloc(struct scsi_device *sdev)
1564 struct megasas_instance *instance ;
1565 instance = megasas_lookup_instance(sdev->host->host_no);
1566 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1567 (sdev->type == TYPE_DISK)) {
1569 * Open the OS scan to the SYSTEM PD
1572 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1574 if ((instance->pd_list[pd_index].driveState ==
1575 MR_PD_STATE_SYSTEM) &&
1576 (instance->pd_list[pd_index].driveType ==
1585 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1587 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1588 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1589 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1590 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
1591 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1593 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1598 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1599 * restored to max value
1600 * @instance: Adapter soft state
1604 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1606 unsigned long flags;
1607 if (instance->flag & MEGASAS_FW_BUSY
1608 && time_after(jiffies, instance->last_time + 5 * HZ)
1609 && atomic_read(&instance->fw_outstanding) <
1610 instance->throttlequeuedepth + 1) {
1612 spin_lock_irqsave(instance->host->host_lock, flags);
1613 instance->flag &= ~MEGASAS_FW_BUSY;
1614 if ((instance->pdev->device ==
1615 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1616 (instance->pdev->device ==
1617 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1618 instance->host->can_queue =
1619 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1621 instance->host->can_queue =
1622 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1624 spin_unlock_irqrestore(instance->host->host_lock, flags);
1629 * megasas_complete_cmd_dpc - Returns FW's controller structure
1630 * @instance_addr: Address of adapter soft state
1632 * Tasklet to complete cmds
1634 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1639 struct megasas_cmd *cmd;
1640 struct megasas_instance *instance =
1641 (struct megasas_instance *)instance_addr;
1642 unsigned long flags;
1644 /* If we have already declared adapter dead, donot complete cmds */
1645 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1648 spin_lock_irqsave(&instance->completion_lock, flags);
1650 producer = *instance->producer;
1651 consumer = *instance->consumer;
1653 while (consumer != producer) {
1654 context = instance->reply_queue[consumer];
1655 if (context >= instance->max_fw_cmds) {
1656 printk(KERN_ERR "Unexpected context value %x\n",
1661 cmd = instance->cmd_list[context];
1663 megasas_complete_cmd(instance, cmd, DID_OK);
1666 if (consumer == (instance->max_fw_cmds + 1)) {
1671 *instance->consumer = producer;
1673 spin_unlock_irqrestore(&instance->completion_lock, flags);
1676 * Check if we can restore can_queue
1678 megasas_check_and_restore_queue_depth(instance);
1682 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1685 process_fw_state_change_wq(struct work_struct *work);
1687 void megasas_do_ocr(struct megasas_instance *instance)
1689 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1690 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1691 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1692 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1694 instance->instancet->disable_intr(instance->reg_set);
1695 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1696 instance->issuepend_done = 0;
1698 atomic_set(&instance->fw_outstanding, 0);
1699 megasas_internal_reset_defer_cmds(instance);
1700 process_fw_state_change_wq(&instance->work_init);
1704 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1705 * @instance: Adapter soft state
1707 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
1708 * complete all its outstanding commands. Returns error if one or more IOs
1709 * are pending after this time period. It also marks the controller dead.
1711 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1715 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1717 unsigned long flags;
1718 struct list_head clist_local;
1719 struct megasas_cmd *reset_cmd;
1721 u8 kill_adapter_flag;
1723 spin_lock_irqsave(&instance->hba_lock, flags);
1724 adprecovery = instance->adprecovery;
1725 spin_unlock_irqrestore(&instance->hba_lock, flags);
1727 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1729 INIT_LIST_HEAD(&clist_local);
1730 spin_lock_irqsave(&instance->hba_lock, flags);
1731 list_splice_init(&instance->internal_reset_pending_q,
1733 spin_unlock_irqrestore(&instance->hba_lock, flags);
1735 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1736 for (i = 0; i < wait_time; i++) {
1738 spin_lock_irqsave(&instance->hba_lock, flags);
1739 adprecovery = instance->adprecovery;
1740 spin_unlock_irqrestore(&instance->hba_lock, flags);
1741 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1745 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1746 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1747 spin_lock_irqsave(&instance->hba_lock, flags);
1748 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1749 spin_unlock_irqrestore(&instance->hba_lock, flags);
1754 while (!list_empty(&clist_local)) {
1755 reset_cmd = list_entry((&clist_local)->next,
1756 struct megasas_cmd, list);
1757 list_del_init(&reset_cmd->list);
1758 if (reset_cmd->scmd) {
1759 reset_cmd->scmd->result = DID_RESET << 16;
1760 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
1761 reset_index, reset_cmd,
1762 reset_cmd->scmd->cmnd[0]);
1764 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1765 megasas_return_cmd(instance, reset_cmd);
1766 } else if (reset_cmd->sync_cmd) {
1767 printk(KERN_NOTICE "megasas:%p synch cmds"
1771 reset_cmd->cmd_status = ENODATA;
1772 instance->instancet->fire_cmd(instance,
1773 reset_cmd->frame_phys_addr,
1774 0, instance->reg_set);
1776 printk(KERN_NOTICE "megasas: %p unexpected"
1786 for (i = 0; i < resetwaittime; i++) {
1788 int outstanding = atomic_read(&instance->fw_outstanding);
1793 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1794 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1795 "commands to complete\n",i,outstanding);
1797 * Call cmd completion routine. Cmd to be
1798 * be completed directly without depending on isr.
1800 megasas_complete_cmd_dpc((unsigned long)instance);
1807 kill_adapter_flag = 0;
1809 fw_state = instance->instancet->read_fw_status_reg(
1810 instance->reg_set) & MFI_STATE_MASK;
1811 if ((fw_state == MFI_STATE_FAULT) &&
1812 (instance->disableOnlineCtrlReset == 0)) {
1814 kill_adapter_flag = 2;
1817 megasas_do_ocr(instance);
1818 kill_adapter_flag = 1;
1820 /* wait for 1 secs to let FW finish the pending cmds */
1826 if (atomic_read(&instance->fw_outstanding) &&
1827 !kill_adapter_flag) {
1828 if (instance->disableOnlineCtrlReset == 0) {
1830 megasas_do_ocr(instance);
1832 /* wait for 5 secs to let FW finish the pending cmds */
1833 for (i = 0; i < wait_time; i++) {
1835 atomic_read(&instance->fw_outstanding);
1843 if (atomic_read(&instance->fw_outstanding) ||
1844 (kill_adapter_flag == 2)) {
1845 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1847 * Send signal to FW to stop processing any pending cmds.
1848 * The controller will be taken offline by the OS now.
1850 if ((instance->pdev->device ==
1851 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1852 (instance->pdev->device ==
1853 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1854 writel(MFI_STOP_ADP,
1855 &instance->reg_set->doorbell);
1857 writel(MFI_STOP_ADP,
1858 &instance->reg_set->inbound_doorbell);
1860 megasas_dump_pending_frames(instance);
1861 spin_lock_irqsave(&instance->hba_lock, flags);
1862 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1863 spin_unlock_irqrestore(&instance->hba_lock, flags);
1867 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1873 * megasas_generic_reset - Generic reset routine
1874 * @scmd: Mid-layer SCSI command
1876 * This routine implements a generic reset handler for device, bus and host
1877 * reset requests. Device, bus and host specific reset handlers can use this
1878 * function after they do their specific tasks.
1880 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1883 struct megasas_instance *instance;
1885 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1887 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1888 scmd->cmnd[0], scmd->retries);
1890 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1891 printk(KERN_ERR "megasas: cannot recover from previous reset "
1896 ret_val = megasas_wait_for_outstanding(instance);
1897 if (ret_val == SUCCESS)
1898 printk(KERN_NOTICE "megasas: reset successful \n");
1900 printk(KERN_ERR "megasas: failed to do reset\n");
1906 * megasas_reset_timer - quiesce the adapter if required
1909 * Sets the FW busy flag and reduces the host->can_queue if the
1910 * cmd has not been completed within the timeout period.
1913 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1915 struct megasas_instance *instance;
1916 unsigned long flags;
1918 if (time_after(jiffies, scmd->jiffies_at_alloc +
1919 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1920 return BLK_EH_NOT_HANDLED;
1923 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1924 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1925 /* FW is busy, throttle IO */
1926 spin_lock_irqsave(instance->host->host_lock, flags);
1928 instance->host->can_queue = instance->throttlequeuedepth;
1929 instance->last_time = jiffies;
1930 instance->flag |= MEGASAS_FW_BUSY;
1932 spin_unlock_irqrestore(instance->host->host_lock, flags);
1934 return BLK_EH_RESET_TIMER;
1938 * megasas_reset_device - Device reset handler entry point
1940 static int megasas_reset_device(struct scsi_cmnd *scmd)
1945 * First wait for all commands to complete
1947 ret = megasas_generic_reset(scmd);
1953 * megasas_reset_bus_host - Bus & host reset handler entry point
1955 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1958 struct megasas_instance *instance;
1959 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1962 * First wait for all commands to complete
1964 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1965 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
1966 ret = megasas_reset_fusion(scmd->device->host);
1968 ret = megasas_generic_reset(scmd);
1974 * megasas_bios_param - Returns disk geometry for a disk
1975 * @sdev: device handle
1976 * @bdev: block device
1977 * @capacity: drive capacity
1978 * @geom: geometry parameters
1981 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1982 sector_t capacity, int geom[])
1988 /* Default heads (64) & sectors (32) */
1992 tmp = heads * sectors;
1993 cylinders = capacity;
1995 sector_div(cylinders, tmp);
1998 * Handle extended translation size for logical drives > 1Gb
2001 if (capacity >= 0x200000) {
2004 tmp = heads*sectors;
2005 cylinders = capacity;
2006 sector_div(cylinders, tmp);
2011 geom[2] = cylinders;
2016 static void megasas_aen_polling(struct work_struct *work);
2019 * megasas_service_aen - Processes an event notification
2020 * @instance: Adapter soft state
2021 * @cmd: AEN command completed by the ISR
2023 * For AEN, driver sends a command down to FW that is held by the FW till an
2024 * event occurs. When an event of interest occurs, FW completes the command
2025 * that it was previously holding.
2027 * This routines sends SIGIO signal to processes that have registered with the
2031 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2033 unsigned long flags;
2035 * Don't signal app if it is just an aborted previously registered aen
2037 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2038 spin_lock_irqsave(&poll_aen_lock, flags);
2039 megasas_poll_wait_aen = 1;
2040 spin_unlock_irqrestore(&poll_aen_lock, flags);
2041 wake_up(&megasas_poll_wait);
2042 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2047 instance->aen_cmd = NULL;
2048 megasas_return_cmd(instance, cmd);
2050 if ((instance->unload == 0) &&
2051 ((instance->issuepend_done == 1))) {
2052 struct megasas_aen_event *ev;
2053 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2055 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2057 ev->instance = instance;
2059 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2060 schedule_delayed_work(
2061 (struct delayed_work *)&ev->hotplug_work, 0);
2066 static int megasas_change_queue_depth(struct scsi_device *sdev,
2067 int queue_depth, int reason)
2069 if (reason != SCSI_QDEPTH_DEFAULT)
2072 if (queue_depth > sdev->host->can_queue)
2073 queue_depth = sdev->host->can_queue;
2074 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2081 * Scsi host template for megaraid_sas driver
2083 static struct scsi_host_template megasas_template = {
2085 .module = THIS_MODULE,
2086 .name = "LSI SAS based MegaRAID driver",
2087 .proc_name = "megaraid_sas",
2088 .slave_configure = megasas_slave_configure,
2089 .slave_alloc = megasas_slave_alloc,
2090 .queuecommand = megasas_queue_command,
2091 .eh_device_reset_handler = megasas_reset_device,
2092 .eh_bus_reset_handler = megasas_reset_bus_host,
2093 .eh_host_reset_handler = megasas_reset_bus_host,
2094 .eh_timed_out = megasas_reset_timer,
2095 .bios_param = megasas_bios_param,
2096 .use_clustering = ENABLE_CLUSTERING,
2097 .change_queue_depth = megasas_change_queue_depth,
2101 * megasas_complete_int_cmd - Completes an internal command
2102 * @instance: Adapter soft state
2103 * @cmd: Command to be completed
2105 * The megasas_issue_blocked_cmd() function waits for a command to complete
2106 * after it issues a command. This function wakes up that waiting routine by
2107 * calling wake_up() on the wait queue.
2110 megasas_complete_int_cmd(struct megasas_instance *instance,
2111 struct megasas_cmd *cmd)
2113 cmd->cmd_status = cmd->frame->io.cmd_status;
2115 if (cmd->cmd_status == ENODATA) {
2116 cmd->cmd_status = 0;
2118 wake_up(&instance->int_cmd_wait_q);
2122 * megasas_complete_abort - Completes aborting a command
2123 * @instance: Adapter soft state
2124 * @cmd: Cmd that was issued to abort another cmd
2126 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2127 * after it issues an abort on a previously issued command. This function
2128 * wakes up all functions waiting on the same wait queue.
2131 megasas_complete_abort(struct megasas_instance *instance,
2132 struct megasas_cmd *cmd)
2134 if (cmd->sync_cmd) {
2136 cmd->cmd_status = 0;
2137 wake_up(&instance->abort_cmd_wait_q);
2144 * megasas_complete_cmd - Completes a command
2145 * @instance: Adapter soft state
2146 * @cmd: Command to be completed
2147 * @alt_status: If non-zero, use this value as status to
2148 * SCSI mid-layer instead of the value returned
2149 * by the FW. This should be used if caller wants
2150 * an alternate status (as in the case of aborted
2154 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2158 struct megasas_header *hdr = &cmd->frame->hdr;
2159 unsigned long flags;
2160 struct fusion_context *fusion = instance->ctrl_context;
2162 /* flag for the retry reset */
2163 cmd->retry_for_fw_reset = 0;
2166 cmd->scmd->SCp.ptr = NULL;
2169 case MFI_CMD_INVALID:
2170 /* Some older 1068 controller FW may keep a pended
2171 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2172 when booting the kdump kernel. Ignore this command to
2173 prevent a kernel panic on shutdown of the kdump kernel. */
2174 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2176 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2177 "other than PERC5, please upgrade your firmware.\n");
2179 case MFI_CMD_PD_SCSI_IO:
2180 case MFI_CMD_LD_SCSI_IO:
2183 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2184 * issued either through an IO path or an IOCTL path. If it
2185 * was via IOCTL, we will send it to internal completion.
2187 if (cmd->sync_cmd) {
2189 megasas_complete_int_cmd(instance, cmd);
2193 case MFI_CMD_LD_READ:
2194 case MFI_CMD_LD_WRITE:
2197 cmd->scmd->result = alt_status << 16;
2203 atomic_dec(&instance->fw_outstanding);
2205 scsi_dma_unmap(cmd->scmd);
2206 cmd->scmd->scsi_done(cmd->scmd);
2207 megasas_return_cmd(instance, cmd);
2212 switch (hdr->cmd_status) {
2215 cmd->scmd->result = DID_OK << 16;
2218 case MFI_STAT_SCSI_IO_FAILED:
2219 case MFI_STAT_LD_INIT_IN_PROGRESS:
2221 (DID_ERROR << 16) | hdr->scsi_status;
2224 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2226 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2228 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2229 memset(cmd->scmd->sense_buffer, 0,
2230 SCSI_SENSE_BUFFERSIZE);
2231 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2234 cmd->scmd->result |= DRIVER_SENSE << 24;
2239 case MFI_STAT_LD_OFFLINE:
2240 case MFI_STAT_DEVICE_NOT_FOUND:
2241 cmd->scmd->result = DID_BAD_TARGET << 16;
2245 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2247 cmd->scmd->result = DID_ERROR << 16;
2251 atomic_dec(&instance->fw_outstanding);
2253 scsi_dma_unmap(cmd->scmd);
2254 cmd->scmd->scsi_done(cmd->scmd);
2255 megasas_return_cmd(instance, cmd);
2262 /* Check for LD map update */
2263 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2264 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2265 spin_lock_irqsave(instance->host->host_lock, flags);
2266 if (cmd->frame->hdr.cmd_status != 0) {
2267 if (cmd->frame->hdr.cmd_status !=
2269 printk(KERN_WARNING "megasas: map sync"
2270 "failed, status = 0x%x.\n",
2271 cmd->frame->hdr.cmd_status);
2273 megasas_return_cmd(instance, cmd);
2274 spin_unlock_irqrestore(
2275 instance->host->host_lock,
2281 megasas_return_cmd(instance, cmd);
2282 if (MR_ValidateMapInfo(
2283 fusion->ld_map[(instance->map_id & 1)],
2284 fusion->load_balance_info))
2285 fusion->fast_path_io = 1;
2287 fusion->fast_path_io = 0;
2288 megasas_sync_map_info(instance);
2289 spin_unlock_irqrestore(instance->host->host_lock,
2293 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2294 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2295 spin_lock_irqsave(&poll_aen_lock, flags);
2296 megasas_poll_wait_aen = 0;
2297 spin_unlock_irqrestore(&poll_aen_lock, flags);
2301 * See if got an event notification
2303 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2304 megasas_service_aen(instance, cmd);
2306 megasas_complete_int_cmd(instance, cmd);
2312 * Cmd issued to abort another cmd returned
2314 megasas_complete_abort(instance, cmd);
2318 printk("megasas: Unknown command completed! [0x%X]\n",
2325 * megasas_issue_pending_cmds_again - issue all pending cmds
2326 * in FW again because of the fw reset
2327 * @instance: Adapter soft state
2330 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2332 struct megasas_cmd *cmd;
2333 struct list_head clist_local;
2334 union megasas_evt_class_locale class_locale;
2335 unsigned long flags;
2338 INIT_LIST_HEAD(&clist_local);
2339 spin_lock_irqsave(&instance->hba_lock, flags);
2340 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2341 spin_unlock_irqrestore(&instance->hba_lock, flags);
2343 while (!list_empty(&clist_local)) {
2344 cmd = list_entry((&clist_local)->next,
2345 struct megasas_cmd, list);
2346 list_del_init(&cmd->list);
2348 if (cmd->sync_cmd || cmd->scmd) {
2349 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2350 "detected to be pending while HBA reset.\n",
2351 cmd, cmd->scmd, cmd->sync_cmd);
2353 cmd->retry_for_fw_reset++;
2355 if (cmd->retry_for_fw_reset == 3) {
2356 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2357 "was tried multiple times during reset."
2358 "Shutting down the HBA\n",
2359 cmd, cmd->scmd, cmd->sync_cmd);
2360 megaraid_sas_kill_hba(instance);
2362 instance->adprecovery =
2363 MEGASAS_HW_CRITICAL_ERROR;
2368 if (cmd->sync_cmd == 1) {
2370 printk(KERN_NOTICE "megaraid_sas: unexpected"
2371 "cmd attached to internal command!\n");
2373 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2374 "on the internal reset queue,"
2375 "issue it again.\n", cmd);
2376 cmd->cmd_status = ENODATA;
2377 instance->instancet->fire_cmd(instance,
2378 cmd->frame_phys_addr ,
2379 0, instance->reg_set);
2380 } else if (cmd->scmd) {
2381 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
2382 "detected on the internal queue, issue again.\n",
2383 cmd, cmd->scmd->cmnd[0]);
2385 atomic_inc(&instance->fw_outstanding);
2386 instance->instancet->fire_cmd(instance,
2387 cmd->frame_phys_addr,
2388 cmd->frame_count-1, instance->reg_set);
2390 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2391 "internal reset defer list while re-issue!!\n",
2396 if (instance->aen_cmd) {
2397 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2398 megasas_return_cmd(instance, instance->aen_cmd);
2400 instance->aen_cmd = NULL;
2404 * Initiate AEN (Asynchronous Event Notification)
2406 seq_num = instance->last_seq_num;
2407 class_locale.members.reserved = 0;
2408 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2409 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2411 megasas_register_aen(instance, seq_num, class_locale.word);
2415 * Move the internal reset pending commands to a deferred queue.
2417 * We move the commands pending at internal reset time to a
2418 * pending queue. This queue would be flushed after successful
2419 * completion of the internal reset sequence. if the internal reset
2420 * did not complete in time, the kernel reset handler would flush
2424 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2426 struct megasas_cmd *cmd;
2428 u32 max_cmd = instance->max_fw_cmds;
2430 unsigned long flags;
2433 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2434 for (i = 0; i < max_cmd; i++) {
2435 cmd = instance->cmd_list[i];
2436 if (cmd->sync_cmd == 1 || cmd->scmd) {
2437 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2438 "on the defer queue as internal\n",
2439 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2441 if (!list_empty(&cmd->list)) {
2442 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2443 " moving this cmd:%p, %d %p, it was"
2444 "discovered on some list?\n",
2445 cmd, cmd->sync_cmd, cmd->scmd);
2447 list_del_init(&cmd->list);
2450 list_add_tail(&cmd->list,
2451 &instance->internal_reset_pending_q);
2454 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2459 process_fw_state_change_wq(struct work_struct *work)
2461 struct megasas_instance *instance =
2462 container_of(work, struct megasas_instance, work_init);
2464 unsigned long flags;
2466 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2467 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2468 instance->adprecovery);
2472 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2473 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2474 "state, restarting it...\n");
2476 instance->instancet->disable_intr(instance->reg_set);
2477 atomic_set(&instance->fw_outstanding, 0);
2479 atomic_set(&instance->fw_reset_no_pci_access, 1);
2480 instance->instancet->adp_reset(instance, instance->reg_set);
2481 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2483 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2484 "initiating next stage...\n");
2486 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2487 "state 2 starting...\n");
2489 /*waitting for about 20 second before start the second init*/
2490 for (wait = 0; wait < 30; wait++) {
2494 if (megasas_transition_to_ready(instance, 1)) {
2495 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2497 megaraid_sas_kill_hba(instance);
2498 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2502 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2503 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2504 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2506 *instance->consumer = *instance->producer;
2508 *instance->consumer = 0;
2509 *instance->producer = 0;
2512 megasas_issue_init_mfi(instance);
2514 spin_lock_irqsave(&instance->hba_lock, flags);
2515 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2516 spin_unlock_irqrestore(&instance->hba_lock, flags);
2517 instance->instancet->enable_intr(instance->reg_set);
2519 megasas_issue_pending_cmds_again(instance);
2520 instance->issuepend_done = 1;
2526 * megasas_deplete_reply_queue - Processes all completed commands
2527 * @instance: Adapter soft state
2528 * @alt_status: Alternate status to be returned to
2529 * SCSI mid-layer instead of the status
2530 * returned by the FW
2531 * Note: this must be called with hba lock held
2534 megasas_deplete_reply_queue(struct megasas_instance *instance,
2540 if ((mfiStatus = instance->instancet->check_reset(instance,
2541 instance->reg_set)) == 1) {
2545 if ((mfiStatus = instance->instancet->clear_intr(
2548 /* Hardware may not set outbound_intr_status in MSI-X mode */
2549 if (!instance->msix_vectors)
2553 instance->mfiStatus = mfiStatus;
2555 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2556 fw_state = instance->instancet->read_fw_status_reg(
2557 instance->reg_set) & MFI_STATE_MASK;
2559 if (fw_state != MFI_STATE_FAULT) {
2560 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2564 if ((fw_state == MFI_STATE_FAULT) &&
2565 (instance->disableOnlineCtrlReset == 0)) {
2566 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2568 if ((instance->pdev->device ==
2569 PCI_DEVICE_ID_LSI_SAS1064R) ||
2570 (instance->pdev->device ==
2571 PCI_DEVICE_ID_DELL_PERC5) ||
2572 (instance->pdev->device ==
2573 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2575 *instance->consumer =
2576 MEGASAS_ADPRESET_INPROG_SIGN;
2580 instance->instancet->disable_intr(instance->reg_set);
2581 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2582 instance->issuepend_done = 0;
2584 atomic_set(&instance->fw_outstanding, 0);
2585 megasas_internal_reset_defer_cmds(instance);
2587 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2588 fw_state, instance->adprecovery);
2590 schedule_work(&instance->work_init);
2594 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2595 fw_state, instance->disableOnlineCtrlReset);
2599 tasklet_schedule(&instance->isr_tasklet);
2603 * megasas_isr - isr entry point
2605 static irqreturn_t megasas_isr(int irq, void *devp)
2607 struct megasas_irq_context *irq_context = devp;
2608 struct megasas_instance *instance = irq_context->instance;
2609 unsigned long flags;
2612 if (atomic_read(&instance->fw_reset_no_pci_access))
2615 spin_lock_irqsave(&instance->hba_lock, flags);
2616 rc = megasas_deplete_reply_queue(instance, DID_OK);
2617 spin_unlock_irqrestore(&instance->hba_lock, flags);
2623 * megasas_transition_to_ready - Move the FW to READY state
2624 * @instance: Adapter soft state
2626 * During the initialization, FW passes can potentially be in any one of
2627 * several possible states. If the FW in operational, waiting-for-handshake
2628 * states, driver must take steps to bring it to ready state. Otherwise, it
2629 * has to wait for the ready state.
2632 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
2638 u32 abs_state, curr_abs_state;
2640 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2642 if (fw_state != MFI_STATE_READY)
2643 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2646 while (fw_state != MFI_STATE_READY) {
2649 instance->instancet->read_fw_status_reg(instance->reg_set);
2653 case MFI_STATE_FAULT:
2654 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2656 max_wait = MEGASAS_RESET_WAIT_TIME;
2657 cur_state = MFI_STATE_FAULT;
2662 case MFI_STATE_WAIT_HANDSHAKE:
2664 * Set the CLR bit in inbound doorbell
2666 if ((instance->pdev->device ==
2667 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2668 (instance->pdev->device ==
2669 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2670 (instance->pdev->device ==
2671 PCI_DEVICE_ID_LSI_FUSION) ||
2672 (instance->pdev->device ==
2673 PCI_DEVICE_ID_LSI_INVADER)) {
2675 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2676 &instance->reg_set->doorbell);
2679 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2680 &instance->reg_set->inbound_doorbell);
2683 max_wait = MEGASAS_RESET_WAIT_TIME;
2684 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2687 case MFI_STATE_BOOT_MESSAGE_PENDING:
2688 if ((instance->pdev->device ==
2689 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2690 (instance->pdev->device ==
2691 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2692 (instance->pdev->device ==
2693 PCI_DEVICE_ID_LSI_FUSION) ||
2694 (instance->pdev->device ==
2695 PCI_DEVICE_ID_LSI_INVADER)) {
2696 writel(MFI_INIT_HOTPLUG,
2697 &instance->reg_set->doorbell);
2699 writel(MFI_INIT_HOTPLUG,
2700 &instance->reg_set->inbound_doorbell);
2702 max_wait = MEGASAS_RESET_WAIT_TIME;
2703 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2706 case MFI_STATE_OPERATIONAL:
2708 * Bring it to READY state; assuming max wait 10 secs
2710 instance->instancet->disable_intr(instance->reg_set);
2711 if ((instance->pdev->device ==
2712 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2713 (instance->pdev->device ==
2714 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2715 (instance->pdev->device
2716 == PCI_DEVICE_ID_LSI_FUSION) ||
2717 (instance->pdev->device
2718 == PCI_DEVICE_ID_LSI_INVADER)) {
2719 writel(MFI_RESET_FLAGS,
2720 &instance->reg_set->doorbell);
2721 if ((instance->pdev->device ==
2722 PCI_DEVICE_ID_LSI_FUSION) ||
2723 (instance->pdev->device ==
2724 PCI_DEVICE_ID_LSI_INVADER)) {
2725 for (i = 0; i < (10 * 1000); i += 20) {
2736 writel(MFI_RESET_FLAGS,
2737 &instance->reg_set->inbound_doorbell);
2739 max_wait = MEGASAS_RESET_WAIT_TIME;
2740 cur_state = MFI_STATE_OPERATIONAL;
2743 case MFI_STATE_UNDEFINED:
2745 * This state should not last for more than 2 seconds
2747 max_wait = MEGASAS_RESET_WAIT_TIME;
2748 cur_state = MFI_STATE_UNDEFINED;
2751 case MFI_STATE_BB_INIT:
2752 max_wait = MEGASAS_RESET_WAIT_TIME;
2753 cur_state = MFI_STATE_BB_INIT;
2756 case MFI_STATE_FW_INIT:
2757 max_wait = MEGASAS_RESET_WAIT_TIME;
2758 cur_state = MFI_STATE_FW_INIT;
2761 case MFI_STATE_FW_INIT_2:
2762 max_wait = MEGASAS_RESET_WAIT_TIME;
2763 cur_state = MFI_STATE_FW_INIT_2;
2766 case MFI_STATE_DEVICE_SCAN:
2767 max_wait = MEGASAS_RESET_WAIT_TIME;
2768 cur_state = MFI_STATE_DEVICE_SCAN;
2771 case MFI_STATE_FLUSH_CACHE:
2772 max_wait = MEGASAS_RESET_WAIT_TIME;
2773 cur_state = MFI_STATE_FLUSH_CACHE;
2777 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2783 * The cur_state should not last for more than max_wait secs
2785 for (i = 0; i < (max_wait * 1000); i++) {
2786 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2789 instance->instancet->read_fw_status_reg(instance->reg_set);
2791 if (abs_state == curr_abs_state) {
2798 * Return error if fw_state hasn't changed after max_wait
2800 if (curr_abs_state == abs_state) {
2801 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2802 "in %d secs\n", fw_state, max_wait);
2806 printk(KERN_INFO "megasas: FW now in Ready state\n");
2812 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2813 * @instance: Adapter soft state
2815 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2818 u32 max_cmd = instance->max_mfi_cmds;
2819 struct megasas_cmd *cmd;
2821 if (!instance->frame_dma_pool)
2825 * Return all frames to pool
2827 for (i = 0; i < max_cmd; i++) {
2829 cmd = instance->cmd_list[i];
2832 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2833 cmd->frame_phys_addr);
2836 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2837 cmd->sense_phys_addr);
2841 * Now destroy the pool itself
2843 pci_pool_destroy(instance->frame_dma_pool);
2844 pci_pool_destroy(instance->sense_dma_pool);
2846 instance->frame_dma_pool = NULL;
2847 instance->sense_dma_pool = NULL;
2851 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2852 * @instance: Adapter soft state
2854 * Each command packet has an embedded DMA memory buffer that is used for
2855 * filling MFI frame and the SG list that immediately follows the frame. This
2856 * function creates those DMA memory buffers for each command packet by using
2857 * PCI pool facility.
2859 static int megasas_create_frame_pool(struct megasas_instance *instance)
2867 struct megasas_cmd *cmd;
2869 max_cmd = instance->max_mfi_cmds;
2872 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2873 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2875 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2876 sizeof(struct megasas_sge32);
2878 if (instance->flag_ieee) {
2879 sge_sz = sizeof(struct megasas_sge_skinny);
2883 * Calculated the number of 64byte frames required for SGL
2885 sgl_sz = sge_sz * instance->max_num_sge;
2886 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2890 * We need one extra frame for the MFI command
2894 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2896 * Use DMA pool facility provided by PCI layer
2898 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2899 instance->pdev, total_sz, 64,
2902 if (!instance->frame_dma_pool) {
2903 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2907 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2908 instance->pdev, 128, 4, 0);
2910 if (!instance->sense_dma_pool) {
2911 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2913 pci_pool_destroy(instance->frame_dma_pool);
2914 instance->frame_dma_pool = NULL;
2920 * Allocate and attach a frame to each of the commands in cmd_list.
2921 * By making cmd->index as the context instead of the &cmd, we can
2922 * always use 32bit context regardless of the architecture
2924 for (i = 0; i < max_cmd; i++) {
2926 cmd = instance->cmd_list[i];
2928 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2929 GFP_KERNEL, &cmd->frame_phys_addr);
2931 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2932 GFP_KERNEL, &cmd->sense_phys_addr);
2935 * megasas_teardown_frame_pool() takes care of freeing
2936 * whatever has been allocated
2938 if (!cmd->frame || !cmd->sense) {
2939 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2940 megasas_teardown_frame_pool(instance);
2944 memset(cmd->frame, 0, total_sz);
2945 cmd->frame->io.context = cmd->index;
2946 cmd->frame->io.pad_0 = 0;
2947 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2948 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2950 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
2957 * megasas_free_cmds - Free all the cmds in the free cmd pool
2958 * @instance: Adapter soft state
2960 void megasas_free_cmds(struct megasas_instance *instance)
2963 /* First free the MFI frame pool */
2964 megasas_teardown_frame_pool(instance);
2966 /* Free all the commands in the cmd_list */
2967 for (i = 0; i < instance->max_mfi_cmds; i++)
2969 kfree(instance->cmd_list[i]);
2971 /* Free the cmd_list buffer itself */
2972 kfree(instance->cmd_list);
2973 instance->cmd_list = NULL;
2975 INIT_LIST_HEAD(&instance->cmd_pool);
2979 * megasas_alloc_cmds - Allocates the command packets
2980 * @instance: Adapter soft state
2982 * Each command that is issued to the FW, whether IO commands from the OS or
2983 * internal commands like IOCTLs, are wrapped in local data structure called
2984 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2987 * Each frame has a 32-bit field called context (tag). This context is used
2988 * to get back the megasas_cmd from the frame when a frame gets completed in
2989 * the ISR. Typically the address of the megasas_cmd itself would be used as
2990 * the context. But we wanted to keep the differences between 32 and 64 bit
2991 * systems to the mininum. We always use 32 bit integers for the context. In
2992 * this driver, the 32 bit values are the indices into an array cmd_list.
2993 * This array is used only to look up the megasas_cmd given the context. The
2994 * free commands themselves are maintained in a linked list called cmd_pool.
2996 int megasas_alloc_cmds(struct megasas_instance *instance)
3001 struct megasas_cmd *cmd;
3003 max_cmd = instance->max_mfi_cmds;
3006 * instance->cmd_list is an array of struct megasas_cmd pointers.
3007 * Allocate the dynamic array first and then allocate individual
3010 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3012 if (!instance->cmd_list) {
3013 printk(KERN_DEBUG "megasas: out of memory\n");
3017 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3019 for (i = 0; i < max_cmd; i++) {
3020 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3023 if (!instance->cmd_list[i]) {
3025 for (j = 0; j < i; j++)
3026 kfree(instance->cmd_list[j]);
3028 kfree(instance->cmd_list);
3029 instance->cmd_list = NULL;
3036 * Add all the commands to command pool (instance->cmd_pool)
3038 for (i = 0; i < max_cmd; i++) {
3039 cmd = instance->cmd_list[i];
3040 memset(cmd, 0, sizeof(struct megasas_cmd));
3043 cmd->instance = instance;
3045 list_add_tail(&cmd->list, &instance->cmd_pool);
3049 * Create a frame pool and assign one frame to each cmd
3051 if (megasas_create_frame_pool(instance)) {
3052 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3053 megasas_free_cmds(instance);
3060 * megasas_get_pd_list_info - Returns FW's pd_list structure
3061 * @instance: Adapter soft state
3062 * @pd_list: pd_list structure
3064 * Issues an internal command (DCMD) to get the FW's controller PD
3065 * list structure. This information is mainly used to find out SYSTEM
3066 * supported by the FW.
3069 megasas_get_pd_list(struct megasas_instance *instance)
3071 int ret = 0, pd_index = 0;
3072 struct megasas_cmd *cmd;
3073 struct megasas_dcmd_frame *dcmd;
3074 struct MR_PD_LIST *ci;
3075 struct MR_PD_ADDRESS *pd_addr;
3076 dma_addr_t ci_h = 0;
3078 cmd = megasas_get_cmd(instance);
3081 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3085 dcmd = &cmd->frame->dcmd;
3087 ci = pci_alloc_consistent(instance->pdev,
3088 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3091 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3092 megasas_return_cmd(instance, cmd);
3096 memset(ci, 0, sizeof(*ci));
3097 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3099 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3100 dcmd->mbox.b[1] = 0;
3101 dcmd->cmd = MFI_CMD_DCMD;
3102 dcmd->cmd_status = 0xFF;
3103 dcmd->sge_count = 1;
3104 dcmd->flags = MFI_FRAME_DIR_READ;
3107 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3108 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3109 dcmd->sgl.sge32[0].phys_addr = ci_h;
3110 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3112 if (!megasas_issue_polled(instance, cmd)) {
3119 * the following function will get the instance PD LIST.
3126 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3128 memset(instance->pd_list, 0,
3129 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3131 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3133 instance->pd_list[pd_addr->deviceId].tid =
3135 instance->pd_list[pd_addr->deviceId].driveType =
3136 pd_addr->scsiDevType;
3137 instance->pd_list[pd_addr->deviceId].driveState =
3143 pci_free_consistent(instance->pdev,
3144 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3146 megasas_return_cmd(instance, cmd);
3152 * megasas_get_ld_list_info - Returns FW's ld_list structure
3153 * @instance: Adapter soft state
3154 * @ld_list: ld_list structure
3156 * Issues an internal command (DCMD) to get the FW's controller PD
3157 * list structure. This information is mainly used to find out SYSTEM
3158 * supported by the FW.
3161 megasas_get_ld_list(struct megasas_instance *instance)
3163 int ret = 0, ld_index = 0, ids = 0;
3164 struct megasas_cmd *cmd;
3165 struct megasas_dcmd_frame *dcmd;
3166 struct MR_LD_LIST *ci;
3167 dma_addr_t ci_h = 0;
3169 cmd = megasas_get_cmd(instance);
3172 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3176 dcmd = &cmd->frame->dcmd;
3178 ci = pci_alloc_consistent(instance->pdev,
3179 sizeof(struct MR_LD_LIST),
3183 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3184 megasas_return_cmd(instance, cmd);
3188 memset(ci, 0, sizeof(*ci));
3189 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3191 dcmd->cmd = MFI_CMD_DCMD;
3192 dcmd->cmd_status = 0xFF;
3193 dcmd->sge_count = 1;
3194 dcmd->flags = MFI_FRAME_DIR_READ;
3196 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3197 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3198 dcmd->sgl.sge32[0].phys_addr = ci_h;
3199 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3202 if (!megasas_issue_polled(instance, cmd)) {
3208 /* the following function will get the instance PD LIST */
3210 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3211 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3213 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3214 if (ci->ldList[ld_index].state != 0) {
3215 ids = ci->ldList[ld_index].ref.targetId;
3216 instance->ld_ids[ids] =
3217 ci->ldList[ld_index].ref.targetId;
3222 pci_free_consistent(instance->pdev,
3223 sizeof(struct MR_LD_LIST),
3227 megasas_return_cmd(instance, cmd);
3232 * megasas_get_controller_info - Returns FW's controller structure
3233 * @instance: Adapter soft state
3234 * @ctrl_info: Controller information structure
3236 * Issues an internal command (DCMD) to get the FW's controller structure.
3237 * This information is mainly used to find out the maximum IO transfer per
3238 * command supported by the FW.
3241 megasas_get_ctrl_info(struct megasas_instance *instance,
3242 struct megasas_ctrl_info *ctrl_info)
3245 struct megasas_cmd *cmd;
3246 struct megasas_dcmd_frame *dcmd;
3247 struct megasas_ctrl_info *ci;
3248 dma_addr_t ci_h = 0;
3250 cmd = megasas_get_cmd(instance);
3253 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3257 dcmd = &cmd->frame->dcmd;
3259 ci = pci_alloc_consistent(instance->pdev,
3260 sizeof(struct megasas_ctrl_info), &ci_h);
3263 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3264 megasas_return_cmd(instance, cmd);
3268 memset(ci, 0, sizeof(*ci));
3269 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3271 dcmd->cmd = MFI_CMD_DCMD;
3272 dcmd->cmd_status = 0xFF;
3273 dcmd->sge_count = 1;
3274 dcmd->flags = MFI_FRAME_DIR_READ;
3277 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3278 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3279 dcmd->sgl.sge32[0].phys_addr = ci_h;
3280 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3282 if (!megasas_issue_polled(instance, cmd)) {
3284 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3289 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3292 megasas_return_cmd(instance, cmd);
3297 * megasas_issue_init_mfi - Initializes the FW
3298 * @instance: Adapter soft state
3300 * Issues the INIT MFI cmd
3303 megasas_issue_init_mfi(struct megasas_instance *instance)
3307 struct megasas_cmd *cmd;
3309 struct megasas_init_frame *init_frame;
3310 struct megasas_init_queue_info *initq_info;
3311 dma_addr_t init_frame_h;
3312 dma_addr_t initq_info_h;
3315 * Prepare a init frame. Note the init frame points to queue info
3316 * structure. Each frame has SGL allocated after first 64 bytes. For
3317 * this frame - since we don't need any SGL - we use SGL's space as
3318 * queue info structure
3320 * We will not get a NULL command below. We just created the pool.
3322 cmd = megasas_get_cmd(instance);
3324 init_frame = (struct megasas_init_frame *)cmd->frame;
3325 initq_info = (struct megasas_init_queue_info *)
3326 ((unsigned long)init_frame + 64);
3328 init_frame_h = cmd->frame_phys_addr;
3329 initq_info_h = init_frame_h + 64;
3331 context = init_frame->context;
3332 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3333 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3334 init_frame->context = context;
3336 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3337 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3339 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3340 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3342 init_frame->cmd = MFI_CMD_INIT;
3343 init_frame->cmd_status = 0xFF;
3344 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3346 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3349 * disable the intr before firing the init frame to FW
3351 instance->instancet->disable_intr(instance->reg_set);
3354 * Issue the init frame in polled mode
3357 if (megasas_issue_polled(instance, cmd)) {
3358 printk(KERN_ERR "megasas: Failed to init firmware\n");
3359 megasas_return_cmd(instance, cmd);
3363 megasas_return_cmd(instance, cmd);
3372 megasas_init_adapter_mfi(struct megasas_instance *instance)
3374 struct megasas_register_set __iomem *reg_set;
3378 reg_set = instance->reg_set;
3381 * Get various operational parameters from status register
3383 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3385 * Reduce the max supported cmds by 1. This is to ensure that the
3386 * reply_q_sz (1 more than the max cmd that driver may send)
3387 * does not exceed max cmds that the FW can support
3389 instance->max_fw_cmds = instance->max_fw_cmds-1;
3390 instance->max_mfi_cmds = instance->max_fw_cmds;
3391 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3394 * Create a pool of commands
3396 if (megasas_alloc_cmds(instance))
3397 goto fail_alloc_cmds;
3400 * Allocate memory for reply queue. Length of reply queue should
3401 * be _one_ more than the maximum commands handled by the firmware.
3403 * Note: When FW completes commands, it places corresponding contex
3404 * values in this circular reply queue. This circular queue is a fairly
3405 * typical producer-consumer queue. FW is the producer (of completed
3406 * commands) and the driver is the consumer.
3408 context_sz = sizeof(u32);
3409 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3411 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3413 &instance->reply_queue_h);
3415 if (!instance->reply_queue) {
3416 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3417 goto fail_reply_queue;
3420 if (megasas_issue_init_mfi(instance))
3423 instance->fw_support_ieee = 0;
3424 instance->fw_support_ieee =
3425 (instance->instancet->read_fw_status_reg(reg_set) &
3428 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3429 instance->fw_support_ieee);
3431 if (instance->fw_support_ieee)
3432 instance->flag_ieee = 1;
3438 pci_free_consistent(instance->pdev, reply_q_sz,
3439 instance->reply_queue, instance->reply_queue_h);
3441 megasas_free_cmds(instance);
3448 * megasas_init_fw - Initializes the FW
3449 * @instance: Adapter soft state
3451 * This is the main function for initializing firmware
3454 static int megasas_init_fw(struct megasas_instance *instance)
3458 u32 tmp_sectors, msix_enable;
3459 struct megasas_register_set __iomem *reg_set;
3460 struct megasas_ctrl_info *ctrl_info;
3461 unsigned long bar_list;
3464 /* Find first memory bar */
3465 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3466 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3467 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3468 if (pci_request_selected_regions(instance->pdev, instance->bar,
3470 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3474 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3476 if (!instance->reg_set) {
3477 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3481 reg_set = instance->reg_set;
3483 switch (instance->pdev->device) {
3484 case PCI_DEVICE_ID_LSI_FUSION:
3485 case PCI_DEVICE_ID_LSI_INVADER:
3486 instance->instancet = &megasas_instance_template_fusion;
3488 case PCI_DEVICE_ID_LSI_SAS1078R:
3489 case PCI_DEVICE_ID_LSI_SAS1078DE:
3490 instance->instancet = &megasas_instance_template_ppc;
3492 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3493 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3494 instance->instancet = &megasas_instance_template_gen2;
3496 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3497 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3498 instance->instancet = &megasas_instance_template_skinny;
3500 case PCI_DEVICE_ID_LSI_SAS1064R:
3501 case PCI_DEVICE_ID_DELL_PERC5:
3503 instance->instancet = &megasas_instance_template_xscale;
3508 * We expect the FW state to be READY
3510 if (megasas_transition_to_ready(instance, 0))
3511 goto fail_ready_state;
3513 /* Check if MSI-X is supported while in ready state */
3514 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3516 if (msix_enable && !msix_disable) {
3517 /* Check max MSI-X vectors */
3518 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3519 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3520 instance->msix_vectors = (readl(&instance->reg_set->
3521 outbound_scratch_pad_2
3524 instance->msix_vectors = 1;
3525 /* Don't bother allocating more MSI-X vectors than cpus */
3526 instance->msix_vectors = min(instance->msix_vectors,
3527 (unsigned int)num_online_cpus());
3528 for (i = 0; i < instance->msix_vectors; i++)
3529 instance->msixentry[i].entry = i;
3530 i = pci_enable_msix(instance->pdev, instance->msixentry,
3531 instance->msix_vectors);
3534 if (!pci_enable_msix(instance->pdev,
3535 instance->msixentry, i))
3536 instance->msix_vectors = i;
3538 instance->msix_vectors = 0;
3541 instance->msix_vectors = 0;
3544 /* Get operational params, sge flags, send init cmd to controller */
3545 if (instance->instancet->init_adapter(instance))
3546 goto fail_init_adapter;
3548 printk(KERN_ERR "megasas: INIT adapter done\n");
3551 * the following function will get the PD LIST.
3554 memset(instance->pd_list, 0 ,
3555 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3556 megasas_get_pd_list(instance);
3558 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3559 megasas_get_ld_list(instance);
3561 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3564 * Compute the max allowed sectors per IO: The controller info has two
3565 * limits on max sectors. Driver should use the minimum of these two.
3567 * 1 << stripe_sz_ops.min = max sectors per strip
3569 * Note that older firmwares ( < FW ver 30) didn't report information
3570 * to calculate max_sectors_1. So the number ended up as zero always.
3573 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3575 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3576 ctrl_info->max_strips_per_io;
3577 max_sectors_2 = ctrl_info->max_request_size;
3579 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3580 instance->disableOnlineCtrlReset =
3581 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3584 instance->max_sectors_per_req = instance->max_num_sge *
3586 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3587 instance->max_sectors_per_req = tmp_sectors;
3591 /* Check for valid throttlequeuedepth module parameter */
3592 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3593 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3594 if (throttlequeuedepth > (instance->max_fw_cmds -
3595 MEGASAS_SKINNY_INT_CMDS))
3596 instance->throttlequeuedepth =
3597 MEGASAS_THROTTLE_QUEUE_DEPTH;
3599 instance->throttlequeuedepth = throttlequeuedepth;
3601 if (throttlequeuedepth > (instance->max_fw_cmds -
3603 instance->throttlequeuedepth =
3604 MEGASAS_THROTTLE_QUEUE_DEPTH;
3606 instance->throttlequeuedepth = throttlequeuedepth;
3610 * Setup tasklet for cmd completion
3613 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3614 (unsigned long)instance);
3620 iounmap(instance->reg_set);
3623 pci_release_selected_regions(instance->pdev, instance->bar);
3629 * megasas_release_mfi - Reverses the FW initialization
3630 * @intance: Adapter soft state
3632 static void megasas_release_mfi(struct megasas_instance *instance)
3634 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3636 if (instance->reply_queue)
3637 pci_free_consistent(instance->pdev, reply_q_sz,
3638 instance->reply_queue, instance->reply_queue_h);
3640 megasas_free_cmds(instance);
3642 iounmap(instance->reg_set);
3644 pci_release_selected_regions(instance->pdev, instance->bar);
3648 * megasas_get_seq_num - Gets latest event sequence numbers
3649 * @instance: Adapter soft state
3650 * @eli: FW event log sequence numbers information
3652 * FW maintains a log of all events in a non-volatile area. Upper layers would
3653 * usually find out the latest sequence number of the events, the seq number at
3654 * the boot etc. They would "read" all the events below the latest seq number
3655 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3656 * number), they would subsribe to AEN (asynchronous event notification) and
3657 * wait for the events to happen.
3660 megasas_get_seq_num(struct megasas_instance *instance,
3661 struct megasas_evt_log_info *eli)
3663 struct megasas_cmd *cmd;
3664 struct megasas_dcmd_frame *dcmd;
3665 struct megasas_evt_log_info *el_info;
3666 dma_addr_t el_info_h = 0;
3668 cmd = megasas_get_cmd(instance);
3674 dcmd = &cmd->frame->dcmd;
3675 el_info = pci_alloc_consistent(instance->pdev,
3676 sizeof(struct megasas_evt_log_info),
3680 megasas_return_cmd(instance, cmd);
3684 memset(el_info, 0, sizeof(*el_info));
3685 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3687 dcmd->cmd = MFI_CMD_DCMD;
3688 dcmd->cmd_status = 0x0;
3689 dcmd->sge_count = 1;
3690 dcmd->flags = MFI_FRAME_DIR_READ;
3693 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3694 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3695 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3696 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3698 megasas_issue_blocked_cmd(instance, cmd);
3701 * Copy the data back into callers buffer
3703 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3705 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3706 el_info, el_info_h);
3708 megasas_return_cmd(instance, cmd);
3714 * megasas_register_aen - Registers for asynchronous event notification
3715 * @instance: Adapter soft state
3716 * @seq_num: The starting sequence number
3717 * @class_locale: Class of the event
3719 * This function subscribes for AEN for events beyond the @seq_num. It requests
3720 * to be notified if and only if the event is of type @class_locale
3723 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3724 u32 class_locale_word)
3727 struct megasas_cmd *cmd;
3728 struct megasas_dcmd_frame *dcmd;
3729 union megasas_evt_class_locale curr_aen;
3730 union megasas_evt_class_locale prev_aen;
3733 * If there an AEN pending already (aen_cmd), check if the
3734 * class_locale of that pending AEN is inclusive of the new
3735 * AEN request we currently have. If it is, then we don't have
3736 * to do anything. In other words, whichever events the current
3737 * AEN request is subscribing to, have already been subscribed
3740 * If the old_cmd is _not_ inclusive, then we have to abort
3741 * that command, form a class_locale that is superset of both
3742 * old and current and re-issue to the FW
3745 curr_aen.word = class_locale_word;
3747 if (instance->aen_cmd) {
3749 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3752 * A class whose enum value is smaller is inclusive of all
3753 * higher values. If a PROGRESS (= -1) was previously
3754 * registered, then a new registration requests for higher
3755 * classes need not be sent to FW. They are automatically
3758 * Locale numbers don't have such hierarchy. They are bitmap
3761 if ((prev_aen.members.class <= curr_aen.members.class) &&
3762 !((prev_aen.members.locale & curr_aen.members.locale) ^
3763 curr_aen.members.locale)) {
3765 * Previously issued event registration includes
3766 * current request. Nothing to do.
3770 curr_aen.members.locale |= prev_aen.members.locale;
3772 if (prev_aen.members.class < curr_aen.members.class)
3773 curr_aen.members.class = prev_aen.members.class;
3775 instance->aen_cmd->abort_aen = 1;
3776 ret_val = megasas_issue_blocked_abort_cmd(instance,
3781 printk(KERN_DEBUG "megasas: Failed to abort "
3782 "previous AEN command\n");
3788 cmd = megasas_get_cmd(instance);
3793 dcmd = &cmd->frame->dcmd;
3795 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3798 * Prepare DCMD for aen registration
3800 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3802 dcmd->cmd = MFI_CMD_DCMD;
3803 dcmd->cmd_status = 0x0;
3804 dcmd->sge_count = 1;
3805 dcmd->flags = MFI_FRAME_DIR_READ;
3808 instance->last_seq_num = seq_num;
3809 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3810 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3811 dcmd->mbox.w[0] = seq_num;
3812 dcmd->mbox.w[1] = curr_aen.word;
3813 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3814 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3816 if (instance->aen_cmd != NULL) {
3817 megasas_return_cmd(instance, cmd);
3822 * Store reference to the cmd used to register for AEN. When an
3823 * application wants us to register for AEN, we have to abort this
3824 * cmd and re-register with a new EVENT LOCALE supplied by that app
3826 instance->aen_cmd = cmd;
3829 * Issue the aen registration frame
3831 instance->instancet->issue_dcmd(instance, cmd);
3837 * megasas_start_aen - Subscribes to AEN during driver load time
3838 * @instance: Adapter soft state
3840 static int megasas_start_aen(struct megasas_instance *instance)
3842 struct megasas_evt_log_info eli;
3843 union megasas_evt_class_locale class_locale;
3846 * Get the latest sequence number from FW
3848 memset(&eli, 0, sizeof(eli));
3850 if (megasas_get_seq_num(instance, &eli))
3854 * Register AEN with FW for latest sequence number plus 1
3856 class_locale.members.reserved = 0;
3857 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3858 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3860 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3865 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3866 * @instance: Adapter soft state
3868 static int megasas_io_attach(struct megasas_instance *instance)
3870 struct Scsi_Host *host = instance->host;
3873 * Export parameters required by SCSI mid-layer
3875 host->irq = instance->pdev->irq;
3876 host->unique_id = instance->unique_id;
3877 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3878 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3880 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3883 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3884 host->this_id = instance->init_id;
3885 host->sg_tablesize = instance->max_num_sge;
3887 if (instance->fw_support_ieee)
3888 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3891 * Check if the module parameter value for max_sectors can be used
3893 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3894 instance->max_sectors_per_req = max_sectors;
3897 if (((instance->pdev->device ==
3898 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3899 (instance->pdev->device ==
3900 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3901 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3902 instance->max_sectors_per_req = max_sectors;
3904 printk(KERN_INFO "megasas: max_sectors should be > 0"
3905 "and <= %d (or < 1MB for GEN2 controller)\n",
3906 instance->max_sectors_per_req);
3911 host->max_sectors = instance->max_sectors_per_req;
3912 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3913 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3914 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3915 host->max_lun = MEGASAS_MAX_LUN;
3916 host->max_cmd_len = 16;
3918 /* Fusion only supports host reset */
3919 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3920 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3921 host->hostt->eh_device_reset_handler = NULL;
3922 host->hostt->eh_bus_reset_handler = NULL;
3926 * Notify the mid-layer about the new controller
3928 if (scsi_add_host(host, &instance->pdev->dev)) {
3929 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3934 * Trigger SCSI to scan our drives
3936 scsi_scan_host(host);
3941 megasas_set_dma_mask(struct pci_dev *pdev)
3944 * All our contollers are capable of performing 64-bit DMA
3947 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3949 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3950 goto fail_set_dma_mask;
3953 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3954 goto fail_set_dma_mask;
3963 * megasas_probe_one - PCI hotplug entry point
3964 * @pdev: PCI device structure
3965 * @id: PCI ids of supported hotplugged adapter
3967 static int __devinit
3968 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3970 int rval, pos, i, j;
3971 struct Scsi_Host *host;
3972 struct megasas_instance *instance;
3975 /* Reset MSI-X in the kdump kernel */
3976 if (reset_devices) {
3977 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3979 pci_read_config_word(pdev, msi_control_reg(pos),
3981 if (control & PCI_MSIX_FLAGS_ENABLE) {
3982 dev_info(&pdev->dev, "resetting MSI-X\n");
3983 pci_write_config_word(pdev,
3984 msi_control_reg(pos),
3986 ~PCI_MSIX_FLAGS_ENABLE);
3992 * Announce PCI information
3994 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3995 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3996 pdev->subsystem_device);
3998 printk("bus %d:slot %d:func %d\n",
3999 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4002 * PCI prepping: enable device set bus mastering and dma mask
4004 rval = pci_enable_device_mem(pdev);
4010 pci_set_master(pdev);
4012 if (megasas_set_dma_mask(pdev))
4013 goto fail_set_dma_mask;
4015 host = scsi_host_alloc(&megasas_template,
4016 sizeof(struct megasas_instance));
4019 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4020 goto fail_alloc_instance;
4023 instance = (struct megasas_instance *)host->hostdata;
4024 memset(instance, 0, sizeof(*instance));
4025 atomic_set( &instance->fw_reset_no_pci_access, 0 );
4026 instance->pdev = pdev;
4028 switch (instance->pdev->device) {
4029 case PCI_DEVICE_ID_LSI_FUSION:
4030 case PCI_DEVICE_ID_LSI_INVADER:
4032 struct fusion_context *fusion;
4034 instance->ctrl_context =
4035 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4036 if (!instance->ctrl_context) {
4037 printk(KERN_DEBUG "megasas: Failed to allocate "
4038 "memory for Fusion context info\n");
4039 goto fail_alloc_dma_buf;
4041 fusion = instance->ctrl_context;
4042 INIT_LIST_HEAD(&fusion->cmd_pool);
4043 spin_lock_init(&fusion->cmd_pool_lock);
4046 default: /* For all other supported controllers */
4048 instance->producer =
4049 pci_alloc_consistent(pdev, sizeof(u32),
4050 &instance->producer_h);
4051 instance->consumer =
4052 pci_alloc_consistent(pdev, sizeof(u32),
4053 &instance->consumer_h);
4055 if (!instance->producer || !instance->consumer) {
4056 printk(KERN_DEBUG "megasas: Failed to allocate"
4057 "memory for producer, consumer\n");
4058 goto fail_alloc_dma_buf;
4061 *instance->producer = 0;
4062 *instance->consumer = 0;
4066 megasas_poll_wait_aen = 0;
4067 instance->flag_ieee = 0;
4068 instance->ev = NULL;
4069 instance->issuepend_done = 1;
4070 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4071 megasas_poll_wait_aen = 0;
4073 instance->evt_detail = pci_alloc_consistent(pdev,
4075 megasas_evt_detail),
4076 &instance->evt_detail_h);
4078 if (!instance->evt_detail) {
4079 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4080 "event detail structure\n");
4081 goto fail_alloc_dma_buf;
4085 * Initialize locks and queues
4087 INIT_LIST_HEAD(&instance->cmd_pool);
4088 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4090 atomic_set(&instance->fw_outstanding,0);
4092 init_waitqueue_head(&instance->int_cmd_wait_q);
4093 init_waitqueue_head(&instance->abort_cmd_wait_q);
4095 spin_lock_init(&instance->cmd_pool_lock);
4096 spin_lock_init(&instance->hba_lock);
4097 spin_lock_init(&instance->completion_lock);
4099 mutex_init(&instance->aen_mutex);
4100 mutex_init(&instance->reset_mutex);
4103 * Initialize PCI related and misc parameters
4105 instance->host = host;
4106 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4107 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4109 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4110 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4111 instance->flag_ieee = 1;
4112 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4114 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4116 megasas_dbg_lvl = 0;
4118 instance->unload = 1;
4119 instance->last_time = 0;
4120 instance->disableOnlineCtrlReset = 1;
4122 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4123 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4124 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4126 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4129 * Initialize MFI Firmware
4131 if (megasas_init_fw(instance))
4137 if (instance->msix_vectors) {
4138 for (i = 0 ; i < instance->msix_vectors; i++) {
4139 instance->irq_context[i].instance = instance;
4140 instance->irq_context[i].MSIxIndex = i;
4141 if (request_irq(instance->msixentry[i].vector,
4142 instance->instancet->service_isr, 0,
4144 &instance->irq_context[i])) {
4145 printk(KERN_DEBUG "megasas: Failed to "
4146 "register IRQ for vector %d.\n", i);
4147 for (j = 0 ; j < i ; j++)
4149 instance->msixentry[j].vector,
4150 &instance->irq_context[j]);
4155 instance->irq_context[0].instance = instance;
4156 instance->irq_context[0].MSIxIndex = 0;
4157 if (request_irq(pdev->irq, instance->instancet->service_isr,
4158 IRQF_SHARED, "megasas",
4159 &instance->irq_context[0])) {
4160 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4165 instance->instancet->enable_intr(instance->reg_set);
4168 * Store instance in PCI softstate
4170 pci_set_drvdata(pdev, instance);
4173 * Add this controller to megasas_mgmt_info structure so that it
4174 * can be exported to management applications
4176 megasas_mgmt_info.count++;
4177 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4178 megasas_mgmt_info.max_index++;
4181 * Register with SCSI mid-layer
4183 if (megasas_io_attach(instance))
4184 goto fail_io_attach;
4186 instance->unload = 0;
4189 * Initiate AEN (Asynchronous Event Notification)
4191 if (megasas_start_aen(instance)) {
4192 printk(KERN_DEBUG "megasas: start aen failed\n");
4193 goto fail_start_aen;
4200 megasas_mgmt_info.count--;
4201 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4202 megasas_mgmt_info.max_index--;
4204 pci_set_drvdata(pdev, NULL);
4205 instance->instancet->disable_intr(instance->reg_set);
4206 if (instance->msix_vectors)
4207 for (i = 0 ; i < instance->msix_vectors; i++)
4208 free_irq(instance->msixentry[i].vector,
4209 &instance->irq_context[i]);
4211 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4213 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4214 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4215 megasas_release_fusion(instance);
4217 megasas_release_mfi(instance);
4219 if (instance->msix_vectors)
4220 pci_disable_msix(instance->pdev);
4222 if (instance->evt_detail)
4223 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4224 instance->evt_detail,
4225 instance->evt_detail_h);
4227 if (instance->producer)
4228 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4229 instance->producer_h);
4230 if (instance->consumer)
4231 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4232 instance->consumer_h);
4233 scsi_host_put(host);
4235 fail_alloc_instance:
4237 pci_disable_device(pdev);
4243 * megasas_flush_cache - Requests FW to flush all its caches
4244 * @instance: Adapter soft state
4246 static void megasas_flush_cache(struct megasas_instance *instance)
4248 struct megasas_cmd *cmd;
4249 struct megasas_dcmd_frame *dcmd;
4251 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4254 cmd = megasas_get_cmd(instance);
4259 dcmd = &cmd->frame->dcmd;
4261 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4263 dcmd->cmd = MFI_CMD_DCMD;
4264 dcmd->cmd_status = 0x0;
4265 dcmd->sge_count = 0;
4266 dcmd->flags = MFI_FRAME_DIR_NONE;
4269 dcmd->data_xfer_len = 0;
4270 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4271 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4273 megasas_issue_blocked_cmd(instance, cmd);
4275 megasas_return_cmd(instance, cmd);
4281 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4282 * @instance: Adapter soft state
4283 * @opcode: Shutdown/Hibernate
4285 static void megasas_shutdown_controller(struct megasas_instance *instance,
4288 struct megasas_cmd *cmd;
4289 struct megasas_dcmd_frame *dcmd;
4291 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4294 cmd = megasas_get_cmd(instance);
4299 if (instance->aen_cmd)
4300 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4301 if (instance->map_update_cmd)
4302 megasas_issue_blocked_abort_cmd(instance,
4303 instance->map_update_cmd);
4304 dcmd = &cmd->frame->dcmd;
4306 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4308 dcmd->cmd = MFI_CMD_DCMD;
4309 dcmd->cmd_status = 0x0;
4310 dcmd->sge_count = 0;
4311 dcmd->flags = MFI_FRAME_DIR_NONE;
4314 dcmd->data_xfer_len = 0;
4315 dcmd->opcode = opcode;
4317 megasas_issue_blocked_cmd(instance, cmd);
4319 megasas_return_cmd(instance, cmd);
4326 * megasas_suspend - driver suspend entry point
4327 * @pdev: PCI device structure
4328 * @state: PCI power state to suspend routine
4331 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4333 struct Scsi_Host *host;
4334 struct megasas_instance *instance;
4337 instance = pci_get_drvdata(pdev);
4338 host = instance->host;
4339 instance->unload = 1;
4341 megasas_flush_cache(instance);
4342 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4344 /* cancel the delayed work if this work still in queue */
4345 if (instance->ev != NULL) {
4346 struct megasas_aen_event *ev = instance->ev;
4347 cancel_delayed_work_sync(
4348 (struct delayed_work *)&ev->hotplug_work);
4349 instance->ev = NULL;
4352 tasklet_kill(&instance->isr_tasklet);
4354 pci_set_drvdata(instance->pdev, instance);
4355 instance->instancet->disable_intr(instance->reg_set);
4357 if (instance->msix_vectors)
4358 for (i = 0 ; i < instance->msix_vectors; i++)
4359 free_irq(instance->msixentry[i].vector,
4360 &instance->irq_context[i]);
4362 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4363 if (instance->msix_vectors)
4364 pci_disable_msix(instance->pdev);
4366 pci_save_state(pdev);
4367 pci_disable_device(pdev);
4369 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4375 * megasas_resume- driver resume entry point
4376 * @pdev: PCI device structure
4379 megasas_resume(struct pci_dev *pdev)
4382 struct Scsi_Host *host;
4383 struct megasas_instance *instance;
4385 instance = pci_get_drvdata(pdev);
4386 host = instance->host;
4387 pci_set_power_state(pdev, PCI_D0);
4388 pci_enable_wake(pdev, PCI_D0, 0);
4389 pci_restore_state(pdev);
4392 * PCI prepping: enable device set bus mastering and dma mask
4394 rval = pci_enable_device_mem(pdev);
4397 printk(KERN_ERR "megasas: Enable device failed\n");
4401 pci_set_master(pdev);
4403 if (megasas_set_dma_mask(pdev))
4404 goto fail_set_dma_mask;
4407 * Initialize MFI Firmware
4410 atomic_set(&instance->fw_outstanding, 0);
4413 * We expect the FW state to be READY
4415 if (megasas_transition_to_ready(instance, 0))
4416 goto fail_ready_state;
4418 /* Now re-enable MSI-X */
4419 if (instance->msix_vectors)
4420 pci_enable_msix(instance->pdev, instance->msixentry,
4421 instance->msix_vectors);
4423 switch (instance->pdev->device) {
4424 case PCI_DEVICE_ID_LSI_FUSION:
4425 case PCI_DEVICE_ID_LSI_INVADER:
4427 megasas_reset_reply_desc(instance);
4428 if (megasas_ioc_init_fusion(instance)) {
4429 megasas_free_cmds(instance);
4430 megasas_free_cmds_fusion(instance);
4433 if (!megasas_get_map_info(instance))
4434 megasas_sync_map_info(instance);
4438 *instance->producer = 0;
4439 *instance->consumer = 0;
4440 if (megasas_issue_init_mfi(instance))
4445 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4446 (unsigned long)instance);
4451 if (instance->msix_vectors) {
4452 for (i = 0 ; i < instance->msix_vectors; i++) {
4453 instance->irq_context[i].instance = instance;
4454 instance->irq_context[i].MSIxIndex = i;
4455 if (request_irq(instance->msixentry[i].vector,
4456 instance->instancet->service_isr, 0,
4458 &instance->irq_context[i])) {
4459 printk(KERN_DEBUG "megasas: Failed to "
4460 "register IRQ for vector %d.\n", i);
4461 for (j = 0 ; j < i ; j++)
4463 instance->msixentry[j].vector,
4464 &instance->irq_context[j]);
4469 instance->irq_context[0].instance = instance;
4470 instance->irq_context[0].MSIxIndex = 0;
4471 if (request_irq(pdev->irq, instance->instancet->service_isr,
4472 IRQF_SHARED, "megasas",
4473 &instance->irq_context[0])) {
4474 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4479 instance->instancet->enable_intr(instance->reg_set);
4480 instance->unload = 0;
4483 * Initiate AEN (Asynchronous Event Notification)
4485 if (megasas_start_aen(instance))
4486 printk(KERN_ERR "megasas: Start AEN failed\n");
4492 if (instance->evt_detail)
4493 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4494 instance->evt_detail,
4495 instance->evt_detail_h);
4497 if (instance->producer)
4498 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4499 instance->producer_h);
4500 if (instance->consumer)
4501 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4502 instance->consumer_h);
4503 scsi_host_put(host);
4508 pci_disable_device(pdev);
4513 #define megasas_suspend NULL
4514 #define megasas_resume NULL
4518 * megasas_detach_one - PCI hot"un"plug entry point
4519 * @pdev: PCI device structure
4521 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4524 struct Scsi_Host *host;
4525 struct megasas_instance *instance;
4526 struct fusion_context *fusion;
4528 instance = pci_get_drvdata(pdev);
4529 instance->unload = 1;
4530 host = instance->host;
4531 fusion = instance->ctrl_context;
4533 scsi_remove_host(instance->host);
4534 megasas_flush_cache(instance);
4535 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4537 /* cancel the delayed work if this work still in queue*/
4538 if (instance->ev != NULL) {
4539 struct megasas_aen_event *ev = instance->ev;
4540 cancel_delayed_work_sync(
4541 (struct delayed_work *)&ev->hotplug_work);
4542 instance->ev = NULL;
4545 tasklet_kill(&instance->isr_tasklet);
4548 * Take the instance off the instance array. Note that we will not
4549 * decrement the max_index. We let this array be sparse array
4551 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4552 if (megasas_mgmt_info.instance[i] == instance) {
4553 megasas_mgmt_info.count--;
4554 megasas_mgmt_info.instance[i] = NULL;
4560 pci_set_drvdata(instance->pdev, NULL);
4562 instance->instancet->disable_intr(instance->reg_set);
4564 if (instance->msix_vectors)
4565 for (i = 0 ; i < instance->msix_vectors; i++)
4566 free_irq(instance->msixentry[i].vector,
4567 &instance->irq_context[i]);
4569 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4570 if (instance->msix_vectors)
4571 pci_disable_msix(instance->pdev);
4573 switch (instance->pdev->device) {
4574 case PCI_DEVICE_ID_LSI_FUSION:
4575 case PCI_DEVICE_ID_LSI_INVADER:
4576 megasas_release_fusion(instance);
4577 for (i = 0; i < 2 ; i++)
4578 if (fusion->ld_map[i])
4579 dma_free_coherent(&instance->pdev->dev,
4584 kfree(instance->ctrl_context);
4587 megasas_release_mfi(instance);
4588 pci_free_consistent(pdev,
4589 sizeof(struct megasas_evt_detail),
4590 instance->evt_detail,
4591 instance->evt_detail_h);
4592 pci_free_consistent(pdev, sizeof(u32),
4594 instance->producer_h);
4595 pci_free_consistent(pdev, sizeof(u32),
4597 instance->consumer_h);
4601 scsi_host_put(host);
4603 pci_set_drvdata(pdev, NULL);
4605 pci_disable_device(pdev);
4611 * megasas_shutdown - Shutdown entry point
4612 * @device: Generic device structure
4614 static void megasas_shutdown(struct pci_dev *pdev)
4617 struct megasas_instance *instance = pci_get_drvdata(pdev);
4619 instance->unload = 1;
4620 megasas_flush_cache(instance);
4621 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4622 instance->instancet->disable_intr(instance->reg_set);
4623 if (instance->msix_vectors)
4624 for (i = 0 ; i < instance->msix_vectors; i++)
4625 free_irq(instance->msixentry[i].vector,
4626 &instance->irq_context[i]);
4628 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4629 if (instance->msix_vectors)
4630 pci_disable_msix(instance->pdev);
4634 * megasas_mgmt_open - char node "open" entry point
4636 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4639 * Allow only those users with admin rights
4641 if (!capable(CAP_SYS_ADMIN))
4648 * megasas_mgmt_fasync - Async notifier registration from applications
4650 * This function adds the calling process to a driver global queue. When an
4651 * event occurs, SIGIO will be sent to all processes in this queue.
4653 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4657 mutex_lock(&megasas_async_queue_mutex);
4659 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4661 mutex_unlock(&megasas_async_queue_mutex);
4664 /* For sanity check when we get ioctl */
4665 filep->private_data = filep;
4669 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4675 * megasas_mgmt_poll - char node "poll" entry point
4677 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4680 unsigned long flags;
4681 poll_wait(file, &megasas_poll_wait, wait);
4682 spin_lock_irqsave(&poll_aen_lock, flags);
4683 if (megasas_poll_wait_aen)
4684 mask = (POLLIN | POLLRDNORM);
4687 spin_unlock_irqrestore(&poll_aen_lock, flags);
4692 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4693 * @instance: Adapter soft state
4694 * @argp: User's ioctl packet
4697 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4698 struct megasas_iocpacket __user * user_ioc,
4699 struct megasas_iocpacket *ioc)
4701 struct megasas_sge32 *kern_sge32;
4702 struct megasas_cmd *cmd;
4703 void *kbuff_arr[MAX_IOCTL_SGE];
4704 dma_addr_t buf_handle = 0;
4707 dma_addr_t sense_handle;
4708 unsigned long *sense_ptr;
4710 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4712 if (ioc->sge_count > MAX_IOCTL_SGE) {
4713 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4714 ioc->sge_count, MAX_IOCTL_SGE);
4718 cmd = megasas_get_cmd(instance);
4720 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4725 * User's IOCTL packet has 2 frames (maximum). Copy those two
4726 * frames into our cmd's frames. cmd->frame's context will get
4727 * overwritten when we copy from user's frames. So set that value
4730 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4731 cmd->frame->hdr.context = cmd->index;
4732 cmd->frame->hdr.pad_0 = 0;
4733 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4737 * The management interface between applications and the fw uses
4738 * MFI frames. E.g, RAID configuration changes, LD property changes
4739 * etc are accomplishes through different kinds of MFI frames. The
4740 * driver needs to care only about substituting user buffers with
4741 * kernel buffers in SGLs. The location of SGL is embedded in the
4742 * struct iocpacket itself.
4744 kern_sge32 = (struct megasas_sge32 *)
4745 ((unsigned long)cmd->frame + ioc->sgl_off);
4748 * For each user buffer, create a mirror buffer and copy in
4750 for (i = 0; i < ioc->sge_count; i++) {
4751 if (!ioc->sgl[i].iov_len)
4754 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4755 ioc->sgl[i].iov_len,
4756 &buf_handle, GFP_KERNEL);
4757 if (!kbuff_arr[i]) {
4758 printk(KERN_DEBUG "megasas: Failed to alloc "
4759 "kernel SGL buffer for IOCTL \n");
4765 * We don't change the dma_coherent_mask, so
4766 * pci_alloc_consistent only returns 32bit addresses
4768 kern_sge32[i].phys_addr = (u32) buf_handle;
4769 kern_sge32[i].length = ioc->sgl[i].iov_len;
4772 * We created a kernel buffer corresponding to the
4773 * user buffer. Now copy in from the user buffer
4775 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4776 (u32) (ioc->sgl[i].iov_len))) {
4782 if (ioc->sense_len) {
4783 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4784 &sense_handle, GFP_KERNEL);
4791 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4792 *sense_ptr = sense_handle;
4796 * Set the sync_cmd flag so that the ISR knows not to complete this
4797 * cmd to the SCSI mid-layer
4800 megasas_issue_blocked_cmd(instance, cmd);
4804 * copy out the kernel buffers to user buffers
4806 for (i = 0; i < ioc->sge_count; i++) {
4807 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4808 ioc->sgl[i].iov_len)) {
4815 * copy out the sense
4817 if (ioc->sense_len) {
4819 * sense_ptr points to the location that has the user
4820 * sense buffer address
4822 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4825 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4826 sense, ioc->sense_len)) {
4827 printk(KERN_ERR "megasas: Failed to copy out to user "
4835 * copy the status codes returned by the fw
4837 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4838 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4839 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4845 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4846 sense, sense_handle);
4849 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4850 dma_free_coherent(&instance->pdev->dev,
4851 kern_sge32[i].length,
4852 kbuff_arr[i], kern_sge32[i].phys_addr);
4855 megasas_return_cmd(instance, cmd);
4859 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4861 struct megasas_iocpacket __user *user_ioc =
4862 (struct megasas_iocpacket __user *)arg;
4863 struct megasas_iocpacket *ioc;
4864 struct megasas_instance *instance;
4867 unsigned long flags;
4868 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4870 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4874 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4879 instance = megasas_lookup_instance(ioc->host_no);
4885 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4886 printk(KERN_ERR "Controller in crit error\n");
4891 if (instance->unload == 1) {
4897 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4899 if (down_interruptible(&instance->ioctl_sem)) {
4900 error = -ERESTARTSYS;
4904 for (i = 0; i < wait_time; i++) {
4906 spin_lock_irqsave(&instance->hba_lock, flags);
4907 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4908 spin_unlock_irqrestore(&instance->hba_lock, flags);
4911 spin_unlock_irqrestore(&instance->hba_lock, flags);
4913 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4914 printk(KERN_NOTICE "megasas: waiting"
4915 "for controller reset to finish\n");
4921 spin_lock_irqsave(&instance->hba_lock, flags);
4922 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4923 spin_unlock_irqrestore(&instance->hba_lock, flags);
4925 printk(KERN_ERR "megaraid_sas: timed out while"
4926 "waiting for HBA to recover\n");
4930 spin_unlock_irqrestore(&instance->hba_lock, flags);
4932 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4933 up(&instance->ioctl_sem);
4940 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4942 struct megasas_instance *instance;
4943 struct megasas_aen aen;
4946 unsigned long flags;
4947 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4949 if (file->private_data != file) {
4950 printk(KERN_DEBUG "megasas: fasync_helper was not "
4955 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4958 instance = megasas_lookup_instance(aen.host_no);
4963 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4967 if (instance->unload == 1) {
4971 for (i = 0; i < wait_time; i++) {
4973 spin_lock_irqsave(&instance->hba_lock, flags);
4974 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4975 spin_unlock_irqrestore(&instance->hba_lock,
4980 spin_unlock_irqrestore(&instance->hba_lock, flags);
4982 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4983 printk(KERN_NOTICE "megasas: waiting for"
4984 "controller reset to finish\n");
4990 spin_lock_irqsave(&instance->hba_lock, flags);
4991 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4992 spin_unlock_irqrestore(&instance->hba_lock, flags);
4993 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4994 "for HBA to recover.\n");
4997 spin_unlock_irqrestore(&instance->hba_lock, flags);
4999 mutex_lock(&instance->aen_mutex);
5000 error = megasas_register_aen(instance, aen.seq_num,
5001 aen.class_locale_word);
5002 mutex_unlock(&instance->aen_mutex);
5007 * megasas_mgmt_ioctl - char node ioctl entry point
5010 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5013 case MEGASAS_IOC_FIRMWARE:
5014 return megasas_mgmt_ioctl_fw(file, arg);
5016 case MEGASAS_IOC_GET_AEN:
5017 return megasas_mgmt_ioctl_aen(file, arg);
5023 #ifdef CONFIG_COMPAT
5024 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5026 struct compat_megasas_iocpacket __user *cioc =
5027 (struct compat_megasas_iocpacket __user *)arg;
5028 struct megasas_iocpacket __user *ioc =
5029 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
5034 if (clear_user(ioc, sizeof(*ioc)))
5037 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5038 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5039 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5040 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5041 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5042 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5046 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5047 * sense_len is not null, so prepare the 64bit value under
5048 * the same condition.
5050 if (ioc->sense_len) {
5051 void __user **sense_ioc_ptr =
5052 (void __user **)(ioc->frame.raw + ioc->sense_off);
5053 compat_uptr_t *sense_cioc_ptr =
5054 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5055 if (get_user(ptr, sense_cioc_ptr) ||
5056 put_user(compat_ptr(ptr), sense_ioc_ptr))
5060 for (i = 0; i < MAX_IOCTL_SGE; i++) {
5061 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5062 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5063 copy_in_user(&ioc->sgl[i].iov_len,
5064 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5068 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5070 if (copy_in_user(&cioc->frame.hdr.cmd_status,
5071 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5072 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5079 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5083 case MEGASAS_IOC_FIRMWARE32:
5084 return megasas_mgmt_compat_ioctl_fw(file, arg);
5085 case MEGASAS_IOC_GET_AEN:
5086 return megasas_mgmt_ioctl_aen(file, arg);
5094 * File operations structure for management interface
5096 static const struct file_operations megasas_mgmt_fops = {
5097 .owner = THIS_MODULE,
5098 .open = megasas_mgmt_open,
5099 .fasync = megasas_mgmt_fasync,
5100 .unlocked_ioctl = megasas_mgmt_ioctl,
5101 .poll = megasas_mgmt_poll,
5102 #ifdef CONFIG_COMPAT
5103 .compat_ioctl = megasas_mgmt_compat_ioctl,
5105 .llseek = noop_llseek,
5109 * PCI hotplug support registration structure
5111 static struct pci_driver megasas_pci_driver = {
5113 .name = "megaraid_sas",
5114 .id_table = megasas_pci_table,
5115 .probe = megasas_probe_one,
5116 .remove = __devexit_p(megasas_detach_one),
5117 .suspend = megasas_suspend,
5118 .resume = megasas_resume,
5119 .shutdown = megasas_shutdown,
5123 * Sysfs driver attributes
5125 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5127 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5131 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5134 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5136 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5140 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5144 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5146 return sprintf(buf, "%u\n", support_poll_for_event);
5149 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5150 megasas_sysfs_show_support_poll_for_event, NULL);
5153 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5155 return sprintf(buf, "%u\n", support_device_change);
5158 static DRIVER_ATTR(support_device_change, S_IRUGO,
5159 megasas_sysfs_show_support_device_change, NULL);
5162 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5164 return sprintf(buf, "%u\n", megasas_dbg_lvl);
5168 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5171 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5172 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5178 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
5179 megasas_sysfs_set_dbg_lvl);
5182 megasas_aen_polling(struct work_struct *work)
5184 struct megasas_aen_event *ev =
5185 container_of(work, struct megasas_aen_event, hotplug_work);
5186 struct megasas_instance *instance = ev->instance;
5187 union megasas_evt_class_locale class_locale;
5188 struct Scsi_Host *host;
5189 struct scsi_device *sdev1;
5192 int i, j, doscan = 0;
5197 printk(KERN_ERR "invalid instance!\n");
5201 instance->ev = NULL;
5202 host = instance->host;
5203 if (instance->evt_detail) {
5205 switch (instance->evt_detail->code) {
5206 case MR_EVT_PD_INSERTED:
5207 if (megasas_get_pd_list(instance) == 0) {
5208 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5210 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5214 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5217 scsi_device_lookup(host, i, j, 0);
5219 if (instance->pd_list[pd_index].driveState
5220 == MR_PD_STATE_SYSTEM) {
5222 scsi_add_device(host, i, j, 0);
5226 scsi_device_put(sdev1);
5234 case MR_EVT_PD_REMOVED:
5235 if (megasas_get_pd_list(instance) == 0) {
5236 megasas_get_pd_list(instance);
5237 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5239 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5243 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5246 scsi_device_lookup(host, i, j, 0);
5248 if (instance->pd_list[pd_index].driveState
5249 == MR_PD_STATE_SYSTEM) {
5251 scsi_device_put(sdev1);
5255 scsi_remove_device(sdev1);
5256 scsi_device_put(sdev1);
5265 case MR_EVT_LD_OFFLINE:
5266 case MR_EVT_CFG_CLEARED:
5267 case MR_EVT_LD_DELETED:
5268 megasas_get_ld_list(instance);
5269 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5271 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5275 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5277 sdev1 = scsi_device_lookup(host,
5278 i + MEGASAS_MAX_LD_CHANNELS,
5282 if (instance->ld_ids[ld_index] != 0xff) {
5284 scsi_device_put(sdev1);
5288 scsi_remove_device(sdev1);
5289 scsi_device_put(sdev1);
5296 case MR_EVT_LD_CREATED:
5297 megasas_get_ld_list(instance);
5298 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5300 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5303 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5305 sdev1 = scsi_device_lookup(host,
5306 i+MEGASAS_MAX_LD_CHANNELS,
5309 if (instance->ld_ids[ld_index] !=
5312 scsi_add_device(host,
5318 scsi_device_put(sdev1);
5324 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
5325 case MR_EVT_FOREIGN_CFG_IMPORTED:
5326 case MR_EVT_LD_STATE_CHANGE:
5334 printk(KERN_ERR "invalid evt_detail!\n");
5340 printk(KERN_INFO "scanning ...\n");
5341 megasas_get_pd_list(instance);
5342 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5343 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5344 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5345 sdev1 = scsi_device_lookup(host, i, j, 0);
5346 if (instance->pd_list[pd_index].driveState ==
5347 MR_PD_STATE_SYSTEM) {
5349 scsi_add_device(host, i, j, 0);
5352 scsi_device_put(sdev1);
5355 scsi_remove_device(sdev1);
5356 scsi_device_put(sdev1);
5362 megasas_get_ld_list(instance);
5363 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5364 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5366 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5368 sdev1 = scsi_device_lookup(host,
5369 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5370 if (instance->ld_ids[ld_index] != 0xff) {
5372 scsi_add_device(host,
5376 scsi_device_put(sdev1);
5380 scsi_remove_device(sdev1);
5381 scsi_device_put(sdev1);
5388 if ( instance->aen_cmd != NULL ) {
5393 seq_num = instance->evt_detail->seq_num + 1;
5395 /* Register AEN with FW for latest sequence number plus 1 */
5396 class_locale.members.reserved = 0;
5397 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5398 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5399 mutex_lock(&instance->aen_mutex);
5400 error = megasas_register_aen(instance, seq_num,
5402 mutex_unlock(&instance->aen_mutex);
5405 printk(KERN_ERR "register aen failed error %x\n", error);
5411 * megasas_init - Driver load entry point
5413 static int __init megasas_init(void)
5418 * Announce driver version and other information
5420 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5421 MEGASAS_EXT_VERSION);
5423 spin_lock_init(&poll_aen_lock);
5425 support_poll_for_event = 2;
5426 support_device_change = 1;
5428 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5431 * Register character device node
5433 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5436 printk(KERN_DEBUG "megasas: failed to open device node\n");
5440 megasas_mgmt_majorno = rval;
5443 * Register ourselves as PCI hotplug module
5445 rval = pci_register_driver(&megasas_pci_driver);
5448 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5452 rval = driver_create_file(&megasas_pci_driver.driver,
5453 &driver_attr_version);
5455 goto err_dcf_attr_ver;
5456 rval = driver_create_file(&megasas_pci_driver.driver,
5457 &driver_attr_release_date);
5459 goto err_dcf_rel_date;
5461 rval = driver_create_file(&megasas_pci_driver.driver,
5462 &driver_attr_support_poll_for_event);
5464 goto err_dcf_support_poll_for_event;
5466 rval = driver_create_file(&megasas_pci_driver.driver,
5467 &driver_attr_dbg_lvl);
5469 goto err_dcf_dbg_lvl;
5470 rval = driver_create_file(&megasas_pci_driver.driver,
5471 &driver_attr_support_device_change);
5473 goto err_dcf_support_device_change;
5477 err_dcf_support_device_change:
5478 driver_remove_file(&megasas_pci_driver.driver,
5479 &driver_attr_dbg_lvl);
5481 driver_remove_file(&megasas_pci_driver.driver,
5482 &driver_attr_support_poll_for_event);
5484 err_dcf_support_poll_for_event:
5485 driver_remove_file(&megasas_pci_driver.driver,
5486 &driver_attr_release_date);
5489 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5491 pci_unregister_driver(&megasas_pci_driver);
5493 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5498 * megasas_exit - Driver unload entry point
5500 static void __exit megasas_exit(void)
5502 driver_remove_file(&megasas_pci_driver.driver,
5503 &driver_attr_dbg_lvl);
5504 driver_remove_file(&megasas_pci_driver.driver,
5505 &driver_attr_support_poll_for_event);
5506 driver_remove_file(&megasas_pci_driver.driver,
5507 &driver_attr_support_device_change);
5508 driver_remove_file(&megasas_pci_driver.driver,
5509 &driver_attr_release_date);
5510 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5512 pci_unregister_driver(&megasas_pci_driver);
5513 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5516 module_init(megasas_init);
5517 module_exit(megasas_exit);