ARM: fix put_user() for gcc-8
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_sas_base.c
blob6835bae33ec4619f737b98080dd9043b4276754a
1 /*
2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Authors: Avago Technologies
21 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
24 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
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 <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION);
97 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
98 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
103 u8 query_type);
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106 u32 seq_num, u32 class_locale_word);
108 * PCI ID table for all supported controllers
110 static struct pci_device_id megasas_pci_table[] = {
112 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
113 /* xscale IOP */
114 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
115 /* ppc IOP */
116 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
117 /* ppc IOP */
118 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
119 /* gen2*/
120 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
121 /* gen2*/
122 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
123 /* skinny*/
124 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
125 /* skinny*/
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127 /* xscale IOP, vega */
128 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
129 /* xscale IOP */
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131 /* Fusion */
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133 /* Plasma */
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
135 /* Invader */
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
137 /* Fury */
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
139 /* Intruder */
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
141 /* Intruder 24 port*/
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
143 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
147 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
149 static int megasas_mgmt_majorno;
150 struct megasas_mgmt_info megasas_mgmt_info;
151 static struct fasync_struct *megasas_async_queue;
152 static DEFINE_MUTEX(megasas_async_queue_mutex);
154 static int megasas_poll_wait_aen;
155 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
156 static u32 support_poll_for_event;
157 u32 megasas_dbg_lvl;
158 static u32 support_device_change;
160 /* define lock for aen poll */
161 spinlock_t poll_aen_lock;
163 void
164 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
165 u8 alt_status);
166 static u32
167 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
168 static int
169 megasas_adp_reset_gen2(struct megasas_instance *instance,
170 struct megasas_register_set __iomem *reg_set);
171 static irqreturn_t megasas_isr(int irq, void *devp);
172 static u32
173 megasas_init_adapter_mfi(struct megasas_instance *instance);
175 megasas_build_and_issue_cmd(struct megasas_instance *instance,
176 struct scsi_cmnd *scmd);
177 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
178 void
179 megasas_release_fusion(struct megasas_instance *instance);
181 megasas_ioc_init_fusion(struct megasas_instance *instance);
182 void
183 megasas_free_cmds_fusion(struct megasas_instance *instance);
185 megasas_get_map_info(struct megasas_instance *instance);
187 megasas_sync_map_info(struct megasas_instance *instance);
189 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
190 int seconds);
191 void megasas_reset_reply_desc(struct megasas_instance *instance);
192 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
193 void megasas_fusion_ocr_wq(struct work_struct *work);
194 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
195 int initial);
196 int megasas_check_mpio_paths(struct megasas_instance *instance,
197 struct scsi_cmnd *scmd);
199 void
200 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
202 instance->instancet->fire_cmd(instance,
203 cmd->frame_phys_addr, 0, instance->reg_set);
207 * megasas_get_cmd - Get a command from the free pool
208 * @instance: Adapter soft state
210 * Returns a free command from the pool
212 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
213 *instance)
215 unsigned long flags;
216 struct megasas_cmd *cmd = NULL;
218 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
220 if (!list_empty(&instance->cmd_pool)) {
221 cmd = list_entry((&instance->cmd_pool)->next,
222 struct megasas_cmd, list);
223 list_del_init(&cmd->list);
224 } else {
225 dev_err(&instance->pdev->dev, "Command pool empty!\n");
228 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
229 return cmd;
233 * megasas_return_cmd - Return a cmd to free command pool
234 * @instance: Adapter soft state
235 * @cmd: Command packet to be returned to free command pool
237 inline void
238 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
240 unsigned long flags;
241 u32 blk_tags;
242 struct megasas_cmd_fusion *cmd_fusion;
243 struct fusion_context *fusion = instance->ctrl_context;
245 /* This flag is used only for fusion adapter.
246 * Wait for Interrupt for Polled mode DCMD
248 if (cmd->flags & DRV_DCMD_POLLED_MODE)
249 return;
251 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
253 if (fusion) {
254 blk_tags = instance->max_scsi_cmds + cmd->index;
255 cmd_fusion = fusion->cmd_list[blk_tags];
256 megasas_return_cmd_fusion(instance, cmd_fusion);
258 cmd->scmd = NULL;
259 cmd->frame_count = 0;
260 cmd->flags = 0;
261 if (!fusion && reset_devices)
262 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
263 list_add(&cmd->list, (&instance->cmd_pool)->next);
265 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
269 static const char *
270 format_timestamp(uint32_t timestamp)
272 static char buffer[32];
274 if ((timestamp & 0xff000000) == 0xff000000)
275 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
276 0x00ffffff);
277 else
278 snprintf(buffer, sizeof(buffer), "%us", timestamp);
279 return buffer;
282 static const char *
283 format_class(int8_t class)
285 static char buffer[6];
287 switch (class) {
288 case MFI_EVT_CLASS_DEBUG:
289 return "debug";
290 case MFI_EVT_CLASS_PROGRESS:
291 return "progress";
292 case MFI_EVT_CLASS_INFO:
293 return "info";
294 case MFI_EVT_CLASS_WARNING:
295 return "WARN";
296 case MFI_EVT_CLASS_CRITICAL:
297 return "CRIT";
298 case MFI_EVT_CLASS_FATAL:
299 return "FATAL";
300 case MFI_EVT_CLASS_DEAD:
301 return "DEAD";
302 default:
303 snprintf(buffer, sizeof(buffer), "%d", class);
304 return buffer;
309 * megasas_decode_evt: Decode FW AEN event and print critical event
310 * for information.
311 * @instance: Adapter soft state
313 static void
314 megasas_decode_evt(struct megasas_instance *instance)
316 struct megasas_evt_detail *evt_detail = instance->evt_detail;
317 union megasas_evt_class_locale class_locale;
318 class_locale.word = le32_to_cpu(evt_detail->cl.word);
320 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
321 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
322 le32_to_cpu(evt_detail->seq_num),
323 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
324 (class_locale.members.locale),
325 format_class(class_locale.members.class),
326 evt_detail->description);
330 * The following functions are defined for xscale
331 * (deviceid : 1064R, PERC5) controllers
335 * megasas_enable_intr_xscale - Enables interrupts
336 * @regs: MFI register set
338 static inline void
339 megasas_enable_intr_xscale(struct megasas_instance *instance)
341 struct megasas_register_set __iomem *regs;
343 regs = instance->reg_set;
344 writel(0, &(regs)->outbound_intr_mask);
346 /* Dummy readl to force pci flush */
347 readl(&regs->outbound_intr_mask);
351 * megasas_disable_intr_xscale -Disables interrupt
352 * @regs: MFI register set
354 static inline void
355 megasas_disable_intr_xscale(struct megasas_instance *instance)
357 struct megasas_register_set __iomem *regs;
358 u32 mask = 0x1f;
360 regs = instance->reg_set;
361 writel(mask, &regs->outbound_intr_mask);
362 /* Dummy readl to force pci flush */
363 readl(&regs->outbound_intr_mask);
367 * megasas_read_fw_status_reg_xscale - returns the current FW status value
368 * @regs: MFI register set
370 static u32
371 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
373 return readl(&(regs)->outbound_msg_0);
376 * megasas_clear_interrupt_xscale - Check & clear interrupt
377 * @regs: MFI register set
379 static int
380 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
382 u32 status;
383 u32 mfiStatus = 0;
386 * Check if it is our interrupt
388 status = readl(&regs->outbound_intr_status);
390 if (status & MFI_OB_INTR_STATUS_MASK)
391 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
392 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
393 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
396 * Clear the interrupt by writing back the same value
398 if (mfiStatus)
399 writel(status, &regs->outbound_intr_status);
401 /* Dummy readl to force pci flush */
402 readl(&regs->outbound_intr_status);
404 return mfiStatus;
408 * megasas_fire_cmd_xscale - Sends command to the FW
409 * @frame_phys_addr : Physical address of cmd
410 * @frame_count : Number of frames for the command
411 * @regs : MFI register set
413 static inline void
414 megasas_fire_cmd_xscale(struct megasas_instance *instance,
415 dma_addr_t frame_phys_addr,
416 u32 frame_count,
417 struct megasas_register_set __iomem *regs)
419 unsigned long flags;
421 spin_lock_irqsave(&instance->hba_lock, flags);
422 writel((frame_phys_addr >> 3)|(frame_count),
423 &(regs)->inbound_queue_port);
424 spin_unlock_irqrestore(&instance->hba_lock, flags);
428 * megasas_adp_reset_xscale - For controller reset
429 * @regs: MFI register set
431 static int
432 megasas_adp_reset_xscale(struct megasas_instance *instance,
433 struct megasas_register_set __iomem *regs)
435 u32 i;
436 u32 pcidata;
438 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
440 for (i = 0; i < 3; i++)
441 msleep(1000); /* sleep for 3 secs */
442 pcidata = 0;
443 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
444 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
445 if (pcidata & 0x2) {
446 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
447 pcidata &= ~0x2;
448 pci_write_config_dword(instance->pdev,
449 MFI_1068_PCSR_OFFSET, pcidata);
451 for (i = 0; i < 2; i++)
452 msleep(1000); /* need to wait 2 secs again */
454 pcidata = 0;
455 pci_read_config_dword(instance->pdev,
456 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
457 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
458 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
459 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
460 pcidata = 0;
461 pci_write_config_dword(instance->pdev,
462 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
465 return 0;
469 * megasas_check_reset_xscale - For controller reset check
470 * @regs: MFI register set
472 static int
473 megasas_check_reset_xscale(struct megasas_instance *instance,
474 struct megasas_register_set __iomem *regs)
476 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
477 (le32_to_cpu(*instance->consumer) ==
478 MEGASAS_ADPRESET_INPROG_SIGN))
479 return 1;
480 return 0;
483 static struct megasas_instance_template megasas_instance_template_xscale = {
485 .fire_cmd = megasas_fire_cmd_xscale,
486 .enable_intr = megasas_enable_intr_xscale,
487 .disable_intr = megasas_disable_intr_xscale,
488 .clear_intr = megasas_clear_intr_xscale,
489 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
490 .adp_reset = megasas_adp_reset_xscale,
491 .check_reset = megasas_check_reset_xscale,
492 .service_isr = megasas_isr,
493 .tasklet = megasas_complete_cmd_dpc,
494 .init_adapter = megasas_init_adapter_mfi,
495 .build_and_issue_cmd = megasas_build_and_issue_cmd,
496 .issue_dcmd = megasas_issue_dcmd,
500 * This is the end of set of functions & definitions specific
501 * to xscale (deviceid : 1064R, PERC5) controllers
505 * The following functions are defined for ppc (deviceid : 0x60)
506 * controllers
510 * megasas_enable_intr_ppc - Enables interrupts
511 * @regs: MFI register set
513 static inline void
514 megasas_enable_intr_ppc(struct megasas_instance *instance)
516 struct megasas_register_set __iomem *regs;
518 regs = instance->reg_set;
519 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
521 writel(~0x80000000, &(regs)->outbound_intr_mask);
523 /* Dummy readl to force pci flush */
524 readl(&regs->outbound_intr_mask);
528 * megasas_disable_intr_ppc - Disable interrupt
529 * @regs: MFI register set
531 static inline void
532 megasas_disable_intr_ppc(struct megasas_instance *instance)
534 struct megasas_register_set __iomem *regs;
535 u32 mask = 0xFFFFFFFF;
537 regs = instance->reg_set;
538 writel(mask, &regs->outbound_intr_mask);
539 /* Dummy readl to force pci flush */
540 readl(&regs->outbound_intr_mask);
544 * megasas_read_fw_status_reg_ppc - returns the current FW status value
545 * @regs: MFI register set
547 static u32
548 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
550 return readl(&(regs)->outbound_scratch_pad);
554 * megasas_clear_interrupt_ppc - Check & clear interrupt
555 * @regs: MFI register set
557 static int
558 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
560 u32 status, mfiStatus = 0;
563 * Check if it is our interrupt
565 status = readl(&regs->outbound_intr_status);
567 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
568 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
570 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
571 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
574 * Clear the interrupt by writing back the same value
576 writel(status, &regs->outbound_doorbell_clear);
578 /* Dummy readl to force pci flush */
579 readl(&regs->outbound_doorbell_clear);
581 return mfiStatus;
585 * megasas_fire_cmd_ppc - Sends command to the FW
586 * @frame_phys_addr : Physical address of cmd
587 * @frame_count : Number of frames for the command
588 * @regs : MFI register set
590 static inline void
591 megasas_fire_cmd_ppc(struct megasas_instance *instance,
592 dma_addr_t frame_phys_addr,
593 u32 frame_count,
594 struct megasas_register_set __iomem *regs)
596 unsigned long flags;
598 spin_lock_irqsave(&instance->hba_lock, flags);
599 writel((frame_phys_addr | (frame_count<<1))|1,
600 &(regs)->inbound_queue_port);
601 spin_unlock_irqrestore(&instance->hba_lock, flags);
605 * megasas_check_reset_ppc - For controller reset check
606 * @regs: MFI register set
608 static int
609 megasas_check_reset_ppc(struct megasas_instance *instance,
610 struct megasas_register_set __iomem *regs)
612 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
613 return 1;
615 return 0;
618 static struct megasas_instance_template megasas_instance_template_ppc = {
620 .fire_cmd = megasas_fire_cmd_ppc,
621 .enable_intr = megasas_enable_intr_ppc,
622 .disable_intr = megasas_disable_intr_ppc,
623 .clear_intr = megasas_clear_intr_ppc,
624 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
625 .adp_reset = megasas_adp_reset_xscale,
626 .check_reset = megasas_check_reset_ppc,
627 .service_isr = megasas_isr,
628 .tasklet = megasas_complete_cmd_dpc,
629 .init_adapter = megasas_init_adapter_mfi,
630 .build_and_issue_cmd = megasas_build_and_issue_cmd,
631 .issue_dcmd = megasas_issue_dcmd,
635 * megasas_enable_intr_skinny - Enables interrupts
636 * @regs: MFI register set
638 static inline void
639 megasas_enable_intr_skinny(struct megasas_instance *instance)
641 struct megasas_register_set __iomem *regs;
643 regs = instance->reg_set;
644 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
646 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
648 /* Dummy readl to force pci flush */
649 readl(&regs->outbound_intr_mask);
653 * megasas_disable_intr_skinny - Disables interrupt
654 * @regs: MFI register set
656 static inline void
657 megasas_disable_intr_skinny(struct megasas_instance *instance)
659 struct megasas_register_set __iomem *regs;
660 u32 mask = 0xFFFFFFFF;
662 regs = instance->reg_set;
663 writel(mask, &regs->outbound_intr_mask);
664 /* Dummy readl to force pci flush */
665 readl(&regs->outbound_intr_mask);
669 * megasas_read_fw_status_reg_skinny - returns the current FW status value
670 * @regs: MFI register set
672 static u32
673 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
675 return readl(&(regs)->outbound_scratch_pad);
679 * megasas_clear_interrupt_skinny - Check & clear interrupt
680 * @regs: MFI register set
682 static int
683 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
685 u32 status;
686 u32 mfiStatus = 0;
689 * Check if it is our interrupt
691 status = readl(&regs->outbound_intr_status);
693 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
694 return 0;
698 * Check if it is our interrupt
700 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
701 MFI_STATE_FAULT) {
702 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
703 } else
704 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
707 * Clear the interrupt by writing back the same value
709 writel(status, &regs->outbound_intr_status);
712 * dummy read to flush PCI
714 readl(&regs->outbound_intr_status);
716 return mfiStatus;
720 * megasas_fire_cmd_skinny - Sends command to the FW
721 * @frame_phys_addr : Physical address of cmd
722 * @frame_count : Number of frames for the command
723 * @regs : MFI register set
725 static inline void
726 megasas_fire_cmd_skinny(struct megasas_instance *instance,
727 dma_addr_t frame_phys_addr,
728 u32 frame_count,
729 struct megasas_register_set __iomem *regs)
731 unsigned long flags;
733 spin_lock_irqsave(&instance->hba_lock, flags);
734 writel(upper_32_bits(frame_phys_addr),
735 &(regs)->inbound_high_queue_port);
736 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
737 &(regs)->inbound_low_queue_port);
738 mmiowb();
739 spin_unlock_irqrestore(&instance->hba_lock, flags);
743 * megasas_check_reset_skinny - For controller reset check
744 * @regs: MFI register set
746 static int
747 megasas_check_reset_skinny(struct megasas_instance *instance,
748 struct megasas_register_set __iomem *regs)
750 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
751 return 1;
753 return 0;
756 static struct megasas_instance_template megasas_instance_template_skinny = {
758 .fire_cmd = megasas_fire_cmd_skinny,
759 .enable_intr = megasas_enable_intr_skinny,
760 .disable_intr = megasas_disable_intr_skinny,
761 .clear_intr = megasas_clear_intr_skinny,
762 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
763 .adp_reset = megasas_adp_reset_gen2,
764 .check_reset = megasas_check_reset_skinny,
765 .service_isr = megasas_isr,
766 .tasklet = megasas_complete_cmd_dpc,
767 .init_adapter = megasas_init_adapter_mfi,
768 .build_and_issue_cmd = megasas_build_and_issue_cmd,
769 .issue_dcmd = megasas_issue_dcmd,
774 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
775 * controllers
779 * megasas_enable_intr_gen2 - Enables interrupts
780 * @regs: MFI register set
782 static inline void
783 megasas_enable_intr_gen2(struct megasas_instance *instance)
785 struct megasas_register_set __iomem *regs;
787 regs = instance->reg_set;
788 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
790 /* write ~0x00000005 (4 & 1) to the intr mask*/
791 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
793 /* Dummy readl to force pci flush */
794 readl(&regs->outbound_intr_mask);
798 * megasas_disable_intr_gen2 - Disables interrupt
799 * @regs: MFI register set
801 static inline void
802 megasas_disable_intr_gen2(struct megasas_instance *instance)
804 struct megasas_register_set __iomem *regs;
805 u32 mask = 0xFFFFFFFF;
807 regs = instance->reg_set;
808 writel(mask, &regs->outbound_intr_mask);
809 /* Dummy readl to force pci flush */
810 readl(&regs->outbound_intr_mask);
814 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
815 * @regs: MFI register set
817 static u32
818 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
820 return readl(&(regs)->outbound_scratch_pad);
824 * megasas_clear_interrupt_gen2 - Check & clear interrupt
825 * @regs: MFI register set
827 static int
828 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
830 u32 status;
831 u32 mfiStatus = 0;
834 * Check if it is our interrupt
836 status = readl(&regs->outbound_intr_status);
838 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
839 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
841 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
842 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
846 * Clear the interrupt by writing back the same value
848 if (mfiStatus)
849 writel(status, &regs->outbound_doorbell_clear);
851 /* Dummy readl to force pci flush */
852 readl(&regs->outbound_intr_status);
854 return mfiStatus;
857 * megasas_fire_cmd_gen2 - Sends command to the FW
858 * @frame_phys_addr : Physical address of cmd
859 * @frame_count : Number of frames for the command
860 * @regs : MFI register set
862 static inline void
863 megasas_fire_cmd_gen2(struct megasas_instance *instance,
864 dma_addr_t frame_phys_addr,
865 u32 frame_count,
866 struct megasas_register_set __iomem *regs)
868 unsigned long flags;
870 spin_lock_irqsave(&instance->hba_lock, flags);
871 writel((frame_phys_addr | (frame_count<<1))|1,
872 &(regs)->inbound_queue_port);
873 spin_unlock_irqrestore(&instance->hba_lock, flags);
877 * megasas_adp_reset_gen2 - For controller reset
878 * @regs: MFI register set
880 static int
881 megasas_adp_reset_gen2(struct megasas_instance *instance,
882 struct megasas_register_set __iomem *reg_set)
884 u32 retry = 0 ;
885 u32 HostDiag;
886 u32 __iomem *seq_offset = &reg_set->seq_offset;
887 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
889 if (instance->instancet == &megasas_instance_template_skinny) {
890 seq_offset = &reg_set->fusion_seq_offset;
891 hostdiag_offset = &reg_set->fusion_host_diag;
894 writel(0, seq_offset);
895 writel(4, seq_offset);
896 writel(0xb, seq_offset);
897 writel(2, seq_offset);
898 writel(7, seq_offset);
899 writel(0xd, seq_offset);
901 msleep(1000);
903 HostDiag = (u32)readl(hostdiag_offset);
905 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
906 msleep(100);
907 HostDiag = (u32)readl(hostdiag_offset);
908 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
909 retry, HostDiag);
911 if (retry++ >= 100)
912 return 1;
916 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
918 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
920 ssleep(10);
922 HostDiag = (u32)readl(hostdiag_offset);
923 while (HostDiag & DIAG_RESET_ADAPTER) {
924 msleep(100);
925 HostDiag = (u32)readl(hostdiag_offset);
926 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
927 retry, HostDiag);
929 if (retry++ >= 1000)
930 return 1;
933 return 0;
937 * megasas_check_reset_gen2 - For controller reset check
938 * @regs: MFI register set
940 static int
941 megasas_check_reset_gen2(struct megasas_instance *instance,
942 struct megasas_register_set __iomem *regs)
944 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
945 return 1;
948 return 0;
951 static struct megasas_instance_template megasas_instance_template_gen2 = {
953 .fire_cmd = megasas_fire_cmd_gen2,
954 .enable_intr = megasas_enable_intr_gen2,
955 .disable_intr = megasas_disable_intr_gen2,
956 .clear_intr = megasas_clear_intr_gen2,
957 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
958 .adp_reset = megasas_adp_reset_gen2,
959 .check_reset = megasas_check_reset_gen2,
960 .service_isr = megasas_isr,
961 .tasklet = megasas_complete_cmd_dpc,
962 .init_adapter = megasas_init_adapter_mfi,
963 .build_and_issue_cmd = megasas_build_and_issue_cmd,
964 .issue_dcmd = megasas_issue_dcmd,
968 * This is the end of set of functions & definitions
969 * specific to gen2 (deviceid : 0x78, 0x79) controllers
973 * Template added for TB (Fusion)
975 extern struct megasas_instance_template megasas_instance_template_fusion;
978 * megasas_issue_polled - Issues a polling command
979 * @instance: Adapter soft state
980 * @cmd: Command packet to be issued
982 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
985 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
987 int seconds;
988 struct megasas_header *frame_hdr = &cmd->frame->hdr;
990 frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
991 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
994 * Issue the frame using inbound queue port
996 instance->instancet->issue_dcmd(instance, cmd);
999 * Wait for cmd_status to change
1001 if (instance->requestorId)
1002 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
1003 else
1004 seconds = MFI_POLL_TIMEOUT_SECS;
1005 return wait_and_poll(instance, cmd, seconds);
1009 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1010 * @instance: Adapter soft state
1011 * @cmd: Command to be issued
1012 * @timeout: Timeout in seconds
1014 * This function waits on an event for the command to be returned from ISR.
1015 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1016 * Used to issue ioctl commands.
1019 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1020 struct megasas_cmd *cmd, int timeout)
1022 int ret = 0;
1024 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1026 instance->instancet->issue_dcmd(instance, cmd);
1027 if (timeout) {
1028 ret = wait_event_timeout(instance->int_cmd_wait_q,
1029 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1030 if (!ret)
1031 return 1;
1032 } else
1033 wait_event(instance->int_cmd_wait_q,
1034 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1036 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1037 0 : 1;
1041 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1042 * @instance: Adapter soft state
1043 * @cmd_to_abort: Previously issued cmd to be aborted
1044 * @timeout: Timeout in seconds
1046 * MFI firmware can abort previously issued AEN comamnd (automatic event
1047 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1048 * cmd and waits for return status.
1049 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1051 static int
1052 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1053 struct megasas_cmd *cmd_to_abort, int timeout)
1055 struct megasas_cmd *cmd;
1056 struct megasas_abort_frame *abort_fr;
1057 int ret = 0;
1059 cmd = megasas_get_cmd(instance);
1061 if (!cmd)
1062 return -1;
1064 abort_fr = &cmd->frame->abort;
1067 * Prepare and issue the abort frame
1069 abort_fr->cmd = MFI_CMD_ABORT;
1070 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1071 abort_fr->flags = cpu_to_le16(0);
1072 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1073 abort_fr->abort_mfi_phys_addr_lo =
1074 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1075 abort_fr->abort_mfi_phys_addr_hi =
1076 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1078 cmd->sync_cmd = 1;
1079 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1081 instance->instancet->issue_dcmd(instance, cmd);
1083 if (timeout) {
1084 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1085 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1086 if (!ret) {
1087 dev_err(&instance->pdev->dev, "Command timedout"
1088 "from %s\n", __func__);
1089 return 1;
1091 } else
1092 wait_event(instance->abort_cmd_wait_q,
1093 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1095 cmd->sync_cmd = 0;
1097 megasas_return_cmd(instance, cmd);
1098 return 0;
1102 * megasas_make_sgl32 - Prepares 32-bit SGL
1103 * @instance: Adapter soft state
1104 * @scp: SCSI command from the mid-layer
1105 * @mfi_sgl: SGL to be filled in
1107 * If successful, this function returns the number of SG elements. Otherwise,
1108 * it returnes -1.
1110 static int
1111 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1112 union megasas_sgl *mfi_sgl)
1114 int i;
1115 int sge_count;
1116 struct scatterlist *os_sgl;
1118 sge_count = scsi_dma_map(scp);
1119 BUG_ON(sge_count < 0);
1121 if (sge_count) {
1122 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1123 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1124 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1127 return sge_count;
1131 * megasas_make_sgl64 - Prepares 64-bit SGL
1132 * @instance: Adapter soft state
1133 * @scp: SCSI command from the mid-layer
1134 * @mfi_sgl: SGL to be filled in
1136 * If successful, this function returns the number of SG elements. Otherwise,
1137 * it returnes -1.
1139 static int
1140 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1141 union megasas_sgl *mfi_sgl)
1143 int i;
1144 int sge_count;
1145 struct scatterlist *os_sgl;
1147 sge_count = scsi_dma_map(scp);
1148 BUG_ON(sge_count < 0);
1150 if (sge_count) {
1151 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1152 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1153 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1156 return sge_count;
1160 * megasas_make_sgl_skinny - Prepares IEEE SGL
1161 * @instance: Adapter soft state
1162 * @scp: SCSI command from the mid-layer
1163 * @mfi_sgl: SGL to be filled in
1165 * If successful, this function returns the number of SG elements. Otherwise,
1166 * it returnes -1.
1168 static int
1169 megasas_make_sgl_skinny(struct megasas_instance *instance,
1170 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1172 int i;
1173 int sge_count;
1174 struct scatterlist *os_sgl;
1176 sge_count = scsi_dma_map(scp);
1178 if (sge_count) {
1179 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1180 mfi_sgl->sge_skinny[i].length =
1181 cpu_to_le32(sg_dma_len(os_sgl));
1182 mfi_sgl->sge_skinny[i].phys_addr =
1183 cpu_to_le64(sg_dma_address(os_sgl));
1184 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1187 return sge_count;
1191 * megasas_get_frame_count - Computes the number of frames
1192 * @frame_type : type of frame- io or pthru frame
1193 * @sge_count : number of sg elements
1195 * Returns the number of frames required for numnber of sge's (sge_count)
1198 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1199 u8 sge_count, u8 frame_type)
1201 int num_cnt;
1202 int sge_bytes;
1203 u32 sge_sz;
1204 u32 frame_count = 0;
1206 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1207 sizeof(struct megasas_sge32);
1209 if (instance->flag_ieee) {
1210 sge_sz = sizeof(struct megasas_sge_skinny);
1214 * Main frame can contain 2 SGEs for 64-bit SGLs and
1215 * 3 SGEs for 32-bit SGLs for ldio &
1216 * 1 SGEs for 64-bit SGLs and
1217 * 2 SGEs for 32-bit SGLs for pthru frame
1219 if (unlikely(frame_type == PTHRU_FRAME)) {
1220 if (instance->flag_ieee == 1) {
1221 num_cnt = sge_count - 1;
1222 } else if (IS_DMA64)
1223 num_cnt = sge_count - 1;
1224 else
1225 num_cnt = sge_count - 2;
1226 } else {
1227 if (instance->flag_ieee == 1) {
1228 num_cnt = sge_count - 1;
1229 } else if (IS_DMA64)
1230 num_cnt = sge_count - 2;
1231 else
1232 num_cnt = sge_count - 3;
1235 if (num_cnt > 0) {
1236 sge_bytes = sge_sz * num_cnt;
1238 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1239 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1241 /* Main frame */
1242 frame_count += 1;
1244 if (frame_count > 7)
1245 frame_count = 8;
1246 return frame_count;
1250 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1251 * @instance: Adapter soft state
1252 * @scp: SCSI command
1253 * @cmd: Command to be prepared in
1255 * This function prepares CDB commands. These are typcially pass-through
1256 * commands to the devices.
1258 static int
1259 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1260 struct megasas_cmd *cmd)
1262 u32 is_logical;
1263 u32 device_id;
1264 u16 flags = 0;
1265 struct megasas_pthru_frame *pthru;
1267 is_logical = MEGASAS_IS_LOGICAL(scp);
1268 device_id = MEGASAS_DEV_INDEX(scp);
1269 pthru = (struct megasas_pthru_frame *)cmd->frame;
1271 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1272 flags = MFI_FRAME_DIR_WRITE;
1273 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1274 flags = MFI_FRAME_DIR_READ;
1275 else if (scp->sc_data_direction == PCI_DMA_NONE)
1276 flags = MFI_FRAME_DIR_NONE;
1278 if (instance->flag_ieee == 1) {
1279 flags |= MFI_FRAME_IEEE;
1283 * Prepare the DCDB frame
1285 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1286 pthru->cmd_status = 0x0;
1287 pthru->scsi_status = 0x0;
1288 pthru->target_id = device_id;
1289 pthru->lun = scp->device->lun;
1290 pthru->cdb_len = scp->cmd_len;
1291 pthru->timeout = 0;
1292 pthru->pad_0 = 0;
1293 pthru->flags = cpu_to_le16(flags);
1294 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1296 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1299 * If the command is for the tape device, set the
1300 * pthru timeout to the os layer timeout value.
1302 if (scp->device->type == TYPE_TAPE) {
1303 if ((scp->request->timeout / HZ) > 0xFFFF)
1304 pthru->timeout = cpu_to_le16(0xFFFF);
1305 else
1306 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1310 * Construct SGL
1312 if (instance->flag_ieee == 1) {
1313 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1314 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1315 &pthru->sgl);
1316 } else if (IS_DMA64) {
1317 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1318 pthru->sge_count = megasas_make_sgl64(instance, scp,
1319 &pthru->sgl);
1320 } else
1321 pthru->sge_count = megasas_make_sgl32(instance, scp,
1322 &pthru->sgl);
1324 if (pthru->sge_count > instance->max_num_sge) {
1325 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1326 pthru->sge_count);
1327 return 0;
1331 * Sense info specific
1333 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1334 pthru->sense_buf_phys_addr_hi =
1335 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1336 pthru->sense_buf_phys_addr_lo =
1337 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1340 * Compute the total number of frames this command consumes. FW uses
1341 * this number to pull sufficient number of frames from host memory.
1343 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1344 PTHRU_FRAME);
1346 return cmd->frame_count;
1350 * megasas_build_ldio - Prepares IOs to logical devices
1351 * @instance: Adapter soft state
1352 * @scp: SCSI command
1353 * @cmd: Command to be prepared
1355 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1357 static int
1358 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1359 struct megasas_cmd *cmd)
1361 u32 device_id;
1362 u8 sc = scp->cmnd[0];
1363 u16 flags = 0;
1364 struct megasas_io_frame *ldio;
1366 device_id = MEGASAS_DEV_INDEX(scp);
1367 ldio = (struct megasas_io_frame *)cmd->frame;
1369 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1370 flags = MFI_FRAME_DIR_WRITE;
1371 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1372 flags = MFI_FRAME_DIR_READ;
1374 if (instance->flag_ieee == 1) {
1375 flags |= MFI_FRAME_IEEE;
1379 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1381 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1382 ldio->cmd_status = 0x0;
1383 ldio->scsi_status = 0x0;
1384 ldio->target_id = device_id;
1385 ldio->timeout = 0;
1386 ldio->reserved_0 = 0;
1387 ldio->pad_0 = 0;
1388 ldio->flags = cpu_to_le16(flags);
1389 ldio->start_lba_hi = 0;
1390 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1393 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1395 if (scp->cmd_len == 6) {
1396 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1397 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1398 ((u32) scp->cmnd[2] << 8) |
1399 (u32) scp->cmnd[3]);
1401 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1405 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1407 else if (scp->cmd_len == 10) {
1408 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1409 ((u32) scp->cmnd[7] << 8));
1410 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1411 ((u32) scp->cmnd[3] << 16) |
1412 ((u32) scp->cmnd[4] << 8) |
1413 (u32) scp->cmnd[5]);
1417 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1419 else if (scp->cmd_len == 12) {
1420 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1421 ((u32) scp->cmnd[7] << 16) |
1422 ((u32) scp->cmnd[8] << 8) |
1423 (u32) scp->cmnd[9]);
1425 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1426 ((u32) scp->cmnd[3] << 16) |
1427 ((u32) scp->cmnd[4] << 8) |
1428 (u32) scp->cmnd[5]);
1432 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1434 else if (scp->cmd_len == 16) {
1435 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1436 ((u32) scp->cmnd[11] << 16) |
1437 ((u32) scp->cmnd[12] << 8) |
1438 (u32) scp->cmnd[13]);
1440 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1441 ((u32) scp->cmnd[7] << 16) |
1442 ((u32) scp->cmnd[8] << 8) |
1443 (u32) scp->cmnd[9]);
1445 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1446 ((u32) scp->cmnd[3] << 16) |
1447 ((u32) scp->cmnd[4] << 8) |
1448 (u32) scp->cmnd[5]);
1453 * Construct SGL
1455 if (instance->flag_ieee) {
1456 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1457 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1458 &ldio->sgl);
1459 } else if (IS_DMA64) {
1460 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1461 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1462 } else
1463 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1465 if (ldio->sge_count > instance->max_num_sge) {
1466 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1467 ldio->sge_count);
1468 return 0;
1472 * Sense info specific
1474 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1475 ldio->sense_buf_phys_addr_hi = 0;
1476 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1479 * Compute the total number of frames this command consumes. FW uses
1480 * this number to pull sufficient number of frames from host memory.
1482 cmd->frame_count = megasas_get_frame_count(instance,
1483 ldio->sge_count, IO_FRAME);
1485 return cmd->frame_count;
1489 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1490 * and whether it's RW or non RW
1491 * @scmd: SCSI command
1494 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1496 int ret;
1498 switch (cmd->cmnd[0]) {
1499 case READ_10:
1500 case WRITE_10:
1501 case READ_12:
1502 case WRITE_12:
1503 case READ_6:
1504 case WRITE_6:
1505 case READ_16:
1506 case WRITE_16:
1507 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1508 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1509 break;
1510 default:
1511 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1512 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1514 return ret;
1518 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1519 * in FW
1520 * @instance: Adapter soft state
1522 static inline void
1523 megasas_dump_pending_frames(struct megasas_instance *instance)
1525 struct megasas_cmd *cmd;
1526 int i,n;
1527 union megasas_sgl *mfi_sgl;
1528 struct megasas_io_frame *ldio;
1529 struct megasas_pthru_frame *pthru;
1530 u32 sgcount;
1531 u32 max_cmd = instance->max_fw_cmds;
1533 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1534 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1535 if (IS_DMA64)
1536 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1537 else
1538 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1540 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1541 for (i = 0; i < max_cmd; i++) {
1542 cmd = instance->cmd_list[i];
1543 if (!cmd->scmd)
1544 continue;
1545 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1546 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1547 ldio = (struct megasas_io_frame *)cmd->frame;
1548 mfi_sgl = &ldio->sgl;
1549 sgcount = ldio->sge_count;
1550 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1551 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1552 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1553 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1554 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1555 } else {
1556 pthru = (struct megasas_pthru_frame *) cmd->frame;
1557 mfi_sgl = &pthru->sgl;
1558 sgcount = pthru->sge_count;
1559 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1560 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1561 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1562 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1563 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1565 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1566 for (n = 0; n < sgcount; n++) {
1567 if (IS_DMA64)
1568 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1569 le32_to_cpu(mfi_sgl->sge64[n].length),
1570 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1571 else
1572 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1573 le32_to_cpu(mfi_sgl->sge32[n].length),
1574 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1577 } /*for max_cmd*/
1578 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1579 for (i = 0; i < max_cmd; i++) {
1581 cmd = instance->cmd_list[i];
1583 if (cmd->sync_cmd == 1)
1584 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1586 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1590 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1591 struct scsi_cmnd *scmd)
1593 struct megasas_cmd *cmd;
1594 u32 frame_count;
1596 cmd = megasas_get_cmd(instance);
1597 if (!cmd)
1598 return SCSI_MLQUEUE_HOST_BUSY;
1601 * Logical drive command
1603 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1604 frame_count = megasas_build_ldio(instance, scmd, cmd);
1605 else
1606 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1608 if (!frame_count)
1609 goto out_return_cmd;
1611 cmd->scmd = scmd;
1612 scmd->SCp.ptr = (char *)cmd;
1615 * Issue the command to the FW
1617 atomic_inc(&instance->fw_outstanding);
1619 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1620 cmd->frame_count-1, instance->reg_set);
1622 return 0;
1623 out_return_cmd:
1624 megasas_return_cmd(instance, cmd);
1625 return 1;
1630 * megasas_queue_command - Queue entry point
1631 * @scmd: SCSI command to be queued
1632 * @done: Callback entry point
1634 static int
1635 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1637 struct megasas_instance *instance;
1638 unsigned long flags;
1640 instance = (struct megasas_instance *)
1641 scmd->device->host->hostdata;
1643 if (instance->unload == 1) {
1644 scmd->result = DID_NO_CONNECT << 16;
1645 scmd->scsi_done(scmd);
1646 return 0;
1649 if (instance->issuepend_done == 0)
1650 return SCSI_MLQUEUE_HOST_BUSY;
1652 spin_lock_irqsave(&instance->hba_lock, flags);
1654 /* Check for an mpio path and adjust behavior */
1655 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1656 if (megasas_check_mpio_paths(instance, scmd) ==
1657 (DID_RESET << 16)) {
1658 spin_unlock_irqrestore(&instance->hba_lock, flags);
1659 return SCSI_MLQUEUE_HOST_BUSY;
1660 } else {
1661 spin_unlock_irqrestore(&instance->hba_lock, flags);
1662 scmd->result = DID_NO_CONNECT << 16;
1663 scmd->scsi_done(scmd);
1664 return 0;
1668 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1669 spin_unlock_irqrestore(&instance->hba_lock, flags);
1670 scmd->result = DID_NO_CONNECT << 16;
1671 scmd->scsi_done(scmd);
1672 return 0;
1675 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1676 spin_unlock_irqrestore(&instance->hba_lock, flags);
1677 return SCSI_MLQUEUE_HOST_BUSY;
1680 spin_unlock_irqrestore(&instance->hba_lock, flags);
1682 scmd->result = 0;
1684 if (MEGASAS_IS_LOGICAL(scmd) &&
1685 (scmd->device->id >= instance->fw_supported_vd_count ||
1686 scmd->device->lun)) {
1687 scmd->result = DID_BAD_TARGET << 16;
1688 goto out_done;
1692 * FW takes care of flush cache on its own for Virtual Disk.
1693 * No need to send it down for VD. For JBOD send SYNCHRONIZE_CACHE to FW.
1695 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) && MEGASAS_IS_LOGICAL(scmd)) {
1696 scmd->result = DID_OK << 16;
1697 goto out_done;
1700 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1701 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1702 return SCSI_MLQUEUE_HOST_BUSY;
1705 return 0;
1707 out_done:
1708 scmd->scsi_done(scmd);
1709 return 0;
1712 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1714 int i;
1716 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1718 if ((megasas_mgmt_info.instance[i]) &&
1719 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1720 return megasas_mgmt_info.instance[i];
1723 return NULL;
1727 * megasas_set_dma_alignment - Set DMA alignment for PI enabled VD
1729 * @sdev: OS provided scsi device
1731 * Returns void
1733 static void megasas_set_dma_alignment(struct scsi_device *sdev)
1735 u32 device_id, ld;
1736 struct megasas_instance *instance;
1737 struct fusion_context *fusion;
1738 struct MR_LD_RAID *raid;
1739 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1741 instance = megasas_lookup_instance(sdev->host->host_no);
1742 fusion = instance->ctrl_context;
1744 if (!fusion)
1745 return;
1747 if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS) {
1748 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1749 + sdev->id;
1750 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1751 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1752 raid = MR_LdRaidGet(ld, local_map_ptr);
1754 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1755 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1759 static int megasas_slave_configure(struct scsi_device *sdev)
1761 u16 pd_index = 0;
1762 struct megasas_instance *instance;
1764 instance = megasas_lookup_instance(sdev->host->host_no);
1765 if (instance->allow_fw_scan) {
1766 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1767 sdev->type == TYPE_DISK) {
1768 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1769 sdev->id;
1770 if (instance->pd_list[pd_index].driveState !=
1771 MR_PD_STATE_SYSTEM)
1772 return -ENXIO;
1775 megasas_set_dma_alignment(sdev);
1777 * The RAID firmware may require extended timeouts.
1779 blk_queue_rq_timeout(sdev->request_queue,
1780 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1782 return 0;
1785 static int megasas_slave_alloc(struct scsi_device *sdev)
1787 u16 pd_index = 0;
1788 struct megasas_instance *instance ;
1790 instance = megasas_lookup_instance(sdev->host->host_no);
1791 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1793 * Open the OS scan to the SYSTEM PD
1795 pd_index =
1796 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1797 sdev->id;
1798 if ((instance->allow_fw_scan || instance->pd_list[pd_index].driveState ==
1799 MR_PD_STATE_SYSTEM)) {
1800 return 0;
1802 return -ENXIO;
1804 return 0;
1808 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1809 * kill adapter
1810 * @instance: Adapter soft state
1813 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1815 int i;
1816 struct megasas_cmd *cmd_mfi;
1817 struct megasas_cmd_fusion *cmd_fusion;
1818 struct fusion_context *fusion = instance->ctrl_context;
1820 /* Find all outstanding ioctls */
1821 if (fusion) {
1822 for (i = 0; i < instance->max_fw_cmds; i++) {
1823 cmd_fusion = fusion->cmd_list[i];
1824 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1825 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1826 if (cmd_mfi->sync_cmd &&
1827 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
1828 cmd_mfi->frame->hdr.cmd_status =
1829 MFI_STAT_WRONG_STATE;
1830 megasas_complete_cmd(instance,
1831 cmd_mfi, DID_OK);
1835 } else {
1836 for (i = 0; i < instance->max_fw_cmds; i++) {
1837 cmd_mfi = instance->cmd_list[i];
1838 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1839 MFI_CMD_ABORT)
1840 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1846 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1848 /* Set critical error to block I/O & ioctls in case caller didn't */
1849 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1850 /* Wait 1 second to ensure IO or ioctls in build have posted */
1851 msleep(1000);
1852 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1853 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1854 (instance->ctrl_context)) {
1855 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1856 /* Flush */
1857 readl(&instance->reg_set->doorbell);
1858 if (instance->mpio && instance->requestorId)
1859 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1860 } else {
1861 writel(MFI_STOP_ADP,
1862 &instance->reg_set->inbound_doorbell);
1864 /* Complete outstanding ioctls when adapter is killed */
1865 megasas_complete_outstanding_ioctls(instance);
1869 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1870 * restored to max value
1871 * @instance: Adapter soft state
1874 void
1875 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1877 unsigned long flags;
1879 if (instance->flag & MEGASAS_FW_BUSY
1880 && time_after(jiffies, instance->last_time + 5 * HZ)
1881 && atomic_read(&instance->fw_outstanding) <
1882 instance->throttlequeuedepth + 1) {
1884 spin_lock_irqsave(instance->host->host_lock, flags);
1885 instance->flag &= ~MEGASAS_FW_BUSY;
1887 instance->host->can_queue = instance->max_scsi_cmds;
1888 spin_unlock_irqrestore(instance->host->host_lock, flags);
1893 * megasas_complete_cmd_dpc - Returns FW's controller structure
1894 * @instance_addr: Address of adapter soft state
1896 * Tasklet to complete cmds
1898 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1900 u32 producer;
1901 u32 consumer;
1902 u32 context;
1903 struct megasas_cmd *cmd;
1904 struct megasas_instance *instance =
1905 (struct megasas_instance *)instance_addr;
1906 unsigned long flags;
1908 /* If we have already declared adapter dead, donot complete cmds */
1909 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1910 return;
1912 spin_lock_irqsave(&instance->completion_lock, flags);
1914 producer = le32_to_cpu(*instance->producer);
1915 consumer = le32_to_cpu(*instance->consumer);
1917 while (consumer != producer) {
1918 context = le32_to_cpu(instance->reply_queue[consumer]);
1919 if (context >= instance->max_fw_cmds) {
1920 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1921 context);
1922 BUG();
1925 cmd = instance->cmd_list[context];
1927 megasas_complete_cmd(instance, cmd, DID_OK);
1929 consumer++;
1930 if (consumer == (instance->max_fw_cmds + 1)) {
1931 consumer = 0;
1935 *instance->consumer = cpu_to_le32(producer);
1937 spin_unlock_irqrestore(&instance->completion_lock, flags);
1940 * Check if we can restore can_queue
1942 megasas_check_and_restore_queue_depth(instance);
1946 * megasas_start_timer - Initializes a timer object
1947 * @instance: Adapter soft state
1948 * @timer: timer object to be initialized
1949 * @fn: timer function
1950 * @interval: time interval between timer function call
1953 void megasas_start_timer(struct megasas_instance *instance,
1954 struct timer_list *timer,
1955 void *fn, unsigned long interval)
1957 init_timer(timer);
1958 timer->expires = jiffies + interval;
1959 timer->data = (unsigned long)instance;
1960 timer->function = fn;
1961 add_timer(timer);
1964 static void
1965 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1967 static void
1968 process_fw_state_change_wq(struct work_struct *work);
1970 void megasas_do_ocr(struct megasas_instance *instance)
1972 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1973 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1974 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1975 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1977 instance->instancet->disable_intr(instance);
1978 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1979 instance->issuepend_done = 0;
1981 atomic_set(&instance->fw_outstanding, 0);
1982 megasas_internal_reset_defer_cmds(instance);
1983 process_fw_state_change_wq(&instance->work_init);
1986 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1987 int initial)
1989 struct megasas_cmd *cmd;
1990 struct megasas_dcmd_frame *dcmd;
1991 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1992 dma_addr_t new_affiliation_111_h;
1993 int ld, retval = 0;
1994 u8 thisVf;
1996 cmd = megasas_get_cmd(instance);
1998 if (!cmd) {
1999 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2000 "Failed to get cmd for scsi%d\n",
2001 instance->host->host_no);
2002 return -ENOMEM;
2005 dcmd = &cmd->frame->dcmd;
2007 if (!instance->vf_affiliation_111) {
2008 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2009 "affiliation for scsi%d\n", instance->host->host_no);
2010 megasas_return_cmd(instance, cmd);
2011 return -ENOMEM;
2014 if (initial)
2015 memset(instance->vf_affiliation_111, 0,
2016 sizeof(struct MR_LD_VF_AFFILIATION_111));
2017 else {
2018 new_affiliation_111 =
2019 pci_alloc_consistent(instance->pdev,
2020 sizeof(struct MR_LD_VF_AFFILIATION_111),
2021 &new_affiliation_111_h);
2022 if (!new_affiliation_111) {
2023 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2024 "memory for new affiliation for scsi%d\n",
2025 instance->host->host_no);
2026 megasas_return_cmd(instance, cmd);
2027 return -ENOMEM;
2029 memset(new_affiliation_111, 0,
2030 sizeof(struct MR_LD_VF_AFFILIATION_111));
2033 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2035 dcmd->cmd = MFI_CMD_DCMD;
2036 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2037 dcmd->sge_count = 1;
2038 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2039 dcmd->timeout = 0;
2040 dcmd->pad_0 = 0;
2041 dcmd->data_xfer_len =
2042 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2043 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2045 if (initial)
2046 dcmd->sgl.sge32[0].phys_addr =
2047 cpu_to_le32(instance->vf_affiliation_111_h);
2048 else
2049 dcmd->sgl.sge32[0].phys_addr =
2050 cpu_to_le32(new_affiliation_111_h);
2052 dcmd->sgl.sge32[0].length = cpu_to_le32(
2053 sizeof(struct MR_LD_VF_AFFILIATION_111));
2055 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2056 "scsi%d\n", instance->host->host_no);
2058 megasas_issue_blocked_cmd(instance, cmd, 0);
2060 if (dcmd->cmd_status) {
2061 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2062 " failed with status 0x%x for scsi%d\n",
2063 dcmd->cmd_status, instance->host->host_no);
2064 retval = 1; /* Do a scan if we couldn't get affiliation */
2065 goto out;
2068 if (!initial) {
2069 thisVf = new_affiliation_111->thisVf;
2070 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2071 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2072 new_affiliation_111->map[ld].policy[thisVf]) {
2073 dev_warn(&instance->pdev->dev, "SR-IOV: "
2074 "Got new LD/VF affiliation for scsi%d\n",
2075 instance->host->host_no);
2076 memcpy(instance->vf_affiliation_111,
2077 new_affiliation_111,
2078 sizeof(struct MR_LD_VF_AFFILIATION_111));
2079 retval = 1;
2080 goto out;
2083 out:
2084 if (new_affiliation_111) {
2085 pci_free_consistent(instance->pdev,
2086 sizeof(struct MR_LD_VF_AFFILIATION_111),
2087 new_affiliation_111,
2088 new_affiliation_111_h);
2091 megasas_return_cmd(instance, cmd);
2093 return retval;
2096 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2097 int initial)
2099 struct megasas_cmd *cmd;
2100 struct megasas_dcmd_frame *dcmd;
2101 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2102 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2103 dma_addr_t new_affiliation_h;
2104 int i, j, retval = 0, found = 0, doscan = 0;
2105 u8 thisVf;
2107 cmd = megasas_get_cmd(instance);
2109 if (!cmd) {
2110 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2111 "Failed to get cmd for scsi%d\n",
2112 instance->host->host_no);
2113 return -ENOMEM;
2116 dcmd = &cmd->frame->dcmd;
2118 if (!instance->vf_affiliation) {
2119 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2120 "affiliation for scsi%d\n", instance->host->host_no);
2121 megasas_return_cmd(instance, cmd);
2122 return -ENOMEM;
2125 if (initial)
2126 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2127 sizeof(struct MR_LD_VF_AFFILIATION));
2128 else {
2129 new_affiliation =
2130 pci_alloc_consistent(instance->pdev,
2131 (MAX_LOGICAL_DRIVES + 1) *
2132 sizeof(struct MR_LD_VF_AFFILIATION),
2133 &new_affiliation_h);
2134 if (!new_affiliation) {
2135 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2136 "memory for new affiliation for scsi%d\n",
2137 instance->host->host_no);
2138 megasas_return_cmd(instance, cmd);
2139 return -ENOMEM;
2141 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2142 sizeof(struct MR_LD_VF_AFFILIATION));
2145 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2147 dcmd->cmd = MFI_CMD_DCMD;
2148 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2149 dcmd->sge_count = 1;
2150 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2151 dcmd->timeout = 0;
2152 dcmd->pad_0 = 0;
2153 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2154 sizeof(struct MR_LD_VF_AFFILIATION));
2155 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2157 if (initial)
2158 dcmd->sgl.sge32[0].phys_addr =
2159 cpu_to_le32(instance->vf_affiliation_h);
2160 else
2161 dcmd->sgl.sge32[0].phys_addr =
2162 cpu_to_le32(new_affiliation_h);
2164 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2165 sizeof(struct MR_LD_VF_AFFILIATION));
2167 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2168 "scsi%d\n", instance->host->host_no);
2170 megasas_issue_blocked_cmd(instance, cmd, 0);
2172 if (dcmd->cmd_status) {
2173 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2174 " failed with status 0x%x for scsi%d\n",
2175 dcmd->cmd_status, instance->host->host_no);
2176 retval = 1; /* Do a scan if we couldn't get affiliation */
2177 goto out;
2180 if (!initial) {
2181 if (!new_affiliation->ldCount) {
2182 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2183 "affiliation for passive path for scsi%d\n",
2184 instance->host->host_no);
2185 retval = 1;
2186 goto out;
2188 newmap = new_affiliation->map;
2189 savedmap = instance->vf_affiliation->map;
2190 thisVf = new_affiliation->thisVf;
2191 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2192 found = 0;
2193 for (j = 0; j < instance->vf_affiliation->ldCount;
2194 j++) {
2195 if (newmap->ref.targetId ==
2196 savedmap->ref.targetId) {
2197 found = 1;
2198 if (newmap->policy[thisVf] !=
2199 savedmap->policy[thisVf]) {
2200 doscan = 1;
2201 goto out;
2204 savedmap = (struct MR_LD_VF_MAP *)
2205 ((unsigned char *)savedmap +
2206 savedmap->size);
2208 if (!found && newmap->policy[thisVf] !=
2209 MR_LD_ACCESS_HIDDEN) {
2210 doscan = 1;
2211 goto out;
2213 newmap = (struct MR_LD_VF_MAP *)
2214 ((unsigned char *)newmap + newmap->size);
2217 newmap = new_affiliation->map;
2218 savedmap = instance->vf_affiliation->map;
2220 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2221 found = 0;
2222 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2223 if (savedmap->ref.targetId ==
2224 newmap->ref.targetId) {
2225 found = 1;
2226 if (savedmap->policy[thisVf] !=
2227 newmap->policy[thisVf]) {
2228 doscan = 1;
2229 goto out;
2232 newmap = (struct MR_LD_VF_MAP *)
2233 ((unsigned char *)newmap +
2234 newmap->size);
2236 if (!found && savedmap->policy[thisVf] !=
2237 MR_LD_ACCESS_HIDDEN) {
2238 doscan = 1;
2239 goto out;
2241 savedmap = (struct MR_LD_VF_MAP *)
2242 ((unsigned char *)savedmap +
2243 savedmap->size);
2246 out:
2247 if (doscan) {
2248 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2249 "affiliation for scsi%d\n", instance->host->host_no);
2250 memcpy(instance->vf_affiliation, new_affiliation,
2251 new_affiliation->size);
2252 retval = 1;
2255 if (new_affiliation)
2256 pci_free_consistent(instance->pdev,
2257 (MAX_LOGICAL_DRIVES + 1) *
2258 sizeof(struct MR_LD_VF_AFFILIATION),
2259 new_affiliation, new_affiliation_h);
2260 megasas_return_cmd(instance, cmd);
2262 return retval;
2265 /* This function will get the current SR-IOV LD/VF affiliation */
2266 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2267 int initial)
2269 int retval;
2271 if (instance->PlasmaFW111)
2272 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2273 else
2274 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2275 return retval;
2278 /* This function will tell FW to start the SR-IOV heartbeat */
2279 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2280 int initial)
2282 struct megasas_cmd *cmd;
2283 struct megasas_dcmd_frame *dcmd;
2284 int retval = 0;
2286 cmd = megasas_get_cmd(instance);
2288 if (!cmd) {
2289 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2290 "Failed to get cmd for scsi%d\n",
2291 instance->host->host_no);
2292 return -ENOMEM;
2295 dcmd = &cmd->frame->dcmd;
2297 if (initial) {
2298 instance->hb_host_mem =
2299 pci_zalloc_consistent(instance->pdev,
2300 sizeof(struct MR_CTRL_HB_HOST_MEM),
2301 &instance->hb_host_mem_h);
2302 if (!instance->hb_host_mem) {
2303 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2304 " memory for heartbeat host memory for scsi%d\n",
2305 instance->host->host_no);
2306 retval = -ENOMEM;
2307 goto out;
2311 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2313 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2314 dcmd->cmd = MFI_CMD_DCMD;
2315 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2316 dcmd->sge_count = 1;
2317 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2318 dcmd->timeout = 0;
2319 dcmd->pad_0 = 0;
2320 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2321 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2322 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2323 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2325 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2326 instance->host->host_no);
2328 if (instance->ctrl_context && !instance->mask_interrupts)
2329 retval = megasas_issue_blocked_cmd(instance, cmd,
2330 MEGASAS_ROUTINE_WAIT_TIME_VF);
2331 else
2332 retval = megasas_issue_polled(instance, cmd);
2334 if (retval) {
2335 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2336 "_MEM_ALLOC DCMD %s for scsi%d\n",
2337 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2338 "timed out" : "failed", instance->host->host_no);
2339 retval = 1;
2342 out:
2343 megasas_return_cmd(instance, cmd);
2345 return retval;
2348 /* Handler for SR-IOV heartbeat */
2349 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2351 struct megasas_instance *instance =
2352 (struct megasas_instance *)instance_addr;
2354 if (instance->hb_host_mem->HB.fwCounter !=
2355 instance->hb_host_mem->HB.driverCounter) {
2356 instance->hb_host_mem->HB.driverCounter =
2357 instance->hb_host_mem->HB.fwCounter;
2358 mod_timer(&instance->sriov_heartbeat_timer,
2359 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2360 } else {
2361 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2362 "completed for scsi%d\n", instance->host->host_no);
2363 schedule_work(&instance->work_init);
2368 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2369 * @instance: Adapter soft state
2371 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2372 * complete all its outstanding commands. Returns error if one or more IOs
2373 * are pending after this time period. It also marks the controller dead.
2375 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2377 int i;
2378 u32 reset_index;
2379 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2380 u8 adprecovery;
2381 unsigned long flags;
2382 struct list_head clist_local;
2383 struct megasas_cmd *reset_cmd;
2384 u32 fw_state;
2385 u8 kill_adapter_flag;
2387 spin_lock_irqsave(&instance->hba_lock, flags);
2388 adprecovery = instance->adprecovery;
2389 spin_unlock_irqrestore(&instance->hba_lock, flags);
2391 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2393 INIT_LIST_HEAD(&clist_local);
2394 spin_lock_irqsave(&instance->hba_lock, flags);
2395 list_splice_init(&instance->internal_reset_pending_q,
2396 &clist_local);
2397 spin_unlock_irqrestore(&instance->hba_lock, flags);
2399 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2400 for (i = 0; i < wait_time; i++) {
2401 msleep(1000);
2402 spin_lock_irqsave(&instance->hba_lock, flags);
2403 adprecovery = instance->adprecovery;
2404 spin_unlock_irqrestore(&instance->hba_lock, flags);
2405 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2406 break;
2409 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2410 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2411 spin_lock_irqsave(&instance->hba_lock, flags);
2412 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2413 spin_unlock_irqrestore(&instance->hba_lock, flags);
2414 return FAILED;
2417 reset_index = 0;
2418 while (!list_empty(&clist_local)) {
2419 reset_cmd = list_entry((&clist_local)->next,
2420 struct megasas_cmd, list);
2421 list_del_init(&reset_cmd->list);
2422 if (reset_cmd->scmd) {
2423 reset_cmd->scmd->result = DID_RESET << 16;
2424 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2425 reset_index, reset_cmd,
2426 reset_cmd->scmd->cmnd[0]);
2428 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2429 megasas_return_cmd(instance, reset_cmd);
2430 } else if (reset_cmd->sync_cmd) {
2431 dev_notice(&instance->pdev->dev, "%p synch cmds"
2432 "reset queue\n",
2433 reset_cmd);
2435 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2436 instance->instancet->fire_cmd(instance,
2437 reset_cmd->frame_phys_addr,
2438 0, instance->reg_set);
2439 } else {
2440 dev_notice(&instance->pdev->dev, "%p unexpected"
2441 "cmds lst\n",
2442 reset_cmd);
2444 reset_index++;
2447 return SUCCESS;
2450 for (i = 0; i < resetwaittime; i++) {
2451 int outstanding = atomic_read(&instance->fw_outstanding);
2453 if (!outstanding)
2454 break;
2456 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2457 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2458 "commands to complete\n",i,outstanding);
2460 * Call cmd completion routine. Cmd to be
2461 * be completed directly without depending on isr.
2463 megasas_complete_cmd_dpc((unsigned long)instance);
2466 msleep(1000);
2469 i = 0;
2470 kill_adapter_flag = 0;
2471 do {
2472 fw_state = instance->instancet->read_fw_status_reg(
2473 instance->reg_set) & MFI_STATE_MASK;
2474 if ((fw_state == MFI_STATE_FAULT) &&
2475 (instance->disableOnlineCtrlReset == 0)) {
2476 if (i == 3) {
2477 kill_adapter_flag = 2;
2478 break;
2480 megasas_do_ocr(instance);
2481 kill_adapter_flag = 1;
2483 /* wait for 1 secs to let FW finish the pending cmds */
2484 msleep(1000);
2486 i++;
2487 } while (i <= 3);
2489 if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
2490 if (instance->disableOnlineCtrlReset == 0) {
2491 megasas_do_ocr(instance);
2493 /* wait for 5 secs to let FW finish the pending cmds */
2494 for (i = 0; i < wait_time; i++) {
2495 int outstanding =
2496 atomic_read(&instance->fw_outstanding);
2497 if (!outstanding)
2498 return SUCCESS;
2499 msleep(1000);
2504 if (atomic_read(&instance->fw_outstanding) ||
2505 (kill_adapter_flag == 2)) {
2506 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2508 * Send signal to FW to stop processing any pending cmds.
2509 * The controller will be taken offline by the OS now.
2511 if ((instance->pdev->device ==
2512 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2513 (instance->pdev->device ==
2514 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2515 writel(MFI_STOP_ADP,
2516 &instance->reg_set->doorbell);
2517 } else {
2518 writel(MFI_STOP_ADP,
2519 &instance->reg_set->inbound_doorbell);
2521 megasas_dump_pending_frames(instance);
2522 spin_lock_irqsave(&instance->hba_lock, flags);
2523 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2524 spin_unlock_irqrestore(&instance->hba_lock, flags);
2525 return FAILED;
2528 dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2530 return SUCCESS;
2534 * megasas_generic_reset - Generic reset routine
2535 * @scmd: Mid-layer SCSI command
2537 * This routine implements a generic reset handler for device, bus and host
2538 * reset requests. Device, bus and host specific reset handlers can use this
2539 * function after they do their specific tasks.
2541 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2543 int ret_val;
2544 struct megasas_instance *instance;
2546 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2548 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2549 scmd->cmnd[0], scmd->retries);
2551 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2552 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2553 return FAILED;
2556 ret_val = megasas_wait_for_outstanding(instance);
2557 if (ret_val == SUCCESS)
2558 dev_notice(&instance->pdev->dev, "reset successful\n");
2559 else
2560 dev_err(&instance->pdev->dev, "failed to do reset\n");
2562 return ret_val;
2566 * megasas_reset_timer - quiesce the adapter if required
2567 * @scmd: scsi cmnd
2569 * Sets the FW busy flag and reduces the host->can_queue if the
2570 * cmd has not been completed within the timeout period.
2572 static enum
2573 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2575 struct megasas_instance *instance;
2576 unsigned long flags;
2578 if (time_after(jiffies, scmd->jiffies_at_alloc +
2579 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2580 return BLK_EH_NOT_HANDLED;
2583 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2584 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2585 /* FW is busy, throttle IO */
2586 spin_lock_irqsave(instance->host->host_lock, flags);
2588 instance->host->can_queue = instance->throttlequeuedepth;
2589 instance->last_time = jiffies;
2590 instance->flag |= MEGASAS_FW_BUSY;
2592 spin_unlock_irqrestore(instance->host->host_lock, flags);
2594 return BLK_EH_RESET_TIMER;
2598 * megasas_reset_device - Device reset handler entry point
2600 static int megasas_reset_device(struct scsi_cmnd *scmd)
2603 * First wait for all commands to complete
2605 return megasas_generic_reset(scmd);
2609 * megasas_reset_bus_host - Bus & host reset handler entry point
2611 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2613 int ret;
2614 struct megasas_instance *instance;
2616 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2619 * First wait for all commands to complete
2621 if (instance->ctrl_context)
2622 ret = megasas_reset_fusion(scmd->device->host, 1);
2623 else
2624 ret = megasas_generic_reset(scmd);
2626 return ret;
2630 * megasas_bios_param - Returns disk geometry for a disk
2631 * @sdev: device handle
2632 * @bdev: block device
2633 * @capacity: drive capacity
2634 * @geom: geometry parameters
2636 static int
2637 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2638 sector_t capacity, int geom[])
2640 int heads;
2641 int sectors;
2642 sector_t cylinders;
2643 unsigned long tmp;
2645 /* Default heads (64) & sectors (32) */
2646 heads = 64;
2647 sectors = 32;
2649 tmp = heads * sectors;
2650 cylinders = capacity;
2652 sector_div(cylinders, tmp);
2655 * Handle extended translation size for logical drives > 1Gb
2658 if (capacity >= 0x200000) {
2659 heads = 255;
2660 sectors = 63;
2661 tmp = heads*sectors;
2662 cylinders = capacity;
2663 sector_div(cylinders, tmp);
2666 geom[0] = heads;
2667 geom[1] = sectors;
2668 geom[2] = cylinders;
2670 return 0;
2673 static void megasas_aen_polling(struct work_struct *work);
2676 * megasas_service_aen - Processes an event notification
2677 * @instance: Adapter soft state
2678 * @cmd: AEN command completed by the ISR
2680 * For AEN, driver sends a command down to FW that is held by the FW till an
2681 * event occurs. When an event of interest occurs, FW completes the command
2682 * that it was previously holding.
2684 * This routines sends SIGIO signal to processes that have registered with the
2685 * driver for AEN.
2687 static void
2688 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2690 unsigned long flags;
2693 * Don't signal app if it is just an aborted previously registered aen
2695 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2696 spin_lock_irqsave(&poll_aen_lock, flags);
2697 megasas_poll_wait_aen = 1;
2698 spin_unlock_irqrestore(&poll_aen_lock, flags);
2699 wake_up(&megasas_poll_wait);
2700 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2702 else
2703 cmd->abort_aen = 0;
2705 instance->aen_cmd = NULL;
2707 megasas_return_cmd(instance, cmd);
2709 if ((instance->unload == 0) &&
2710 ((instance->issuepend_done == 1))) {
2711 struct megasas_aen_event *ev;
2713 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2714 if (!ev) {
2715 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2716 } else {
2717 ev->instance = instance;
2718 instance->ev = ev;
2719 INIT_DELAYED_WORK(&ev->hotplug_work,
2720 megasas_aen_polling);
2721 schedule_delayed_work(&ev->hotplug_work, 0);
2726 static ssize_t
2727 megasas_fw_crash_buffer_store(struct device *cdev,
2728 struct device_attribute *attr, const char *buf, size_t count)
2730 struct Scsi_Host *shost = class_to_shost(cdev);
2731 struct megasas_instance *instance =
2732 (struct megasas_instance *) shost->hostdata;
2733 int val = 0;
2734 unsigned long flags;
2736 if (kstrtoint(buf, 0, &val) != 0)
2737 return -EINVAL;
2739 spin_lock_irqsave(&instance->crashdump_lock, flags);
2740 instance->fw_crash_buffer_offset = val;
2741 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2742 return strlen(buf);
2745 static ssize_t
2746 megasas_fw_crash_buffer_show(struct device *cdev,
2747 struct device_attribute *attr, char *buf)
2749 struct Scsi_Host *shost = class_to_shost(cdev);
2750 struct megasas_instance *instance =
2751 (struct megasas_instance *) shost->hostdata;
2752 u32 size;
2753 unsigned long buff_addr;
2754 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2755 unsigned long src_addr;
2756 unsigned long flags;
2757 u32 buff_offset;
2759 spin_lock_irqsave(&instance->crashdump_lock, flags);
2760 buff_offset = instance->fw_crash_buffer_offset;
2761 if (!instance->crash_dump_buf &&
2762 !((instance->fw_crash_state == AVAILABLE) ||
2763 (instance->fw_crash_state == COPYING))) {
2764 dev_err(&instance->pdev->dev,
2765 "Firmware crash dump is not available\n");
2766 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2767 return -EINVAL;
2770 buff_addr = (unsigned long) buf;
2772 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2773 dev_err(&instance->pdev->dev,
2774 "Firmware crash dump offset is out of range\n");
2775 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2776 return 0;
2779 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2780 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2782 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2783 (buff_offset % dmachunk);
2784 memcpy(buf, (void *)src_addr, size);
2785 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2787 return size;
2790 static ssize_t
2791 megasas_fw_crash_buffer_size_show(struct device *cdev,
2792 struct device_attribute *attr, char *buf)
2794 struct Scsi_Host *shost = class_to_shost(cdev);
2795 struct megasas_instance *instance =
2796 (struct megasas_instance *) shost->hostdata;
2798 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2799 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2802 static ssize_t
2803 megasas_fw_crash_state_store(struct device *cdev,
2804 struct device_attribute *attr, const char *buf, size_t count)
2806 struct Scsi_Host *shost = class_to_shost(cdev);
2807 struct megasas_instance *instance =
2808 (struct megasas_instance *) shost->hostdata;
2809 int val = 0;
2810 unsigned long flags;
2812 if (kstrtoint(buf, 0, &val) != 0)
2813 return -EINVAL;
2815 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2816 dev_err(&instance->pdev->dev, "application updates invalid "
2817 "firmware crash state\n");
2818 return -EINVAL;
2821 instance->fw_crash_state = val;
2823 if ((val == COPIED) || (val == COPY_ERROR)) {
2824 spin_lock_irqsave(&instance->crashdump_lock, flags);
2825 megasas_free_host_crash_buffer(instance);
2826 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2827 if (val == COPY_ERROR)
2828 dev_info(&instance->pdev->dev, "application failed to "
2829 "copy Firmware crash dump\n");
2830 else
2831 dev_info(&instance->pdev->dev, "Firmware crash dump "
2832 "copied successfully\n");
2834 return strlen(buf);
2837 static ssize_t
2838 megasas_fw_crash_state_show(struct device *cdev,
2839 struct device_attribute *attr, char *buf)
2841 struct Scsi_Host *shost = class_to_shost(cdev);
2842 struct megasas_instance *instance =
2843 (struct megasas_instance *) shost->hostdata;
2845 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2848 static ssize_t
2849 megasas_page_size_show(struct device *cdev,
2850 struct device_attribute *attr, char *buf)
2852 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2855 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2856 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2857 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2858 megasas_fw_crash_buffer_size_show, NULL);
2859 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2860 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2861 static DEVICE_ATTR(page_size, S_IRUGO,
2862 megasas_page_size_show, NULL);
2864 struct device_attribute *megaraid_host_attrs[] = {
2865 &dev_attr_fw_crash_buffer_size,
2866 &dev_attr_fw_crash_buffer,
2867 &dev_attr_fw_crash_state,
2868 &dev_attr_page_size,
2869 NULL,
2873 * Scsi host template for megaraid_sas driver
2875 static struct scsi_host_template megasas_template = {
2877 .module = THIS_MODULE,
2878 .name = "Avago SAS based MegaRAID driver",
2879 .proc_name = "megaraid_sas",
2880 .slave_configure = megasas_slave_configure,
2881 .slave_alloc = megasas_slave_alloc,
2882 .queuecommand = megasas_queue_command,
2883 .eh_device_reset_handler = megasas_reset_device,
2884 .eh_bus_reset_handler = megasas_reset_bus_host,
2885 .eh_host_reset_handler = megasas_reset_bus_host,
2886 .eh_timed_out = megasas_reset_timer,
2887 .shost_attrs = megaraid_host_attrs,
2888 .bios_param = megasas_bios_param,
2889 .use_clustering = ENABLE_CLUSTERING,
2890 .change_queue_depth = scsi_change_queue_depth,
2891 .no_write_same = 1,
2895 * megasas_complete_int_cmd - Completes an internal command
2896 * @instance: Adapter soft state
2897 * @cmd: Command to be completed
2899 * The megasas_issue_blocked_cmd() function waits for a command to complete
2900 * after it issues a command. This function wakes up that waiting routine by
2901 * calling wake_up() on the wait queue.
2903 static void
2904 megasas_complete_int_cmd(struct megasas_instance *instance,
2905 struct megasas_cmd *cmd)
2907 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2908 wake_up(&instance->int_cmd_wait_q);
2912 * megasas_complete_abort - Completes aborting a command
2913 * @instance: Adapter soft state
2914 * @cmd: Cmd that was issued to abort another cmd
2916 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2917 * after it issues an abort on a previously issued command. This function
2918 * wakes up all functions waiting on the same wait queue.
2920 static void
2921 megasas_complete_abort(struct megasas_instance *instance,
2922 struct megasas_cmd *cmd)
2924 if (cmd->sync_cmd) {
2925 cmd->sync_cmd = 0;
2926 cmd->cmd_status_drv = 0;
2927 wake_up(&instance->abort_cmd_wait_q);
2932 * megasas_complete_cmd - Completes a command
2933 * @instance: Adapter soft state
2934 * @cmd: Command to be completed
2935 * @alt_status: If non-zero, use this value as status to
2936 * SCSI mid-layer instead of the value returned
2937 * by the FW. This should be used if caller wants
2938 * an alternate status (as in the case of aborted
2939 * commands)
2941 void
2942 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2943 u8 alt_status)
2945 int exception = 0;
2946 struct megasas_header *hdr = &cmd->frame->hdr;
2947 unsigned long flags;
2948 struct fusion_context *fusion = instance->ctrl_context;
2949 u32 opcode, status;
2951 /* flag for the retry reset */
2952 cmd->retry_for_fw_reset = 0;
2954 if (cmd->scmd)
2955 cmd->scmd->SCp.ptr = NULL;
2957 switch (hdr->cmd) {
2958 case MFI_CMD_INVALID:
2959 /* Some older 1068 controller FW may keep a pended
2960 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2961 when booting the kdump kernel. Ignore this command to
2962 prevent a kernel panic on shutdown of the kdump kernel. */
2963 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2964 "completed\n");
2965 dev_warn(&instance->pdev->dev, "If you have a controller "
2966 "other than PERC5, please upgrade your firmware\n");
2967 break;
2968 case MFI_CMD_PD_SCSI_IO:
2969 case MFI_CMD_LD_SCSI_IO:
2972 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2973 * issued either through an IO path or an IOCTL path. If it
2974 * was via IOCTL, we will send it to internal completion.
2976 if (cmd->sync_cmd) {
2977 cmd->sync_cmd = 0;
2978 megasas_complete_int_cmd(instance, cmd);
2979 break;
2982 case MFI_CMD_LD_READ:
2983 case MFI_CMD_LD_WRITE:
2985 if (alt_status) {
2986 cmd->scmd->result = alt_status << 16;
2987 exception = 1;
2990 if (exception) {
2992 atomic_dec(&instance->fw_outstanding);
2994 scsi_dma_unmap(cmd->scmd);
2995 cmd->scmd->scsi_done(cmd->scmd);
2996 megasas_return_cmd(instance, cmd);
2998 break;
3001 switch (hdr->cmd_status) {
3003 case MFI_STAT_OK:
3004 cmd->scmd->result = DID_OK << 16;
3005 break;
3007 case MFI_STAT_SCSI_IO_FAILED:
3008 case MFI_STAT_LD_INIT_IN_PROGRESS:
3009 cmd->scmd->result =
3010 (DID_ERROR << 16) | hdr->scsi_status;
3011 break;
3013 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3015 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3017 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3018 memset(cmd->scmd->sense_buffer, 0,
3019 SCSI_SENSE_BUFFERSIZE);
3020 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3021 hdr->sense_len);
3023 cmd->scmd->result |= DRIVER_SENSE << 24;
3026 break;
3028 case MFI_STAT_LD_OFFLINE:
3029 case MFI_STAT_DEVICE_NOT_FOUND:
3030 cmd->scmd->result = DID_BAD_TARGET << 16;
3031 break;
3033 default:
3034 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3035 hdr->cmd_status);
3036 cmd->scmd->result = DID_ERROR << 16;
3037 break;
3040 atomic_dec(&instance->fw_outstanding);
3042 scsi_dma_unmap(cmd->scmd);
3043 cmd->scmd->scsi_done(cmd->scmd);
3044 megasas_return_cmd(instance, cmd);
3046 break;
3048 case MFI_CMD_SMP:
3049 case MFI_CMD_STP:
3050 case MFI_CMD_DCMD:
3051 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3052 /* Check for LD map update */
3053 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3054 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3055 fusion->fast_path_io = 0;
3056 spin_lock_irqsave(instance->host->host_lock, flags);
3057 instance->map_update_cmd = NULL;
3058 if (cmd->frame->hdr.cmd_status != 0) {
3059 if (cmd->frame->hdr.cmd_status !=
3060 MFI_STAT_NOT_FOUND)
3061 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3062 cmd->frame->hdr.cmd_status);
3063 else {
3064 megasas_return_cmd(instance, cmd);
3065 spin_unlock_irqrestore(
3066 instance->host->host_lock,
3067 flags);
3068 break;
3070 } else
3071 instance->map_id++;
3072 megasas_return_cmd(instance, cmd);
3075 * Set fast path IO to ZERO.
3076 * Validate Map will set proper value.
3077 * Meanwhile all IOs will go as LD IO.
3079 if (MR_ValidateMapInfo(instance))
3080 fusion->fast_path_io = 1;
3081 else
3082 fusion->fast_path_io = 0;
3083 megasas_sync_map_info(instance);
3084 spin_unlock_irqrestore(instance->host->host_lock,
3085 flags);
3086 break;
3088 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3089 opcode == MR_DCMD_CTRL_EVENT_GET) {
3090 spin_lock_irqsave(&poll_aen_lock, flags);
3091 megasas_poll_wait_aen = 0;
3092 spin_unlock_irqrestore(&poll_aen_lock, flags);
3095 /* FW has an updated PD sequence */
3096 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3097 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3099 spin_lock_irqsave(instance->host->host_lock, flags);
3100 status = cmd->frame->hdr.cmd_status;
3101 instance->jbod_seq_cmd = NULL;
3102 megasas_return_cmd(instance, cmd);
3104 if (status == MFI_STAT_OK) {
3105 instance->pd_seq_map_id++;
3106 /* Re-register a pd sync seq num cmd */
3107 if (megasas_sync_pd_seq_num(instance, true))
3108 instance->use_seqnum_jbod_fp = false;
3109 } else
3110 instance->use_seqnum_jbod_fp = false;
3112 spin_unlock_irqrestore(instance->host->host_lock, flags);
3113 break;
3117 * See if got an event notification
3119 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3120 megasas_service_aen(instance, cmd);
3121 else
3122 megasas_complete_int_cmd(instance, cmd);
3124 break;
3126 case MFI_CMD_ABORT:
3128 * Cmd issued to abort another cmd returned
3130 megasas_complete_abort(instance, cmd);
3131 break;
3133 default:
3134 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3135 hdr->cmd);
3136 break;
3141 * megasas_issue_pending_cmds_again - issue all pending cmds
3142 * in FW again because of the fw reset
3143 * @instance: Adapter soft state
3145 static inline void
3146 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3148 struct megasas_cmd *cmd;
3149 struct list_head clist_local;
3150 union megasas_evt_class_locale class_locale;
3151 unsigned long flags;
3152 u32 seq_num;
3154 INIT_LIST_HEAD(&clist_local);
3155 spin_lock_irqsave(&instance->hba_lock, flags);
3156 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3157 spin_unlock_irqrestore(&instance->hba_lock, flags);
3159 while (!list_empty(&clist_local)) {
3160 cmd = list_entry((&clist_local)->next,
3161 struct megasas_cmd, list);
3162 list_del_init(&cmd->list);
3164 if (cmd->sync_cmd || cmd->scmd) {
3165 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3166 "detected to be pending while HBA reset\n",
3167 cmd, cmd->scmd, cmd->sync_cmd);
3169 cmd->retry_for_fw_reset++;
3171 if (cmd->retry_for_fw_reset == 3) {
3172 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3173 "was tried multiple times during reset."
3174 "Shutting down the HBA\n",
3175 cmd, cmd->scmd, cmd->sync_cmd);
3176 instance->instancet->disable_intr(instance);
3177 atomic_set(&instance->fw_reset_no_pci_access, 1);
3178 megaraid_sas_kill_hba(instance);
3179 return;
3183 if (cmd->sync_cmd == 1) {
3184 if (cmd->scmd) {
3185 dev_notice(&instance->pdev->dev, "unexpected"
3186 "cmd attached to internal command!\n");
3188 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3189 "on the internal reset queue,"
3190 "issue it again.\n", cmd);
3191 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3192 instance->instancet->fire_cmd(instance,
3193 cmd->frame_phys_addr,
3194 0, instance->reg_set);
3195 } else if (cmd->scmd) {
3196 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3197 "detected on the internal queue, issue again.\n",
3198 cmd, cmd->scmd->cmnd[0]);
3200 atomic_inc(&instance->fw_outstanding);
3201 instance->instancet->fire_cmd(instance,
3202 cmd->frame_phys_addr,
3203 cmd->frame_count-1, instance->reg_set);
3204 } else {
3205 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3206 "internal reset defer list while re-issue!!\n",
3207 cmd);
3211 if (instance->aen_cmd) {
3212 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3213 megasas_return_cmd(instance, instance->aen_cmd);
3215 instance->aen_cmd = NULL;
3219 * Initiate AEN (Asynchronous Event Notification)
3221 seq_num = instance->last_seq_num;
3222 class_locale.members.reserved = 0;
3223 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3224 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3226 megasas_register_aen(instance, seq_num, class_locale.word);
3230 * Move the internal reset pending commands to a deferred queue.
3232 * We move the commands pending at internal reset time to a
3233 * pending queue. This queue would be flushed after successful
3234 * completion of the internal reset sequence. if the internal reset
3235 * did not complete in time, the kernel reset handler would flush
3236 * these commands.
3238 static void
3239 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3241 struct megasas_cmd *cmd;
3242 int i;
3243 u32 max_cmd = instance->max_fw_cmds;
3244 u32 defer_index;
3245 unsigned long flags;
3247 defer_index = 0;
3248 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3249 for (i = 0; i < max_cmd; i++) {
3250 cmd = instance->cmd_list[i];
3251 if (cmd->sync_cmd == 1 || cmd->scmd) {
3252 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3253 "on the defer queue as internal\n",
3254 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3256 if (!list_empty(&cmd->list)) {
3257 dev_notice(&instance->pdev->dev, "ERROR while"
3258 " moving this cmd:%p, %d %p, it was"
3259 "discovered on some list?\n",
3260 cmd, cmd->sync_cmd, cmd->scmd);
3262 list_del_init(&cmd->list);
3264 defer_index++;
3265 list_add_tail(&cmd->list,
3266 &instance->internal_reset_pending_q);
3269 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3273 static void
3274 process_fw_state_change_wq(struct work_struct *work)
3276 struct megasas_instance *instance =
3277 container_of(work, struct megasas_instance, work_init);
3278 u32 wait;
3279 unsigned long flags;
3281 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3282 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3283 instance->adprecovery);
3284 return ;
3287 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3288 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3289 "state, restarting it...\n");
3291 instance->instancet->disable_intr(instance);
3292 atomic_set(&instance->fw_outstanding, 0);
3294 atomic_set(&instance->fw_reset_no_pci_access, 1);
3295 instance->instancet->adp_reset(instance, instance->reg_set);
3296 atomic_set(&instance->fw_reset_no_pci_access, 0);
3298 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3299 "initiating next stage...\n");
3301 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3302 "state 2 starting...\n");
3304 /* waiting for about 20 second before start the second init */
3305 for (wait = 0; wait < 30; wait++) {
3306 msleep(1000);
3309 if (megasas_transition_to_ready(instance, 1)) {
3310 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3312 atomic_set(&instance->fw_reset_no_pci_access, 1);
3313 megaraid_sas_kill_hba(instance);
3314 return ;
3317 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3318 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3319 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3321 *instance->consumer = *instance->producer;
3322 } else {
3323 *instance->consumer = 0;
3324 *instance->producer = 0;
3327 megasas_issue_init_mfi(instance);
3329 spin_lock_irqsave(&instance->hba_lock, flags);
3330 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3331 spin_unlock_irqrestore(&instance->hba_lock, flags);
3332 instance->instancet->enable_intr(instance);
3334 megasas_issue_pending_cmds_again(instance);
3335 instance->issuepend_done = 1;
3340 * megasas_deplete_reply_queue - Processes all completed commands
3341 * @instance: Adapter soft state
3342 * @alt_status: Alternate status to be returned to
3343 * SCSI mid-layer instead of the status
3344 * returned by the FW
3345 * Note: this must be called with hba lock held
3347 static int
3348 megasas_deplete_reply_queue(struct megasas_instance *instance,
3349 u8 alt_status)
3351 u32 mfiStatus;
3352 u32 fw_state;
3354 if ((mfiStatus = instance->instancet->check_reset(instance,
3355 instance->reg_set)) == 1) {
3356 return IRQ_HANDLED;
3359 if ((mfiStatus = instance->instancet->clear_intr(
3360 instance->reg_set)
3361 ) == 0) {
3362 /* Hardware may not set outbound_intr_status in MSI-X mode */
3363 if (!instance->msix_vectors)
3364 return IRQ_NONE;
3367 instance->mfiStatus = mfiStatus;
3369 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3370 fw_state = instance->instancet->read_fw_status_reg(
3371 instance->reg_set) & MFI_STATE_MASK;
3373 if (fw_state != MFI_STATE_FAULT) {
3374 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3375 fw_state);
3378 if ((fw_state == MFI_STATE_FAULT) &&
3379 (instance->disableOnlineCtrlReset == 0)) {
3380 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3382 if ((instance->pdev->device ==
3383 PCI_DEVICE_ID_LSI_SAS1064R) ||
3384 (instance->pdev->device ==
3385 PCI_DEVICE_ID_DELL_PERC5) ||
3386 (instance->pdev->device ==
3387 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3389 *instance->consumer =
3390 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3394 instance->instancet->disable_intr(instance);
3395 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
3396 instance->issuepend_done = 0;
3398 atomic_set(&instance->fw_outstanding, 0);
3399 megasas_internal_reset_defer_cmds(instance);
3401 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3402 fw_state, instance->adprecovery);
3404 schedule_work(&instance->work_init);
3405 return IRQ_HANDLED;
3407 } else {
3408 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3409 fw_state, instance->disableOnlineCtrlReset);
3413 tasklet_schedule(&instance->isr_tasklet);
3414 return IRQ_HANDLED;
3417 * megasas_isr - isr entry point
3419 static irqreturn_t megasas_isr(int irq, void *devp)
3421 struct megasas_irq_context *irq_context = devp;
3422 struct megasas_instance *instance = irq_context->instance;
3423 unsigned long flags;
3424 irqreturn_t rc;
3426 if (atomic_read(&instance->fw_reset_no_pci_access))
3427 return IRQ_HANDLED;
3429 spin_lock_irqsave(&instance->hba_lock, flags);
3430 rc = megasas_deplete_reply_queue(instance, DID_OK);
3431 spin_unlock_irqrestore(&instance->hba_lock, flags);
3433 return rc;
3437 * megasas_transition_to_ready - Move the FW to READY state
3438 * @instance: Adapter soft state
3440 * During the initialization, FW passes can potentially be in any one of
3441 * several possible states. If the FW in operational, waiting-for-handshake
3442 * states, driver must take steps to bring it to ready state. Otherwise, it
3443 * has to wait for the ready state.
3446 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3448 int i;
3449 u8 max_wait;
3450 u32 fw_state;
3451 u32 cur_state;
3452 u32 abs_state, curr_abs_state;
3454 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3455 fw_state = abs_state & MFI_STATE_MASK;
3457 if (fw_state != MFI_STATE_READY)
3458 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3459 " state\n");
3461 while (fw_state != MFI_STATE_READY) {
3463 switch (fw_state) {
3465 case MFI_STATE_FAULT:
3466 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3467 if (ocr) {
3468 max_wait = MEGASAS_RESET_WAIT_TIME;
3469 cur_state = MFI_STATE_FAULT;
3470 break;
3471 } else
3472 return -ENODEV;
3474 case MFI_STATE_WAIT_HANDSHAKE:
3476 * Set the CLR bit in inbound doorbell
3478 if ((instance->pdev->device ==
3479 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3480 (instance->pdev->device ==
3481 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3482 (instance->ctrl_context))
3483 writel(
3484 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3485 &instance->reg_set->doorbell);
3486 else
3487 writel(
3488 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3489 &instance->reg_set->inbound_doorbell);
3491 max_wait = MEGASAS_RESET_WAIT_TIME;
3492 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3493 break;
3495 case MFI_STATE_BOOT_MESSAGE_PENDING:
3496 if ((instance->pdev->device ==
3497 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3498 (instance->pdev->device ==
3499 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3500 (instance->ctrl_context))
3501 writel(MFI_INIT_HOTPLUG,
3502 &instance->reg_set->doorbell);
3503 else
3504 writel(MFI_INIT_HOTPLUG,
3505 &instance->reg_set->inbound_doorbell);
3507 max_wait = MEGASAS_RESET_WAIT_TIME;
3508 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3509 break;
3511 case MFI_STATE_OPERATIONAL:
3513 * Bring it to READY state; assuming max wait 10 secs
3515 instance->instancet->disable_intr(instance);
3516 if ((instance->pdev->device ==
3517 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3518 (instance->pdev->device ==
3519 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3520 (instance->ctrl_context)) {
3521 writel(MFI_RESET_FLAGS,
3522 &instance->reg_set->doorbell);
3524 if (instance->ctrl_context) {
3525 for (i = 0; i < (10 * 1000); i += 20) {
3526 if (readl(
3527 &instance->
3528 reg_set->
3529 doorbell) & 1)
3530 msleep(20);
3531 else
3532 break;
3535 } else
3536 writel(MFI_RESET_FLAGS,
3537 &instance->reg_set->inbound_doorbell);
3539 max_wait = MEGASAS_RESET_WAIT_TIME;
3540 cur_state = MFI_STATE_OPERATIONAL;
3541 break;
3543 case MFI_STATE_UNDEFINED:
3545 * This state should not last for more than 2 seconds
3547 max_wait = MEGASAS_RESET_WAIT_TIME;
3548 cur_state = MFI_STATE_UNDEFINED;
3549 break;
3551 case MFI_STATE_BB_INIT:
3552 max_wait = MEGASAS_RESET_WAIT_TIME;
3553 cur_state = MFI_STATE_BB_INIT;
3554 break;
3556 case MFI_STATE_FW_INIT:
3557 max_wait = MEGASAS_RESET_WAIT_TIME;
3558 cur_state = MFI_STATE_FW_INIT;
3559 break;
3561 case MFI_STATE_FW_INIT_2:
3562 max_wait = MEGASAS_RESET_WAIT_TIME;
3563 cur_state = MFI_STATE_FW_INIT_2;
3564 break;
3566 case MFI_STATE_DEVICE_SCAN:
3567 max_wait = MEGASAS_RESET_WAIT_TIME;
3568 cur_state = MFI_STATE_DEVICE_SCAN;
3569 break;
3571 case MFI_STATE_FLUSH_CACHE:
3572 max_wait = MEGASAS_RESET_WAIT_TIME;
3573 cur_state = MFI_STATE_FLUSH_CACHE;
3574 break;
3576 default:
3577 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3578 fw_state);
3579 return -ENODEV;
3583 * The cur_state should not last for more than max_wait secs
3585 for (i = 0; i < (max_wait * 1000); i++) {
3586 curr_abs_state = instance->instancet->
3587 read_fw_status_reg(instance->reg_set);
3589 if (abs_state == curr_abs_state) {
3590 msleep(1);
3591 } else
3592 break;
3596 * Return error if fw_state hasn't changed after max_wait
3598 if (curr_abs_state == abs_state) {
3599 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3600 "in %d secs\n", fw_state, max_wait);
3601 return -ENODEV;
3604 abs_state = curr_abs_state;
3605 fw_state = curr_abs_state & MFI_STATE_MASK;
3607 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3609 return 0;
3613 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3614 * @instance: Adapter soft state
3616 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3618 int i;
3619 u32 max_cmd = instance->max_mfi_cmds;
3620 struct megasas_cmd *cmd;
3622 if (!instance->frame_dma_pool)
3623 return;
3626 * Return all frames to pool
3628 for (i = 0; i < max_cmd; i++) {
3630 cmd = instance->cmd_list[i];
3632 if (cmd->frame)
3633 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3634 cmd->frame_phys_addr);
3636 if (cmd->sense)
3637 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3638 cmd->sense_phys_addr);
3642 * Now destroy the pool itself
3644 pci_pool_destroy(instance->frame_dma_pool);
3645 pci_pool_destroy(instance->sense_dma_pool);
3647 instance->frame_dma_pool = NULL;
3648 instance->sense_dma_pool = NULL;
3652 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3653 * @instance: Adapter soft state
3655 * Each command packet has an embedded DMA memory buffer that is used for
3656 * filling MFI frame and the SG list that immediately follows the frame. This
3657 * function creates those DMA memory buffers for each command packet by using
3658 * PCI pool facility.
3660 static int megasas_create_frame_pool(struct megasas_instance *instance)
3662 int i;
3663 u32 max_cmd;
3664 u32 sge_sz;
3665 u32 total_sz;
3666 u32 frame_count;
3667 struct megasas_cmd *cmd;
3669 max_cmd = instance->max_mfi_cmds;
3672 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3673 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3675 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3676 sizeof(struct megasas_sge32);
3678 if (instance->flag_ieee)
3679 sge_sz = sizeof(struct megasas_sge_skinny);
3682 * For MFI controllers.
3683 * max_num_sge = 60
3684 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3685 * Total 960 byte (15 MFI frame of 64 byte)
3687 * Fusion adapter require only 3 extra frame.
3688 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3689 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3690 * Total 192 byte (3 MFI frame of 64 byte)
3692 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3693 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3695 * Use DMA pool facility provided by PCI layer
3697 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3698 instance->pdev, total_sz, 256, 0);
3700 if (!instance->frame_dma_pool) {
3701 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3702 return -ENOMEM;
3705 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3706 instance->pdev, 128, 4, 0);
3708 if (!instance->sense_dma_pool) {
3709 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3711 pci_pool_destroy(instance->frame_dma_pool);
3712 instance->frame_dma_pool = NULL;
3714 return -ENOMEM;
3718 * Allocate and attach a frame to each of the commands in cmd_list.
3719 * By making cmd->index as the context instead of the &cmd, we can
3720 * always use 32bit context regardless of the architecture
3722 for (i = 0; i < max_cmd; i++) {
3724 cmd = instance->cmd_list[i];
3726 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3727 GFP_KERNEL, &cmd->frame_phys_addr);
3729 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3730 GFP_KERNEL, &cmd->sense_phys_addr);
3733 * megasas_teardown_frame_pool() takes care of freeing
3734 * whatever has been allocated
3736 if (!cmd->frame || !cmd->sense) {
3737 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3738 megasas_teardown_frame_pool(instance);
3739 return -ENOMEM;
3742 memset(cmd->frame, 0, total_sz);
3743 cmd->frame->io.context = cpu_to_le32(cmd->index);
3744 cmd->frame->io.pad_0 = 0;
3745 if (!instance->ctrl_context && reset_devices)
3746 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3749 return 0;
3753 * megasas_free_cmds - Free all the cmds in the free cmd pool
3754 * @instance: Adapter soft state
3756 void megasas_free_cmds(struct megasas_instance *instance)
3758 int i;
3760 /* First free the MFI frame pool */
3761 megasas_teardown_frame_pool(instance);
3763 /* Free all the commands in the cmd_list */
3764 for (i = 0; i < instance->max_mfi_cmds; i++)
3766 kfree(instance->cmd_list[i]);
3768 /* Free the cmd_list buffer itself */
3769 kfree(instance->cmd_list);
3770 instance->cmd_list = NULL;
3772 INIT_LIST_HEAD(&instance->cmd_pool);
3776 * megasas_alloc_cmds - Allocates the command packets
3777 * @instance: Adapter soft state
3779 * Each command that is issued to the FW, whether IO commands from the OS or
3780 * internal commands like IOCTLs, are wrapped in local data structure called
3781 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3782 * the FW.
3784 * Each frame has a 32-bit field called context (tag). This context is used
3785 * to get back the megasas_cmd from the frame when a frame gets completed in
3786 * the ISR. Typically the address of the megasas_cmd itself would be used as
3787 * the context. But we wanted to keep the differences between 32 and 64 bit
3788 * systems to the mininum. We always use 32 bit integers for the context. In
3789 * this driver, the 32 bit values are the indices into an array cmd_list.
3790 * This array is used only to look up the megasas_cmd given the context. The
3791 * free commands themselves are maintained in a linked list called cmd_pool.
3793 int megasas_alloc_cmds(struct megasas_instance *instance)
3795 int i;
3796 int j;
3797 u32 max_cmd;
3798 struct megasas_cmd *cmd;
3799 struct fusion_context *fusion;
3801 fusion = instance->ctrl_context;
3802 max_cmd = instance->max_mfi_cmds;
3805 * instance->cmd_list is an array of struct megasas_cmd pointers.
3806 * Allocate the dynamic array first and then allocate individual
3807 * commands.
3809 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3811 if (!instance->cmd_list) {
3812 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3813 return -ENOMEM;
3816 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3818 for (i = 0; i < max_cmd; i++) {
3819 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3820 GFP_KERNEL);
3822 if (!instance->cmd_list[i]) {
3824 for (j = 0; j < i; j++)
3825 kfree(instance->cmd_list[j]);
3827 kfree(instance->cmd_list);
3828 instance->cmd_list = NULL;
3830 return -ENOMEM;
3834 for (i = 0; i < max_cmd; i++) {
3835 cmd = instance->cmd_list[i];
3836 memset(cmd, 0, sizeof(struct megasas_cmd));
3837 cmd->index = i;
3838 cmd->scmd = NULL;
3839 cmd->instance = instance;
3841 list_add_tail(&cmd->list, &instance->cmd_pool);
3845 * Create a frame pool and assign one frame to each cmd
3847 if (megasas_create_frame_pool(instance)) {
3848 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3849 megasas_free_cmds(instance);
3852 return 0;
3856 * megasas_get_pd_list_info - Returns FW's pd_list structure
3857 * @instance: Adapter soft state
3858 * @pd_list: pd_list structure
3860 * Issues an internal command (DCMD) to get the FW's controller PD
3861 * list structure. This information is mainly used to find out SYSTEM
3862 * supported by the FW.
3864 static int
3865 megasas_get_pd_list(struct megasas_instance *instance)
3867 int ret = 0, pd_index = 0;
3868 struct megasas_cmd *cmd;
3869 struct megasas_dcmd_frame *dcmd;
3870 struct MR_PD_LIST *ci;
3871 struct MR_PD_ADDRESS *pd_addr;
3872 dma_addr_t ci_h = 0;
3874 cmd = megasas_get_cmd(instance);
3876 if (!cmd) {
3877 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3878 return -ENOMEM;
3881 dcmd = &cmd->frame->dcmd;
3883 ci = pci_alloc_consistent(instance->pdev,
3884 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3886 if (!ci) {
3887 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3888 megasas_return_cmd(instance, cmd);
3889 return -ENOMEM;
3892 memset(ci, 0, sizeof(*ci));
3893 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3895 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3896 dcmd->mbox.b[1] = 0;
3897 dcmd->cmd = MFI_CMD_DCMD;
3898 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3899 dcmd->sge_count = 1;
3900 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3901 dcmd->timeout = 0;
3902 dcmd->pad_0 = 0;
3903 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3904 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3905 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3906 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3908 if (instance->ctrl_context && !instance->mask_interrupts)
3909 ret = megasas_issue_blocked_cmd(instance, cmd,
3910 MEGASAS_BLOCKED_CMD_TIMEOUT);
3911 else
3912 ret = megasas_issue_polled(instance, cmd);
3915 * the following function will get the instance PD LIST.
3918 pd_addr = ci->addr;
3920 if (ret == 0 &&
3921 (le32_to_cpu(ci->count) <
3922 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3924 memset(instance->local_pd_list, 0,
3925 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3927 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3929 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
3930 le16_to_cpu(pd_addr->deviceId);
3931 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
3932 pd_addr->scsiDevType;
3933 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
3934 MR_PD_STATE_SYSTEM;
3935 pd_addr++;
3937 memcpy(instance->pd_list, instance->local_pd_list,
3938 sizeof(instance->pd_list));
3941 pci_free_consistent(instance->pdev,
3942 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3943 ci, ci_h);
3945 megasas_return_cmd(instance, cmd);
3947 return ret;
3951 * megasas_get_ld_list_info - Returns FW's ld_list structure
3952 * @instance: Adapter soft state
3953 * @ld_list: ld_list structure
3955 * Issues an internal command (DCMD) to get the FW's controller PD
3956 * list structure. This information is mainly used to find out SYSTEM
3957 * supported by the FW.
3959 static int
3960 megasas_get_ld_list(struct megasas_instance *instance)
3962 int ret = 0, ld_index = 0, ids = 0;
3963 struct megasas_cmd *cmd;
3964 struct megasas_dcmd_frame *dcmd;
3965 struct MR_LD_LIST *ci;
3966 dma_addr_t ci_h = 0;
3967 u32 ld_count;
3969 cmd = megasas_get_cmd(instance);
3971 if (!cmd) {
3972 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3973 return -ENOMEM;
3976 dcmd = &cmd->frame->dcmd;
3978 ci = pci_alloc_consistent(instance->pdev,
3979 sizeof(struct MR_LD_LIST),
3980 &ci_h);
3982 if (!ci) {
3983 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3984 megasas_return_cmd(instance, cmd);
3985 return -ENOMEM;
3988 memset(ci, 0, sizeof(*ci));
3989 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3991 if (instance->supportmax256vd)
3992 dcmd->mbox.b[0] = 1;
3993 dcmd->cmd = MFI_CMD_DCMD;
3994 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3995 dcmd->sge_count = 1;
3996 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3997 dcmd->timeout = 0;
3998 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3999 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4000 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4001 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4002 dcmd->pad_0 = 0;
4004 if (instance->ctrl_context && !instance->mask_interrupts)
4005 ret = megasas_issue_blocked_cmd(instance, cmd,
4006 MEGASAS_BLOCKED_CMD_TIMEOUT);
4007 else
4008 ret = megasas_issue_polled(instance, cmd);
4011 ld_count = le32_to_cpu(ci->ldCount);
4013 /* the following function will get the instance PD LIST */
4015 if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
4016 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4018 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4019 if (ci->ldList[ld_index].state != 0) {
4020 ids = ci->ldList[ld_index].ref.targetId;
4021 instance->ld_ids[ids] =
4022 ci->ldList[ld_index].ref.targetId;
4027 pci_free_consistent(instance->pdev,
4028 sizeof(struct MR_LD_LIST),
4030 ci_h);
4032 megasas_return_cmd(instance, cmd);
4033 return ret;
4037 * megasas_ld_list_query - Returns FW's ld_list structure
4038 * @instance: Adapter soft state
4039 * @ld_list: ld_list structure
4041 * Issues an internal command (DCMD) to get the FW's controller PD
4042 * list structure. This information is mainly used to find out SYSTEM
4043 * supported by the FW.
4045 static int
4046 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4048 int ret = 0, ld_index = 0, ids = 0;
4049 struct megasas_cmd *cmd;
4050 struct megasas_dcmd_frame *dcmd;
4051 struct MR_LD_TARGETID_LIST *ci;
4052 dma_addr_t ci_h = 0;
4053 u32 tgtid_count;
4055 cmd = megasas_get_cmd(instance);
4057 if (!cmd) {
4058 dev_warn(&instance->pdev->dev,
4059 "megasas_ld_list_query: Failed to get cmd\n");
4060 return -ENOMEM;
4063 dcmd = &cmd->frame->dcmd;
4065 ci = pci_alloc_consistent(instance->pdev,
4066 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4068 if (!ci) {
4069 dev_warn(&instance->pdev->dev,
4070 "Failed to alloc mem for ld_list_query\n");
4071 megasas_return_cmd(instance, cmd);
4072 return -ENOMEM;
4075 memset(ci, 0, sizeof(*ci));
4076 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4078 dcmd->mbox.b[0] = query_type;
4079 if (instance->supportmax256vd)
4080 dcmd->mbox.b[2] = 1;
4082 dcmd->cmd = MFI_CMD_DCMD;
4083 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4084 dcmd->sge_count = 1;
4085 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4086 dcmd->timeout = 0;
4087 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4088 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4089 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4090 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4091 dcmd->pad_0 = 0;
4093 if (instance->ctrl_context && !instance->mask_interrupts)
4094 ret = megasas_issue_blocked_cmd(instance, cmd,
4095 MEGASAS_BLOCKED_CMD_TIMEOUT);
4096 else
4097 ret = megasas_issue_polled(instance, cmd);
4099 tgtid_count = le32_to_cpu(ci->count);
4101 if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4102 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4103 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4104 ids = ci->targetId[ld_index];
4105 instance->ld_ids[ids] = ci->targetId[ld_index];
4110 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4111 ci, ci_h);
4113 megasas_return_cmd(instance, cmd);
4115 return ret;
4119 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4120 * instance : Controller's instance
4122 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4124 struct fusion_context *fusion;
4125 u32 old_map_sz;
4126 u32 new_map_sz;
4128 fusion = instance->ctrl_context;
4129 /* For MFI based controllers return dummy success */
4130 if (!fusion)
4131 return;
4133 instance->supportmax256vd =
4134 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4135 /* Below is additional check to address future FW enhancement */
4136 if (instance->ctrl_info->max_lds > 64)
4137 instance->supportmax256vd = 1;
4139 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4140 * MEGASAS_MAX_DEV_PER_CHANNEL;
4141 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4142 * MEGASAS_MAX_DEV_PER_CHANNEL;
4143 if (instance->supportmax256vd) {
4144 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4145 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4146 } else {
4147 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4148 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4151 dev_info(&instance->pdev->dev,
4152 "firmware type\t: %s\n",
4153 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4154 "Legacy(64 VD) firmware");
4156 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4157 (sizeof(struct MR_LD_SPAN_MAP) *
4158 (instance->fw_supported_vd_count - 1));
4159 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4160 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4161 (sizeof(struct MR_LD_SPAN_MAP) *
4162 (instance->drv_supported_vd_count - 1));
4164 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4167 if (instance->supportmax256vd)
4168 fusion->current_map_sz = new_map_sz;
4169 else
4170 fusion->current_map_sz = old_map_sz;
4174 * megasas_get_controller_info - Returns FW's controller structure
4175 * @instance: Adapter soft state
4177 * Issues an internal command (DCMD) to get the FW's controller structure.
4178 * This information is mainly used to find out the maximum IO transfer per
4179 * command supported by the FW.
4182 megasas_get_ctrl_info(struct megasas_instance *instance)
4184 int ret = 0;
4185 struct megasas_cmd *cmd;
4186 struct megasas_dcmd_frame *dcmd;
4187 struct megasas_ctrl_info *ci;
4188 struct megasas_ctrl_info *ctrl_info;
4189 dma_addr_t ci_h = 0;
4191 ctrl_info = instance->ctrl_info;
4193 cmd = megasas_get_cmd(instance);
4195 if (!cmd) {
4196 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4197 return -ENOMEM;
4200 dcmd = &cmd->frame->dcmd;
4202 ci = pci_alloc_consistent(instance->pdev,
4203 sizeof(struct megasas_ctrl_info), &ci_h);
4205 if (!ci) {
4206 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4207 megasas_return_cmd(instance, cmd);
4208 return -ENOMEM;
4211 memset(ci, 0, sizeof(*ci));
4212 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4214 dcmd->cmd = MFI_CMD_DCMD;
4215 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4216 dcmd->sge_count = 1;
4217 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4218 dcmd->timeout = 0;
4219 dcmd->pad_0 = 0;
4220 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4221 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4222 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4223 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4224 dcmd->mbox.b[0] = 1;
4226 if (instance->ctrl_context && !instance->mask_interrupts)
4227 ret = megasas_issue_blocked_cmd(instance, cmd,
4228 MEGASAS_BLOCKED_CMD_TIMEOUT);
4229 else
4230 ret = megasas_issue_polled(instance, cmd);
4232 if (!ret) {
4233 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4234 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4235 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4236 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4237 megasas_update_ext_vd_details(instance);
4238 instance->use_seqnum_jbod_fp =
4239 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4240 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4241 dev_info(&instance->pdev->dev,
4242 "controller type\t: %s(%dMB)\n",
4243 instance->is_imr ? "iMR" : "MR",
4244 le16_to_cpu(ctrl_info->memory_size));
4245 instance->disableOnlineCtrlReset =
4246 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4247 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4248 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4249 instance->secure_jbod_support =
4250 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4251 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4252 instance->secure_jbod_support ? "Yes" : "No");
4255 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4256 ci, ci_h);
4258 megasas_return_cmd(instance, cmd);
4259 return ret;
4263 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4264 * to firmware
4266 * @instance: Adapter soft state
4267 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4268 MR_CRASH_BUF_TURN_OFF = 0
4269 MR_CRASH_BUF_TURN_ON = 1
4270 * @return 0 on success non-zero on failure.
4271 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4272 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4273 * that driver supports crash dump feature. This DCMD will be sent only if
4274 * crash dump feature is supported by the FW.
4277 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4278 u8 crash_buf_state)
4280 int ret = 0;
4281 struct megasas_cmd *cmd;
4282 struct megasas_dcmd_frame *dcmd;
4284 cmd = megasas_get_cmd(instance);
4286 if (!cmd) {
4287 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4288 return -ENOMEM;
4292 dcmd = &cmd->frame->dcmd;
4294 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4295 dcmd->mbox.b[0] = crash_buf_state;
4296 dcmd->cmd = MFI_CMD_DCMD;
4297 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4298 dcmd->sge_count = 1;
4299 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4300 dcmd->timeout = 0;
4301 dcmd->pad_0 = 0;
4302 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4303 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4304 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4305 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4307 if (instance->ctrl_context && !instance->mask_interrupts)
4308 ret = megasas_issue_blocked_cmd(instance, cmd,
4309 MEGASAS_BLOCKED_CMD_TIMEOUT);
4310 else
4311 ret = megasas_issue_polled(instance, cmd);
4313 megasas_return_cmd(instance, cmd);
4314 return ret;
4318 * megasas_issue_init_mfi - Initializes the FW
4319 * @instance: Adapter soft state
4321 * Issues the INIT MFI cmd
4323 static int
4324 megasas_issue_init_mfi(struct megasas_instance *instance)
4326 __le32 context;
4327 struct megasas_cmd *cmd;
4328 struct megasas_init_frame *init_frame;
4329 struct megasas_init_queue_info *initq_info;
4330 dma_addr_t init_frame_h;
4331 dma_addr_t initq_info_h;
4334 * Prepare a init frame. Note the init frame points to queue info
4335 * structure. Each frame has SGL allocated after first 64 bytes. For
4336 * this frame - since we don't need any SGL - we use SGL's space as
4337 * queue info structure
4339 * We will not get a NULL command below. We just created the pool.
4341 cmd = megasas_get_cmd(instance);
4343 init_frame = (struct megasas_init_frame *)cmd->frame;
4344 initq_info = (struct megasas_init_queue_info *)
4345 ((unsigned long)init_frame + 64);
4347 init_frame_h = cmd->frame_phys_addr;
4348 initq_info_h = init_frame_h + 64;
4350 context = init_frame->context;
4351 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4352 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4353 init_frame->context = context;
4355 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4356 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4358 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4359 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4361 init_frame->cmd = MFI_CMD_INIT;
4362 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4363 init_frame->queue_info_new_phys_addr_lo =
4364 cpu_to_le32(lower_32_bits(initq_info_h));
4365 init_frame->queue_info_new_phys_addr_hi =
4366 cpu_to_le32(upper_32_bits(initq_info_h));
4368 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4371 * disable the intr before firing the init frame to FW
4373 instance->instancet->disable_intr(instance);
4376 * Issue the init frame in polled mode
4379 if (megasas_issue_polled(instance, cmd)) {
4380 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4381 megasas_return_cmd(instance, cmd);
4382 goto fail_fw_init;
4385 megasas_return_cmd(instance, cmd);
4387 return 0;
4389 fail_fw_init:
4390 return -EINVAL;
4393 static u32
4394 megasas_init_adapter_mfi(struct megasas_instance *instance)
4396 struct megasas_register_set __iomem *reg_set;
4397 u32 context_sz;
4398 u32 reply_q_sz;
4400 reg_set = instance->reg_set;
4403 * Get various operational parameters from status register
4405 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4407 * Reduce the max supported cmds by 1. This is to ensure that the
4408 * reply_q_sz (1 more than the max cmd that driver may send)
4409 * does not exceed max cmds that the FW can support
4411 instance->max_fw_cmds = instance->max_fw_cmds-1;
4412 instance->max_mfi_cmds = instance->max_fw_cmds;
4413 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4414 0x10;
4416 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4417 * are reserved for IOCTL + driver's internal DCMDs.
4419 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4420 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4421 instance->max_scsi_cmds = (instance->max_fw_cmds -
4422 MEGASAS_SKINNY_INT_CMDS);
4423 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4424 } else {
4425 instance->max_scsi_cmds = (instance->max_fw_cmds -
4426 MEGASAS_INT_CMDS);
4427 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4431 * Create a pool of commands
4433 if (megasas_alloc_cmds(instance))
4434 goto fail_alloc_cmds;
4437 * Allocate memory for reply queue. Length of reply queue should
4438 * be _one_ more than the maximum commands handled by the firmware.
4440 * Note: When FW completes commands, it places corresponding contex
4441 * values in this circular reply queue. This circular queue is a fairly
4442 * typical producer-consumer queue. FW is the producer (of completed
4443 * commands) and the driver is the consumer.
4445 context_sz = sizeof(u32);
4446 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4448 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4449 reply_q_sz,
4450 &instance->reply_queue_h);
4452 if (!instance->reply_queue) {
4453 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4454 goto fail_reply_queue;
4457 if (megasas_issue_init_mfi(instance))
4458 goto fail_fw_init;
4460 if (megasas_get_ctrl_info(instance)) {
4461 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4462 "Fail from %s %d\n", instance->unique_id,
4463 __func__, __LINE__);
4464 goto fail_fw_init;
4467 instance->fw_support_ieee = 0;
4468 instance->fw_support_ieee =
4469 (instance->instancet->read_fw_status_reg(reg_set) &
4470 0x04000000);
4472 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4473 instance->fw_support_ieee);
4475 if (instance->fw_support_ieee)
4476 instance->flag_ieee = 1;
4478 return 0;
4480 fail_fw_init:
4482 pci_free_consistent(instance->pdev, reply_q_sz,
4483 instance->reply_queue, instance->reply_queue_h);
4484 fail_reply_queue:
4485 megasas_free_cmds(instance);
4487 fail_alloc_cmds:
4488 return 1;
4492 * megasas_setup_irqs_msix - register legacy interrupts.
4493 * @instance: Adapter soft state
4495 * Do not enable interrupt, only setup ISRs.
4497 * Return 0 on success.
4499 static int
4500 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4502 struct pci_dev *pdev;
4504 pdev = instance->pdev;
4505 instance->irq_context[0].instance = instance;
4506 instance->irq_context[0].MSIxIndex = 0;
4507 if (request_irq(pdev->irq, instance->instancet->service_isr,
4508 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4509 dev_err(&instance->pdev->dev,
4510 "Failed to register IRQ from %s %d\n",
4511 __func__, __LINE__);
4512 return -1;
4514 return 0;
4518 * megasas_setup_irqs_msix - register MSI-x interrupts.
4519 * @instance: Adapter soft state
4520 * @is_probe: Driver probe check
4522 * Do not enable interrupt, only setup ISRs.
4524 * Return 0 on success.
4526 static int
4527 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4529 int i, j, cpu;
4530 struct pci_dev *pdev;
4532 pdev = instance->pdev;
4534 /* Try MSI-x */
4535 cpu = cpumask_first(cpu_online_mask);
4536 for (i = 0; i < instance->msix_vectors; i++) {
4537 instance->irq_context[i].instance = instance;
4538 instance->irq_context[i].MSIxIndex = i;
4539 if (request_irq(instance->msixentry[i].vector,
4540 instance->instancet->service_isr, 0, "megasas",
4541 &instance->irq_context[i])) {
4542 dev_err(&instance->pdev->dev,
4543 "Failed to register IRQ for vector %d.\n", i);
4544 for (j = 0; j < i; j++) {
4545 if (smp_affinity_enable)
4546 irq_set_affinity_hint(
4547 instance->msixentry[j].vector, NULL);
4548 free_irq(instance->msixentry[j].vector,
4549 &instance->irq_context[j]);
4551 /* Retry irq register for IO_APIC*/
4552 instance->msix_vectors = 0;
4553 if (is_probe)
4554 return megasas_setup_irqs_ioapic(instance);
4555 else
4556 return -1;
4558 if (smp_affinity_enable) {
4559 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4560 get_cpu_mask(cpu)))
4561 dev_err(&instance->pdev->dev,
4562 "Failed to set affinity hint"
4563 " for cpu %d\n", cpu);
4564 cpu = cpumask_next(cpu, cpu_online_mask);
4567 return 0;
4571 * megasas_destroy_irqs- unregister interrupts.
4572 * @instance: Adapter soft state
4573 * return: void
4575 static void
4576 megasas_destroy_irqs(struct megasas_instance *instance) {
4578 int i;
4580 if (instance->msix_vectors)
4581 for (i = 0; i < instance->msix_vectors; i++) {
4582 if (smp_affinity_enable)
4583 irq_set_affinity_hint(
4584 instance->msixentry[i].vector, NULL);
4585 free_irq(instance->msixentry[i].vector,
4586 &instance->irq_context[i]);
4588 else
4589 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4593 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4594 * @instance: Adapter soft state
4595 * @is_probe: Driver probe check
4597 * Return 0 on success.
4599 void
4600 megasas_setup_jbod_map(struct megasas_instance *instance)
4602 int i;
4603 struct fusion_context *fusion = instance->ctrl_context;
4604 u32 pd_seq_map_sz;
4606 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4607 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4609 if (reset_devices || !fusion ||
4610 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4611 dev_info(&instance->pdev->dev,
4612 "Jbod map is not supported %s %d\n",
4613 __func__, __LINE__);
4614 instance->use_seqnum_jbod_fp = false;
4615 return;
4618 if (fusion->pd_seq_sync[0])
4619 goto skip_alloc;
4621 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4622 fusion->pd_seq_sync[i] = dma_alloc_coherent
4623 (&instance->pdev->dev, pd_seq_map_sz,
4624 &fusion->pd_seq_phys[i], GFP_KERNEL);
4625 if (!fusion->pd_seq_sync[i]) {
4626 dev_err(&instance->pdev->dev,
4627 "Failed to allocate memory from %s %d\n",
4628 __func__, __LINE__);
4629 if (i == 1) {
4630 dma_free_coherent(&instance->pdev->dev,
4631 pd_seq_map_sz, fusion->pd_seq_sync[0],
4632 fusion->pd_seq_phys[0]);
4633 fusion->pd_seq_sync[0] = NULL;
4635 instance->use_seqnum_jbod_fp = false;
4636 return;
4640 skip_alloc:
4641 if (!megasas_sync_pd_seq_num(instance, false) &&
4642 !megasas_sync_pd_seq_num(instance, true))
4643 instance->use_seqnum_jbod_fp = true;
4644 else
4645 instance->use_seqnum_jbod_fp = false;
4649 * megasas_init_fw - Initializes the FW
4650 * @instance: Adapter soft state
4652 * This is the main function for initializing firmware
4655 static int megasas_init_fw(struct megasas_instance *instance)
4657 u32 max_sectors_1;
4658 u32 max_sectors_2;
4659 u32 tmp_sectors, msix_enable, scratch_pad_2;
4660 resource_size_t base_addr;
4661 struct megasas_register_set __iomem *reg_set;
4662 struct megasas_ctrl_info *ctrl_info = NULL;
4663 unsigned long bar_list;
4664 int i, loop, fw_msix_count = 0;
4665 struct IOV_111 *iovPtr;
4666 struct fusion_context *fusion;
4668 fusion = instance->ctrl_context;
4670 /* Find first memory bar */
4671 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4672 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4673 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
4674 "megasas: LSI")) {
4675 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4676 return -EBUSY;
4679 base_addr = pci_resource_start(instance->pdev, instance->bar);
4680 instance->reg_set = ioremap_nocache(base_addr, 8192);
4682 if (!instance->reg_set) {
4683 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4684 goto fail_ioremap;
4687 reg_set = instance->reg_set;
4689 switch (instance->pdev->device) {
4690 case PCI_DEVICE_ID_LSI_FUSION:
4691 case PCI_DEVICE_ID_LSI_PLASMA:
4692 case PCI_DEVICE_ID_LSI_INVADER:
4693 case PCI_DEVICE_ID_LSI_FURY:
4694 case PCI_DEVICE_ID_LSI_INTRUDER:
4695 case PCI_DEVICE_ID_LSI_INTRUDER_24:
4696 case PCI_DEVICE_ID_LSI_CUTLASS_52:
4697 case PCI_DEVICE_ID_LSI_CUTLASS_53:
4698 instance->instancet = &megasas_instance_template_fusion;
4699 break;
4700 case PCI_DEVICE_ID_LSI_SAS1078R:
4701 case PCI_DEVICE_ID_LSI_SAS1078DE:
4702 instance->instancet = &megasas_instance_template_ppc;
4703 break;
4704 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4705 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4706 instance->instancet = &megasas_instance_template_gen2;
4707 break;
4708 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4709 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4710 instance->instancet = &megasas_instance_template_skinny;
4711 break;
4712 case PCI_DEVICE_ID_LSI_SAS1064R:
4713 case PCI_DEVICE_ID_DELL_PERC5:
4714 default:
4715 instance->instancet = &megasas_instance_template_xscale;
4716 instance->allow_fw_scan = 1;
4717 break;
4720 if (megasas_transition_to_ready(instance, 0)) {
4721 atomic_set(&instance->fw_reset_no_pci_access, 1);
4722 instance->instancet->adp_reset
4723 (instance, instance->reg_set);
4724 atomic_set(&instance->fw_reset_no_pci_access, 0);
4725 dev_info(&instance->pdev->dev,
4726 "FW restarted successfully from %s!\n",
4727 __func__);
4729 /*waitting for about 30 second before retry*/
4730 ssleep(30);
4732 if (megasas_transition_to_ready(instance, 0))
4733 goto fail_ready_state;
4737 * MSI-X host index 0 is common for all adapter.
4738 * It is used for all MPT based Adapters.
4740 instance->reply_post_host_index_addr[0] =
4741 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4742 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4744 /* Check if MSI-X is supported while in ready state */
4745 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4746 0x4000000) >> 0x1a;
4747 if (msix_enable && !msix_disable) {
4748 scratch_pad_2 = readl
4749 (&instance->reg_set->outbound_scratch_pad_2);
4750 /* Check max MSI-X vectors */
4751 if (fusion) {
4752 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4753 instance->msix_vectors = (scratch_pad_2
4754 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4755 fw_msix_count = instance->msix_vectors;
4756 } else { /* Invader series supports more than 8 MSI-x vectors*/
4757 instance->msix_vectors = ((scratch_pad_2
4758 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4759 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4760 fw_msix_count = instance->msix_vectors;
4761 /* Save 1-15 reply post index address to local memory
4762 * Index 0 is already saved from reg offset
4763 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4765 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4766 instance->reply_post_host_index_addr[loop] =
4767 (u32 __iomem *)
4768 ((u8 __iomem *)instance->reg_set +
4769 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4770 + (loop * 0x10));
4773 if (msix_vectors)
4774 instance->msix_vectors = min(msix_vectors,
4775 instance->msix_vectors);
4776 } else /* MFI adapters */
4777 instance->msix_vectors = 1;
4778 /* Don't bother allocating more MSI-X vectors than cpus */
4779 instance->msix_vectors = min(instance->msix_vectors,
4780 (unsigned int)num_online_cpus());
4781 for (i = 0; i < instance->msix_vectors; i++)
4782 instance->msixentry[i].entry = i;
4783 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4784 1, instance->msix_vectors);
4785 if (i > 0)
4786 instance->msix_vectors = i;
4787 else
4788 instance->msix_vectors = 0;
4791 dev_info(&instance->pdev->dev,
4792 "firmware supports msix\t: (%d)", fw_msix_count);
4793 dev_info(&instance->pdev->dev,
4794 "current msix/online cpus\t: (%d/%d)\n",
4795 instance->msix_vectors, (unsigned int)num_online_cpus());
4797 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4798 (unsigned long)instance);
4800 if (instance->msix_vectors ?
4801 megasas_setup_irqs_msix(instance, 1) :
4802 megasas_setup_irqs_ioapic(instance))
4803 goto fail_setup_irqs;
4805 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4806 GFP_KERNEL);
4807 if (instance->ctrl_info == NULL)
4808 goto fail_init_adapter;
4811 * Below are default value for legacy Firmware.
4812 * non-fusion based controllers
4814 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4815 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4816 /* Get operational params, sge flags, send init cmd to controller */
4817 if (instance->instancet->init_adapter(instance))
4818 goto fail_init_adapter;
4821 instance->instancet->enable_intr(instance);
4823 dev_err(&instance->pdev->dev, "INIT adapter done\n");
4825 megasas_setup_jbod_map(instance);
4827 /** for passthrough
4828 * the following function will get the PD LIST.
4830 memset(instance->pd_list, 0,
4831 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4832 if (megasas_get_pd_list(instance) < 0) {
4833 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4834 goto fail_get_pd_list;
4837 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4838 if (megasas_ld_list_query(instance,
4839 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4840 megasas_get_ld_list(instance);
4843 * Compute the max allowed sectors per IO: The controller info has two
4844 * limits on max sectors. Driver should use the minimum of these two.
4846 * 1 << stripe_sz_ops.min = max sectors per strip
4848 * Note that older firmwares ( < FW ver 30) didn't report information
4849 * to calculate max_sectors_1. So the number ended up as zero always.
4851 tmp_sectors = 0;
4852 ctrl_info = instance->ctrl_info;
4854 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4855 le16_to_cpu(ctrl_info->max_strips_per_io);
4856 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4858 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
4860 instance->mpio = ctrl_info->adapterOperations2.mpio;
4861 instance->UnevenSpanSupport =
4862 ctrl_info->adapterOperations2.supportUnevenSpans;
4863 if (instance->UnevenSpanSupport) {
4864 struct fusion_context *fusion = instance->ctrl_context;
4865 if (MR_ValidateMapInfo(instance))
4866 fusion->fast_path_io = 1;
4867 else
4868 fusion->fast_path_io = 0;
4871 if (ctrl_info->host_interface.SRIOV) {
4872 instance->requestorId = ctrl_info->iov.requestorId;
4873 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
4874 if (!ctrl_info->adapterOperations2.activePassive)
4875 instance->PlasmaFW111 = 1;
4877 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
4878 instance->PlasmaFW111 ? "1.11" : "new");
4880 if (instance->PlasmaFW111) {
4881 iovPtr = (struct IOV_111 *)
4882 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
4883 instance->requestorId = iovPtr->requestorId;
4886 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
4887 instance->requestorId);
4890 instance->crash_dump_fw_support =
4891 ctrl_info->adapterOperations3.supportCrashDump;
4892 instance->crash_dump_drv_support =
4893 (instance->crash_dump_fw_support &&
4894 instance->crash_dump_buf);
4895 if (instance->crash_dump_drv_support)
4896 megasas_set_crash_dump_params(instance,
4897 MR_CRASH_BUF_TURN_OFF);
4899 else {
4900 if (instance->crash_dump_buf)
4901 pci_free_consistent(instance->pdev,
4902 CRASH_DMA_BUF_SIZE,
4903 instance->crash_dump_buf,
4904 instance->crash_dump_h);
4905 instance->crash_dump_buf = NULL;
4909 dev_info(&instance->pdev->dev,
4910 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4911 le16_to_cpu(ctrl_info->pci.vendor_id),
4912 le16_to_cpu(ctrl_info->pci.device_id),
4913 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4914 le16_to_cpu(ctrl_info->pci.sub_device_id));
4915 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
4916 instance->UnevenSpanSupport ? "yes" : "no");
4917 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
4918 instance->crash_dump_drv_support ? "yes" : "no");
4919 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
4920 instance->use_seqnum_jbod_fp ? "yes" : "no");
4923 instance->max_sectors_per_req = instance->max_num_sge *
4924 SGE_BUFFER_SIZE / 512;
4925 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4926 instance->max_sectors_per_req = tmp_sectors;
4928 /* Check for valid throttlequeuedepth module parameter */
4929 if (throttlequeuedepth &&
4930 throttlequeuedepth <= instance->max_scsi_cmds)
4931 instance->throttlequeuedepth = throttlequeuedepth;
4932 else
4933 instance->throttlequeuedepth =
4934 MEGASAS_THROTTLE_QUEUE_DEPTH;
4937 /* Launch SR-IOV heartbeat timer */
4938 if (instance->requestorId) {
4939 if (!megasas_sriov_start_heartbeat(instance, 1))
4940 megasas_start_timer(instance,
4941 &instance->sriov_heartbeat_timer,
4942 megasas_sriov_heartbeat_handler,
4943 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4944 else
4945 instance->skip_heartbeat_timer_del = 1;
4948 return 0;
4950 fail_get_pd_list:
4951 instance->instancet->disable_intr(instance);
4952 fail_init_adapter:
4953 megasas_destroy_irqs(instance);
4954 fail_setup_irqs:
4955 if (instance->msix_vectors)
4956 pci_disable_msix(instance->pdev);
4957 instance->msix_vectors = 0;
4958 fail_ready_state:
4959 kfree(instance->ctrl_info);
4960 instance->ctrl_info = NULL;
4961 iounmap(instance->reg_set);
4963 fail_ioremap:
4964 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
4966 return -EINVAL;
4970 * megasas_release_mfi - Reverses the FW initialization
4971 * @instance: Adapter soft state
4973 static void megasas_release_mfi(struct megasas_instance *instance)
4975 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4977 if (instance->reply_queue)
4978 pci_free_consistent(instance->pdev, reply_q_sz,
4979 instance->reply_queue, instance->reply_queue_h);
4981 megasas_free_cmds(instance);
4983 iounmap(instance->reg_set);
4985 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
4989 * megasas_get_seq_num - Gets latest event sequence numbers
4990 * @instance: Adapter soft state
4991 * @eli: FW event log sequence numbers information
4993 * FW maintains a log of all events in a non-volatile area. Upper layers would
4994 * usually find out the latest sequence number of the events, the seq number at
4995 * the boot etc. They would "read" all the events below the latest seq number
4996 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4997 * number), they would subsribe to AEN (asynchronous event notification) and
4998 * wait for the events to happen.
5000 static int
5001 megasas_get_seq_num(struct megasas_instance *instance,
5002 struct megasas_evt_log_info *eli)
5004 struct megasas_cmd *cmd;
5005 struct megasas_dcmd_frame *dcmd;
5006 struct megasas_evt_log_info *el_info;
5007 dma_addr_t el_info_h = 0;
5009 cmd = megasas_get_cmd(instance);
5011 if (!cmd) {
5012 return -ENOMEM;
5015 dcmd = &cmd->frame->dcmd;
5016 el_info = pci_alloc_consistent(instance->pdev,
5017 sizeof(struct megasas_evt_log_info),
5018 &el_info_h);
5020 if (!el_info) {
5021 megasas_return_cmd(instance, cmd);
5022 return -ENOMEM;
5025 memset(el_info, 0, sizeof(*el_info));
5026 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5028 dcmd->cmd = MFI_CMD_DCMD;
5029 dcmd->cmd_status = 0x0;
5030 dcmd->sge_count = 1;
5031 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5032 dcmd->timeout = 0;
5033 dcmd->pad_0 = 0;
5034 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5035 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5036 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5037 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5039 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5040 dev_err(&instance->pdev->dev, "Command timedout"
5041 "from %s\n", __func__);
5042 else {
5044 * Copy the data back into callers buffer
5046 eli->newest_seq_num = el_info->newest_seq_num;
5047 eli->oldest_seq_num = el_info->oldest_seq_num;
5048 eli->clear_seq_num = el_info->clear_seq_num;
5049 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5050 eli->boot_seq_num = el_info->boot_seq_num;
5053 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5054 el_info, el_info_h);
5056 megasas_return_cmd(instance, cmd);
5058 return 0;
5062 * megasas_register_aen - Registers for asynchronous event notification
5063 * @instance: Adapter soft state
5064 * @seq_num: The starting sequence number
5065 * @class_locale: Class of the event
5067 * This function subscribes for AEN for events beyond the @seq_num. It requests
5068 * to be notified if and only if the event is of type @class_locale
5070 static int
5071 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5072 u32 class_locale_word)
5074 int ret_val;
5075 struct megasas_cmd *cmd;
5076 struct megasas_dcmd_frame *dcmd;
5077 union megasas_evt_class_locale curr_aen;
5078 union megasas_evt_class_locale prev_aen;
5081 * If there an AEN pending already (aen_cmd), check if the
5082 * class_locale of that pending AEN is inclusive of the new
5083 * AEN request we currently have. If it is, then we don't have
5084 * to do anything. In other words, whichever events the current
5085 * AEN request is subscribing to, have already been subscribed
5086 * to.
5088 * If the old_cmd is _not_ inclusive, then we have to abort
5089 * that command, form a class_locale that is superset of both
5090 * old and current and re-issue to the FW
5093 curr_aen.word = class_locale_word;
5095 if (instance->aen_cmd) {
5097 prev_aen.word =
5098 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5100 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5101 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5102 dev_info(&instance->pdev->dev,
5103 "%s %d out of range class %d send by application\n",
5104 __func__, __LINE__, curr_aen.members.class);
5105 return 0;
5109 * A class whose enum value is smaller is inclusive of all
5110 * higher values. If a PROGRESS (= -1) was previously
5111 * registered, then a new registration requests for higher
5112 * classes need not be sent to FW. They are automatically
5113 * included.
5115 * Locale numbers don't have such hierarchy. They are bitmap
5116 * values
5118 if ((prev_aen.members.class <= curr_aen.members.class) &&
5119 !((prev_aen.members.locale & curr_aen.members.locale) ^
5120 curr_aen.members.locale)) {
5122 * Previously issued event registration includes
5123 * current request. Nothing to do.
5125 return 0;
5126 } else {
5127 curr_aen.members.locale |= prev_aen.members.locale;
5129 if (prev_aen.members.class < curr_aen.members.class)
5130 curr_aen.members.class = prev_aen.members.class;
5132 instance->aen_cmd->abort_aen = 1;
5133 ret_val = megasas_issue_blocked_abort_cmd(instance,
5134 instance->
5135 aen_cmd, 30);
5137 if (ret_val) {
5138 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5139 "previous AEN command\n");
5140 return ret_val;
5145 cmd = megasas_get_cmd(instance);
5147 if (!cmd)
5148 return -ENOMEM;
5150 dcmd = &cmd->frame->dcmd;
5152 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5155 * Prepare DCMD for aen registration
5157 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5159 dcmd->cmd = MFI_CMD_DCMD;
5160 dcmd->cmd_status = 0x0;
5161 dcmd->sge_count = 1;
5162 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5163 dcmd->timeout = 0;
5164 dcmd->pad_0 = 0;
5165 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5166 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5167 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5168 instance->last_seq_num = seq_num;
5169 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5170 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5171 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5173 if (instance->aen_cmd != NULL) {
5174 megasas_return_cmd(instance, cmd);
5175 return 0;
5179 * Store reference to the cmd used to register for AEN. When an
5180 * application wants us to register for AEN, we have to abort this
5181 * cmd and re-register with a new EVENT LOCALE supplied by that app
5183 instance->aen_cmd = cmd;
5186 * Issue the aen registration frame
5188 instance->instancet->issue_dcmd(instance, cmd);
5190 return 0;
5194 * megasas_start_aen - Subscribes to AEN during driver load time
5195 * @instance: Adapter soft state
5197 static int megasas_start_aen(struct megasas_instance *instance)
5199 struct megasas_evt_log_info eli;
5200 union megasas_evt_class_locale class_locale;
5203 * Get the latest sequence number from FW
5205 memset(&eli, 0, sizeof(eli));
5207 if (megasas_get_seq_num(instance, &eli))
5208 return -1;
5211 * Register AEN with FW for latest sequence number plus 1
5213 class_locale.members.reserved = 0;
5214 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5215 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5217 return megasas_register_aen(instance,
5218 le32_to_cpu(eli.newest_seq_num) + 1,
5219 class_locale.word);
5223 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5224 * @instance: Adapter soft state
5226 static int megasas_io_attach(struct megasas_instance *instance)
5228 struct Scsi_Host *host = instance->host;
5231 * Export parameters required by SCSI mid-layer
5233 host->irq = instance->pdev->irq;
5234 host->unique_id = instance->unique_id;
5235 host->can_queue = instance->max_scsi_cmds;
5236 host->this_id = instance->init_id;
5237 host->sg_tablesize = instance->max_num_sge;
5239 if (instance->fw_support_ieee)
5240 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5243 * Check if the module parameter value for max_sectors can be used
5245 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5246 instance->max_sectors_per_req = max_sectors;
5247 else {
5248 if (max_sectors) {
5249 if (((instance->pdev->device ==
5250 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5251 (instance->pdev->device ==
5252 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5253 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5254 instance->max_sectors_per_req = max_sectors;
5255 } else {
5256 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5257 "and <= %d (or < 1MB for GEN2 controller)\n",
5258 instance->max_sectors_per_req);
5263 host->max_sectors = instance->max_sectors_per_req;
5264 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5265 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5266 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5267 host->max_lun = MEGASAS_MAX_LUN;
5268 host->max_cmd_len = 16;
5270 /* Fusion only supports host reset */
5271 if (instance->ctrl_context) {
5272 host->hostt->eh_device_reset_handler = NULL;
5273 host->hostt->eh_bus_reset_handler = NULL;
5277 * Notify the mid-layer about the new controller
5279 if (scsi_add_host(host, &instance->pdev->dev)) {
5280 dev_err(&instance->pdev->dev,
5281 "Failed to add host from %s %d\n",
5282 __func__, __LINE__);
5283 return -ENODEV;
5286 return 0;
5289 static int
5290 megasas_set_dma_mask(struct pci_dev *pdev)
5293 * All our controllers are capable of performing 64-bit DMA
5295 if (IS_DMA64) {
5296 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5298 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5299 goto fail_set_dma_mask;
5301 } else {
5302 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5303 goto fail_set_dma_mask;
5306 * Ensure that all data structures are allocated in 32-bit
5307 * memory.
5309 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5310 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5311 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5312 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5313 dev_info(&pdev->dev, "set 32bit DMA mask"
5314 "and 32 bit consistent mask\n");
5315 else
5316 goto fail_set_dma_mask;
5319 return 0;
5321 fail_set_dma_mask:
5322 return 1;
5326 * megasas_probe_one - PCI hotplug entry point
5327 * @pdev: PCI device structure
5328 * @id: PCI ids of supported hotplugged adapter
5330 static int megasas_probe_one(struct pci_dev *pdev,
5331 const struct pci_device_id *id)
5333 int rval, pos;
5334 struct Scsi_Host *host;
5335 struct megasas_instance *instance;
5336 u16 control = 0;
5337 struct fusion_context *fusion = NULL;
5339 /* Reset MSI-X in the kdump kernel */
5340 if (reset_devices) {
5341 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5342 if (pos) {
5343 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5344 &control);
5345 if (control & PCI_MSIX_FLAGS_ENABLE) {
5346 dev_info(&pdev->dev, "resetting MSI-X\n");
5347 pci_write_config_word(pdev,
5348 pos + PCI_MSIX_FLAGS,
5349 control &
5350 ~PCI_MSIX_FLAGS_ENABLE);
5356 * PCI prepping: enable device set bus mastering and dma mask
5358 rval = pci_enable_device_mem(pdev);
5360 if (rval) {
5361 return rval;
5364 pci_set_master(pdev);
5366 if (megasas_set_dma_mask(pdev))
5367 goto fail_set_dma_mask;
5369 host = scsi_host_alloc(&megasas_template,
5370 sizeof(struct megasas_instance));
5372 if (!host) {
5373 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5374 goto fail_alloc_instance;
5377 instance = (struct megasas_instance *)host->hostdata;
5378 memset(instance, 0, sizeof(*instance));
5379 atomic_set(&instance->fw_reset_no_pci_access, 0);
5380 instance->pdev = pdev;
5382 switch (instance->pdev->device) {
5383 case PCI_DEVICE_ID_LSI_FUSION:
5384 case PCI_DEVICE_ID_LSI_PLASMA:
5385 case PCI_DEVICE_ID_LSI_INVADER:
5386 case PCI_DEVICE_ID_LSI_FURY:
5387 case PCI_DEVICE_ID_LSI_INTRUDER:
5388 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5389 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5390 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5392 instance->ctrl_context_pages =
5393 get_order(sizeof(struct fusion_context));
5394 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5395 instance->ctrl_context_pages);
5396 if (!instance->ctrl_context) {
5397 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5398 "memory for Fusion context info\n");
5399 goto fail_alloc_dma_buf;
5401 fusion = instance->ctrl_context;
5402 memset(fusion, 0,
5403 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5404 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5405 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5406 fusion->adapter_type = THUNDERBOLT_SERIES;
5407 else
5408 fusion->adapter_type = INVADER_SERIES;
5410 break;
5411 default: /* For all other supported controllers */
5413 instance->producer =
5414 pci_alloc_consistent(pdev, sizeof(u32),
5415 &instance->producer_h);
5416 instance->consumer =
5417 pci_alloc_consistent(pdev, sizeof(u32),
5418 &instance->consumer_h);
5420 if (!instance->producer || !instance->consumer) {
5421 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5422 "memory for producer, consumer\n");
5423 goto fail_alloc_dma_buf;
5426 *instance->producer = 0;
5427 *instance->consumer = 0;
5428 break;
5431 instance->system_info_buf = pci_zalloc_consistent(pdev,
5432 sizeof(struct MR_DRV_SYSTEM_INFO),
5433 &instance->system_info_h);
5435 if (!instance->system_info_buf)
5436 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5438 /* Crash dump feature related initialisation*/
5439 instance->drv_buf_index = 0;
5440 instance->drv_buf_alloc = 0;
5441 instance->crash_dump_fw_support = 0;
5442 instance->crash_dump_app_support = 0;
5443 instance->fw_crash_state = UNAVAILABLE;
5444 spin_lock_init(&instance->crashdump_lock);
5445 instance->crash_dump_buf = NULL;
5447 if (!reset_devices)
5448 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5449 CRASH_DMA_BUF_SIZE,
5450 &instance->crash_dump_h);
5451 if (!instance->crash_dump_buf)
5452 dev_err(&pdev->dev, "Can't allocate Firmware "
5453 "crash dump DMA buffer\n");
5455 megasas_poll_wait_aen = 0;
5456 instance->flag_ieee = 0;
5457 instance->ev = NULL;
5458 instance->issuepend_done = 1;
5459 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5460 instance->is_imr = 0;
5462 instance->evt_detail = pci_alloc_consistent(pdev,
5463 sizeof(struct
5464 megasas_evt_detail),
5465 &instance->evt_detail_h);
5467 if (!instance->evt_detail) {
5468 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5469 "event detail structure\n");
5470 goto fail_alloc_dma_buf;
5474 * Initialize locks and queues
5476 INIT_LIST_HEAD(&instance->cmd_pool);
5477 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5479 atomic_set(&instance->fw_outstanding,0);
5481 init_waitqueue_head(&instance->int_cmd_wait_q);
5482 init_waitqueue_head(&instance->abort_cmd_wait_q);
5484 spin_lock_init(&instance->mfi_pool_lock);
5485 spin_lock_init(&instance->hba_lock);
5486 spin_lock_init(&instance->completion_lock);
5488 mutex_init(&instance->reset_mutex);
5491 * Initialize PCI related and misc parameters
5493 instance->host = host;
5494 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5495 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5496 instance->ctrl_info = NULL;
5499 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5500 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5501 instance->flag_ieee = 1;
5503 megasas_dbg_lvl = 0;
5504 instance->flag = 0;
5505 instance->unload = 1;
5506 instance->last_time = 0;
5507 instance->disableOnlineCtrlReset = 1;
5508 instance->UnevenSpanSupport = 0;
5510 if (instance->ctrl_context) {
5511 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5512 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5513 } else
5514 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5517 * Initialize MFI Firmware
5519 if (megasas_init_fw(instance))
5520 goto fail_init_mfi;
5522 if (instance->requestorId) {
5523 if (instance->PlasmaFW111) {
5524 instance->vf_affiliation_111 =
5525 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5526 &instance->vf_affiliation_111_h);
5527 if (!instance->vf_affiliation_111)
5528 dev_warn(&pdev->dev, "Can't allocate "
5529 "memory for VF affiliation buffer\n");
5530 } else {
5531 instance->vf_affiliation =
5532 pci_alloc_consistent(pdev,
5533 (MAX_LOGICAL_DRIVES + 1) *
5534 sizeof(struct MR_LD_VF_AFFILIATION),
5535 &instance->vf_affiliation_h);
5536 if (!instance->vf_affiliation)
5537 dev_warn(&pdev->dev, "Can't allocate "
5538 "memory for VF affiliation buffer\n");
5543 * Store instance in PCI softstate
5545 pci_set_drvdata(pdev, instance);
5548 * Add this controller to megasas_mgmt_info structure so that it
5549 * can be exported to management applications
5551 megasas_mgmt_info.count++;
5552 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5553 megasas_mgmt_info.max_index++;
5556 * Register with SCSI mid-layer
5558 if (megasas_io_attach(instance))
5559 goto fail_io_attach;
5561 instance->unload = 0;
5563 * Trigger SCSI to scan our drives
5565 scsi_scan_host(host);
5568 * Initiate AEN (Asynchronous Event Notification)
5570 if (megasas_start_aen(instance)) {
5571 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5572 goto fail_start_aen;
5575 /* Get current SR-IOV LD/VF affiliation */
5576 if (instance->requestorId)
5577 megasas_get_ld_vf_affiliation(instance, 1);
5579 return 0;
5581 fail_start_aen:
5582 fail_io_attach:
5583 megasas_mgmt_info.count--;
5584 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5585 megasas_mgmt_info.max_index--;
5587 instance->instancet->disable_intr(instance);
5588 megasas_destroy_irqs(instance);
5590 if (instance->ctrl_context)
5591 megasas_release_fusion(instance);
5592 else
5593 megasas_release_mfi(instance);
5594 if (instance->msix_vectors)
5595 pci_disable_msix(instance->pdev);
5596 fail_init_mfi:
5597 fail_alloc_dma_buf:
5598 if (instance->evt_detail)
5599 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5600 instance->evt_detail,
5601 instance->evt_detail_h);
5603 if (instance->producer)
5604 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5605 instance->producer_h);
5606 if (instance->consumer)
5607 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5608 instance->consumer_h);
5609 scsi_host_put(host);
5611 fail_alloc_instance:
5612 fail_set_dma_mask:
5613 pci_disable_device(pdev);
5615 return -ENODEV;
5619 * megasas_flush_cache - Requests FW to flush all its caches
5620 * @instance: Adapter soft state
5622 static void megasas_flush_cache(struct megasas_instance *instance)
5624 struct megasas_cmd *cmd;
5625 struct megasas_dcmd_frame *dcmd;
5627 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5628 return;
5630 cmd = megasas_get_cmd(instance);
5632 if (!cmd)
5633 return;
5635 dcmd = &cmd->frame->dcmd;
5637 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5639 dcmd->cmd = MFI_CMD_DCMD;
5640 dcmd->cmd_status = 0x0;
5641 dcmd->sge_count = 0;
5642 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5643 dcmd->timeout = 0;
5644 dcmd->pad_0 = 0;
5645 dcmd->data_xfer_len = 0;
5646 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5647 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5649 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5650 dev_err(&instance->pdev->dev, "Command timedout"
5651 " from %s\n", __func__);
5653 megasas_return_cmd(instance, cmd);
5657 * megasas_shutdown_controller - Instructs FW to shutdown the controller
5658 * @instance: Adapter soft state
5659 * @opcode: Shutdown/Hibernate
5661 static void megasas_shutdown_controller(struct megasas_instance *instance,
5662 u32 opcode)
5664 struct megasas_cmd *cmd;
5665 struct megasas_dcmd_frame *dcmd;
5667 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5668 return;
5670 cmd = megasas_get_cmd(instance);
5672 if (!cmd)
5673 return;
5675 if (instance->aen_cmd)
5676 megasas_issue_blocked_abort_cmd(instance,
5677 instance->aen_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5678 if (instance->map_update_cmd)
5679 megasas_issue_blocked_abort_cmd(instance,
5680 instance->map_update_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5681 if (instance->jbod_seq_cmd)
5682 megasas_issue_blocked_abort_cmd(instance,
5683 instance->jbod_seq_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5685 dcmd = &cmd->frame->dcmd;
5687 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5689 dcmd->cmd = MFI_CMD_DCMD;
5690 dcmd->cmd_status = 0x0;
5691 dcmd->sge_count = 0;
5692 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5693 dcmd->timeout = 0;
5694 dcmd->pad_0 = 0;
5695 dcmd->data_xfer_len = 0;
5696 dcmd->opcode = cpu_to_le32(opcode);
5698 if (megasas_issue_blocked_cmd(instance, cmd, 30))
5699 dev_err(&instance->pdev->dev, "Command timedout"
5700 "from %s\n", __func__);
5702 megasas_return_cmd(instance, cmd);
5705 #ifdef CONFIG_PM
5707 * megasas_suspend - driver suspend entry point
5708 * @pdev: PCI device structure
5709 * @state: PCI power state to suspend routine
5711 static int
5712 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5714 struct Scsi_Host *host;
5715 struct megasas_instance *instance;
5717 instance = pci_get_drvdata(pdev);
5718 host = instance->host;
5719 instance->unload = 1;
5721 /* Shutdown SR-IOV heartbeat timer */
5722 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5723 del_timer_sync(&instance->sriov_heartbeat_timer);
5725 megasas_flush_cache(instance);
5726 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5728 /* cancel the delayed work if this work still in queue */
5729 if (instance->ev != NULL) {
5730 struct megasas_aen_event *ev = instance->ev;
5731 cancel_delayed_work_sync(&ev->hotplug_work);
5732 instance->ev = NULL;
5735 tasklet_kill(&instance->isr_tasklet);
5737 pci_set_drvdata(instance->pdev, instance);
5738 instance->instancet->disable_intr(instance);
5740 megasas_destroy_irqs(instance);
5742 if (instance->msix_vectors)
5743 pci_disable_msix(instance->pdev);
5745 pci_save_state(pdev);
5746 pci_disable_device(pdev);
5748 pci_set_power_state(pdev, pci_choose_state(pdev, state));
5750 return 0;
5754 * megasas_resume- driver resume entry point
5755 * @pdev: PCI device structure
5757 static int
5758 megasas_resume(struct pci_dev *pdev)
5760 int rval;
5761 struct Scsi_Host *host;
5762 struct megasas_instance *instance;
5764 instance = pci_get_drvdata(pdev);
5765 host = instance->host;
5766 pci_set_power_state(pdev, PCI_D0);
5767 pci_enable_wake(pdev, PCI_D0, 0);
5768 pci_restore_state(pdev);
5771 * PCI prepping: enable device set bus mastering and dma mask
5773 rval = pci_enable_device_mem(pdev);
5775 if (rval) {
5776 dev_err(&pdev->dev, "Enable device failed\n");
5777 return rval;
5780 pci_set_master(pdev);
5782 if (megasas_set_dma_mask(pdev))
5783 goto fail_set_dma_mask;
5786 * Initialize MFI Firmware
5789 atomic_set(&instance->fw_outstanding, 0);
5792 * We expect the FW state to be READY
5794 if (megasas_transition_to_ready(instance, 0))
5795 goto fail_ready_state;
5797 /* Now re-enable MSI-X */
5798 if (instance->msix_vectors &&
5799 pci_enable_msix_exact(instance->pdev, instance->msixentry,
5800 instance->msix_vectors))
5801 goto fail_reenable_msix;
5803 if (instance->ctrl_context) {
5804 megasas_reset_reply_desc(instance);
5805 if (megasas_ioc_init_fusion(instance)) {
5806 megasas_free_cmds(instance);
5807 megasas_free_cmds_fusion(instance);
5808 goto fail_init_mfi;
5810 if (!megasas_get_map_info(instance))
5811 megasas_sync_map_info(instance);
5812 } else {
5813 *instance->producer = 0;
5814 *instance->consumer = 0;
5815 if (megasas_issue_init_mfi(instance))
5816 goto fail_init_mfi;
5819 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5820 (unsigned long)instance);
5822 if (instance->msix_vectors ?
5823 megasas_setup_irqs_msix(instance, 0) :
5824 megasas_setup_irqs_ioapic(instance))
5825 goto fail_init_mfi;
5827 /* Re-launch SR-IOV heartbeat timer */
5828 if (instance->requestorId) {
5829 if (!megasas_sriov_start_heartbeat(instance, 0))
5830 megasas_start_timer(instance,
5831 &instance->sriov_heartbeat_timer,
5832 megasas_sriov_heartbeat_handler,
5833 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5834 else {
5835 instance->skip_heartbeat_timer_del = 1;
5836 goto fail_init_mfi;
5840 instance->instancet->enable_intr(instance);
5841 megasas_setup_jbod_map(instance);
5842 instance->unload = 0;
5845 * Initiate AEN (Asynchronous Event Notification)
5847 if (megasas_start_aen(instance))
5848 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5850 return 0;
5852 fail_init_mfi:
5853 if (instance->evt_detail)
5854 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5855 instance->evt_detail,
5856 instance->evt_detail_h);
5858 if (instance->producer)
5859 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5860 instance->producer_h);
5861 if (instance->consumer)
5862 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5863 instance->consumer_h);
5864 scsi_host_put(host);
5866 fail_set_dma_mask:
5867 fail_ready_state:
5868 fail_reenable_msix:
5870 pci_disable_device(pdev);
5872 return -ENODEV;
5874 #else
5875 #define megasas_suspend NULL
5876 #define megasas_resume NULL
5877 #endif
5880 * megasas_detach_one - PCI hot"un"plug entry point
5881 * @pdev: PCI device structure
5883 static void megasas_detach_one(struct pci_dev *pdev)
5885 int i;
5886 struct Scsi_Host *host;
5887 struct megasas_instance *instance;
5888 struct fusion_context *fusion;
5889 u32 pd_seq_map_sz;
5891 instance = pci_get_drvdata(pdev);
5892 instance->unload = 1;
5893 host = instance->host;
5894 fusion = instance->ctrl_context;
5896 /* Shutdown SR-IOV heartbeat timer */
5897 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5898 del_timer_sync(&instance->sriov_heartbeat_timer);
5900 if (instance->fw_crash_state != UNAVAILABLE)
5901 megasas_free_host_crash_buffer(instance);
5902 scsi_remove_host(instance->host);
5903 megasas_flush_cache(instance);
5904 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5906 /* cancel the delayed work if this work still in queue*/
5907 if (instance->ev != NULL) {
5908 struct megasas_aen_event *ev = instance->ev;
5909 cancel_delayed_work_sync(&ev->hotplug_work);
5910 instance->ev = NULL;
5913 /* cancel all wait events */
5914 wake_up_all(&instance->int_cmd_wait_q);
5916 tasklet_kill(&instance->isr_tasklet);
5919 * Take the instance off the instance array. Note that we will not
5920 * decrement the max_index. We let this array be sparse array
5922 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5923 if (megasas_mgmt_info.instance[i] == instance) {
5924 megasas_mgmt_info.count--;
5925 megasas_mgmt_info.instance[i] = NULL;
5927 break;
5931 instance->instancet->disable_intr(instance);
5933 megasas_destroy_irqs(instance);
5935 if (instance->msix_vectors)
5936 pci_disable_msix(instance->pdev);
5938 if (instance->ctrl_context) {
5939 megasas_release_fusion(instance);
5940 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5941 (sizeof(struct MR_PD_CFG_SEQ) *
5942 (MAX_PHYSICAL_DEVICES - 1));
5943 for (i = 0; i < 2 ; i++) {
5944 if (fusion->ld_map[i])
5945 dma_free_coherent(&instance->pdev->dev,
5946 fusion->max_map_sz,
5947 fusion->ld_map[i],
5948 fusion->ld_map_phys[i]);
5949 if (fusion->ld_drv_map[i])
5950 free_pages((ulong)fusion->ld_drv_map[i],
5951 fusion->drv_map_pages);
5952 if (fusion->pd_seq_sync[i])
5953 dma_free_coherent(&instance->pdev->dev,
5954 pd_seq_map_sz,
5955 fusion->pd_seq_sync[i],
5956 fusion->pd_seq_phys[i]);
5958 free_pages((ulong)instance->ctrl_context,
5959 instance->ctrl_context_pages);
5960 } else {
5961 megasas_release_mfi(instance);
5962 pci_free_consistent(pdev, sizeof(u32),
5963 instance->producer,
5964 instance->producer_h);
5965 pci_free_consistent(pdev, sizeof(u32),
5966 instance->consumer,
5967 instance->consumer_h);
5970 kfree(instance->ctrl_info);
5972 if (instance->evt_detail)
5973 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5974 instance->evt_detail, instance->evt_detail_h);
5976 if (instance->vf_affiliation)
5977 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5978 sizeof(struct MR_LD_VF_AFFILIATION),
5979 instance->vf_affiliation,
5980 instance->vf_affiliation_h);
5982 if (instance->vf_affiliation_111)
5983 pci_free_consistent(pdev,
5984 sizeof(struct MR_LD_VF_AFFILIATION_111),
5985 instance->vf_affiliation_111,
5986 instance->vf_affiliation_111_h);
5988 if (instance->hb_host_mem)
5989 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5990 instance->hb_host_mem,
5991 instance->hb_host_mem_h);
5993 if (instance->crash_dump_buf)
5994 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5995 instance->crash_dump_buf, instance->crash_dump_h);
5997 if (instance->system_info_buf)
5998 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5999 instance->system_info_buf, instance->system_info_h);
6001 scsi_host_put(host);
6003 pci_disable_device(pdev);
6007 * megasas_shutdown - Shutdown entry point
6008 * @device: Generic device structure
6010 static void megasas_shutdown(struct pci_dev *pdev)
6012 struct megasas_instance *instance = pci_get_drvdata(pdev);
6014 instance->unload = 1;
6015 megasas_flush_cache(instance);
6016 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6017 instance->instancet->disable_intr(instance);
6018 megasas_destroy_irqs(instance);
6020 if (instance->msix_vectors)
6021 pci_disable_msix(instance->pdev);
6025 * megasas_mgmt_open - char node "open" entry point
6027 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6030 * Allow only those users with admin rights
6032 if (!capable(CAP_SYS_ADMIN))
6033 return -EACCES;
6035 return 0;
6039 * megasas_mgmt_fasync - Async notifier registration from applications
6041 * This function adds the calling process to a driver global queue. When an
6042 * event occurs, SIGIO will be sent to all processes in this queue.
6044 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6046 int rc;
6048 mutex_lock(&megasas_async_queue_mutex);
6050 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6052 mutex_unlock(&megasas_async_queue_mutex);
6054 if (rc >= 0) {
6055 /* For sanity check when we get ioctl */
6056 filep->private_data = filep;
6057 return 0;
6060 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6062 return rc;
6066 * megasas_mgmt_poll - char node "poll" entry point
6067 * */
6068 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6070 unsigned int mask;
6071 unsigned long flags;
6073 poll_wait(file, &megasas_poll_wait, wait);
6074 spin_lock_irqsave(&poll_aen_lock, flags);
6075 if (megasas_poll_wait_aen)
6076 mask = (POLLIN | POLLRDNORM);
6077 else
6078 mask = 0;
6079 megasas_poll_wait_aen = 0;
6080 spin_unlock_irqrestore(&poll_aen_lock, flags);
6081 return mask;
6085 * megasas_set_crash_dump_params_ioctl:
6086 * Send CRASH_DUMP_MODE DCMD to all controllers
6087 * @cmd: MFI command frame
6090 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6092 struct megasas_instance *local_instance;
6093 int i, error = 0;
6094 int crash_support;
6096 crash_support = cmd->frame->dcmd.mbox.w[0];
6098 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6099 local_instance = megasas_mgmt_info.instance[i];
6100 if (local_instance && local_instance->crash_dump_drv_support) {
6101 if ((local_instance->adprecovery ==
6102 MEGASAS_HBA_OPERATIONAL) &&
6103 !megasas_set_crash_dump_params(local_instance,
6104 crash_support)) {
6105 local_instance->crash_dump_app_support =
6106 crash_support;
6107 dev_info(&local_instance->pdev->dev,
6108 "Application firmware crash "
6109 "dump mode set success\n");
6110 error = 0;
6111 } else {
6112 dev_info(&local_instance->pdev->dev,
6113 "Application firmware crash "
6114 "dump mode set failed\n");
6115 error = -1;
6119 return error;
6123 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6124 * @instance: Adapter soft state
6125 * @argp: User's ioctl packet
6127 static int
6128 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6129 struct megasas_iocpacket __user * user_ioc,
6130 struct megasas_iocpacket *ioc)
6132 struct megasas_sge32 *kern_sge32;
6133 struct megasas_cmd *cmd;
6134 void *kbuff_arr[MAX_IOCTL_SGE];
6135 dma_addr_t buf_handle = 0;
6136 int error = 0, i;
6137 void *sense = NULL;
6138 dma_addr_t sense_handle;
6139 unsigned long *sense_ptr;
6141 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6143 if (ioc->sge_count > MAX_IOCTL_SGE) {
6144 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6145 ioc->sge_count, MAX_IOCTL_SGE);
6146 return -EINVAL;
6149 cmd = megasas_get_cmd(instance);
6150 if (!cmd) {
6151 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6152 return -ENOMEM;
6156 * User's IOCTL packet has 2 frames (maximum). Copy those two
6157 * frames into our cmd's frames. cmd->frame's context will get
6158 * overwritten when we copy from user's frames. So set that value
6159 * alone separately
6161 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6162 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6163 cmd->frame->hdr.pad_0 = 0;
6164 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6165 MFI_FRAME_SGL64 |
6166 MFI_FRAME_SENSE64));
6168 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6169 error = megasas_set_crash_dump_params_ioctl(cmd);
6170 megasas_return_cmd(instance, cmd);
6171 return error;
6175 * The management interface between applications and the fw uses
6176 * MFI frames. E.g, RAID configuration changes, LD property changes
6177 * etc are accomplishes through different kinds of MFI frames. The
6178 * driver needs to care only about substituting user buffers with
6179 * kernel buffers in SGLs. The location of SGL is embedded in the
6180 * struct iocpacket itself.
6182 kern_sge32 = (struct megasas_sge32 *)
6183 ((unsigned long)cmd->frame + ioc->sgl_off);
6186 * For each user buffer, create a mirror buffer and copy in
6188 for (i = 0; i < ioc->sge_count; i++) {
6189 if (!ioc->sgl[i].iov_len)
6190 continue;
6192 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6193 ioc->sgl[i].iov_len,
6194 &buf_handle, GFP_KERNEL);
6195 if (!kbuff_arr[i]) {
6196 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6197 "kernel SGL buffer for IOCTL\n");
6198 error = -ENOMEM;
6199 goto out;
6203 * We don't change the dma_coherent_mask, so
6204 * pci_alloc_consistent only returns 32bit addresses
6206 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6207 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6210 * We created a kernel buffer corresponding to the
6211 * user buffer. Now copy in from the user buffer
6213 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6214 (u32) (ioc->sgl[i].iov_len))) {
6215 error = -EFAULT;
6216 goto out;
6220 if (ioc->sense_len) {
6221 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6222 &sense_handle, GFP_KERNEL);
6223 if (!sense) {
6224 error = -ENOMEM;
6225 goto out;
6228 sense_ptr =
6229 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6230 *sense_ptr = cpu_to_le32(sense_handle);
6234 * Set the sync_cmd flag so that the ISR knows not to complete this
6235 * cmd to the SCSI mid-layer
6237 cmd->sync_cmd = 1;
6238 megasas_issue_blocked_cmd(instance, cmd, 0);
6239 cmd->sync_cmd = 0;
6241 if (instance->unload == 1) {
6242 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6243 "don't submit data to application\n");
6244 goto out;
6247 * copy out the kernel buffers to user buffers
6249 for (i = 0; i < ioc->sge_count; i++) {
6250 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6251 ioc->sgl[i].iov_len)) {
6252 error = -EFAULT;
6253 goto out;
6258 * copy out the sense
6260 if (ioc->sense_len) {
6262 * sense_ptr points to the location that has the user
6263 * sense buffer address
6265 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6266 ioc->sense_off);
6268 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6269 sense, ioc->sense_len)) {
6270 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6271 "sense data\n");
6272 error = -EFAULT;
6273 goto out;
6278 * copy the status codes returned by the fw
6280 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6281 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6282 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6283 error = -EFAULT;
6286 out:
6287 if (sense) {
6288 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6289 sense, sense_handle);
6292 for (i = 0; i < ioc->sge_count; i++) {
6293 if (kbuff_arr[i]) {
6294 dma_free_coherent(&instance->pdev->dev,
6295 le32_to_cpu(kern_sge32[i].length),
6296 kbuff_arr[i],
6297 le32_to_cpu(kern_sge32[i].phys_addr));
6298 kbuff_arr[i] = NULL;
6302 megasas_return_cmd(instance, cmd);
6303 return error;
6306 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6308 struct megasas_iocpacket __user *user_ioc =
6309 (struct megasas_iocpacket __user *)arg;
6310 struct megasas_iocpacket *ioc;
6311 struct megasas_instance *instance;
6312 int error;
6313 int i;
6314 unsigned long flags;
6315 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6317 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6318 if (!ioc)
6319 return -ENOMEM;
6321 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6322 error = -EFAULT;
6323 goto out_kfree_ioc;
6326 instance = megasas_lookup_instance(ioc->host_no);
6327 if (!instance) {
6328 error = -ENODEV;
6329 goto out_kfree_ioc;
6332 /* Adjust ioctl wait time for VF mode */
6333 if (instance->requestorId)
6334 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6336 /* Block ioctls in VF mode */
6337 if (instance->requestorId && !allow_vf_ioctls) {
6338 error = -ENODEV;
6339 goto out_kfree_ioc;
6342 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6343 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6344 error = -ENODEV;
6345 goto out_kfree_ioc;
6348 if (instance->unload == 1) {
6349 error = -ENODEV;
6350 goto out_kfree_ioc;
6353 if (down_interruptible(&instance->ioctl_sem)) {
6354 error = -ERESTARTSYS;
6355 goto out_kfree_ioc;
6358 for (i = 0; i < wait_time; i++) {
6360 spin_lock_irqsave(&instance->hba_lock, flags);
6361 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6362 spin_unlock_irqrestore(&instance->hba_lock, flags);
6363 break;
6365 spin_unlock_irqrestore(&instance->hba_lock, flags);
6367 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6368 dev_notice(&instance->pdev->dev, "waiting"
6369 "for controller reset to finish\n");
6372 msleep(1000);
6375 spin_lock_irqsave(&instance->hba_lock, flags);
6376 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6377 spin_unlock_irqrestore(&instance->hba_lock, flags);
6379 dev_err(&instance->pdev->dev, "timed out while"
6380 "waiting for HBA to recover\n");
6381 error = -ENODEV;
6382 goto out_up;
6384 spin_unlock_irqrestore(&instance->hba_lock, flags);
6386 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6387 out_up:
6388 up(&instance->ioctl_sem);
6390 out_kfree_ioc:
6391 kfree(ioc);
6392 return error;
6395 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6397 struct megasas_instance *instance;
6398 struct megasas_aen aen;
6399 int error;
6400 int i;
6401 unsigned long flags;
6402 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6404 if (file->private_data != file) {
6405 printk(KERN_DEBUG "megasas: fasync_helper was not "
6406 "called first\n");
6407 return -EINVAL;
6410 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6411 return -EFAULT;
6413 instance = megasas_lookup_instance(aen.host_no);
6415 if (!instance)
6416 return -ENODEV;
6418 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6419 return -ENODEV;
6422 if (instance->unload == 1) {
6423 return -ENODEV;
6426 for (i = 0; i < wait_time; i++) {
6428 spin_lock_irqsave(&instance->hba_lock, flags);
6429 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6430 spin_unlock_irqrestore(&instance->hba_lock,
6431 flags);
6432 break;
6435 spin_unlock_irqrestore(&instance->hba_lock, flags);
6437 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6438 dev_notice(&instance->pdev->dev, "waiting for"
6439 "controller reset to finish\n");
6442 msleep(1000);
6445 spin_lock_irqsave(&instance->hba_lock, flags);
6446 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6447 spin_unlock_irqrestore(&instance->hba_lock, flags);
6448 dev_err(&instance->pdev->dev, "timed out while waiting"
6449 "for HBA to recover\n");
6450 return -ENODEV;
6452 spin_unlock_irqrestore(&instance->hba_lock, flags);
6454 mutex_lock(&instance->reset_mutex);
6455 error = megasas_register_aen(instance, aen.seq_num,
6456 aen.class_locale_word);
6457 mutex_unlock(&instance->reset_mutex);
6458 return error;
6462 * megasas_mgmt_ioctl - char node ioctl entry point
6464 static long
6465 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6467 switch (cmd) {
6468 case MEGASAS_IOC_FIRMWARE:
6469 return megasas_mgmt_ioctl_fw(file, arg);
6471 case MEGASAS_IOC_GET_AEN:
6472 return megasas_mgmt_ioctl_aen(file, arg);
6475 return -ENOTTY;
6478 #ifdef CONFIG_COMPAT
6479 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6481 struct compat_megasas_iocpacket __user *cioc =
6482 (struct compat_megasas_iocpacket __user *)arg;
6483 struct megasas_iocpacket __user *ioc =
6484 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6485 int i;
6486 int error = 0;
6487 compat_uptr_t ptr;
6488 u32 local_sense_off;
6489 u32 local_sense_len;
6490 u32 user_sense_off;
6492 if (clear_user(ioc, sizeof(*ioc)))
6493 return -EFAULT;
6495 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6496 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6497 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6498 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6499 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6500 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6501 return -EFAULT;
6504 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6505 * sense_len is not null, so prepare the 64bit value under
6506 * the same condition.
6508 if (get_user(local_sense_off, &ioc->sense_off) ||
6509 get_user(local_sense_len, &ioc->sense_len) ||
6510 get_user(user_sense_off, &cioc->sense_off))
6511 return -EFAULT;
6513 if (local_sense_len) {
6514 void __user **sense_ioc_ptr =
6515 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
6516 compat_uptr_t *sense_cioc_ptr =
6517 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
6518 if (get_user(ptr, sense_cioc_ptr) ||
6519 put_user(compat_ptr(ptr), sense_ioc_ptr))
6520 return -EFAULT;
6523 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6524 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6525 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6526 copy_in_user(&ioc->sgl[i].iov_len,
6527 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6528 return -EFAULT;
6531 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6533 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6534 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6535 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6536 return -EFAULT;
6538 return error;
6541 static long
6542 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6543 unsigned long arg)
6545 switch (cmd) {
6546 case MEGASAS_IOC_FIRMWARE32:
6547 return megasas_mgmt_compat_ioctl_fw(file, arg);
6548 case MEGASAS_IOC_GET_AEN:
6549 return megasas_mgmt_ioctl_aen(file, arg);
6552 return -ENOTTY;
6554 #endif
6557 * File operations structure for management interface
6559 static const struct file_operations megasas_mgmt_fops = {
6560 .owner = THIS_MODULE,
6561 .open = megasas_mgmt_open,
6562 .fasync = megasas_mgmt_fasync,
6563 .unlocked_ioctl = megasas_mgmt_ioctl,
6564 .poll = megasas_mgmt_poll,
6565 #ifdef CONFIG_COMPAT
6566 .compat_ioctl = megasas_mgmt_compat_ioctl,
6567 #endif
6568 .llseek = noop_llseek,
6572 * PCI hotplug support registration structure
6574 static struct pci_driver megasas_pci_driver = {
6576 .name = "megaraid_sas",
6577 .id_table = megasas_pci_table,
6578 .probe = megasas_probe_one,
6579 .remove = megasas_detach_one,
6580 .suspend = megasas_suspend,
6581 .resume = megasas_resume,
6582 .shutdown = megasas_shutdown,
6586 * Sysfs driver attributes
6588 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6590 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6591 MEGASAS_VERSION);
6594 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6596 static ssize_t
6597 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6599 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6600 MEGASAS_RELDATE);
6603 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6605 static ssize_t
6606 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6608 return sprintf(buf, "%u\n", support_poll_for_event);
6611 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6612 megasas_sysfs_show_support_poll_for_event, NULL);
6614 static ssize_t
6615 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6617 return sprintf(buf, "%u\n", support_device_change);
6620 static DRIVER_ATTR(support_device_change, S_IRUGO,
6621 megasas_sysfs_show_support_device_change, NULL);
6623 static ssize_t
6624 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6626 return sprintf(buf, "%u\n", megasas_dbg_lvl);
6629 static ssize_t
6630 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6632 int retval = count;
6634 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
6635 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6636 retval = -EINVAL;
6638 return retval;
6641 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6642 megasas_sysfs_set_dbg_lvl);
6644 static void
6645 megasas_aen_polling(struct work_struct *work)
6647 struct megasas_aen_event *ev =
6648 container_of(work, struct megasas_aen_event, hotplug_work.work);
6649 struct megasas_instance *instance = ev->instance;
6650 union megasas_evt_class_locale class_locale;
6651 struct Scsi_Host *host;
6652 struct scsi_device *sdev1;
6653 u16 pd_index = 0;
6654 u16 ld_index = 0;
6655 int i, j, doscan = 0;
6656 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6657 int error;
6658 u8 dcmd_ret = 0;
6660 if (!instance) {
6661 printk(KERN_ERR "invalid instance!\n");
6662 kfree(ev);
6663 return;
6666 /* Adjust event workqueue thread wait time for VF mode */
6667 if (instance->requestorId)
6668 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6670 /* Don't run the event workqueue thread if OCR is running */
6671 mutex_lock(&instance->reset_mutex);
6673 instance->ev = NULL;
6674 host = instance->host;
6675 if (instance->evt_detail) {
6676 megasas_decode_evt(instance);
6678 switch (le32_to_cpu(instance->evt_detail->code)) {
6680 case MR_EVT_PD_INSERTED:
6681 case MR_EVT_PD_REMOVED:
6682 dcmd_ret = megasas_get_pd_list(instance);
6683 if (dcmd_ret == 0)
6684 doscan = SCAN_PD_CHANNEL;
6685 break;
6687 case MR_EVT_LD_OFFLINE:
6688 case MR_EVT_CFG_CLEARED:
6689 case MR_EVT_LD_DELETED:
6690 case MR_EVT_LD_CREATED:
6691 if (!instance->requestorId ||
6692 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6693 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
6695 if (dcmd_ret == 0)
6696 doscan = SCAN_VD_CHANNEL;
6698 break;
6700 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6701 case MR_EVT_FOREIGN_CFG_IMPORTED:
6702 case MR_EVT_LD_STATE_CHANGE:
6703 dcmd_ret = megasas_get_pd_list(instance);
6705 if (dcmd_ret != 0)
6706 break;
6708 if (!instance->requestorId ||
6709 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
6710 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
6712 if (dcmd_ret != 0)
6713 break;
6715 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
6716 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6717 instance->host->host_no);
6718 break;
6720 case MR_EVT_CTRL_PROP_CHANGED:
6721 dcmd_ret = megasas_get_ctrl_info(instance);
6722 break;
6723 default:
6724 doscan = 0;
6725 break;
6727 } else {
6728 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6729 mutex_unlock(&instance->reset_mutex);
6730 kfree(ev);
6731 return;
6734 mutex_unlock(&instance->reset_mutex);
6736 if (doscan & SCAN_PD_CHANNEL) {
6737 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6738 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6739 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6740 sdev1 = scsi_device_lookup(host, i, j, 0);
6741 if (instance->pd_list[pd_index].driveState ==
6742 MR_PD_STATE_SYSTEM) {
6743 if (!sdev1)
6744 scsi_add_device(host, i, j, 0);
6745 else
6746 scsi_device_put(sdev1);
6747 } else {
6748 if (sdev1) {
6749 scsi_remove_device(sdev1);
6750 scsi_device_put(sdev1);
6757 if (doscan & SCAN_VD_CHANNEL) {
6758 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6759 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6760 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6761 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6762 if (instance->ld_ids[ld_index] != 0xff) {
6763 if (!sdev1)
6764 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6765 else
6766 scsi_device_put(sdev1);
6767 } else {
6768 if (sdev1) {
6769 scsi_remove_device(sdev1);
6770 scsi_device_put(sdev1);
6777 if (dcmd_ret == 0)
6778 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6779 else
6780 seq_num = instance->last_seq_num;
6782 /* Register AEN with FW for latest sequence number plus 1 */
6783 class_locale.members.reserved = 0;
6784 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6785 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6787 if (instance->aen_cmd != NULL) {
6788 kfree(ev);
6789 return;
6792 mutex_lock(&instance->reset_mutex);
6793 error = megasas_register_aen(instance, seq_num,
6794 class_locale.word);
6795 if (error)
6796 dev_err(&instance->pdev->dev,
6797 "register aen failed error %x\n", error);
6799 mutex_unlock(&instance->reset_mutex);
6800 kfree(ev);
6804 * megasas_init - Driver load entry point
6806 static int __init megasas_init(void)
6808 int rval;
6811 * Announce driver version and other information
6813 pr_info("megasas: %s\n", MEGASAS_VERSION);
6815 spin_lock_init(&poll_aen_lock);
6817 support_poll_for_event = 2;
6818 support_device_change = 1;
6820 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6823 * Register character device node
6825 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6827 if (rval < 0) {
6828 printk(KERN_DEBUG "megasas: failed to open device node\n");
6829 return rval;
6832 megasas_mgmt_majorno = rval;
6835 * Register ourselves as PCI hotplug module
6837 rval = pci_register_driver(&megasas_pci_driver);
6839 if (rval) {
6840 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6841 goto err_pcidrv;
6844 rval = driver_create_file(&megasas_pci_driver.driver,
6845 &driver_attr_version);
6846 if (rval)
6847 goto err_dcf_attr_ver;
6849 rval = driver_create_file(&megasas_pci_driver.driver,
6850 &driver_attr_release_date);
6851 if (rval)
6852 goto err_dcf_rel_date;
6854 rval = driver_create_file(&megasas_pci_driver.driver,
6855 &driver_attr_support_poll_for_event);
6856 if (rval)
6857 goto err_dcf_support_poll_for_event;
6859 rval = driver_create_file(&megasas_pci_driver.driver,
6860 &driver_attr_dbg_lvl);
6861 if (rval)
6862 goto err_dcf_dbg_lvl;
6863 rval = driver_create_file(&megasas_pci_driver.driver,
6864 &driver_attr_support_device_change);
6865 if (rval)
6866 goto err_dcf_support_device_change;
6868 return rval;
6870 err_dcf_support_device_change:
6871 driver_remove_file(&megasas_pci_driver.driver,
6872 &driver_attr_dbg_lvl);
6873 err_dcf_dbg_lvl:
6874 driver_remove_file(&megasas_pci_driver.driver,
6875 &driver_attr_support_poll_for_event);
6876 err_dcf_support_poll_for_event:
6877 driver_remove_file(&megasas_pci_driver.driver,
6878 &driver_attr_release_date);
6879 err_dcf_rel_date:
6880 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6881 err_dcf_attr_ver:
6882 pci_unregister_driver(&megasas_pci_driver);
6883 err_pcidrv:
6884 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6885 return rval;
6889 * megasas_exit - Driver unload entry point
6891 static void __exit megasas_exit(void)
6893 driver_remove_file(&megasas_pci_driver.driver,
6894 &driver_attr_dbg_lvl);
6895 driver_remove_file(&megasas_pci_driver.driver,
6896 &driver_attr_support_poll_for_event);
6897 driver_remove_file(&megasas_pci_driver.driver,
6898 &driver_attr_support_device_change);
6899 driver_remove_file(&megasas_pci_driver.driver,
6900 &driver_attr_release_date);
6901 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6903 pci_unregister_driver(&megasas_pci_driver);
6904 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6907 module_init(megasas_init);
6908 module_exit(megasas_exit);