[SCSI] megaraid_sas: Fix failure gotos
[linux-2.6/btrfs-unstable.git] / drivers / scsi / megaraid / megaraid_sas_base.c
blobc4ac4a5a075a0d9a44f50b6e589861062eda2dd6
1 /*
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.05.29-rc1
23 * Authors: LSI Corporation
24 * Sreenivas Bagalkote
25 * Sumant Patro
26 * Bo Yang
28 * Send feedback to: <megaraidlinux@lsi.com>
30 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
31 * ATTN: Linuxraid
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/slab.h>
45 #include <asm/uaccess.h>
46 #include <linux/fs.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include "megaraid_sas_fusion.h"
57 #include "megaraid_sas.h"
60 * poll_mode_io:1- schedule complete completion from q cmd
62 static unsigned int poll_mode_io;
63 module_param_named(poll_mode_io, poll_mode_io, int, 0);
64 MODULE_PARM_DESC(poll_mode_io,
65 "Complete cmds from IO path, (default=0)");
68 * Number of sectors per IO command
69 * Will be set in megasas_init_mfi if user does not provide
71 static unsigned int max_sectors;
72 module_param_named(max_sectors, max_sectors, int, 0);
73 MODULE_PARM_DESC(max_sectors,
74 "Maximum number of sectors per IO command");
76 static int msix_disable;
77 module_param(msix_disable, int, S_IRUGO);
78 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
80 MODULE_LICENSE("GPL");
81 MODULE_VERSION(MEGASAS_VERSION);
82 MODULE_AUTHOR("megaraidlinux@lsi.com");
83 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
85 int megasas_transition_to_ready(struct megasas_instance *instance);
86 static int megasas_get_pd_list(struct megasas_instance *instance);
87 static int megasas_issue_init_mfi(struct megasas_instance *instance);
88 static int megasas_register_aen(struct megasas_instance *instance,
89 u32 seq_num, u32 class_locale_word);
91 * PCI ID table for all supported controllers
93 static struct pci_device_id megasas_pci_table[] = {
95 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
96 /* xscale IOP */
97 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
98 /* ppc IOP */
99 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
100 /* ppc IOP */
101 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
102 /* gen2*/
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
104 /* gen2*/
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
106 /* skinny*/
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
108 /* skinny*/
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
110 /* xscale IOP, vega */
111 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
112 /* xscale IOP */
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
114 /* Fusion */
118 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
120 static int megasas_mgmt_majorno;
121 static struct megasas_mgmt_info megasas_mgmt_info;
122 static struct fasync_struct *megasas_async_queue;
123 static DEFINE_MUTEX(megasas_async_queue_mutex);
125 static int megasas_poll_wait_aen;
126 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
127 static u32 support_poll_for_event;
128 u32 megasas_dbg_lvl;
129 static u32 support_device_change;
131 /* define lock for aen poll */
132 spinlock_t poll_aen_lock;
134 void
135 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
136 u8 alt_status);
138 static irqreturn_t megasas_isr(int irq, void *devp);
139 static u32
140 megasas_init_adapter_mfi(struct megasas_instance *instance);
142 megasas_build_and_issue_cmd(struct megasas_instance *instance,
143 struct scsi_cmnd *scmd);
144 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
145 void
146 megasas_release_fusion(struct megasas_instance *instance);
148 megasas_ioc_init_fusion(struct megasas_instance *instance);
149 void
150 megasas_free_cmds_fusion(struct megasas_instance *instance);
152 megasas_get_map_info(struct megasas_instance *instance);
154 megasas_sync_map_info(struct megasas_instance *instance);
156 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
157 void megasas_reset_reply_desc(struct megasas_instance *instance);
158 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
159 struct LD_LOAD_BALANCE_INFO *lbInfo);
160 int megasas_reset_fusion(struct Scsi_Host *shost);
161 void megasas_fusion_ocr_wq(struct work_struct *work);
163 void
164 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
166 instance->instancet->fire_cmd(instance,
167 cmd->frame_phys_addr, 0, instance->reg_set);
171 * megasas_get_cmd - Get a command from the free pool
172 * @instance: Adapter soft state
174 * Returns a free command from the pool
176 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
177 *instance)
179 unsigned long flags;
180 struct megasas_cmd *cmd = NULL;
182 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
184 if (!list_empty(&instance->cmd_pool)) {
185 cmd = list_entry((&instance->cmd_pool)->next,
186 struct megasas_cmd, list);
187 list_del_init(&cmd->list);
188 } else {
189 printk(KERN_ERR "megasas: Command pool empty!\n");
192 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
193 return cmd;
197 * megasas_return_cmd - Return a cmd to free command pool
198 * @instance: Adapter soft state
199 * @cmd: Command packet to be returned to free command pool
201 inline void
202 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
204 unsigned long flags;
206 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
208 cmd->scmd = NULL;
209 cmd->frame_count = 0;
210 list_add_tail(&cmd->list, &instance->cmd_pool);
212 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
217 * The following functions are defined for xscale
218 * (deviceid : 1064R, PERC5) controllers
222 * megasas_enable_intr_xscale - Enables interrupts
223 * @regs: MFI register set
225 static inline void
226 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
228 writel(0, &(regs)->outbound_intr_mask);
230 /* Dummy readl to force pci flush */
231 readl(&regs->outbound_intr_mask);
235 * megasas_disable_intr_xscale -Disables interrupt
236 * @regs: MFI register set
238 static inline void
239 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
241 u32 mask = 0x1f;
242 writel(mask, &regs->outbound_intr_mask);
243 /* Dummy readl to force pci flush */
244 readl(&regs->outbound_intr_mask);
248 * megasas_read_fw_status_reg_xscale - returns the current FW status value
249 * @regs: MFI register set
251 static u32
252 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
254 return readl(&(regs)->outbound_msg_0);
257 * megasas_clear_interrupt_xscale - Check & clear interrupt
258 * @regs: MFI register set
260 static int
261 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
263 u32 status;
264 u32 mfiStatus = 0;
266 * Check if it is our interrupt
268 status = readl(&regs->outbound_intr_status);
270 if (status & MFI_OB_INTR_STATUS_MASK)
271 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
272 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
273 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
276 * Clear the interrupt by writing back the same value
278 if (mfiStatus)
279 writel(status, &regs->outbound_intr_status);
281 /* Dummy readl to force pci flush */
282 readl(&regs->outbound_intr_status);
284 return mfiStatus;
288 * megasas_fire_cmd_xscale - Sends command to the FW
289 * @frame_phys_addr : Physical address of cmd
290 * @frame_count : Number of frames for the command
291 * @regs : MFI register set
293 static inline void
294 megasas_fire_cmd_xscale(struct megasas_instance *instance,
295 dma_addr_t frame_phys_addr,
296 u32 frame_count,
297 struct megasas_register_set __iomem *regs)
299 unsigned long flags;
300 spin_lock_irqsave(&instance->hba_lock, flags);
301 writel((frame_phys_addr >> 3)|(frame_count),
302 &(regs)->inbound_queue_port);
303 spin_unlock_irqrestore(&instance->hba_lock, flags);
307 * megasas_adp_reset_xscale - For controller reset
308 * @regs: MFI register set
310 static int
311 megasas_adp_reset_xscale(struct megasas_instance *instance,
312 struct megasas_register_set __iomem *regs)
314 u32 i;
315 u32 pcidata;
316 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
318 for (i = 0; i < 3; i++)
319 msleep(1000); /* sleep for 3 secs */
320 pcidata = 0;
321 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
322 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
323 if (pcidata & 0x2) {
324 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
325 pcidata &= ~0x2;
326 pci_write_config_dword(instance->pdev,
327 MFI_1068_PCSR_OFFSET, pcidata);
329 for (i = 0; i < 2; i++)
330 msleep(1000); /* need to wait 2 secs again */
332 pcidata = 0;
333 pci_read_config_dword(instance->pdev,
334 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
335 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
336 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
337 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
338 pcidata = 0;
339 pci_write_config_dword(instance->pdev,
340 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
343 return 0;
347 * megasas_check_reset_xscale - For controller reset check
348 * @regs: MFI register set
350 static int
351 megasas_check_reset_xscale(struct megasas_instance *instance,
352 struct megasas_register_set __iomem *regs)
354 u32 consumer;
355 consumer = *instance->consumer;
357 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
358 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
359 return 1;
361 return 0;
364 static struct megasas_instance_template megasas_instance_template_xscale = {
366 .fire_cmd = megasas_fire_cmd_xscale,
367 .enable_intr = megasas_enable_intr_xscale,
368 .disable_intr = megasas_disable_intr_xscale,
369 .clear_intr = megasas_clear_intr_xscale,
370 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
371 .adp_reset = megasas_adp_reset_xscale,
372 .check_reset = megasas_check_reset_xscale,
373 .service_isr = megasas_isr,
374 .tasklet = megasas_complete_cmd_dpc,
375 .init_adapter = megasas_init_adapter_mfi,
376 .build_and_issue_cmd = megasas_build_and_issue_cmd,
377 .issue_dcmd = megasas_issue_dcmd,
381 * This is the end of set of functions & definitions specific
382 * to xscale (deviceid : 1064R, PERC5) controllers
386 * The following functions are defined for ppc (deviceid : 0x60)
387 * controllers
391 * megasas_enable_intr_ppc - Enables interrupts
392 * @regs: MFI register set
394 static inline void
395 megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
397 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
399 writel(~0x80000000, &(regs)->outbound_intr_mask);
401 /* Dummy readl to force pci flush */
402 readl(&regs->outbound_intr_mask);
406 * megasas_disable_intr_ppc - Disable interrupt
407 * @regs: MFI register set
409 static inline void
410 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
412 u32 mask = 0xFFFFFFFF;
413 writel(mask, &regs->outbound_intr_mask);
414 /* Dummy readl to force pci flush */
415 readl(&regs->outbound_intr_mask);
419 * megasas_read_fw_status_reg_ppc - returns the current FW status value
420 * @regs: MFI register set
422 static u32
423 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
425 return readl(&(regs)->outbound_scratch_pad);
429 * megasas_clear_interrupt_ppc - Check & clear interrupt
430 * @regs: MFI register set
432 static int
433 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
435 u32 status;
437 * Check if it is our interrupt
439 status = readl(&regs->outbound_intr_status);
441 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) {
442 return 0;
446 * Clear the interrupt by writing back the same value
448 writel(status, &regs->outbound_doorbell_clear);
450 /* Dummy readl to force pci flush */
451 readl(&regs->outbound_doorbell_clear);
453 return 1;
456 * megasas_fire_cmd_ppc - Sends command to the FW
457 * @frame_phys_addr : Physical address of cmd
458 * @frame_count : Number of frames for the command
459 * @regs : MFI register set
461 static inline void
462 megasas_fire_cmd_ppc(struct megasas_instance *instance,
463 dma_addr_t frame_phys_addr,
464 u32 frame_count,
465 struct megasas_register_set __iomem *regs)
467 unsigned long flags;
468 spin_lock_irqsave(&instance->hba_lock, flags);
469 writel((frame_phys_addr | (frame_count<<1))|1,
470 &(regs)->inbound_queue_port);
471 spin_unlock_irqrestore(&instance->hba_lock, flags);
475 * megasas_adp_reset_ppc - For controller reset
476 * @regs: MFI register set
478 static int
479 megasas_adp_reset_ppc(struct megasas_instance *instance,
480 struct megasas_register_set __iomem *regs)
482 return 0;
486 * megasas_check_reset_ppc - For controller reset check
487 * @regs: MFI register set
489 static int
490 megasas_check_reset_ppc(struct megasas_instance *instance,
491 struct megasas_register_set __iomem *regs)
493 return 0;
495 static struct megasas_instance_template megasas_instance_template_ppc = {
497 .fire_cmd = megasas_fire_cmd_ppc,
498 .enable_intr = megasas_enable_intr_ppc,
499 .disable_intr = megasas_disable_intr_ppc,
500 .clear_intr = megasas_clear_intr_ppc,
501 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
502 .adp_reset = megasas_adp_reset_ppc,
503 .check_reset = megasas_check_reset_ppc,
504 .service_isr = megasas_isr,
505 .tasklet = megasas_complete_cmd_dpc,
506 .init_adapter = megasas_init_adapter_mfi,
507 .build_and_issue_cmd = megasas_build_and_issue_cmd,
508 .issue_dcmd = megasas_issue_dcmd,
512 * megasas_enable_intr_skinny - Enables interrupts
513 * @regs: MFI register set
515 static inline void
516 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
518 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
520 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
522 /* Dummy readl to force pci flush */
523 readl(&regs->outbound_intr_mask);
527 * megasas_disable_intr_skinny - Disables interrupt
528 * @regs: MFI register set
530 static inline void
531 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
533 u32 mask = 0xFFFFFFFF;
534 writel(mask, &regs->outbound_intr_mask);
535 /* Dummy readl to force pci flush */
536 readl(&regs->outbound_intr_mask);
540 * megasas_read_fw_status_reg_skinny - returns the current FW status value
541 * @regs: MFI register set
543 static u32
544 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
546 return readl(&(regs)->outbound_scratch_pad);
550 * megasas_clear_interrupt_skinny - Check & clear interrupt
551 * @regs: MFI register set
553 static int
554 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
556 u32 status;
558 * Check if it is our interrupt
560 status = readl(&regs->outbound_intr_status);
562 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
563 return 0;
567 * Clear the interrupt by writing back the same value
569 writel(status, &regs->outbound_intr_status);
572 * dummy read to flush PCI
574 readl(&regs->outbound_intr_status);
576 return 1;
580 * megasas_fire_cmd_skinny - Sends command to the FW
581 * @frame_phys_addr : Physical address of cmd
582 * @frame_count : Number of frames for the command
583 * @regs : MFI register set
585 static inline void
586 megasas_fire_cmd_skinny(struct megasas_instance *instance,
587 dma_addr_t frame_phys_addr,
588 u32 frame_count,
589 struct megasas_register_set __iomem *regs)
591 unsigned long flags;
592 spin_lock_irqsave(&instance->hba_lock, flags);
593 writel(0, &(regs)->inbound_high_queue_port);
594 writel((frame_phys_addr | (frame_count<<1))|1,
595 &(regs)->inbound_low_queue_port);
596 spin_unlock_irqrestore(&instance->hba_lock, flags);
600 * megasas_adp_reset_skinny - For controller reset
601 * @regs: MFI register set
603 static int
604 megasas_adp_reset_skinny(struct megasas_instance *instance,
605 struct megasas_register_set __iomem *regs)
607 return 0;
611 * megasas_check_reset_skinny - For controller reset check
612 * @regs: MFI register set
614 static int
615 megasas_check_reset_skinny(struct megasas_instance *instance,
616 struct megasas_register_set __iomem *regs)
618 return 0;
621 static struct megasas_instance_template megasas_instance_template_skinny = {
623 .fire_cmd = megasas_fire_cmd_skinny,
624 .enable_intr = megasas_enable_intr_skinny,
625 .disable_intr = megasas_disable_intr_skinny,
626 .clear_intr = megasas_clear_intr_skinny,
627 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
628 .adp_reset = megasas_adp_reset_skinny,
629 .check_reset = megasas_check_reset_skinny,
630 .service_isr = megasas_isr,
631 .tasklet = megasas_complete_cmd_dpc,
632 .init_adapter = megasas_init_adapter_mfi,
633 .build_and_issue_cmd = megasas_build_and_issue_cmd,
634 .issue_dcmd = megasas_issue_dcmd,
639 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
640 * controllers
644 * megasas_enable_intr_gen2 - Enables interrupts
645 * @regs: MFI register set
647 static inline void
648 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
650 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
652 /* write ~0x00000005 (4 & 1) to the intr mask*/
653 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
655 /* Dummy readl to force pci flush */
656 readl(&regs->outbound_intr_mask);
660 * megasas_disable_intr_gen2 - Disables interrupt
661 * @regs: MFI register set
663 static inline void
664 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
666 u32 mask = 0xFFFFFFFF;
667 writel(mask, &regs->outbound_intr_mask);
668 /* Dummy readl to force pci flush */
669 readl(&regs->outbound_intr_mask);
673 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
674 * @regs: MFI register set
676 static u32
677 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
679 return readl(&(regs)->outbound_scratch_pad);
683 * megasas_clear_interrupt_gen2 - Check & clear interrupt
684 * @regs: MFI register set
686 static int
687 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
689 u32 status;
690 u32 mfiStatus = 0;
692 * Check if it is our interrupt
694 status = readl(&regs->outbound_intr_status);
696 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
697 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
699 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
700 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
704 * Clear the interrupt by writing back the same value
706 if (mfiStatus)
707 writel(status, &regs->outbound_doorbell_clear);
709 /* Dummy readl to force pci flush */
710 readl(&regs->outbound_intr_status);
712 return mfiStatus;
715 * megasas_fire_cmd_gen2 - Sends command to the FW
716 * @frame_phys_addr : Physical address of cmd
717 * @frame_count : Number of frames for the command
718 * @regs : MFI register set
720 static inline void
721 megasas_fire_cmd_gen2(struct megasas_instance *instance,
722 dma_addr_t frame_phys_addr,
723 u32 frame_count,
724 struct megasas_register_set __iomem *regs)
726 unsigned long flags;
727 spin_lock_irqsave(&instance->hba_lock, flags);
728 writel((frame_phys_addr | (frame_count<<1))|1,
729 &(regs)->inbound_queue_port);
730 spin_unlock_irqrestore(&instance->hba_lock, flags);
734 * megasas_adp_reset_gen2 - For controller reset
735 * @regs: MFI register set
737 static int
738 megasas_adp_reset_gen2(struct megasas_instance *instance,
739 struct megasas_register_set __iomem *reg_set)
741 u32 retry = 0 ;
742 u32 HostDiag;
744 writel(0, &reg_set->seq_offset);
745 writel(4, &reg_set->seq_offset);
746 writel(0xb, &reg_set->seq_offset);
747 writel(2, &reg_set->seq_offset);
748 writel(7, &reg_set->seq_offset);
749 writel(0xd, &reg_set->seq_offset);
750 msleep(1000);
752 HostDiag = (u32)readl(&reg_set->host_diag);
754 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
755 msleep(100);
756 HostDiag = (u32)readl(&reg_set->host_diag);
757 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
758 retry, HostDiag);
760 if (retry++ >= 100)
761 return 1;
765 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
767 writel((HostDiag | DIAG_RESET_ADAPTER), &reg_set->host_diag);
769 ssleep(10);
771 HostDiag = (u32)readl(&reg_set->host_diag);
772 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
773 msleep(100);
774 HostDiag = (u32)readl(&reg_set->host_diag);
775 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
776 retry, HostDiag);
778 if (retry++ >= 1000)
779 return 1;
782 return 0;
786 * megasas_check_reset_gen2 - For controller reset check
787 * @regs: MFI register set
789 static int
790 megasas_check_reset_gen2(struct megasas_instance *instance,
791 struct megasas_register_set __iomem *regs)
793 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
794 return 1;
797 return 0;
800 static struct megasas_instance_template megasas_instance_template_gen2 = {
802 .fire_cmd = megasas_fire_cmd_gen2,
803 .enable_intr = megasas_enable_intr_gen2,
804 .disable_intr = megasas_disable_intr_gen2,
805 .clear_intr = megasas_clear_intr_gen2,
806 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
807 .adp_reset = megasas_adp_reset_gen2,
808 .check_reset = megasas_check_reset_gen2,
809 .service_isr = megasas_isr,
810 .tasklet = megasas_complete_cmd_dpc,
811 .init_adapter = megasas_init_adapter_mfi,
812 .build_and_issue_cmd = megasas_build_and_issue_cmd,
813 .issue_dcmd = megasas_issue_dcmd,
817 * This is the end of set of functions & definitions
818 * specific to gen2 (deviceid : 0x78, 0x79) controllers
822 * Template added for TB (Fusion)
824 extern struct megasas_instance_template megasas_instance_template_fusion;
827 * megasas_issue_polled - Issues a polling command
828 * @instance: Adapter soft state
829 * @cmd: Command packet to be issued
831 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
834 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
837 struct megasas_header *frame_hdr = &cmd->frame->hdr;
839 frame_hdr->cmd_status = 0xFF;
840 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
843 * Issue the frame using inbound queue port
845 instance->instancet->issue_dcmd(instance, cmd);
848 * Wait for cmd_status to change
850 return wait_and_poll(instance, cmd);
854 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
855 * @instance: Adapter soft state
856 * @cmd: Command to be issued
858 * This function waits on an event for the command to be returned from ISR.
859 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
860 * Used to issue ioctl commands.
862 static int
863 megasas_issue_blocked_cmd(struct megasas_instance *instance,
864 struct megasas_cmd *cmd)
866 cmd->cmd_status = ENODATA;
868 instance->instancet->issue_dcmd(instance, cmd);
870 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
872 return 0;
876 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
877 * @instance: Adapter soft state
878 * @cmd_to_abort: Previously issued cmd to be aborted
880 * MFI firmware can abort previously issued AEN comamnd (automatic event
881 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
882 * cmd and waits for return status.
883 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
885 static int
886 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
887 struct megasas_cmd *cmd_to_abort)
889 struct megasas_cmd *cmd;
890 struct megasas_abort_frame *abort_fr;
892 cmd = megasas_get_cmd(instance);
894 if (!cmd)
895 return -1;
897 abort_fr = &cmd->frame->abort;
900 * Prepare and issue the abort frame
902 abort_fr->cmd = MFI_CMD_ABORT;
903 abort_fr->cmd_status = 0xFF;
904 abort_fr->flags = 0;
905 abort_fr->abort_context = cmd_to_abort->index;
906 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
907 abort_fr->abort_mfi_phys_addr_hi = 0;
909 cmd->sync_cmd = 1;
910 cmd->cmd_status = 0xFF;
912 instance->instancet->issue_dcmd(instance, cmd);
915 * Wait for this cmd to complete
917 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
918 cmd->sync_cmd = 0;
920 megasas_return_cmd(instance, cmd);
921 return 0;
925 * megasas_make_sgl32 - Prepares 32-bit SGL
926 * @instance: Adapter soft state
927 * @scp: SCSI command from the mid-layer
928 * @mfi_sgl: SGL to be filled in
930 * If successful, this function returns the number of SG elements. Otherwise,
931 * it returnes -1.
933 static int
934 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
935 union megasas_sgl *mfi_sgl)
937 int i;
938 int sge_count;
939 struct scatterlist *os_sgl;
941 sge_count = scsi_dma_map(scp);
942 BUG_ON(sge_count < 0);
944 if (sge_count) {
945 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
946 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
947 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
950 return sge_count;
954 * megasas_make_sgl64 - Prepares 64-bit SGL
955 * @instance: Adapter soft state
956 * @scp: SCSI command from the mid-layer
957 * @mfi_sgl: SGL to be filled in
959 * If successful, this function returns the number of SG elements. Otherwise,
960 * it returnes -1.
962 static int
963 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
964 union megasas_sgl *mfi_sgl)
966 int i;
967 int sge_count;
968 struct scatterlist *os_sgl;
970 sge_count = scsi_dma_map(scp);
971 BUG_ON(sge_count < 0);
973 if (sge_count) {
974 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
975 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
976 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
979 return sge_count;
983 * megasas_make_sgl_skinny - Prepares IEEE SGL
984 * @instance: Adapter soft state
985 * @scp: SCSI command from the mid-layer
986 * @mfi_sgl: SGL to be filled in
988 * If successful, this function returns the number of SG elements. Otherwise,
989 * it returnes -1.
991 static int
992 megasas_make_sgl_skinny(struct megasas_instance *instance,
993 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
995 int i;
996 int sge_count;
997 struct scatterlist *os_sgl;
999 sge_count = scsi_dma_map(scp);
1001 if (sge_count) {
1002 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1003 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1004 mfi_sgl->sge_skinny[i].phys_addr =
1005 sg_dma_address(os_sgl);
1006 mfi_sgl->sge_skinny[i].flag = 0;
1009 return sge_count;
1013 * megasas_get_frame_count - Computes the number of frames
1014 * @frame_type : type of frame- io or pthru frame
1015 * @sge_count : number of sg elements
1017 * Returns the number of frames required for numnber of sge's (sge_count)
1020 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1021 u8 sge_count, u8 frame_type)
1023 int num_cnt;
1024 int sge_bytes;
1025 u32 sge_sz;
1026 u32 frame_count=0;
1028 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1029 sizeof(struct megasas_sge32);
1031 if (instance->flag_ieee) {
1032 sge_sz = sizeof(struct megasas_sge_skinny);
1036 * Main frame can contain 2 SGEs for 64-bit SGLs and
1037 * 3 SGEs for 32-bit SGLs for ldio &
1038 * 1 SGEs for 64-bit SGLs and
1039 * 2 SGEs for 32-bit SGLs for pthru frame
1041 if (unlikely(frame_type == PTHRU_FRAME)) {
1042 if (instance->flag_ieee == 1) {
1043 num_cnt = sge_count - 1;
1044 } else if (IS_DMA64)
1045 num_cnt = sge_count - 1;
1046 else
1047 num_cnt = sge_count - 2;
1048 } else {
1049 if (instance->flag_ieee == 1) {
1050 num_cnt = sge_count - 1;
1051 } else if (IS_DMA64)
1052 num_cnt = sge_count - 2;
1053 else
1054 num_cnt = sge_count - 3;
1057 if(num_cnt>0){
1058 sge_bytes = sge_sz * num_cnt;
1060 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1061 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1063 /* Main frame */
1064 frame_count +=1;
1066 if (frame_count > 7)
1067 frame_count = 8;
1068 return frame_count;
1072 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1073 * @instance: Adapter soft state
1074 * @scp: SCSI command
1075 * @cmd: Command to be prepared in
1077 * This function prepares CDB commands. These are typcially pass-through
1078 * commands to the devices.
1080 static int
1081 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1082 struct megasas_cmd *cmd)
1084 u32 is_logical;
1085 u32 device_id;
1086 u16 flags = 0;
1087 struct megasas_pthru_frame *pthru;
1089 is_logical = MEGASAS_IS_LOGICAL(scp);
1090 device_id = MEGASAS_DEV_INDEX(instance, scp);
1091 pthru = (struct megasas_pthru_frame *)cmd->frame;
1093 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1094 flags = MFI_FRAME_DIR_WRITE;
1095 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1096 flags = MFI_FRAME_DIR_READ;
1097 else if (scp->sc_data_direction == PCI_DMA_NONE)
1098 flags = MFI_FRAME_DIR_NONE;
1100 if (instance->flag_ieee == 1) {
1101 flags |= MFI_FRAME_IEEE;
1105 * Prepare the DCDB frame
1107 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1108 pthru->cmd_status = 0x0;
1109 pthru->scsi_status = 0x0;
1110 pthru->target_id = device_id;
1111 pthru->lun = scp->device->lun;
1112 pthru->cdb_len = scp->cmd_len;
1113 pthru->timeout = 0;
1114 pthru->pad_0 = 0;
1115 pthru->flags = flags;
1116 pthru->data_xfer_len = scsi_bufflen(scp);
1118 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1121 * If the command is for the tape device, set the
1122 * pthru timeout to the os layer timeout value.
1124 if (scp->device->type == TYPE_TAPE) {
1125 if ((scp->request->timeout / HZ) > 0xFFFF)
1126 pthru->timeout = 0xFFFF;
1127 else
1128 pthru->timeout = scp->request->timeout / HZ;
1132 * Construct SGL
1134 if (instance->flag_ieee == 1) {
1135 pthru->flags |= MFI_FRAME_SGL64;
1136 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1137 &pthru->sgl);
1138 } else if (IS_DMA64) {
1139 pthru->flags |= MFI_FRAME_SGL64;
1140 pthru->sge_count = megasas_make_sgl64(instance, scp,
1141 &pthru->sgl);
1142 } else
1143 pthru->sge_count = megasas_make_sgl32(instance, scp,
1144 &pthru->sgl);
1146 if (pthru->sge_count > instance->max_num_sge) {
1147 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1148 pthru->sge_count);
1149 return 0;
1153 * Sense info specific
1155 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1156 pthru->sense_buf_phys_addr_hi = 0;
1157 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1160 * Compute the total number of frames this command consumes. FW uses
1161 * this number to pull sufficient number of frames from host memory.
1163 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1164 PTHRU_FRAME);
1166 return cmd->frame_count;
1170 * megasas_build_ldio - Prepares IOs to logical devices
1171 * @instance: Adapter soft state
1172 * @scp: SCSI command
1173 * @cmd: Command to be prepared
1175 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1177 static int
1178 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1179 struct megasas_cmd *cmd)
1181 u32 device_id;
1182 u8 sc = scp->cmnd[0];
1183 u16 flags = 0;
1184 struct megasas_io_frame *ldio;
1186 device_id = MEGASAS_DEV_INDEX(instance, scp);
1187 ldio = (struct megasas_io_frame *)cmd->frame;
1189 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1190 flags = MFI_FRAME_DIR_WRITE;
1191 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1192 flags = MFI_FRAME_DIR_READ;
1194 if (instance->flag_ieee == 1) {
1195 flags |= MFI_FRAME_IEEE;
1199 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1201 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1202 ldio->cmd_status = 0x0;
1203 ldio->scsi_status = 0x0;
1204 ldio->target_id = device_id;
1205 ldio->timeout = 0;
1206 ldio->reserved_0 = 0;
1207 ldio->pad_0 = 0;
1208 ldio->flags = flags;
1209 ldio->start_lba_hi = 0;
1210 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1213 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1215 if (scp->cmd_len == 6) {
1216 ldio->lba_count = (u32) scp->cmnd[4];
1217 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1218 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1220 ldio->start_lba_lo &= 0x1FFFFF;
1224 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1226 else if (scp->cmd_len == 10) {
1227 ldio->lba_count = (u32) scp->cmnd[8] |
1228 ((u32) scp->cmnd[7] << 8);
1229 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1230 ((u32) scp->cmnd[3] << 16) |
1231 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1235 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1237 else if (scp->cmd_len == 12) {
1238 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1239 ((u32) scp->cmnd[7] << 16) |
1240 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1242 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1243 ((u32) scp->cmnd[3] << 16) |
1244 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1248 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1250 else if (scp->cmd_len == 16) {
1251 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1252 ((u32) scp->cmnd[11] << 16) |
1253 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1255 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1256 ((u32) scp->cmnd[7] << 16) |
1257 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1259 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1260 ((u32) scp->cmnd[3] << 16) |
1261 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1266 * Construct SGL
1268 if (instance->flag_ieee) {
1269 ldio->flags |= MFI_FRAME_SGL64;
1270 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1271 &ldio->sgl);
1272 } else if (IS_DMA64) {
1273 ldio->flags |= MFI_FRAME_SGL64;
1274 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1275 } else
1276 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1278 if (ldio->sge_count > instance->max_num_sge) {
1279 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1280 ldio->sge_count);
1281 return 0;
1285 * Sense info specific
1287 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1288 ldio->sense_buf_phys_addr_hi = 0;
1289 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1292 * Compute the total number of frames this command consumes. FW uses
1293 * this number to pull sufficient number of frames from host memory.
1295 cmd->frame_count = megasas_get_frame_count(instance,
1296 ldio->sge_count, IO_FRAME);
1298 return cmd->frame_count;
1302 * megasas_is_ldio - Checks if the cmd is for logical drive
1303 * @scmd: SCSI command
1305 * Called by megasas_queue_command to find out if the command to be queued
1306 * is a logical drive command
1308 inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1310 if (!MEGASAS_IS_LOGICAL(cmd))
1311 return 0;
1312 switch (cmd->cmnd[0]) {
1313 case READ_10:
1314 case WRITE_10:
1315 case READ_12:
1316 case WRITE_12:
1317 case READ_6:
1318 case WRITE_6:
1319 case READ_16:
1320 case WRITE_16:
1321 return 1;
1322 default:
1323 return 0;
1328 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1329 * in FW
1330 * @instance: Adapter soft state
1332 static inline void
1333 megasas_dump_pending_frames(struct megasas_instance *instance)
1335 struct megasas_cmd *cmd;
1336 int i,n;
1337 union megasas_sgl *mfi_sgl;
1338 struct megasas_io_frame *ldio;
1339 struct megasas_pthru_frame *pthru;
1340 u32 sgcount;
1341 u32 max_cmd = instance->max_fw_cmds;
1343 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1344 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1345 if (IS_DMA64)
1346 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1347 else
1348 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1350 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1351 for (i = 0; i < max_cmd; i++) {
1352 cmd = instance->cmd_list[i];
1353 if(!cmd->scmd)
1354 continue;
1355 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1356 if (megasas_is_ldio(cmd->scmd)){
1357 ldio = (struct megasas_io_frame *)cmd->frame;
1358 mfi_sgl = &ldio->sgl;
1359 sgcount = ldio->sge_count;
1360 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);
1362 else {
1363 pthru = (struct megasas_pthru_frame *) cmd->frame;
1364 mfi_sgl = &pthru->sgl;
1365 sgcount = pthru->sge_count;
1366 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);
1368 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1369 for (n = 0; n < sgcount; n++){
1370 if (IS_DMA64)
1371 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) ;
1372 else
1373 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1376 printk(KERN_ERR "\n");
1377 } /*for max_cmd*/
1378 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1379 for (i = 0; i < max_cmd; i++) {
1381 cmd = instance->cmd_list[i];
1383 if(cmd->sync_cmd == 1){
1384 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1387 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1391 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1392 struct scsi_cmnd *scmd)
1394 struct megasas_cmd *cmd;
1395 u32 frame_count;
1397 cmd = megasas_get_cmd(instance);
1398 if (!cmd)
1399 return SCSI_MLQUEUE_HOST_BUSY;
1402 * Logical drive command
1404 if (megasas_is_ldio(scmd))
1405 frame_count = megasas_build_ldio(instance, scmd, cmd);
1406 else
1407 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1409 if (!frame_count)
1410 goto out_return_cmd;
1412 cmd->scmd = scmd;
1413 scmd->SCp.ptr = (char *)cmd;
1416 * Issue the command to the FW
1418 atomic_inc(&instance->fw_outstanding);
1420 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1421 cmd->frame_count-1, instance->reg_set);
1423 * Check if we have pend cmds to be completed
1425 if (poll_mode_io && atomic_read(&instance->fw_outstanding))
1426 tasklet_schedule(&instance->isr_tasklet);
1428 return 0;
1429 out_return_cmd:
1430 megasas_return_cmd(instance, cmd);
1431 return 1;
1436 * megasas_queue_command - Queue entry point
1437 * @scmd: SCSI command to be queued
1438 * @done: Callback entry point
1440 static int
1441 megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1443 struct megasas_instance *instance;
1444 unsigned long flags;
1446 instance = (struct megasas_instance *)
1447 scmd->device->host->hostdata;
1449 if (instance->issuepend_done == 0)
1450 return SCSI_MLQUEUE_HOST_BUSY;
1452 spin_lock_irqsave(&instance->hba_lock, flags);
1453 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1454 spin_unlock_irqrestore(&instance->hba_lock, flags);
1455 return SCSI_MLQUEUE_HOST_BUSY;
1458 spin_unlock_irqrestore(&instance->hba_lock, flags);
1460 scmd->scsi_done = done;
1461 scmd->result = 0;
1463 if (MEGASAS_IS_LOGICAL(scmd) &&
1464 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1465 scmd->result = DID_BAD_TARGET << 16;
1466 goto out_done;
1469 switch (scmd->cmnd[0]) {
1470 case SYNCHRONIZE_CACHE:
1472 * FW takes care of flush cache on its own
1473 * No need to send it down
1475 scmd->result = DID_OK << 16;
1476 goto out_done;
1477 default:
1478 break;
1481 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1482 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1483 return SCSI_MLQUEUE_HOST_BUSY;
1486 return 0;
1488 out_done:
1489 done(scmd);
1490 return 0;
1493 static DEF_SCSI_QCMD(megasas_queue_command)
1495 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1497 int i;
1499 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1501 if ((megasas_mgmt_info.instance[i]) &&
1502 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1503 return megasas_mgmt_info.instance[i];
1506 return NULL;
1509 static int megasas_slave_configure(struct scsi_device *sdev)
1511 u16 pd_index = 0;
1512 struct megasas_instance *instance ;
1514 instance = megasas_lookup_instance(sdev->host->host_no);
1517 * Don't export physical disk devices to the disk driver.
1519 * FIXME: Currently we don't export them to the midlayer at all.
1520 * That will be fixed once LSI engineers have audited the
1521 * firmware for possible issues.
1523 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1524 sdev->type == TYPE_DISK) {
1525 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1526 sdev->id;
1527 if (instance->pd_list[pd_index].driveState ==
1528 MR_PD_STATE_SYSTEM) {
1529 blk_queue_rq_timeout(sdev->request_queue,
1530 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1531 return 0;
1533 return -ENXIO;
1537 * The RAID firmware may require extended timeouts.
1539 blk_queue_rq_timeout(sdev->request_queue,
1540 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1541 return 0;
1544 static int megasas_slave_alloc(struct scsi_device *sdev)
1546 u16 pd_index = 0;
1547 struct megasas_instance *instance ;
1548 instance = megasas_lookup_instance(sdev->host->host_no);
1549 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1550 (sdev->type == TYPE_DISK)) {
1552 * Open the OS scan to the SYSTEM PD
1554 pd_index =
1555 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1556 sdev->id;
1557 if ((instance->pd_list[pd_index].driveState ==
1558 MR_PD_STATE_SYSTEM) &&
1559 (instance->pd_list[pd_index].driveType ==
1560 TYPE_DISK)) {
1561 return 0;
1563 return -ENXIO;
1565 return 0;
1568 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1570 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1571 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1572 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)) {
1573 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1574 } else {
1575 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1580 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1581 * restored to max value
1582 * @instance: Adapter soft state
1585 void
1586 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1588 unsigned long flags;
1589 if (instance->flag & MEGASAS_FW_BUSY
1590 && time_after(jiffies, instance->last_time + 5 * HZ)
1591 && atomic_read(&instance->fw_outstanding) < 17) {
1593 spin_lock_irqsave(instance->host->host_lock, flags);
1594 instance->flag &= ~MEGASAS_FW_BUSY;
1595 if ((instance->pdev->device ==
1596 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1597 (instance->pdev->device ==
1598 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1599 instance->host->can_queue =
1600 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1601 } else
1602 instance->host->can_queue =
1603 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1605 spin_unlock_irqrestore(instance->host->host_lock, flags);
1610 * megasas_complete_cmd_dpc - Returns FW's controller structure
1611 * @instance_addr: Address of adapter soft state
1613 * Tasklet to complete cmds
1615 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1617 u32 producer;
1618 u32 consumer;
1619 u32 context;
1620 struct megasas_cmd *cmd;
1621 struct megasas_instance *instance =
1622 (struct megasas_instance *)instance_addr;
1623 unsigned long flags;
1625 /* If we have already declared adapter dead, donot complete cmds */
1626 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1627 return;
1629 spin_lock_irqsave(&instance->completion_lock, flags);
1631 producer = *instance->producer;
1632 consumer = *instance->consumer;
1634 while (consumer != producer) {
1635 context = instance->reply_queue[consumer];
1636 if (context >= instance->max_fw_cmds) {
1637 printk(KERN_ERR "Unexpected context value %x\n",
1638 context);
1639 BUG();
1642 cmd = instance->cmd_list[context];
1644 megasas_complete_cmd(instance, cmd, DID_OK);
1646 consumer++;
1647 if (consumer == (instance->max_fw_cmds + 1)) {
1648 consumer = 0;
1652 *instance->consumer = producer;
1654 spin_unlock_irqrestore(&instance->completion_lock, flags);
1657 * Check if we can restore can_queue
1659 megasas_check_and_restore_queue_depth(instance);
1662 static void
1663 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1665 static void
1666 process_fw_state_change_wq(struct work_struct *work);
1668 void megasas_do_ocr(struct megasas_instance *instance)
1670 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1671 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1672 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1673 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1675 instance->instancet->disable_intr(instance->reg_set);
1676 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1677 instance->issuepend_done = 0;
1679 atomic_set(&instance->fw_outstanding, 0);
1680 megasas_internal_reset_defer_cmds(instance);
1681 process_fw_state_change_wq(&instance->work_init);
1685 * megasas_wait_for_outstanding - Wait for all outstanding cmds
1686 * @instance: Adapter soft state
1688 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1689 * complete all its outstanding commands. Returns error if one or more IOs
1690 * are pending after this time period. It also marks the controller dead.
1692 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1694 int i;
1695 u32 reset_index;
1696 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1697 u8 adprecovery;
1698 unsigned long flags;
1699 struct list_head clist_local;
1700 struct megasas_cmd *reset_cmd;
1701 u32 fw_state;
1702 u8 kill_adapter_flag;
1704 spin_lock_irqsave(&instance->hba_lock, flags);
1705 adprecovery = instance->adprecovery;
1706 spin_unlock_irqrestore(&instance->hba_lock, flags);
1708 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1710 INIT_LIST_HEAD(&clist_local);
1711 spin_lock_irqsave(&instance->hba_lock, flags);
1712 list_splice_init(&instance->internal_reset_pending_q,
1713 &clist_local);
1714 spin_unlock_irqrestore(&instance->hba_lock, flags);
1716 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1717 for (i = 0; i < wait_time; i++) {
1718 msleep(1000);
1719 spin_lock_irqsave(&instance->hba_lock, flags);
1720 adprecovery = instance->adprecovery;
1721 spin_unlock_irqrestore(&instance->hba_lock, flags);
1722 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1723 break;
1726 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1727 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1728 spin_lock_irqsave(&instance->hba_lock, flags);
1729 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1730 spin_unlock_irqrestore(&instance->hba_lock, flags);
1731 return FAILED;
1734 reset_index = 0;
1735 while (!list_empty(&clist_local)) {
1736 reset_cmd = list_entry((&clist_local)->next,
1737 struct megasas_cmd, list);
1738 list_del_init(&reset_cmd->list);
1739 if (reset_cmd->scmd) {
1740 reset_cmd->scmd->result = DID_RESET << 16;
1741 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1742 reset_index, reset_cmd,
1743 reset_cmd->scmd->cmnd[0],
1744 reset_cmd->scmd->serial_number);
1746 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1747 megasas_return_cmd(instance, reset_cmd);
1748 } else if (reset_cmd->sync_cmd) {
1749 printk(KERN_NOTICE "megasas:%p synch cmds"
1750 "reset queue\n",
1751 reset_cmd);
1753 reset_cmd->cmd_status = ENODATA;
1754 instance->instancet->fire_cmd(instance,
1755 reset_cmd->frame_phys_addr,
1756 0, instance->reg_set);
1757 } else {
1758 printk(KERN_NOTICE "megasas: %p unexpected"
1759 "cmds lst\n",
1760 reset_cmd);
1762 reset_index++;
1765 return SUCCESS;
1768 for (i = 0; i < wait_time; i++) {
1770 int outstanding = atomic_read(&instance->fw_outstanding);
1772 if (!outstanding)
1773 break;
1775 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1776 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1777 "commands to complete\n",i,outstanding);
1779 * Call cmd completion routine. Cmd to be
1780 * be completed directly without depending on isr.
1782 megasas_complete_cmd_dpc((unsigned long)instance);
1785 msleep(1000);
1788 i = 0;
1789 kill_adapter_flag = 0;
1790 do {
1791 fw_state = instance->instancet->read_fw_status_reg(
1792 instance->reg_set) & MFI_STATE_MASK;
1793 if ((fw_state == MFI_STATE_FAULT) &&
1794 (instance->disableOnlineCtrlReset == 0)) {
1795 if (i == 3) {
1796 kill_adapter_flag = 2;
1797 break;
1799 megasas_do_ocr(instance);
1800 kill_adapter_flag = 1;
1802 /* wait for 1 secs to let FW finish the pending cmds */
1803 msleep(1000);
1805 i++;
1806 } while (i <= 3);
1808 if (atomic_read(&instance->fw_outstanding) &&
1809 !kill_adapter_flag) {
1810 if (instance->disableOnlineCtrlReset == 0) {
1812 megasas_do_ocr(instance);
1814 /* wait for 5 secs to let FW finish the pending cmds */
1815 for (i = 0; i < wait_time; i++) {
1816 int outstanding =
1817 atomic_read(&instance->fw_outstanding);
1818 if (!outstanding)
1819 return SUCCESS;
1820 msleep(1000);
1825 if (atomic_read(&instance->fw_outstanding) ||
1826 (kill_adapter_flag == 2)) {
1827 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1829 * Send signal to FW to stop processing any pending cmds.
1830 * The controller will be taken offline by the OS now.
1832 if ((instance->pdev->device ==
1833 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1834 (instance->pdev->device ==
1835 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1836 writel(MFI_STOP_ADP,
1837 &instance->reg_set->doorbell);
1838 } else {
1839 writel(MFI_STOP_ADP,
1840 &instance->reg_set->inbound_doorbell);
1842 megasas_dump_pending_frames(instance);
1843 spin_lock_irqsave(&instance->hba_lock, flags);
1844 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1845 spin_unlock_irqrestore(&instance->hba_lock, flags);
1846 return FAILED;
1849 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1851 return SUCCESS;
1855 * megasas_generic_reset - Generic reset routine
1856 * @scmd: Mid-layer SCSI command
1858 * This routine implements a generic reset handler for device, bus and host
1859 * reset requests. Device, bus and host specific reset handlers can use this
1860 * function after they do their specific tasks.
1862 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1864 int ret_val;
1865 struct megasas_instance *instance;
1867 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1869 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1870 scmd->serial_number, scmd->cmnd[0], scmd->retries);
1872 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1873 printk(KERN_ERR "megasas: cannot recover from previous reset "
1874 "failures\n");
1875 return FAILED;
1878 ret_val = megasas_wait_for_outstanding(instance);
1879 if (ret_val == SUCCESS)
1880 printk(KERN_NOTICE "megasas: reset successful \n");
1881 else
1882 printk(KERN_ERR "megasas: failed to do reset\n");
1884 return ret_val;
1888 * megasas_reset_timer - quiesce the adapter if required
1889 * @scmd: scsi cmnd
1891 * Sets the FW busy flag and reduces the host->can_queue if the
1892 * cmd has not been completed within the timeout period.
1894 static enum
1895 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1897 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1898 struct megasas_instance *instance;
1899 unsigned long flags;
1901 if (time_after(jiffies, scmd->jiffies_at_alloc +
1902 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1903 return BLK_EH_NOT_HANDLED;
1906 instance = cmd->instance;
1907 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1908 /* FW is busy, throttle IO */
1909 spin_lock_irqsave(instance->host->host_lock, flags);
1911 instance->host->can_queue = 16;
1912 instance->last_time = jiffies;
1913 instance->flag |= MEGASAS_FW_BUSY;
1915 spin_unlock_irqrestore(instance->host->host_lock, flags);
1917 return BLK_EH_RESET_TIMER;
1921 * megasas_reset_device - Device reset handler entry point
1923 static int megasas_reset_device(struct scsi_cmnd *scmd)
1925 int ret;
1928 * First wait for all commands to complete
1930 ret = megasas_generic_reset(scmd);
1932 return ret;
1936 * megasas_reset_bus_host - Bus & host reset handler entry point
1938 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1940 int ret;
1941 struct megasas_instance *instance;
1942 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1945 * First wait for all commands to complete
1947 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
1948 ret = megasas_reset_fusion(scmd->device->host);
1949 else
1950 ret = megasas_generic_reset(scmd);
1952 return ret;
1956 * megasas_bios_param - Returns disk geometry for a disk
1957 * @sdev: device handle
1958 * @bdev: block device
1959 * @capacity: drive capacity
1960 * @geom: geometry parameters
1962 static int
1963 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1964 sector_t capacity, int geom[])
1966 int heads;
1967 int sectors;
1968 sector_t cylinders;
1969 unsigned long tmp;
1970 /* Default heads (64) & sectors (32) */
1971 heads = 64;
1972 sectors = 32;
1974 tmp = heads * sectors;
1975 cylinders = capacity;
1977 sector_div(cylinders, tmp);
1980 * Handle extended translation size for logical drives > 1Gb
1983 if (capacity >= 0x200000) {
1984 heads = 255;
1985 sectors = 63;
1986 tmp = heads*sectors;
1987 cylinders = capacity;
1988 sector_div(cylinders, tmp);
1991 geom[0] = heads;
1992 geom[1] = sectors;
1993 geom[2] = cylinders;
1995 return 0;
1998 static void megasas_aen_polling(struct work_struct *work);
2001 * megasas_service_aen - Processes an event notification
2002 * @instance: Adapter soft state
2003 * @cmd: AEN command completed by the ISR
2005 * For AEN, driver sends a command down to FW that is held by the FW till an
2006 * event occurs. When an event of interest occurs, FW completes the command
2007 * that it was previously holding.
2009 * This routines sends SIGIO signal to processes that have registered with the
2010 * driver for AEN.
2012 static void
2013 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2015 unsigned long flags;
2017 * Don't signal app if it is just an aborted previously registered aen
2019 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2020 spin_lock_irqsave(&poll_aen_lock, flags);
2021 megasas_poll_wait_aen = 1;
2022 spin_unlock_irqrestore(&poll_aen_lock, flags);
2023 wake_up(&megasas_poll_wait);
2024 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2026 else
2027 cmd->abort_aen = 0;
2029 instance->aen_cmd = NULL;
2030 megasas_return_cmd(instance, cmd);
2032 if ((instance->unload == 0) &&
2033 ((instance->issuepend_done == 1))) {
2034 struct megasas_aen_event *ev;
2035 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2036 if (!ev) {
2037 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2038 } else {
2039 ev->instance = instance;
2040 instance->ev = ev;
2041 INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
2042 schedule_delayed_work(
2043 (struct delayed_work *)&ev->hotplug_work, 0);
2049 * Scsi host template for megaraid_sas driver
2051 static struct scsi_host_template megasas_template = {
2053 .module = THIS_MODULE,
2054 .name = "LSI SAS based MegaRAID driver",
2055 .proc_name = "megaraid_sas",
2056 .slave_configure = megasas_slave_configure,
2057 .slave_alloc = megasas_slave_alloc,
2058 .queuecommand = megasas_queue_command,
2059 .eh_device_reset_handler = megasas_reset_device,
2060 .eh_bus_reset_handler = megasas_reset_bus_host,
2061 .eh_host_reset_handler = megasas_reset_bus_host,
2062 .eh_timed_out = megasas_reset_timer,
2063 .bios_param = megasas_bios_param,
2064 .use_clustering = ENABLE_CLUSTERING,
2068 * megasas_complete_int_cmd - Completes an internal command
2069 * @instance: Adapter soft state
2070 * @cmd: Command to be completed
2072 * The megasas_issue_blocked_cmd() function waits for a command to complete
2073 * after it issues a command. This function wakes up that waiting routine by
2074 * calling wake_up() on the wait queue.
2076 static void
2077 megasas_complete_int_cmd(struct megasas_instance *instance,
2078 struct megasas_cmd *cmd)
2080 cmd->cmd_status = cmd->frame->io.cmd_status;
2082 if (cmd->cmd_status == ENODATA) {
2083 cmd->cmd_status = 0;
2085 wake_up(&instance->int_cmd_wait_q);
2089 * megasas_complete_abort - Completes aborting a command
2090 * @instance: Adapter soft state
2091 * @cmd: Cmd that was issued to abort another cmd
2093 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2094 * after it issues an abort on a previously issued command. This function
2095 * wakes up all functions waiting on the same wait queue.
2097 static void
2098 megasas_complete_abort(struct megasas_instance *instance,
2099 struct megasas_cmd *cmd)
2101 if (cmd->sync_cmd) {
2102 cmd->sync_cmd = 0;
2103 cmd->cmd_status = 0;
2104 wake_up(&instance->abort_cmd_wait_q);
2107 return;
2111 * megasas_complete_cmd - Completes a command
2112 * @instance: Adapter soft state
2113 * @cmd: Command to be completed
2114 * @alt_status: If non-zero, use this value as status to
2115 * SCSI mid-layer instead of the value returned
2116 * by the FW. This should be used if caller wants
2117 * an alternate status (as in the case of aborted
2118 * commands)
2120 void
2121 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2122 u8 alt_status)
2124 int exception = 0;
2125 struct megasas_header *hdr = &cmd->frame->hdr;
2126 unsigned long flags;
2127 struct fusion_context *fusion = instance->ctrl_context;
2129 /* flag for the retry reset */
2130 cmd->retry_for_fw_reset = 0;
2132 if (cmd->scmd)
2133 cmd->scmd->SCp.ptr = NULL;
2135 switch (hdr->cmd) {
2137 case MFI_CMD_PD_SCSI_IO:
2138 case MFI_CMD_LD_SCSI_IO:
2141 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2142 * issued either through an IO path or an IOCTL path. If it
2143 * was via IOCTL, we will send it to internal completion.
2145 if (cmd->sync_cmd) {
2146 cmd->sync_cmd = 0;
2147 megasas_complete_int_cmd(instance, cmd);
2148 break;
2151 case MFI_CMD_LD_READ:
2152 case MFI_CMD_LD_WRITE:
2154 if (alt_status) {
2155 cmd->scmd->result = alt_status << 16;
2156 exception = 1;
2159 if (exception) {
2161 atomic_dec(&instance->fw_outstanding);
2163 scsi_dma_unmap(cmd->scmd);
2164 cmd->scmd->scsi_done(cmd->scmd);
2165 megasas_return_cmd(instance, cmd);
2167 break;
2170 switch (hdr->cmd_status) {
2172 case MFI_STAT_OK:
2173 cmd->scmd->result = DID_OK << 16;
2174 break;
2176 case MFI_STAT_SCSI_IO_FAILED:
2177 case MFI_STAT_LD_INIT_IN_PROGRESS:
2178 cmd->scmd->result =
2179 (DID_ERROR << 16) | hdr->scsi_status;
2180 break;
2182 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2184 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2186 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2187 memset(cmd->scmd->sense_buffer, 0,
2188 SCSI_SENSE_BUFFERSIZE);
2189 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2190 hdr->sense_len);
2192 cmd->scmd->result |= DRIVER_SENSE << 24;
2195 break;
2197 case MFI_STAT_LD_OFFLINE:
2198 case MFI_STAT_DEVICE_NOT_FOUND:
2199 cmd->scmd->result = DID_BAD_TARGET << 16;
2200 break;
2202 default:
2203 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2204 hdr->cmd_status);
2205 cmd->scmd->result = DID_ERROR << 16;
2206 break;
2209 atomic_dec(&instance->fw_outstanding);
2211 scsi_dma_unmap(cmd->scmd);
2212 cmd->scmd->scsi_done(cmd->scmd);
2213 megasas_return_cmd(instance, cmd);
2215 break;
2217 case MFI_CMD_SMP:
2218 case MFI_CMD_STP:
2219 case MFI_CMD_DCMD:
2220 /* Check for LD map update */
2221 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2222 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2223 spin_lock_irqsave(instance->host->host_lock, flags);
2224 if (cmd->frame->hdr.cmd_status != 0) {
2225 if (cmd->frame->hdr.cmd_status !=
2226 MFI_STAT_NOT_FOUND)
2227 printk(KERN_WARNING "megasas: map sync"
2228 "failed, status = 0x%x.\n",
2229 cmd->frame->hdr.cmd_status);
2230 else {
2231 megasas_return_cmd(instance, cmd);
2232 spin_unlock_irqrestore(
2233 instance->host->host_lock,
2234 flags);
2235 break;
2237 } else
2238 instance->map_id++;
2239 megasas_return_cmd(instance, cmd);
2240 if (MR_ValidateMapInfo(
2241 fusion->ld_map[(instance->map_id & 1)],
2242 fusion->load_balance_info))
2243 fusion->fast_path_io = 1;
2244 else
2245 fusion->fast_path_io = 0;
2246 megasas_sync_map_info(instance);
2247 spin_unlock_irqrestore(instance->host->host_lock,
2248 flags);
2249 break;
2251 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2252 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2253 spin_lock_irqsave(&poll_aen_lock, flags);
2254 megasas_poll_wait_aen = 0;
2255 spin_unlock_irqrestore(&poll_aen_lock, flags);
2259 * See if got an event notification
2261 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2262 megasas_service_aen(instance, cmd);
2263 else
2264 megasas_complete_int_cmd(instance, cmd);
2266 break;
2268 case MFI_CMD_ABORT:
2270 * Cmd issued to abort another cmd returned
2272 megasas_complete_abort(instance, cmd);
2273 break;
2275 default:
2276 printk("megasas: Unknown command completed! [0x%X]\n",
2277 hdr->cmd);
2278 break;
2283 * megasas_issue_pending_cmds_again - issue all pending cmds
2284 * in FW again because of the fw reset
2285 * @instance: Adapter soft state
2287 static inline void
2288 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2290 struct megasas_cmd *cmd;
2291 struct list_head clist_local;
2292 union megasas_evt_class_locale class_locale;
2293 unsigned long flags;
2294 u32 seq_num;
2296 INIT_LIST_HEAD(&clist_local);
2297 spin_lock_irqsave(&instance->hba_lock, flags);
2298 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2299 spin_unlock_irqrestore(&instance->hba_lock, flags);
2301 while (!list_empty(&clist_local)) {
2302 cmd = list_entry((&clist_local)->next,
2303 struct megasas_cmd, list);
2304 list_del_init(&cmd->list);
2306 if (cmd->sync_cmd || cmd->scmd) {
2307 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2308 "detected to be pending while HBA reset.\n",
2309 cmd, cmd->scmd, cmd->sync_cmd);
2311 cmd->retry_for_fw_reset++;
2313 if (cmd->retry_for_fw_reset == 3) {
2314 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2315 "was tried multiple times during reset."
2316 "Shutting down the HBA\n",
2317 cmd, cmd->scmd, cmd->sync_cmd);
2318 megaraid_sas_kill_hba(instance);
2320 instance->adprecovery =
2321 MEGASAS_HW_CRITICAL_ERROR;
2322 return;
2326 if (cmd->sync_cmd == 1) {
2327 if (cmd->scmd) {
2328 printk(KERN_NOTICE "megaraid_sas: unexpected"
2329 "cmd attached to internal command!\n");
2331 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2332 "on the internal reset queue,"
2333 "issue it again.\n", cmd);
2334 cmd->cmd_status = ENODATA;
2335 instance->instancet->fire_cmd(instance,
2336 cmd->frame_phys_addr ,
2337 0, instance->reg_set);
2338 } else if (cmd->scmd) {
2339 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2340 "detected on the internal queue, issue again.\n",
2341 cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2343 atomic_inc(&instance->fw_outstanding);
2344 instance->instancet->fire_cmd(instance,
2345 cmd->frame_phys_addr,
2346 cmd->frame_count-1, instance->reg_set);
2347 } else {
2348 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2349 "internal reset defer list while re-issue!!\n",
2350 cmd);
2354 if (instance->aen_cmd) {
2355 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2356 megasas_return_cmd(instance, instance->aen_cmd);
2358 instance->aen_cmd = NULL;
2362 * Initiate AEN (Asynchronous Event Notification)
2364 seq_num = instance->last_seq_num;
2365 class_locale.members.reserved = 0;
2366 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2367 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2369 megasas_register_aen(instance, seq_num, class_locale.word);
2373 * Move the internal reset pending commands to a deferred queue.
2375 * We move the commands pending at internal reset time to a
2376 * pending queue. This queue would be flushed after successful
2377 * completion of the internal reset sequence. if the internal reset
2378 * did not complete in time, the kernel reset handler would flush
2379 * these commands.
2381 static void
2382 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2384 struct megasas_cmd *cmd;
2385 int i;
2386 u32 max_cmd = instance->max_fw_cmds;
2387 u32 defer_index;
2388 unsigned long flags;
2390 defer_index = 0;
2391 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2392 for (i = 0; i < max_cmd; i++) {
2393 cmd = instance->cmd_list[i];
2394 if (cmd->sync_cmd == 1 || cmd->scmd) {
2395 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2396 "on the defer queue as internal\n",
2397 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2399 if (!list_empty(&cmd->list)) {
2400 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2401 " moving this cmd:%p, %d %p, it was"
2402 "discovered on some list?\n",
2403 cmd, cmd->sync_cmd, cmd->scmd);
2405 list_del_init(&cmd->list);
2407 defer_index++;
2408 list_add_tail(&cmd->list,
2409 &instance->internal_reset_pending_q);
2412 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2416 static void
2417 process_fw_state_change_wq(struct work_struct *work)
2419 struct megasas_instance *instance =
2420 container_of(work, struct megasas_instance, work_init);
2421 u32 wait;
2422 unsigned long flags;
2424 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2425 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2426 instance->adprecovery);
2427 return ;
2430 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2431 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2432 "state, restarting it...\n");
2434 instance->instancet->disable_intr(instance->reg_set);
2435 atomic_set(&instance->fw_outstanding, 0);
2437 atomic_set(&instance->fw_reset_no_pci_access, 1);
2438 instance->instancet->adp_reset(instance, instance->reg_set);
2439 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2441 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2442 "initiating next stage...\n");
2444 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2445 "state 2 starting...\n");
2447 /*waitting for about 20 second before start the second init*/
2448 for (wait = 0; wait < 30; wait++) {
2449 msleep(1000);
2452 if (megasas_transition_to_ready(instance)) {
2453 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2455 megaraid_sas_kill_hba(instance);
2456 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2457 return ;
2460 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2461 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2462 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2464 *instance->consumer = *instance->producer;
2465 } else {
2466 *instance->consumer = 0;
2467 *instance->producer = 0;
2470 megasas_issue_init_mfi(instance);
2472 spin_lock_irqsave(&instance->hba_lock, flags);
2473 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2474 spin_unlock_irqrestore(&instance->hba_lock, flags);
2475 instance->instancet->enable_intr(instance->reg_set);
2477 megasas_issue_pending_cmds_again(instance);
2478 instance->issuepend_done = 1;
2480 return ;
2484 * megasas_deplete_reply_queue - Processes all completed commands
2485 * @instance: Adapter soft state
2486 * @alt_status: Alternate status to be returned to
2487 * SCSI mid-layer instead of the status
2488 * returned by the FW
2489 * Note: this must be called with hba lock held
2491 static int
2492 megasas_deplete_reply_queue(struct megasas_instance *instance,
2493 u8 alt_status)
2495 u32 mfiStatus;
2496 u32 fw_state;
2498 if ((mfiStatus = instance->instancet->check_reset(instance,
2499 instance->reg_set)) == 1) {
2500 return IRQ_HANDLED;
2503 if ((mfiStatus = instance->instancet->clear_intr(
2504 instance->reg_set)
2505 ) == 0) {
2506 return IRQ_NONE;
2509 instance->mfiStatus = mfiStatus;
2511 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2512 fw_state = instance->instancet->read_fw_status_reg(
2513 instance->reg_set) & MFI_STATE_MASK;
2515 if (fw_state != MFI_STATE_FAULT) {
2516 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2517 fw_state);
2520 if ((fw_state == MFI_STATE_FAULT) &&
2521 (instance->disableOnlineCtrlReset == 0)) {
2522 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2524 if ((instance->pdev->device ==
2525 PCI_DEVICE_ID_LSI_SAS1064R) ||
2526 (instance->pdev->device ==
2527 PCI_DEVICE_ID_DELL_PERC5) ||
2528 (instance->pdev->device ==
2529 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2531 *instance->consumer =
2532 MEGASAS_ADPRESET_INPROG_SIGN;
2536 instance->instancet->disable_intr(instance->reg_set);
2537 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2538 instance->issuepend_done = 0;
2540 atomic_set(&instance->fw_outstanding, 0);
2541 megasas_internal_reset_defer_cmds(instance);
2543 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2544 fw_state, instance->adprecovery);
2546 schedule_work(&instance->work_init);
2547 return IRQ_HANDLED;
2549 } else {
2550 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2551 fw_state, instance->disableOnlineCtrlReset);
2555 tasklet_schedule(&instance->isr_tasklet);
2556 return IRQ_HANDLED;
2559 * megasas_isr - isr entry point
2561 static irqreturn_t megasas_isr(int irq, void *devp)
2563 struct megasas_instance *instance;
2564 unsigned long flags;
2565 irqreturn_t rc;
2567 if (atomic_read(
2568 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2569 return IRQ_HANDLED;
2571 instance = (struct megasas_instance *)devp;
2573 spin_lock_irqsave(&instance->hba_lock, flags);
2574 rc = megasas_deplete_reply_queue(instance, DID_OK);
2575 spin_unlock_irqrestore(&instance->hba_lock, flags);
2577 return rc;
2581 * megasas_transition_to_ready - Move the FW to READY state
2582 * @instance: Adapter soft state
2584 * During the initialization, FW passes can potentially be in any one of
2585 * several possible states. If the FW in operational, waiting-for-handshake
2586 * states, driver must take steps to bring it to ready state. Otherwise, it
2587 * has to wait for the ready state.
2590 megasas_transition_to_ready(struct megasas_instance* instance)
2592 int i;
2593 u8 max_wait;
2594 u32 fw_state;
2595 u32 cur_state;
2596 u32 abs_state, curr_abs_state;
2598 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2600 if (fw_state != MFI_STATE_READY)
2601 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2602 " state\n");
2604 while (fw_state != MFI_STATE_READY) {
2606 abs_state =
2607 instance->instancet->read_fw_status_reg(instance->reg_set);
2609 switch (fw_state) {
2611 case MFI_STATE_FAULT:
2613 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2614 return -ENODEV;
2616 case MFI_STATE_WAIT_HANDSHAKE:
2618 * Set the CLR bit in inbound doorbell
2620 if ((instance->pdev->device ==
2621 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2622 (instance->pdev->device ==
2623 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2624 (instance->pdev->device ==
2625 PCI_DEVICE_ID_LSI_FUSION)) {
2626 writel(
2627 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2628 &instance->reg_set->doorbell);
2629 } else {
2630 writel(
2631 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2632 &instance->reg_set->inbound_doorbell);
2635 max_wait = MEGASAS_RESET_WAIT_TIME;
2636 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2637 break;
2639 case MFI_STATE_BOOT_MESSAGE_PENDING:
2640 if ((instance->pdev->device ==
2641 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2642 (instance->pdev->device ==
2643 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2644 (instance->pdev->device ==
2645 PCI_DEVICE_ID_LSI_FUSION)) {
2646 writel(MFI_INIT_HOTPLUG,
2647 &instance->reg_set->doorbell);
2648 } else
2649 writel(MFI_INIT_HOTPLUG,
2650 &instance->reg_set->inbound_doorbell);
2652 max_wait = MEGASAS_RESET_WAIT_TIME;
2653 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2654 break;
2656 case MFI_STATE_OPERATIONAL:
2658 * Bring it to READY state; assuming max wait 10 secs
2660 instance->instancet->disable_intr(instance->reg_set);
2661 if ((instance->pdev->device ==
2662 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2663 (instance->pdev->device ==
2664 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2665 (instance->pdev->device
2666 == PCI_DEVICE_ID_LSI_FUSION)) {
2667 writel(MFI_RESET_FLAGS,
2668 &instance->reg_set->doorbell);
2669 if (instance->pdev->device ==
2670 PCI_DEVICE_ID_LSI_FUSION) {
2671 for (i = 0; i < (10 * 1000); i += 20) {
2672 if (readl(
2673 &instance->
2674 reg_set->
2675 doorbell) & 1)
2676 msleep(20);
2677 else
2678 break;
2681 } else
2682 writel(MFI_RESET_FLAGS,
2683 &instance->reg_set->inbound_doorbell);
2685 max_wait = MEGASAS_RESET_WAIT_TIME;
2686 cur_state = MFI_STATE_OPERATIONAL;
2687 break;
2689 case MFI_STATE_UNDEFINED:
2691 * This state should not last for more than 2 seconds
2693 max_wait = MEGASAS_RESET_WAIT_TIME;
2694 cur_state = MFI_STATE_UNDEFINED;
2695 break;
2697 case MFI_STATE_BB_INIT:
2698 max_wait = MEGASAS_RESET_WAIT_TIME;
2699 cur_state = MFI_STATE_BB_INIT;
2700 break;
2702 case MFI_STATE_FW_INIT:
2703 max_wait = MEGASAS_RESET_WAIT_TIME;
2704 cur_state = MFI_STATE_FW_INIT;
2705 break;
2707 case MFI_STATE_FW_INIT_2:
2708 max_wait = MEGASAS_RESET_WAIT_TIME;
2709 cur_state = MFI_STATE_FW_INIT_2;
2710 break;
2712 case MFI_STATE_DEVICE_SCAN:
2713 max_wait = MEGASAS_RESET_WAIT_TIME;
2714 cur_state = MFI_STATE_DEVICE_SCAN;
2715 break;
2717 case MFI_STATE_FLUSH_CACHE:
2718 max_wait = MEGASAS_RESET_WAIT_TIME;
2719 cur_state = MFI_STATE_FLUSH_CACHE;
2720 break;
2722 default:
2723 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2724 fw_state);
2725 return -ENODEV;
2729 * The cur_state should not last for more than max_wait secs
2731 for (i = 0; i < (max_wait * 1000); i++) {
2732 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2733 MFI_STATE_MASK ;
2734 curr_abs_state =
2735 instance->instancet->read_fw_status_reg(instance->reg_set);
2737 if (abs_state == curr_abs_state) {
2738 msleep(1);
2739 } else
2740 break;
2744 * Return error if fw_state hasn't changed after max_wait
2746 if (curr_abs_state == abs_state) {
2747 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2748 "in %d secs\n", fw_state, max_wait);
2749 return -ENODEV;
2752 printk(KERN_INFO "megasas: FW now in Ready state\n");
2754 return 0;
2758 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
2759 * @instance: Adapter soft state
2761 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2763 int i;
2764 u32 max_cmd = instance->max_mfi_cmds;
2765 struct megasas_cmd *cmd;
2767 if (!instance->frame_dma_pool)
2768 return;
2771 * Return all frames to pool
2773 for (i = 0; i < max_cmd; i++) {
2775 cmd = instance->cmd_list[i];
2777 if (cmd->frame)
2778 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2779 cmd->frame_phys_addr);
2781 if (cmd->sense)
2782 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2783 cmd->sense_phys_addr);
2787 * Now destroy the pool itself
2789 pci_pool_destroy(instance->frame_dma_pool);
2790 pci_pool_destroy(instance->sense_dma_pool);
2792 instance->frame_dma_pool = NULL;
2793 instance->sense_dma_pool = NULL;
2797 * megasas_create_frame_pool - Creates DMA pool for cmd frames
2798 * @instance: Adapter soft state
2800 * Each command packet has an embedded DMA memory buffer that is used for
2801 * filling MFI frame and the SG list that immediately follows the frame. This
2802 * function creates those DMA memory buffers for each command packet by using
2803 * PCI pool facility.
2805 static int megasas_create_frame_pool(struct megasas_instance *instance)
2807 int i;
2808 u32 max_cmd;
2809 u32 sge_sz;
2810 u32 sgl_sz;
2811 u32 total_sz;
2812 u32 frame_count;
2813 struct megasas_cmd *cmd;
2815 max_cmd = instance->max_mfi_cmds;
2818 * Size of our frame is 64 bytes for MFI frame, followed by max SG
2819 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2821 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2822 sizeof(struct megasas_sge32);
2824 if (instance->flag_ieee) {
2825 sge_sz = sizeof(struct megasas_sge_skinny);
2829 * Calculated the number of 64byte frames required for SGL
2831 sgl_sz = sge_sz * instance->max_num_sge;
2832 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2833 frame_count = 15;
2836 * We need one extra frame for the MFI command
2838 frame_count++;
2840 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2842 * Use DMA pool facility provided by PCI layer
2844 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2845 instance->pdev, total_sz, 64,
2848 if (!instance->frame_dma_pool) {
2849 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2850 return -ENOMEM;
2853 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2854 instance->pdev, 128, 4, 0);
2856 if (!instance->sense_dma_pool) {
2857 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2859 pci_pool_destroy(instance->frame_dma_pool);
2860 instance->frame_dma_pool = NULL;
2862 return -ENOMEM;
2866 * Allocate and attach a frame to each of the commands in cmd_list.
2867 * By making cmd->index as the context instead of the &cmd, we can
2868 * always use 32bit context regardless of the architecture
2870 for (i = 0; i < max_cmd; i++) {
2872 cmd = instance->cmd_list[i];
2874 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2875 GFP_KERNEL, &cmd->frame_phys_addr);
2877 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2878 GFP_KERNEL, &cmd->sense_phys_addr);
2881 * megasas_teardown_frame_pool() takes care of freeing
2882 * whatever has been allocated
2884 if (!cmd->frame || !cmd->sense) {
2885 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2886 megasas_teardown_frame_pool(instance);
2887 return -ENOMEM;
2890 memset(cmd->frame, 0, total_sz);
2891 cmd->frame->io.context = cmd->index;
2892 cmd->frame->io.pad_0 = 0;
2895 return 0;
2899 * megasas_free_cmds - Free all the cmds in the free cmd pool
2900 * @instance: Adapter soft state
2902 void megasas_free_cmds(struct megasas_instance *instance)
2904 int i;
2905 /* First free the MFI frame pool */
2906 megasas_teardown_frame_pool(instance);
2908 /* Free all the commands in the cmd_list */
2909 for (i = 0; i < instance->max_mfi_cmds; i++)
2911 kfree(instance->cmd_list[i]);
2913 /* Free the cmd_list buffer itself */
2914 kfree(instance->cmd_list);
2915 instance->cmd_list = NULL;
2917 INIT_LIST_HEAD(&instance->cmd_pool);
2921 * megasas_alloc_cmds - Allocates the command packets
2922 * @instance: Adapter soft state
2924 * Each command that is issued to the FW, whether IO commands from the OS or
2925 * internal commands like IOCTLs, are wrapped in local data structure called
2926 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2927 * the FW.
2929 * Each frame has a 32-bit field called context (tag). This context is used
2930 * to get back the megasas_cmd from the frame when a frame gets completed in
2931 * the ISR. Typically the address of the megasas_cmd itself would be used as
2932 * the context. But we wanted to keep the differences between 32 and 64 bit
2933 * systems to the mininum. We always use 32 bit integers for the context. In
2934 * this driver, the 32 bit values are the indices into an array cmd_list.
2935 * This array is used only to look up the megasas_cmd given the context. The
2936 * free commands themselves are maintained in a linked list called cmd_pool.
2938 int megasas_alloc_cmds(struct megasas_instance *instance)
2940 int i;
2941 int j;
2942 u32 max_cmd;
2943 struct megasas_cmd *cmd;
2945 max_cmd = instance->max_mfi_cmds;
2948 * instance->cmd_list is an array of struct megasas_cmd pointers.
2949 * Allocate the dynamic array first and then allocate individual
2950 * commands.
2952 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2954 if (!instance->cmd_list) {
2955 printk(KERN_DEBUG "megasas: out of memory\n");
2956 return -ENOMEM;
2959 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
2961 for (i = 0; i < max_cmd; i++) {
2962 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2963 GFP_KERNEL);
2965 if (!instance->cmd_list[i]) {
2967 for (j = 0; j < i; j++)
2968 kfree(instance->cmd_list[j]);
2970 kfree(instance->cmd_list);
2971 instance->cmd_list = NULL;
2973 return -ENOMEM;
2978 * Add all the commands to command pool (instance->cmd_pool)
2980 for (i = 0; i < max_cmd; i++) {
2981 cmd = instance->cmd_list[i];
2982 memset(cmd, 0, sizeof(struct megasas_cmd));
2983 cmd->index = i;
2984 cmd->scmd = NULL;
2985 cmd->instance = instance;
2987 list_add_tail(&cmd->list, &instance->cmd_pool);
2991 * Create a frame pool and assign one frame to each cmd
2993 if (megasas_create_frame_pool(instance)) {
2994 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2995 megasas_free_cmds(instance);
2998 return 0;
3002 * megasas_get_pd_list_info - Returns FW's pd_list structure
3003 * @instance: Adapter soft state
3004 * @pd_list: pd_list structure
3006 * Issues an internal command (DCMD) to get the FW's controller PD
3007 * list structure. This information is mainly used to find out SYSTEM
3008 * supported by the FW.
3010 static int
3011 megasas_get_pd_list(struct megasas_instance *instance)
3013 int ret = 0, pd_index = 0;
3014 struct megasas_cmd *cmd;
3015 struct megasas_dcmd_frame *dcmd;
3016 struct MR_PD_LIST *ci;
3017 struct MR_PD_ADDRESS *pd_addr;
3018 dma_addr_t ci_h = 0;
3020 cmd = megasas_get_cmd(instance);
3022 if (!cmd) {
3023 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3024 return -ENOMEM;
3027 dcmd = &cmd->frame->dcmd;
3029 ci = pci_alloc_consistent(instance->pdev,
3030 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3032 if (!ci) {
3033 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3034 megasas_return_cmd(instance, cmd);
3035 return -ENOMEM;
3038 memset(ci, 0, sizeof(*ci));
3039 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3041 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3042 dcmd->mbox.b[1] = 0;
3043 dcmd->cmd = MFI_CMD_DCMD;
3044 dcmd->cmd_status = 0xFF;
3045 dcmd->sge_count = 1;
3046 dcmd->flags = MFI_FRAME_DIR_READ;
3047 dcmd->timeout = 0;
3048 dcmd->pad_0 = 0;
3049 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3050 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3051 dcmd->sgl.sge32[0].phys_addr = ci_h;
3052 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3054 if (!megasas_issue_polled(instance, cmd)) {
3055 ret = 0;
3056 } else {
3057 ret = -1;
3061 * the following function will get the instance PD LIST.
3064 pd_addr = ci->addr;
3066 if ( ret == 0 &&
3067 (ci->count <
3068 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3070 memset(instance->pd_list, 0,
3071 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3073 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3075 instance->pd_list[pd_addr->deviceId].tid =
3076 pd_addr->deviceId;
3077 instance->pd_list[pd_addr->deviceId].driveType =
3078 pd_addr->scsiDevType;
3079 instance->pd_list[pd_addr->deviceId].driveState =
3080 MR_PD_STATE_SYSTEM;
3081 pd_addr++;
3085 pci_free_consistent(instance->pdev,
3086 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3087 ci, ci_h);
3088 megasas_return_cmd(instance, cmd);
3090 return ret;
3094 * megasas_get_ld_list_info - Returns FW's ld_list structure
3095 * @instance: Adapter soft state
3096 * @ld_list: ld_list structure
3098 * Issues an internal command (DCMD) to get the FW's controller PD
3099 * list structure. This information is mainly used to find out SYSTEM
3100 * supported by the FW.
3102 static int
3103 megasas_get_ld_list(struct megasas_instance *instance)
3105 int ret = 0, ld_index = 0, ids = 0;
3106 struct megasas_cmd *cmd;
3107 struct megasas_dcmd_frame *dcmd;
3108 struct MR_LD_LIST *ci;
3109 dma_addr_t ci_h = 0;
3111 cmd = megasas_get_cmd(instance);
3113 if (!cmd) {
3114 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3115 return -ENOMEM;
3118 dcmd = &cmd->frame->dcmd;
3120 ci = pci_alloc_consistent(instance->pdev,
3121 sizeof(struct MR_LD_LIST),
3122 &ci_h);
3124 if (!ci) {
3125 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3126 megasas_return_cmd(instance, cmd);
3127 return -ENOMEM;
3130 memset(ci, 0, sizeof(*ci));
3131 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3133 dcmd->cmd = MFI_CMD_DCMD;
3134 dcmd->cmd_status = 0xFF;
3135 dcmd->sge_count = 1;
3136 dcmd->flags = MFI_FRAME_DIR_READ;
3137 dcmd->timeout = 0;
3138 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3139 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3140 dcmd->sgl.sge32[0].phys_addr = ci_h;
3141 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3142 dcmd->pad_0 = 0;
3144 if (!megasas_issue_polled(instance, cmd)) {
3145 ret = 0;
3146 } else {
3147 ret = -1;
3150 /* the following function will get the instance PD LIST */
3152 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3153 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3155 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3156 if (ci->ldList[ld_index].state != 0) {
3157 ids = ci->ldList[ld_index].ref.targetId;
3158 instance->ld_ids[ids] =
3159 ci->ldList[ld_index].ref.targetId;
3164 pci_free_consistent(instance->pdev,
3165 sizeof(struct MR_LD_LIST),
3167 ci_h);
3169 megasas_return_cmd(instance, cmd);
3170 return ret;
3174 * megasas_get_controller_info - Returns FW's controller structure
3175 * @instance: Adapter soft state
3176 * @ctrl_info: Controller information structure
3178 * Issues an internal command (DCMD) to get the FW's controller structure.
3179 * This information is mainly used to find out the maximum IO transfer per
3180 * command supported by the FW.
3182 static int
3183 megasas_get_ctrl_info(struct megasas_instance *instance,
3184 struct megasas_ctrl_info *ctrl_info)
3186 int ret = 0;
3187 struct megasas_cmd *cmd;
3188 struct megasas_dcmd_frame *dcmd;
3189 struct megasas_ctrl_info *ci;
3190 dma_addr_t ci_h = 0;
3192 cmd = megasas_get_cmd(instance);
3194 if (!cmd) {
3195 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3196 return -ENOMEM;
3199 dcmd = &cmd->frame->dcmd;
3201 ci = pci_alloc_consistent(instance->pdev,
3202 sizeof(struct megasas_ctrl_info), &ci_h);
3204 if (!ci) {
3205 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3206 megasas_return_cmd(instance, cmd);
3207 return -ENOMEM;
3210 memset(ci, 0, sizeof(*ci));
3211 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3213 dcmd->cmd = MFI_CMD_DCMD;
3214 dcmd->cmd_status = 0xFF;
3215 dcmd->sge_count = 1;
3216 dcmd->flags = MFI_FRAME_DIR_READ;
3217 dcmd->timeout = 0;
3218 dcmd->pad_0 = 0;
3219 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3220 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3221 dcmd->sgl.sge32[0].phys_addr = ci_h;
3222 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3224 if (!megasas_issue_polled(instance, cmd)) {
3225 ret = 0;
3226 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3227 } else {
3228 ret = -1;
3231 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3232 ci, ci_h);
3234 megasas_return_cmd(instance, cmd);
3235 return ret;
3239 * megasas_issue_init_mfi - Initializes the FW
3240 * @instance: Adapter soft state
3242 * Issues the INIT MFI cmd
3244 static int
3245 megasas_issue_init_mfi(struct megasas_instance *instance)
3247 u32 context;
3249 struct megasas_cmd *cmd;
3251 struct megasas_init_frame *init_frame;
3252 struct megasas_init_queue_info *initq_info;
3253 dma_addr_t init_frame_h;
3254 dma_addr_t initq_info_h;
3257 * Prepare a init frame. Note the init frame points to queue info
3258 * structure. Each frame has SGL allocated after first 64 bytes. For
3259 * this frame - since we don't need any SGL - we use SGL's space as
3260 * queue info structure
3262 * We will not get a NULL command below. We just created the pool.
3264 cmd = megasas_get_cmd(instance);
3266 init_frame = (struct megasas_init_frame *)cmd->frame;
3267 initq_info = (struct megasas_init_queue_info *)
3268 ((unsigned long)init_frame + 64);
3270 init_frame_h = cmd->frame_phys_addr;
3271 initq_info_h = init_frame_h + 64;
3273 context = init_frame->context;
3274 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3275 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3276 init_frame->context = context;
3278 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3279 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3281 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3282 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3284 init_frame->cmd = MFI_CMD_INIT;
3285 init_frame->cmd_status = 0xFF;
3286 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3288 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3291 * disable the intr before firing the init frame to FW
3293 instance->instancet->disable_intr(instance->reg_set);
3296 * Issue the init frame in polled mode
3299 if (megasas_issue_polled(instance, cmd)) {
3300 printk(KERN_ERR "megasas: Failed to init firmware\n");
3301 megasas_return_cmd(instance, cmd);
3302 goto fail_fw_init;
3305 megasas_return_cmd(instance, cmd);
3307 return 0;
3309 fail_fw_init:
3310 return -EINVAL;
3314 * megasas_start_timer - Initializes a timer object
3315 * @instance: Adapter soft state
3316 * @timer: timer object to be initialized
3317 * @fn: timer function
3318 * @interval: time interval between timer function call
3320 static inline void
3321 megasas_start_timer(struct megasas_instance *instance,
3322 struct timer_list *timer,
3323 void *fn, unsigned long interval)
3325 init_timer(timer);
3326 timer->expires = jiffies + interval;
3327 timer->data = (unsigned long)instance;
3328 timer->function = fn;
3329 add_timer(timer);
3333 * megasas_io_completion_timer - Timer fn
3334 * @instance_addr: Address of adapter soft state
3336 * Schedules tasklet for cmd completion
3337 * if poll_mode_io is set
3339 static void
3340 megasas_io_completion_timer(unsigned long instance_addr)
3342 struct megasas_instance *instance =
3343 (struct megasas_instance *)instance_addr;
3345 if (atomic_read(&instance->fw_outstanding))
3346 tasklet_schedule(&instance->isr_tasklet);
3348 /* Restart timer */
3349 if (poll_mode_io)
3350 mod_timer(&instance->io_completion_timer,
3351 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3354 static u32
3355 megasas_init_adapter_mfi(struct megasas_instance *instance)
3357 struct megasas_register_set __iomem *reg_set;
3358 u32 context_sz;
3359 u32 reply_q_sz;
3361 reg_set = instance->reg_set;
3364 * Get various operational parameters from status register
3366 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3368 * Reduce the max supported cmds by 1. This is to ensure that the
3369 * reply_q_sz (1 more than the max cmd that driver may send)
3370 * does not exceed max cmds that the FW can support
3372 instance->max_fw_cmds = instance->max_fw_cmds-1;
3373 instance->max_mfi_cmds = instance->max_fw_cmds;
3374 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3375 0x10;
3377 * Create a pool of commands
3379 if (megasas_alloc_cmds(instance))
3380 goto fail_alloc_cmds;
3383 * Allocate memory for reply queue. Length of reply queue should
3384 * be _one_ more than the maximum commands handled by the firmware.
3386 * Note: When FW completes commands, it places corresponding contex
3387 * values in this circular reply queue. This circular queue is a fairly
3388 * typical producer-consumer queue. FW is the producer (of completed
3389 * commands) and the driver is the consumer.
3391 context_sz = sizeof(u32);
3392 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3394 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3395 reply_q_sz,
3396 &instance->reply_queue_h);
3398 if (!instance->reply_queue) {
3399 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3400 goto fail_reply_queue;
3403 if (megasas_issue_init_mfi(instance))
3404 goto fail_fw_init;
3406 instance->fw_support_ieee = 0;
3407 instance->fw_support_ieee =
3408 (instance->instancet->read_fw_status_reg(reg_set) &
3409 0x04000000);
3411 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3412 instance->fw_support_ieee);
3414 if (instance->fw_support_ieee)
3415 instance->flag_ieee = 1;
3417 return 0;
3419 fail_fw_init:
3421 pci_free_consistent(instance->pdev, reply_q_sz,
3422 instance->reply_queue, instance->reply_queue_h);
3423 fail_reply_queue:
3424 megasas_free_cmds(instance);
3426 fail_alloc_cmds:
3427 return 1;
3431 * megasas_init_fw - Initializes the FW
3432 * @instance: Adapter soft state
3434 * This is the main function for initializing firmware
3437 static int megasas_init_fw(struct megasas_instance *instance)
3439 u32 max_sectors_1;
3440 u32 max_sectors_2;
3441 u32 tmp_sectors;
3442 struct megasas_register_set __iomem *reg_set;
3443 struct megasas_ctrl_info *ctrl_info;
3444 unsigned long bar_list;
3446 /* Find first memory bar */
3447 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3448 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3449 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3450 if (pci_request_selected_regions(instance->pdev, instance->bar,
3451 "megasas: LSI")) {
3452 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3453 return -EBUSY;
3456 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3458 if (!instance->reg_set) {
3459 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3460 goto fail_ioremap;
3463 reg_set = instance->reg_set;
3465 switch (instance->pdev->device) {
3466 case PCI_DEVICE_ID_LSI_FUSION:
3467 instance->instancet = &megasas_instance_template_fusion;
3468 break;
3469 case PCI_DEVICE_ID_LSI_SAS1078R:
3470 case PCI_DEVICE_ID_LSI_SAS1078DE:
3471 instance->instancet = &megasas_instance_template_ppc;
3472 break;
3473 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3474 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3475 instance->instancet = &megasas_instance_template_gen2;
3476 break;
3477 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3478 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3479 instance->instancet = &megasas_instance_template_skinny;
3480 break;
3481 case PCI_DEVICE_ID_LSI_SAS1064R:
3482 case PCI_DEVICE_ID_DELL_PERC5:
3483 default:
3484 instance->instancet = &megasas_instance_template_xscale;
3485 break;
3489 * We expect the FW state to be READY
3491 if (megasas_transition_to_ready(instance))
3492 goto fail_ready_state;
3494 /* Get operational params, sge flags, send init cmd to controller */
3495 if (instance->instancet->init_adapter(instance))
3496 goto fail_init_adapter;
3498 printk(KERN_ERR "megasas: INIT adapter done\n");
3500 /** for passthrough
3501 * the following function will get the PD LIST.
3504 memset(instance->pd_list, 0 ,
3505 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3506 megasas_get_pd_list(instance);
3508 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3509 megasas_get_ld_list(instance);
3511 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3514 * Compute the max allowed sectors per IO: The controller info has two
3515 * limits on max sectors. Driver should use the minimum of these two.
3517 * 1 << stripe_sz_ops.min = max sectors per strip
3519 * Note that older firmwares ( < FW ver 30) didn't report information
3520 * to calculate max_sectors_1. So the number ended up as zero always.
3522 tmp_sectors = 0;
3523 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3525 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3526 ctrl_info->max_strips_per_io;
3527 max_sectors_2 = ctrl_info->max_request_size;
3529 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3530 instance->disableOnlineCtrlReset =
3531 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3534 instance->max_sectors_per_req = instance->max_num_sge *
3535 PAGE_SIZE / 512;
3536 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3537 instance->max_sectors_per_req = tmp_sectors;
3539 kfree(ctrl_info);
3542 * Setup tasklet for cmd completion
3545 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3546 (unsigned long)instance);
3548 /* Initialize the cmd completion timer */
3549 if (poll_mode_io)
3550 megasas_start_timer(instance, &instance->io_completion_timer,
3551 megasas_io_completion_timer,
3552 MEGASAS_COMPLETION_TIMER_INTERVAL);
3553 return 0;
3555 fail_init_adapter:
3556 fail_ready_state:
3557 iounmap(instance->reg_set);
3559 fail_ioremap:
3560 pci_release_selected_regions(instance->pdev, instance->bar);
3562 return -EINVAL;
3566 * megasas_release_mfi - Reverses the FW initialization
3567 * @intance: Adapter soft state
3569 static void megasas_release_mfi(struct megasas_instance *instance)
3571 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3573 if (instance->reply_queue)
3574 pci_free_consistent(instance->pdev, reply_q_sz,
3575 instance->reply_queue, instance->reply_queue_h);
3577 megasas_free_cmds(instance);
3579 iounmap(instance->reg_set);
3581 pci_release_selected_regions(instance->pdev, instance->bar);
3585 * megasas_get_seq_num - Gets latest event sequence numbers
3586 * @instance: Adapter soft state
3587 * @eli: FW event log sequence numbers information
3589 * FW maintains a log of all events in a non-volatile area. Upper layers would
3590 * usually find out the latest sequence number of the events, the seq number at
3591 * the boot etc. They would "read" all the events below the latest seq number
3592 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3593 * number), they would subsribe to AEN (asynchronous event notification) and
3594 * wait for the events to happen.
3596 static int
3597 megasas_get_seq_num(struct megasas_instance *instance,
3598 struct megasas_evt_log_info *eli)
3600 struct megasas_cmd *cmd;
3601 struct megasas_dcmd_frame *dcmd;
3602 struct megasas_evt_log_info *el_info;
3603 dma_addr_t el_info_h = 0;
3605 cmd = megasas_get_cmd(instance);
3607 if (!cmd) {
3608 return -ENOMEM;
3611 dcmd = &cmd->frame->dcmd;
3612 el_info = pci_alloc_consistent(instance->pdev,
3613 sizeof(struct megasas_evt_log_info),
3614 &el_info_h);
3616 if (!el_info) {
3617 megasas_return_cmd(instance, cmd);
3618 return -ENOMEM;
3621 memset(el_info, 0, sizeof(*el_info));
3622 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3624 dcmd->cmd = MFI_CMD_DCMD;
3625 dcmd->cmd_status = 0x0;
3626 dcmd->sge_count = 1;
3627 dcmd->flags = MFI_FRAME_DIR_READ;
3628 dcmd->timeout = 0;
3629 dcmd->pad_0 = 0;
3630 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3631 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3632 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3633 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3635 megasas_issue_blocked_cmd(instance, cmd);
3638 * Copy the data back into callers buffer
3640 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3642 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3643 el_info, el_info_h);
3645 megasas_return_cmd(instance, cmd);
3647 return 0;
3651 * megasas_register_aen - Registers for asynchronous event notification
3652 * @instance: Adapter soft state
3653 * @seq_num: The starting sequence number
3654 * @class_locale: Class of the event
3656 * This function subscribes for AEN for events beyond the @seq_num. It requests
3657 * to be notified if and only if the event is of type @class_locale
3659 static int
3660 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3661 u32 class_locale_word)
3663 int ret_val;
3664 struct megasas_cmd *cmd;
3665 struct megasas_dcmd_frame *dcmd;
3666 union megasas_evt_class_locale curr_aen;
3667 union megasas_evt_class_locale prev_aen;
3670 * If there an AEN pending already (aen_cmd), check if the
3671 * class_locale of that pending AEN is inclusive of the new
3672 * AEN request we currently have. If it is, then we don't have
3673 * to do anything. In other words, whichever events the current
3674 * AEN request is subscribing to, have already been subscribed
3675 * to.
3677 * If the old_cmd is _not_ inclusive, then we have to abort
3678 * that command, form a class_locale that is superset of both
3679 * old and current and re-issue to the FW
3682 curr_aen.word = class_locale_word;
3684 if (instance->aen_cmd) {
3686 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3689 * A class whose enum value is smaller is inclusive of all
3690 * higher values. If a PROGRESS (= -1) was previously
3691 * registered, then a new registration requests for higher
3692 * classes need not be sent to FW. They are automatically
3693 * included.
3695 * Locale numbers don't have such hierarchy. They are bitmap
3696 * values
3698 if ((prev_aen.members.class <= curr_aen.members.class) &&
3699 !((prev_aen.members.locale & curr_aen.members.locale) ^
3700 curr_aen.members.locale)) {
3702 * Previously issued event registration includes
3703 * current request. Nothing to do.
3705 return 0;
3706 } else {
3707 curr_aen.members.locale |= prev_aen.members.locale;
3709 if (prev_aen.members.class < curr_aen.members.class)
3710 curr_aen.members.class = prev_aen.members.class;
3712 instance->aen_cmd->abort_aen = 1;
3713 ret_val = megasas_issue_blocked_abort_cmd(instance,
3714 instance->
3715 aen_cmd);
3717 if (ret_val) {
3718 printk(KERN_DEBUG "megasas: Failed to abort "
3719 "previous AEN command\n");
3720 return ret_val;
3725 cmd = megasas_get_cmd(instance);
3727 if (!cmd)
3728 return -ENOMEM;
3730 dcmd = &cmd->frame->dcmd;
3732 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3735 * Prepare DCMD for aen registration
3737 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3739 dcmd->cmd = MFI_CMD_DCMD;
3740 dcmd->cmd_status = 0x0;
3741 dcmd->sge_count = 1;
3742 dcmd->flags = MFI_FRAME_DIR_READ;
3743 dcmd->timeout = 0;
3744 dcmd->pad_0 = 0;
3745 instance->last_seq_num = seq_num;
3746 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3747 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3748 dcmd->mbox.w[0] = seq_num;
3749 dcmd->mbox.w[1] = curr_aen.word;
3750 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3751 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3753 if (instance->aen_cmd != NULL) {
3754 megasas_return_cmd(instance, cmd);
3755 return 0;
3759 * Store reference to the cmd used to register for AEN. When an
3760 * application wants us to register for AEN, we have to abort this
3761 * cmd and re-register with a new EVENT LOCALE supplied by that app
3763 instance->aen_cmd = cmd;
3766 * Issue the aen registration frame
3768 instance->instancet->issue_dcmd(instance, cmd);
3770 return 0;
3774 * megasas_start_aen - Subscribes to AEN during driver load time
3775 * @instance: Adapter soft state
3777 static int megasas_start_aen(struct megasas_instance *instance)
3779 struct megasas_evt_log_info eli;
3780 union megasas_evt_class_locale class_locale;
3783 * Get the latest sequence number from FW
3785 memset(&eli, 0, sizeof(eli));
3787 if (megasas_get_seq_num(instance, &eli))
3788 return -1;
3791 * Register AEN with FW for latest sequence number plus 1
3793 class_locale.members.reserved = 0;
3794 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3795 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3797 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3798 class_locale.word);
3802 * megasas_io_attach - Attaches this driver to SCSI mid-layer
3803 * @instance: Adapter soft state
3805 static int megasas_io_attach(struct megasas_instance *instance)
3807 struct Scsi_Host *host = instance->host;
3810 * Export parameters required by SCSI mid-layer
3812 host->irq = instance->pdev->irq;
3813 host->unique_id = instance->unique_id;
3814 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3815 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3816 host->can_queue =
3817 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3818 } else
3819 host->can_queue =
3820 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3821 host->this_id = instance->init_id;
3822 host->sg_tablesize = instance->max_num_sge;
3824 * Check if the module parameter value for max_sectors can be used
3826 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3827 instance->max_sectors_per_req = max_sectors;
3828 else {
3829 if (max_sectors) {
3830 if (((instance->pdev->device ==
3831 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3832 (instance->pdev->device ==
3833 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3834 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3835 instance->max_sectors_per_req = max_sectors;
3836 } else {
3837 printk(KERN_INFO "megasas: max_sectors should be > 0"
3838 "and <= %d (or < 1MB for GEN2 controller)\n",
3839 instance->max_sectors_per_req);
3844 host->max_sectors = instance->max_sectors_per_req;
3845 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3846 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3847 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3848 host->max_lun = MEGASAS_MAX_LUN;
3849 host->max_cmd_len = 16;
3851 /* Fusion only supports host reset */
3852 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) {
3853 host->hostt->eh_device_reset_handler = NULL;
3854 host->hostt->eh_bus_reset_handler = NULL;
3858 * Notify the mid-layer about the new controller
3860 if (scsi_add_host(host, &instance->pdev->dev)) {
3861 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3862 return -ENODEV;
3866 * Trigger SCSI to scan our drives
3868 scsi_scan_host(host);
3869 return 0;
3872 static int
3873 megasas_set_dma_mask(struct pci_dev *pdev)
3876 * All our contollers are capable of performing 64-bit DMA
3878 if (IS_DMA64) {
3879 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3881 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3882 goto fail_set_dma_mask;
3884 } else {
3885 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3886 goto fail_set_dma_mask;
3888 return 0;
3890 fail_set_dma_mask:
3891 return 1;
3895 * megasas_probe_one - PCI hotplug entry point
3896 * @pdev: PCI device structure
3897 * @id: PCI ids of supported hotplugged adapter
3899 static int __devinit
3900 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3902 int rval;
3903 struct Scsi_Host *host;
3904 struct megasas_instance *instance;
3907 * Announce PCI information
3909 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3910 pdev->vendor, pdev->device, pdev->subsystem_vendor,
3911 pdev->subsystem_device);
3913 printk("bus %d:slot %d:func %d\n",
3914 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3917 * PCI prepping: enable device set bus mastering and dma mask
3919 rval = pci_enable_device_mem(pdev);
3921 if (rval) {
3922 return rval;
3925 pci_set_master(pdev);
3927 if (megasas_set_dma_mask(pdev))
3928 goto fail_set_dma_mask;
3930 host = scsi_host_alloc(&megasas_template,
3931 sizeof(struct megasas_instance));
3933 if (!host) {
3934 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3935 goto fail_alloc_instance;
3938 instance = (struct megasas_instance *)host->hostdata;
3939 memset(instance, 0, sizeof(*instance));
3940 atomic_set( &instance->fw_reset_no_pci_access, 0 );
3941 instance->pdev = pdev;
3943 switch (instance->pdev->device) {
3944 case PCI_DEVICE_ID_LSI_FUSION:
3946 struct fusion_context *fusion;
3948 instance->ctrl_context =
3949 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
3950 if (!instance->ctrl_context) {
3951 printk(KERN_DEBUG "megasas: Failed to allocate "
3952 "memory for Fusion context info\n");
3953 goto fail_alloc_dma_buf;
3955 fusion = instance->ctrl_context;
3956 INIT_LIST_HEAD(&fusion->cmd_pool);
3957 spin_lock_init(&fusion->cmd_pool_lock);
3959 break;
3960 default: /* For all other supported controllers */
3962 instance->producer =
3963 pci_alloc_consistent(pdev, sizeof(u32),
3964 &instance->producer_h);
3965 instance->consumer =
3966 pci_alloc_consistent(pdev, sizeof(u32),
3967 &instance->consumer_h);
3969 if (!instance->producer || !instance->consumer) {
3970 printk(KERN_DEBUG "megasas: Failed to allocate"
3971 "memory for producer, consumer\n");
3972 goto fail_alloc_dma_buf;
3975 *instance->producer = 0;
3976 *instance->consumer = 0;
3977 break;
3980 megasas_poll_wait_aen = 0;
3981 instance->flag_ieee = 0;
3982 instance->ev = NULL;
3983 instance->issuepend_done = 1;
3984 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3985 megasas_poll_wait_aen = 0;
3987 instance->evt_detail = pci_alloc_consistent(pdev,
3988 sizeof(struct
3989 megasas_evt_detail),
3990 &instance->evt_detail_h);
3992 if (!instance->evt_detail) {
3993 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3994 "event detail structure\n");
3995 goto fail_alloc_dma_buf;
3999 * Initialize locks and queues
4001 INIT_LIST_HEAD(&instance->cmd_pool);
4002 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4004 atomic_set(&instance->fw_outstanding,0);
4006 init_waitqueue_head(&instance->int_cmd_wait_q);
4007 init_waitqueue_head(&instance->abort_cmd_wait_q);
4009 spin_lock_init(&instance->cmd_pool_lock);
4010 spin_lock_init(&instance->hba_lock);
4011 spin_lock_init(&instance->completion_lock);
4012 spin_lock_init(&poll_aen_lock);
4014 mutex_init(&instance->aen_mutex);
4015 mutex_init(&instance->reset_mutex);
4018 * Initialize PCI related and misc parameters
4020 instance->host = host;
4021 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4022 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4024 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4025 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4026 instance->flag_ieee = 1;
4027 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4028 } else
4029 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4031 megasas_dbg_lvl = 0;
4032 instance->flag = 0;
4033 instance->unload = 1;
4034 instance->last_time = 0;
4035 instance->disableOnlineCtrlReset = 1;
4037 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4038 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4039 else
4040 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4043 * Initialize MFI Firmware
4045 if (megasas_init_fw(instance))
4046 goto fail_init_mfi;
4048 /* Try to enable MSI-X */
4049 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078R) &&
4050 (instance->pdev->device != PCI_DEVICE_ID_LSI_SAS1078DE) &&
4051 (instance->pdev->device != PCI_DEVICE_ID_LSI_VERDE_ZCR) &&
4052 !msix_disable && !pci_enable_msix(instance->pdev,
4053 &instance->msixentry, 1))
4054 instance->msi_flag = 1;
4057 * Register IRQ
4059 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
4060 pdev->irq, instance->instancet->service_isr,
4061 IRQF_SHARED, "megasas", instance)) {
4062 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4063 goto fail_irq;
4066 instance->instancet->enable_intr(instance->reg_set);
4069 * Store instance in PCI softstate
4071 pci_set_drvdata(pdev, instance);
4074 * Add this controller to megasas_mgmt_info structure so that it
4075 * can be exported to management applications
4077 megasas_mgmt_info.count++;
4078 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4079 megasas_mgmt_info.max_index++;
4082 * Initiate AEN (Asynchronous Event Notification)
4084 if (megasas_start_aen(instance)) {
4085 printk(KERN_DEBUG "megasas: start aen failed\n");
4086 goto fail_start_aen;
4090 * Register with SCSI mid-layer
4092 if (megasas_io_attach(instance))
4093 goto fail_io_attach;
4095 instance->unload = 0;
4096 return 0;
4098 fail_start_aen:
4099 fail_io_attach:
4100 megasas_mgmt_info.count--;
4101 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4102 megasas_mgmt_info.max_index--;
4104 pci_set_drvdata(pdev, NULL);
4105 instance->instancet->disable_intr(instance->reg_set);
4106 free_irq(instance->msi_flag ? instance->msixentry.vector :
4107 instance->pdev->irq, instance);
4108 fail_irq:
4109 if (instance->msi_flag)
4110 pci_disable_msix(instance->pdev);
4111 if (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION)
4112 megasas_release_fusion(instance);
4113 else
4114 megasas_release_mfi(instance);
4115 fail_init_mfi:
4116 fail_alloc_dma_buf:
4117 if (instance->evt_detail)
4118 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4119 instance->evt_detail,
4120 instance->evt_detail_h);
4122 if (instance->producer)
4123 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4124 instance->producer_h);
4125 if (instance->consumer)
4126 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4127 instance->consumer_h);
4128 scsi_host_put(host);
4130 fail_alloc_instance:
4131 fail_set_dma_mask:
4132 pci_disable_device(pdev);
4134 return -ENODEV;
4138 * megasas_flush_cache - Requests FW to flush all its caches
4139 * @instance: Adapter soft state
4141 static void megasas_flush_cache(struct megasas_instance *instance)
4143 struct megasas_cmd *cmd;
4144 struct megasas_dcmd_frame *dcmd;
4146 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4147 return;
4149 cmd = megasas_get_cmd(instance);
4151 if (!cmd)
4152 return;
4154 dcmd = &cmd->frame->dcmd;
4156 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4158 dcmd->cmd = MFI_CMD_DCMD;
4159 dcmd->cmd_status = 0x0;
4160 dcmd->sge_count = 0;
4161 dcmd->flags = MFI_FRAME_DIR_NONE;
4162 dcmd->timeout = 0;
4163 dcmd->pad_0 = 0;
4164 dcmd->data_xfer_len = 0;
4165 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4166 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4168 megasas_issue_blocked_cmd(instance, cmd);
4170 megasas_return_cmd(instance, cmd);
4172 return;
4176 * megasas_shutdown_controller - Instructs FW to shutdown the controller
4177 * @instance: Adapter soft state
4178 * @opcode: Shutdown/Hibernate
4180 static void megasas_shutdown_controller(struct megasas_instance *instance,
4181 u32 opcode)
4183 struct megasas_cmd *cmd;
4184 struct megasas_dcmd_frame *dcmd;
4186 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4187 return;
4189 cmd = megasas_get_cmd(instance);
4191 if (!cmd)
4192 return;
4194 if (instance->aen_cmd)
4195 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4196 if (instance->map_update_cmd)
4197 megasas_issue_blocked_abort_cmd(instance,
4198 instance->map_update_cmd);
4199 dcmd = &cmd->frame->dcmd;
4201 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4203 dcmd->cmd = MFI_CMD_DCMD;
4204 dcmd->cmd_status = 0x0;
4205 dcmd->sge_count = 0;
4206 dcmd->flags = MFI_FRAME_DIR_NONE;
4207 dcmd->timeout = 0;
4208 dcmd->pad_0 = 0;
4209 dcmd->data_xfer_len = 0;
4210 dcmd->opcode = opcode;
4212 megasas_issue_blocked_cmd(instance, cmd);
4214 megasas_return_cmd(instance, cmd);
4216 return;
4219 #ifdef CONFIG_PM
4221 * megasas_suspend - driver suspend entry point
4222 * @pdev: PCI device structure
4223 * @state: PCI power state to suspend routine
4225 static int
4226 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4228 struct Scsi_Host *host;
4229 struct megasas_instance *instance;
4231 instance = pci_get_drvdata(pdev);
4232 host = instance->host;
4233 instance->unload = 1;
4235 if (poll_mode_io)
4236 del_timer_sync(&instance->io_completion_timer);
4238 megasas_flush_cache(instance);
4239 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4241 /* cancel the delayed work if this work still in queue */
4242 if (instance->ev != NULL) {
4243 struct megasas_aen_event *ev = instance->ev;
4244 cancel_delayed_work_sync(
4245 (struct delayed_work *)&ev->hotplug_work);
4246 instance->ev = NULL;
4249 tasklet_kill(&instance->isr_tasklet);
4251 pci_set_drvdata(instance->pdev, instance);
4252 instance->instancet->disable_intr(instance->reg_set);
4253 free_irq(instance->msi_flag ? instance->msixentry.vector :
4254 instance->pdev->irq, instance);
4255 if (instance->msi_flag)
4256 pci_disable_msix(instance->pdev);
4258 pci_save_state(pdev);
4259 pci_disable_device(pdev);
4261 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4263 return 0;
4267 * megasas_resume- driver resume entry point
4268 * @pdev: PCI device structure
4270 static int
4271 megasas_resume(struct pci_dev *pdev)
4273 int rval;
4274 struct Scsi_Host *host;
4275 struct megasas_instance *instance;
4277 instance = pci_get_drvdata(pdev);
4278 host = instance->host;
4279 pci_set_power_state(pdev, PCI_D0);
4280 pci_enable_wake(pdev, PCI_D0, 0);
4281 pci_restore_state(pdev);
4284 * PCI prepping: enable device set bus mastering and dma mask
4286 rval = pci_enable_device_mem(pdev);
4288 if (rval) {
4289 printk(KERN_ERR "megasas: Enable device failed\n");
4290 return rval;
4293 pci_set_master(pdev);
4295 if (megasas_set_dma_mask(pdev))
4296 goto fail_set_dma_mask;
4299 * Initialize MFI Firmware
4302 atomic_set(&instance->fw_outstanding, 0);
4305 * We expect the FW state to be READY
4307 if (megasas_transition_to_ready(instance))
4308 goto fail_ready_state;
4310 switch (instance->pdev->device) {
4311 case PCI_DEVICE_ID_LSI_FUSION:
4313 megasas_reset_reply_desc(instance);
4314 if (megasas_ioc_init_fusion(instance)) {
4315 megasas_free_cmds(instance);
4316 megasas_free_cmds_fusion(instance);
4317 goto fail_init_mfi;
4319 if (!megasas_get_map_info(instance))
4320 megasas_sync_map_info(instance);
4322 break;
4323 default:
4324 *instance->producer = 0;
4325 *instance->consumer = 0;
4326 if (megasas_issue_init_mfi(instance))
4327 goto fail_init_mfi;
4328 break;
4331 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4332 (unsigned long)instance);
4334 /* Now re-enable MSI-X */
4335 if (instance->msi_flag)
4336 pci_enable_msix(instance->pdev, &instance->msixentry, 1);
4339 * Register IRQ
4341 if (request_irq(instance->msi_flag ? instance->msixentry.vector :
4342 pdev->irq, instance->instancet->service_isr,
4343 IRQF_SHARED, "megasas", instance)) {
4344 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4345 goto fail_irq;
4348 instance->instancet->enable_intr(instance->reg_set);
4351 * Initiate AEN (Asynchronous Event Notification)
4353 if (megasas_start_aen(instance))
4354 printk(KERN_ERR "megasas: Start AEN failed\n");
4356 /* Initialize the cmd completion timer */
4357 if (poll_mode_io)
4358 megasas_start_timer(instance, &instance->io_completion_timer,
4359 megasas_io_completion_timer,
4360 MEGASAS_COMPLETION_TIMER_INTERVAL);
4361 instance->unload = 0;
4363 return 0;
4365 fail_irq:
4366 fail_init_mfi:
4367 if (instance->evt_detail)
4368 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4369 instance->evt_detail,
4370 instance->evt_detail_h);
4372 if (instance->producer)
4373 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4374 instance->producer_h);
4375 if (instance->consumer)
4376 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4377 instance->consumer_h);
4378 scsi_host_put(host);
4380 fail_set_dma_mask:
4381 fail_ready_state:
4383 pci_disable_device(pdev);
4385 return -ENODEV;
4387 #else
4388 #define megasas_suspend NULL
4389 #define megasas_resume NULL
4390 #endif
4393 * megasas_detach_one - PCI hot"un"plug entry point
4394 * @pdev: PCI device structure
4396 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4398 int i;
4399 struct Scsi_Host *host;
4400 struct megasas_instance *instance;
4401 struct fusion_context *fusion;
4403 instance = pci_get_drvdata(pdev);
4404 instance->unload = 1;
4405 host = instance->host;
4406 fusion = instance->ctrl_context;
4408 if (poll_mode_io)
4409 del_timer_sync(&instance->io_completion_timer);
4411 scsi_remove_host(instance->host);
4412 megasas_flush_cache(instance);
4413 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4415 /* cancel the delayed work if this work still in queue*/
4416 if (instance->ev != NULL) {
4417 struct megasas_aen_event *ev = instance->ev;
4418 cancel_delayed_work_sync(
4419 (struct delayed_work *)&ev->hotplug_work);
4420 instance->ev = NULL;
4423 tasklet_kill(&instance->isr_tasklet);
4426 * Take the instance off the instance array. Note that we will not
4427 * decrement the max_index. We let this array be sparse array
4429 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4430 if (megasas_mgmt_info.instance[i] == instance) {
4431 megasas_mgmt_info.count--;
4432 megasas_mgmt_info.instance[i] = NULL;
4434 break;
4438 pci_set_drvdata(instance->pdev, NULL);
4440 instance->instancet->disable_intr(instance->reg_set);
4442 free_irq(instance->msi_flag ? instance->msixentry.vector :
4443 instance->pdev->irq, instance);
4444 if (instance->msi_flag)
4445 pci_disable_msix(instance->pdev);
4447 switch (instance->pdev->device) {
4448 case PCI_DEVICE_ID_LSI_FUSION:
4449 megasas_release_fusion(instance);
4450 for (i = 0; i < 2 ; i++)
4451 if (fusion->ld_map[i])
4452 dma_free_coherent(&instance->pdev->dev,
4453 fusion->map_sz,
4454 fusion->ld_map[i],
4455 fusion->
4456 ld_map_phys[i]);
4457 kfree(instance->ctrl_context);
4458 break;
4459 default:
4460 megasas_release_mfi(instance);
4461 pci_free_consistent(pdev,
4462 sizeof(struct megasas_evt_detail),
4463 instance->evt_detail,
4464 instance->evt_detail_h);
4465 pci_free_consistent(pdev, sizeof(u32),
4466 instance->producer,
4467 instance->producer_h);
4468 pci_free_consistent(pdev, sizeof(u32),
4469 instance->consumer,
4470 instance->consumer_h);
4471 break;
4474 scsi_host_put(host);
4476 pci_set_drvdata(pdev, NULL);
4478 pci_disable_device(pdev);
4480 return;
4484 * megasas_shutdown - Shutdown entry point
4485 * @device: Generic device structure
4487 static void megasas_shutdown(struct pci_dev *pdev)
4489 struct megasas_instance *instance = pci_get_drvdata(pdev);
4490 instance->unload = 1;
4491 megasas_flush_cache(instance);
4492 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4496 * megasas_mgmt_open - char node "open" entry point
4498 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4501 * Allow only those users with admin rights
4503 if (!capable(CAP_SYS_ADMIN))
4504 return -EACCES;
4506 return 0;
4510 * megasas_mgmt_fasync - Async notifier registration from applications
4512 * This function adds the calling process to a driver global queue. When an
4513 * event occurs, SIGIO will be sent to all processes in this queue.
4515 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4517 int rc;
4519 mutex_lock(&megasas_async_queue_mutex);
4521 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4523 mutex_unlock(&megasas_async_queue_mutex);
4525 if (rc >= 0) {
4526 /* For sanity check when we get ioctl */
4527 filep->private_data = filep;
4528 return 0;
4531 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4533 return rc;
4537 * megasas_mgmt_poll - char node "poll" entry point
4538 * */
4539 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4541 unsigned int mask;
4542 unsigned long flags;
4543 poll_wait(file, &megasas_poll_wait, wait);
4544 spin_lock_irqsave(&poll_aen_lock, flags);
4545 if (megasas_poll_wait_aen)
4546 mask = (POLLIN | POLLRDNORM);
4547 else
4548 mask = 0;
4549 spin_unlock_irqrestore(&poll_aen_lock, flags);
4550 return mask;
4554 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
4555 * @instance: Adapter soft state
4556 * @argp: User's ioctl packet
4558 static int
4559 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4560 struct megasas_iocpacket __user * user_ioc,
4561 struct megasas_iocpacket *ioc)
4563 struct megasas_sge32 *kern_sge32;
4564 struct megasas_cmd *cmd;
4565 void *kbuff_arr[MAX_IOCTL_SGE];
4566 dma_addr_t buf_handle = 0;
4567 int error = 0, i;
4568 void *sense = NULL;
4569 dma_addr_t sense_handle;
4570 unsigned long *sense_ptr;
4572 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4574 if (ioc->sge_count > MAX_IOCTL_SGE) {
4575 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4576 ioc->sge_count, MAX_IOCTL_SGE);
4577 return -EINVAL;
4580 cmd = megasas_get_cmd(instance);
4581 if (!cmd) {
4582 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4583 return -ENOMEM;
4587 * User's IOCTL packet has 2 frames (maximum). Copy those two
4588 * frames into our cmd's frames. cmd->frame's context will get
4589 * overwritten when we copy from user's frames. So set that value
4590 * alone separately
4592 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4593 cmd->frame->hdr.context = cmd->index;
4594 cmd->frame->hdr.pad_0 = 0;
4597 * The management interface between applications and the fw uses
4598 * MFI frames. E.g, RAID configuration changes, LD property changes
4599 * etc are accomplishes through different kinds of MFI frames. The
4600 * driver needs to care only about substituting user buffers with
4601 * kernel buffers in SGLs. The location of SGL is embedded in the
4602 * struct iocpacket itself.
4604 kern_sge32 = (struct megasas_sge32 *)
4605 ((unsigned long)cmd->frame + ioc->sgl_off);
4608 * For each user buffer, create a mirror buffer and copy in
4610 for (i = 0; i < ioc->sge_count; i++) {
4611 if (!ioc->sgl[i].iov_len)
4612 continue;
4614 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4615 ioc->sgl[i].iov_len,
4616 &buf_handle, GFP_KERNEL);
4617 if (!kbuff_arr[i]) {
4618 printk(KERN_DEBUG "megasas: Failed to alloc "
4619 "kernel SGL buffer for IOCTL \n");
4620 error = -ENOMEM;
4621 goto out;
4625 * We don't change the dma_coherent_mask, so
4626 * pci_alloc_consistent only returns 32bit addresses
4628 kern_sge32[i].phys_addr = (u32) buf_handle;
4629 kern_sge32[i].length = ioc->sgl[i].iov_len;
4632 * We created a kernel buffer corresponding to the
4633 * user buffer. Now copy in from the user buffer
4635 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4636 (u32) (ioc->sgl[i].iov_len))) {
4637 error = -EFAULT;
4638 goto out;
4642 if (ioc->sense_len) {
4643 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4644 &sense_handle, GFP_KERNEL);
4645 if (!sense) {
4646 error = -ENOMEM;
4647 goto out;
4650 sense_ptr =
4651 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4652 *sense_ptr = sense_handle;
4656 * Set the sync_cmd flag so that the ISR knows not to complete this
4657 * cmd to the SCSI mid-layer
4659 cmd->sync_cmd = 1;
4660 megasas_issue_blocked_cmd(instance, cmd);
4661 cmd->sync_cmd = 0;
4664 * copy out the kernel buffers to user buffers
4666 for (i = 0; i < ioc->sge_count; i++) {
4667 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4668 ioc->sgl[i].iov_len)) {
4669 error = -EFAULT;
4670 goto out;
4675 * copy out the sense
4677 if (ioc->sense_len) {
4679 * sense_ptr points to the location that has the user
4680 * sense buffer address
4682 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4683 ioc->sense_off);
4685 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4686 sense, ioc->sense_len)) {
4687 printk(KERN_ERR "megasas: Failed to copy out to user "
4688 "sense data\n");
4689 error = -EFAULT;
4690 goto out;
4695 * copy the status codes returned by the fw
4697 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4698 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4699 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4700 error = -EFAULT;
4703 out:
4704 if (sense) {
4705 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4706 sense, sense_handle);
4709 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4710 dma_free_coherent(&instance->pdev->dev,
4711 kern_sge32[i].length,
4712 kbuff_arr[i], kern_sge32[i].phys_addr);
4715 megasas_return_cmd(instance, cmd);
4716 return error;
4719 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4721 struct megasas_iocpacket __user *user_ioc =
4722 (struct megasas_iocpacket __user *)arg;
4723 struct megasas_iocpacket *ioc;
4724 struct megasas_instance *instance;
4725 int error;
4726 int i;
4727 unsigned long flags;
4728 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4730 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4731 if (!ioc)
4732 return -ENOMEM;
4734 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4735 error = -EFAULT;
4736 goto out_kfree_ioc;
4739 instance = megasas_lookup_instance(ioc->host_no);
4740 if (!instance) {
4741 error = -ENODEV;
4742 goto out_kfree_ioc;
4745 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4746 printk(KERN_ERR "Controller in crit error\n");
4747 error = -ENODEV;
4748 goto out_kfree_ioc;
4751 if (instance->unload == 1) {
4752 error = -ENODEV;
4753 goto out_kfree_ioc;
4757 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4759 if (down_interruptible(&instance->ioctl_sem)) {
4760 error = -ERESTARTSYS;
4761 goto out_kfree_ioc;
4764 for (i = 0; i < wait_time; i++) {
4766 spin_lock_irqsave(&instance->hba_lock, flags);
4767 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4768 spin_unlock_irqrestore(&instance->hba_lock, flags);
4769 break;
4771 spin_unlock_irqrestore(&instance->hba_lock, flags);
4773 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4774 printk(KERN_NOTICE "megasas: waiting"
4775 "for controller reset to finish\n");
4778 msleep(1000);
4781 spin_lock_irqsave(&instance->hba_lock, flags);
4782 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4783 spin_unlock_irqrestore(&instance->hba_lock, flags);
4785 printk(KERN_ERR "megaraid_sas: timed out while"
4786 "waiting for HBA to recover\n");
4787 error = -ENODEV;
4788 goto out_kfree_ioc;
4790 spin_unlock_irqrestore(&instance->hba_lock, flags);
4792 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4793 up(&instance->ioctl_sem);
4795 out_kfree_ioc:
4796 kfree(ioc);
4797 return error;
4800 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4802 struct megasas_instance *instance;
4803 struct megasas_aen aen;
4804 int error;
4805 int i;
4806 unsigned long flags;
4807 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4809 if (file->private_data != file) {
4810 printk(KERN_DEBUG "megasas: fasync_helper was not "
4811 "called first\n");
4812 return -EINVAL;
4815 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4816 return -EFAULT;
4818 instance = megasas_lookup_instance(aen.host_no);
4820 if (!instance)
4821 return -ENODEV;
4823 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4824 return -ENODEV;
4827 if (instance->unload == 1) {
4828 return -ENODEV;
4831 for (i = 0; i < wait_time; i++) {
4833 spin_lock_irqsave(&instance->hba_lock, flags);
4834 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4835 spin_unlock_irqrestore(&instance->hba_lock,
4836 flags);
4837 break;
4840 spin_unlock_irqrestore(&instance->hba_lock, flags);
4842 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4843 printk(KERN_NOTICE "megasas: waiting for"
4844 "controller reset to finish\n");
4847 msleep(1000);
4850 spin_lock_irqsave(&instance->hba_lock, flags);
4851 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4852 spin_unlock_irqrestore(&instance->hba_lock, flags);
4853 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4854 "for HBA to recover.\n");
4855 return -ENODEV;
4857 spin_unlock_irqrestore(&instance->hba_lock, flags);
4859 mutex_lock(&instance->aen_mutex);
4860 error = megasas_register_aen(instance, aen.seq_num,
4861 aen.class_locale_word);
4862 mutex_unlock(&instance->aen_mutex);
4863 return error;
4867 * megasas_mgmt_ioctl - char node ioctl entry point
4869 static long
4870 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4872 switch (cmd) {
4873 case MEGASAS_IOC_FIRMWARE:
4874 return megasas_mgmt_ioctl_fw(file, arg);
4876 case MEGASAS_IOC_GET_AEN:
4877 return megasas_mgmt_ioctl_aen(file, arg);
4880 return -ENOTTY;
4883 #ifdef CONFIG_COMPAT
4884 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4886 struct compat_megasas_iocpacket __user *cioc =
4887 (struct compat_megasas_iocpacket __user *)arg;
4888 struct megasas_iocpacket __user *ioc =
4889 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4890 int i;
4891 int error = 0;
4892 compat_uptr_t ptr;
4894 if (clear_user(ioc, sizeof(*ioc)))
4895 return -EFAULT;
4897 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4898 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4899 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4900 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4901 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4902 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4903 return -EFAULT;
4906 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4907 * sense_len is not null, so prepare the 64bit value under
4908 * the same condition.
4910 if (ioc->sense_len) {
4911 void __user **sense_ioc_ptr =
4912 (void __user **)(ioc->frame.raw + ioc->sense_off);
4913 compat_uptr_t *sense_cioc_ptr =
4914 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4915 if (get_user(ptr, sense_cioc_ptr) ||
4916 put_user(compat_ptr(ptr), sense_ioc_ptr))
4917 return -EFAULT;
4920 for (i = 0; i < MAX_IOCTL_SGE; i++) {
4921 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4922 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4923 copy_in_user(&ioc->sgl[i].iov_len,
4924 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4925 return -EFAULT;
4928 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4930 if (copy_in_user(&cioc->frame.hdr.cmd_status,
4931 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4932 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4933 return -EFAULT;
4935 return error;
4938 static long
4939 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4940 unsigned long arg)
4942 switch (cmd) {
4943 case MEGASAS_IOC_FIRMWARE32:
4944 return megasas_mgmt_compat_ioctl_fw(file, arg);
4945 case MEGASAS_IOC_GET_AEN:
4946 return megasas_mgmt_ioctl_aen(file, arg);
4949 return -ENOTTY;
4951 #endif
4954 * File operations structure for management interface
4956 static const struct file_operations megasas_mgmt_fops = {
4957 .owner = THIS_MODULE,
4958 .open = megasas_mgmt_open,
4959 .fasync = megasas_mgmt_fasync,
4960 .unlocked_ioctl = megasas_mgmt_ioctl,
4961 .poll = megasas_mgmt_poll,
4962 #ifdef CONFIG_COMPAT
4963 .compat_ioctl = megasas_mgmt_compat_ioctl,
4964 #endif
4965 .llseek = noop_llseek,
4969 * PCI hotplug support registration structure
4971 static struct pci_driver megasas_pci_driver = {
4973 .name = "megaraid_sas",
4974 .id_table = megasas_pci_table,
4975 .probe = megasas_probe_one,
4976 .remove = __devexit_p(megasas_detach_one),
4977 .suspend = megasas_suspend,
4978 .resume = megasas_resume,
4979 .shutdown = megasas_shutdown,
4983 * Sysfs driver attributes
4985 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4987 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4988 MEGASAS_VERSION);
4991 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4993 static ssize_t
4994 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4996 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4997 MEGASAS_RELDATE);
5000 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5001 NULL);
5003 static ssize_t
5004 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5006 return sprintf(buf, "%u\n", support_poll_for_event);
5009 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5010 megasas_sysfs_show_support_poll_for_event, NULL);
5012 static ssize_t
5013 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5015 return sprintf(buf, "%u\n", support_device_change);
5018 static DRIVER_ATTR(support_device_change, S_IRUGO,
5019 megasas_sysfs_show_support_device_change, NULL);
5021 static ssize_t
5022 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5024 return sprintf(buf, "%u\n", megasas_dbg_lvl);
5027 static ssize_t
5028 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5030 int retval = count;
5031 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5032 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5033 retval = -EINVAL;
5035 return retval;
5038 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
5039 megasas_sysfs_set_dbg_lvl);
5041 static ssize_t
5042 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
5044 return sprintf(buf, "%u\n", poll_mode_io);
5047 static ssize_t
5048 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
5049 const char *buf, size_t count)
5051 int retval = count;
5052 int tmp = poll_mode_io;
5053 int i;
5054 struct megasas_instance *instance;
5056 if (sscanf(buf, "%u", &poll_mode_io) < 1) {
5057 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
5058 retval = -EINVAL;
5062 * Check if poll_mode_io is already set or is same as previous value
5064 if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
5065 goto out;
5067 if (poll_mode_io) {
5069 * Start timers for all adapters
5071 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5072 instance = megasas_mgmt_info.instance[i];
5073 if (instance) {
5074 megasas_start_timer(instance,
5075 &instance->io_completion_timer,
5076 megasas_io_completion_timer,
5077 MEGASAS_COMPLETION_TIMER_INTERVAL);
5080 } else {
5082 * Delete timers for all adapters
5084 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5085 instance = megasas_mgmt_info.instance[i];
5086 if (instance)
5087 del_timer_sync(&instance->io_completion_timer);
5091 out:
5092 return retval;
5095 static void
5096 megasas_aen_polling(struct work_struct *work)
5098 struct megasas_aen_event *ev =
5099 container_of(work, struct megasas_aen_event, hotplug_work);
5100 struct megasas_instance *instance = ev->instance;
5101 union megasas_evt_class_locale class_locale;
5102 struct Scsi_Host *host;
5103 struct scsi_device *sdev1;
5104 u16 pd_index = 0;
5105 u16 ld_index = 0;
5106 int i, j, doscan = 0;
5107 u32 seq_num;
5108 int error;
5110 if (!instance) {
5111 printk(KERN_ERR "invalid instance!\n");
5112 kfree(ev);
5113 return;
5115 instance->ev = NULL;
5116 host = instance->host;
5117 if (instance->evt_detail) {
5119 switch (instance->evt_detail->code) {
5120 case MR_EVT_PD_INSERTED:
5121 if (megasas_get_pd_list(instance) == 0) {
5122 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5123 for (j = 0;
5124 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5125 j++) {
5127 pd_index =
5128 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5130 sdev1 =
5131 scsi_device_lookup(host, i, j, 0);
5133 if (instance->pd_list[pd_index].driveState
5134 == MR_PD_STATE_SYSTEM) {
5135 if (!sdev1) {
5136 scsi_add_device(host, i, j, 0);
5139 if (sdev1)
5140 scsi_device_put(sdev1);
5145 doscan = 0;
5146 break;
5148 case MR_EVT_PD_REMOVED:
5149 if (megasas_get_pd_list(instance) == 0) {
5150 megasas_get_pd_list(instance);
5151 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5152 for (j = 0;
5153 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5154 j++) {
5156 pd_index =
5157 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5159 sdev1 =
5160 scsi_device_lookup(host, i, j, 0);
5162 if (instance->pd_list[pd_index].driveState
5163 == MR_PD_STATE_SYSTEM) {
5164 if (sdev1) {
5165 scsi_device_put(sdev1);
5167 } else {
5168 if (sdev1) {
5169 scsi_remove_device(sdev1);
5170 scsi_device_put(sdev1);
5176 doscan = 0;
5177 break;
5179 case MR_EVT_LD_OFFLINE:
5180 case MR_EVT_LD_DELETED:
5181 megasas_get_ld_list(instance);
5182 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5183 for (j = 0;
5184 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5185 j++) {
5187 ld_index =
5188 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5190 sdev1 = scsi_device_lookup(host,
5191 i + MEGASAS_MAX_LD_CHANNELS,
5195 if (instance->ld_ids[ld_index] != 0xff) {
5196 if (sdev1) {
5197 scsi_device_put(sdev1);
5199 } else {
5200 if (sdev1) {
5201 scsi_remove_device(sdev1);
5202 scsi_device_put(sdev1);
5207 doscan = 0;
5208 break;
5209 case MR_EVT_LD_CREATED:
5210 megasas_get_ld_list(instance);
5211 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5212 for (j = 0;
5213 j < MEGASAS_MAX_DEV_PER_CHANNEL;
5214 j++) {
5215 ld_index =
5216 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5218 sdev1 = scsi_device_lookup(host,
5219 i+MEGASAS_MAX_LD_CHANNELS,
5220 j, 0);
5222 if (instance->ld_ids[ld_index] !=
5223 0xff) {
5224 if (!sdev1) {
5225 scsi_add_device(host,
5226 i + 2,
5227 j, 0);
5230 if (sdev1) {
5231 scsi_device_put(sdev1);
5235 doscan = 0;
5236 break;
5237 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
5238 case MR_EVT_FOREIGN_CFG_IMPORTED:
5239 case MR_EVT_LD_STATE_CHANGE:
5240 doscan = 1;
5241 break;
5242 default:
5243 doscan = 0;
5244 break;
5246 } else {
5247 printk(KERN_ERR "invalid evt_detail!\n");
5248 kfree(ev);
5249 return;
5252 if (doscan) {
5253 printk(KERN_INFO "scanning ...\n");
5254 megasas_get_pd_list(instance);
5255 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5256 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5257 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5258 sdev1 = scsi_device_lookup(host, i, j, 0);
5259 if (instance->pd_list[pd_index].driveState ==
5260 MR_PD_STATE_SYSTEM) {
5261 if (!sdev1) {
5262 scsi_add_device(host, i, j, 0);
5264 if (sdev1)
5265 scsi_device_put(sdev1);
5266 } else {
5267 if (sdev1) {
5268 scsi_remove_device(sdev1);
5269 scsi_device_put(sdev1);
5275 megasas_get_ld_list(instance);
5276 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5277 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5278 ld_index =
5279 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5281 sdev1 = scsi_device_lookup(host,
5282 i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5283 if (instance->ld_ids[ld_index] != 0xff) {
5284 if (!sdev1) {
5285 scsi_add_device(host,
5286 i+2,
5287 j, 0);
5288 } else {
5289 scsi_device_put(sdev1);
5291 } else {
5292 if (sdev1) {
5293 scsi_remove_device(sdev1);
5294 scsi_device_put(sdev1);
5301 if ( instance->aen_cmd != NULL ) {
5302 kfree(ev);
5303 return ;
5306 seq_num = instance->evt_detail->seq_num + 1;
5308 /* Register AEN with FW for latest sequence number plus 1 */
5309 class_locale.members.reserved = 0;
5310 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5311 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5312 mutex_lock(&instance->aen_mutex);
5313 error = megasas_register_aen(instance, seq_num,
5314 class_locale.word);
5315 mutex_unlock(&instance->aen_mutex);
5317 if (error)
5318 printk(KERN_ERR "register aen failed error %x\n", error);
5320 kfree(ev);
5324 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
5325 megasas_sysfs_show_poll_mode_io,
5326 megasas_sysfs_set_poll_mode_io);
5329 * megasas_init - Driver load entry point
5331 static int __init megasas_init(void)
5333 int rval;
5336 * Announce driver version and other information
5338 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5339 MEGASAS_EXT_VERSION);
5341 support_poll_for_event = 2;
5342 support_device_change = 1;
5344 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5347 * Register character device node
5349 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5351 if (rval < 0) {
5352 printk(KERN_DEBUG "megasas: failed to open device node\n");
5353 return rval;
5356 megasas_mgmt_majorno = rval;
5359 * Register ourselves as PCI hotplug module
5361 rval = pci_register_driver(&megasas_pci_driver);
5363 if (rval) {
5364 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5365 goto err_pcidrv;
5368 rval = driver_create_file(&megasas_pci_driver.driver,
5369 &driver_attr_version);
5370 if (rval)
5371 goto err_dcf_attr_ver;
5372 rval = driver_create_file(&megasas_pci_driver.driver,
5373 &driver_attr_release_date);
5374 if (rval)
5375 goto err_dcf_rel_date;
5377 rval = driver_create_file(&megasas_pci_driver.driver,
5378 &driver_attr_support_poll_for_event);
5379 if (rval)
5380 goto err_dcf_support_poll_for_event;
5382 rval = driver_create_file(&megasas_pci_driver.driver,
5383 &driver_attr_dbg_lvl);
5384 if (rval)
5385 goto err_dcf_dbg_lvl;
5386 rval = driver_create_file(&megasas_pci_driver.driver,
5387 &driver_attr_poll_mode_io);
5388 if (rval)
5389 goto err_dcf_poll_mode_io;
5391 rval = driver_create_file(&megasas_pci_driver.driver,
5392 &driver_attr_support_device_change);
5393 if (rval)
5394 goto err_dcf_support_device_change;
5396 return rval;
5398 err_dcf_support_device_change:
5399 driver_remove_file(&megasas_pci_driver.driver,
5400 &driver_attr_poll_mode_io);
5402 err_dcf_poll_mode_io:
5403 driver_remove_file(&megasas_pci_driver.driver,
5404 &driver_attr_dbg_lvl);
5405 err_dcf_dbg_lvl:
5406 driver_remove_file(&megasas_pci_driver.driver,
5407 &driver_attr_support_poll_for_event);
5409 err_dcf_support_poll_for_event:
5410 driver_remove_file(&megasas_pci_driver.driver,
5411 &driver_attr_release_date);
5413 err_dcf_rel_date:
5414 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5415 err_dcf_attr_ver:
5416 pci_unregister_driver(&megasas_pci_driver);
5417 err_pcidrv:
5418 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5419 return rval;
5423 * megasas_exit - Driver unload entry point
5425 static void __exit megasas_exit(void)
5427 driver_remove_file(&megasas_pci_driver.driver,
5428 &driver_attr_poll_mode_io);
5429 driver_remove_file(&megasas_pci_driver.driver,
5430 &driver_attr_dbg_lvl);
5431 driver_remove_file(&megasas_pci_driver.driver,
5432 &driver_attr_support_poll_for_event);
5433 driver_remove_file(&megasas_pci_driver.driver,
5434 &driver_attr_support_device_change);
5435 driver_remove_file(&megasas_pci_driver.driver,
5436 &driver_attr_release_date);
5437 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5439 pci_unregister_driver(&megasas_pci_driver);
5440 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5443 module_init(megasas_init);
5444 module_exit(megasas_exit);