]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/scsi/megaraid/megaraid_sas_base.c
scsi: megaraid_sas: remove set but not used variable 'cur_state'
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / megaraid / megaraid_sas_base.c
1 /*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * Authors: Broadcom Inc.
22 * Sreenivas Bagalkote
23 * Sumant Patro
24 * Bo Yang
25 * Adam Radford
26 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
28 *
29 * Send feedback to: megaraidlinux.pdl@broadcom.com
30 */
31
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <asm/unaligned.h>
45 #include <linux/fs.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
50 #include <linux/vmalloc.h>
51 #include <linux/irq_poll.h>
52
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include <scsi/scsi_dbg.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
61
62 /*
63 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
65 */
66 static unsigned int max_sectors;
67 module_param_named(max_sectors, max_sectors, int, 0);
68 MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
70
71 static int msix_disable;
72 module_param(msix_disable, int, S_IRUGO);
73 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74
75 static unsigned int msix_vectors;
76 module_param(msix_vectors, int, S_IRUGO);
77 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
78
79 static int allow_vf_ioctls;
80 module_param(allow_vf_ioctls, int, S_IRUGO);
81 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
82
83 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
84 module_param(throttlequeuedepth, int, S_IRUGO);
85 MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87
88 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
89 module_param(resetwaittime, int, S_IRUGO);
90 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
91
92 int smp_affinity_enable = 1;
93 module_param(smp_affinity_enable, int, S_IRUGO);
94 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
95
96 int rdpq_enable = 1;
97 module_param(rdpq_enable, int, S_IRUGO);
98 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
99
100 unsigned int dual_qdepth_disable;
101 module_param(dual_qdepth_disable, int, S_IRUGO);
102 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103
104 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
105 module_param(scmd_timeout, int, S_IRUGO);
106 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107
108 MODULE_LICENSE("GPL");
109 MODULE_VERSION(MEGASAS_VERSION);
110 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
111 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
112
113 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
114 static int megasas_get_pd_list(struct megasas_instance *instance);
115 static int megasas_ld_list_query(struct megasas_instance *instance,
116 u8 query_type);
117 static int megasas_issue_init_mfi(struct megasas_instance *instance);
118 static int megasas_register_aen(struct megasas_instance *instance,
119 u32 seq_num, u32 class_locale_word);
120 static void megasas_get_pd_info(struct megasas_instance *instance,
121 struct scsi_device *sdev);
122
123 /*
124 * PCI ID table for all supported controllers
125 */
126 static struct pci_device_id megasas_pci_table[] = {
127
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
129 /* xscale IOP */
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
131 /* ppc IOP */
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
133 /* ppc IOP */
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
135 /* gen2*/
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
137 /* gen2*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
139 /* skinny*/
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
141 /* skinny*/
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
143 /* xscale IOP, vega */
144 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
145 /* xscale IOP */
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
147 /* Fusion */
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
149 /* Plasma */
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
151 /* Invader */
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
153 /* Fury */
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
155 /* Intruder */
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
157 /* Intruder 24 port*/
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
160 /* VENTURA */
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
171 {}
172 };
173
174 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
175
176 static int megasas_mgmt_majorno;
177 struct megasas_mgmt_info megasas_mgmt_info;
178 static struct fasync_struct *megasas_async_queue;
179 static DEFINE_MUTEX(megasas_async_queue_mutex);
180
181 static int megasas_poll_wait_aen;
182 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
183 static u32 support_poll_for_event;
184 u32 megasas_dbg_lvl;
185 static u32 support_device_change;
186 static bool support_nvme_encapsulation;
187
188 /* define lock for aen poll */
189 spinlock_t poll_aen_lock;
190
191 extern struct dentry *megasas_debugfs_root;
192 extern void megasas_init_debugfs(void);
193 extern void megasas_exit_debugfs(void);
194 extern void megasas_setup_debugfs(struct megasas_instance *instance);
195 extern void megasas_destroy_debugfs(struct megasas_instance *instance);
196
197 void
198 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
199 u8 alt_status);
200 static u32
201 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
202 static int
203 megasas_adp_reset_gen2(struct megasas_instance *instance,
204 struct megasas_register_set __iomem *reg_set);
205 static irqreturn_t megasas_isr(int irq, void *devp);
206 static u32
207 megasas_init_adapter_mfi(struct megasas_instance *instance);
208 u32
209 megasas_build_and_issue_cmd(struct megasas_instance *instance,
210 struct scsi_cmnd *scmd);
211 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
212 int
213 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
214 int seconds);
215 void megasas_fusion_ocr_wq(struct work_struct *work);
216 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
217 int initial);
218 static int
219 megasas_set_dma_mask(struct megasas_instance *instance);
220 static int
221 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
222 static inline void
223 megasas_free_ctrl_mem(struct megasas_instance *instance);
224 static inline int
225 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
226 static inline void
227 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
228 static inline void
229 megasas_init_ctrl_params(struct megasas_instance *instance);
230
231 u32 megasas_readl(struct megasas_instance *instance,
232 const volatile void __iomem *addr)
233 {
234 u32 i = 0, ret_val;
235 /*
236 * Due to a HW errata in Aero controllers, reads to certain
237 * Fusion registers could intermittently return all zeroes.
238 * This behavior is transient in nature and subsequent reads will
239 * return valid value. As a workaround in driver, retry readl for
240 * upto three times until a non-zero value is read.
241 */
242 if (instance->adapter_type == AERO_SERIES) {
243 do {
244 ret_val = readl(addr);
245 i++;
246 } while (ret_val == 0 && i < 3);
247 return ret_val;
248 } else {
249 return readl(addr);
250 }
251 }
252
253 /**
254 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
255 * @instance: Adapter soft state
256 * @dcmd: DCMD frame inside MFI command
257 * @dma_addr: DMA address of buffer to be passed to FW
258 * @dma_len: Length of DMA buffer to be passed to FW
259 * @return: void
260 */
261 void megasas_set_dma_settings(struct megasas_instance *instance,
262 struct megasas_dcmd_frame *dcmd,
263 dma_addr_t dma_addr, u32 dma_len)
264 {
265 if (instance->consistent_mask_64bit) {
266 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
267 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
268 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
269
270 } else {
271 dcmd->sgl.sge32[0].phys_addr =
272 cpu_to_le32(lower_32_bits(dma_addr));
273 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
274 dcmd->flags = cpu_to_le16(dcmd->flags);
275 }
276 }
277
278 void
279 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
280 {
281 instance->instancet->fire_cmd(instance,
282 cmd->frame_phys_addr, 0, instance->reg_set);
283 return;
284 }
285
286 /**
287 * megasas_get_cmd - Get a command from the free pool
288 * @instance: Adapter soft state
289 *
290 * Returns a free command from the pool
291 */
292 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
293 *instance)
294 {
295 unsigned long flags;
296 struct megasas_cmd *cmd = NULL;
297
298 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
299
300 if (!list_empty(&instance->cmd_pool)) {
301 cmd = list_entry((&instance->cmd_pool)->next,
302 struct megasas_cmd, list);
303 list_del_init(&cmd->list);
304 } else {
305 dev_err(&instance->pdev->dev, "Command pool empty!\n");
306 }
307
308 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
309 return cmd;
310 }
311
312 /**
313 * megasas_return_cmd - Return a cmd to free command pool
314 * @instance: Adapter soft state
315 * @cmd: Command packet to be returned to free command pool
316 */
317 void
318 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
319 {
320 unsigned long flags;
321 u32 blk_tags;
322 struct megasas_cmd_fusion *cmd_fusion;
323 struct fusion_context *fusion = instance->ctrl_context;
324
325 /* This flag is used only for fusion adapter.
326 * Wait for Interrupt for Polled mode DCMD
327 */
328 if (cmd->flags & DRV_DCMD_POLLED_MODE)
329 return;
330
331 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
332
333 if (fusion) {
334 blk_tags = instance->max_scsi_cmds + cmd->index;
335 cmd_fusion = fusion->cmd_list[blk_tags];
336 megasas_return_cmd_fusion(instance, cmd_fusion);
337 }
338 cmd->scmd = NULL;
339 cmd->frame_count = 0;
340 cmd->flags = 0;
341 memset(cmd->frame, 0, instance->mfi_frame_size);
342 cmd->frame->io.context = cpu_to_le32(cmd->index);
343 if (!fusion && reset_devices)
344 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
345 list_add(&cmd->list, (&instance->cmd_pool)->next);
346
347 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
348
349 }
350
351 static const char *
352 format_timestamp(uint32_t timestamp)
353 {
354 static char buffer[32];
355
356 if ((timestamp & 0xff000000) == 0xff000000)
357 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
358 0x00ffffff);
359 else
360 snprintf(buffer, sizeof(buffer), "%us", timestamp);
361 return buffer;
362 }
363
364 static const char *
365 format_class(int8_t class)
366 {
367 static char buffer[6];
368
369 switch (class) {
370 case MFI_EVT_CLASS_DEBUG:
371 return "debug";
372 case MFI_EVT_CLASS_PROGRESS:
373 return "progress";
374 case MFI_EVT_CLASS_INFO:
375 return "info";
376 case MFI_EVT_CLASS_WARNING:
377 return "WARN";
378 case MFI_EVT_CLASS_CRITICAL:
379 return "CRIT";
380 case MFI_EVT_CLASS_FATAL:
381 return "FATAL";
382 case MFI_EVT_CLASS_DEAD:
383 return "DEAD";
384 default:
385 snprintf(buffer, sizeof(buffer), "%d", class);
386 return buffer;
387 }
388 }
389
390 /**
391 * megasas_decode_evt: Decode FW AEN event and print critical event
392 * for information.
393 * @instance: Adapter soft state
394 */
395 static void
396 megasas_decode_evt(struct megasas_instance *instance)
397 {
398 struct megasas_evt_detail *evt_detail = instance->evt_detail;
399 union megasas_evt_class_locale class_locale;
400 class_locale.word = le32_to_cpu(evt_detail->cl.word);
401
402 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
403 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
404 le32_to_cpu(evt_detail->seq_num),
405 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
406 (class_locale.members.locale),
407 format_class(class_locale.members.class),
408 evt_detail->description);
409 }
410
411 /**
412 * The following functions are defined for xscale
413 * (deviceid : 1064R, PERC5) controllers
414 */
415
416 /**
417 * megasas_enable_intr_xscale - Enables interrupts
418 * @regs: MFI register set
419 */
420 static inline void
421 megasas_enable_intr_xscale(struct megasas_instance *instance)
422 {
423 struct megasas_register_set __iomem *regs;
424
425 regs = instance->reg_set;
426 writel(0, &(regs)->outbound_intr_mask);
427
428 /* Dummy readl to force pci flush */
429 readl(&regs->outbound_intr_mask);
430 }
431
432 /**
433 * megasas_disable_intr_xscale -Disables interrupt
434 * @regs: MFI register set
435 */
436 static inline void
437 megasas_disable_intr_xscale(struct megasas_instance *instance)
438 {
439 struct megasas_register_set __iomem *regs;
440 u32 mask = 0x1f;
441
442 regs = instance->reg_set;
443 writel(mask, &regs->outbound_intr_mask);
444 /* Dummy readl to force pci flush */
445 readl(&regs->outbound_intr_mask);
446 }
447
448 /**
449 * megasas_read_fw_status_reg_xscale - returns the current FW status value
450 * @regs: MFI register set
451 */
452 static u32
453 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
454 {
455 return readl(&instance->reg_set->outbound_msg_0);
456 }
457 /**
458 * megasas_clear_interrupt_xscale - Check & clear interrupt
459 * @regs: MFI register set
460 */
461 static int
462 megasas_clear_intr_xscale(struct megasas_instance *instance)
463 {
464 u32 status;
465 u32 mfiStatus = 0;
466 struct megasas_register_set __iomem *regs;
467 regs = instance->reg_set;
468
469 /*
470 * Check if it is our interrupt
471 */
472 status = readl(&regs->outbound_intr_status);
473
474 if (status & MFI_OB_INTR_STATUS_MASK)
475 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
476 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
477 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
478
479 /*
480 * Clear the interrupt by writing back the same value
481 */
482 if (mfiStatus)
483 writel(status, &regs->outbound_intr_status);
484
485 /* Dummy readl to force pci flush */
486 readl(&regs->outbound_intr_status);
487
488 return mfiStatus;
489 }
490
491 /**
492 * megasas_fire_cmd_xscale - Sends command to the FW
493 * @frame_phys_addr : Physical address of cmd
494 * @frame_count : Number of frames for the command
495 * @regs : MFI register set
496 */
497 static inline void
498 megasas_fire_cmd_xscale(struct megasas_instance *instance,
499 dma_addr_t frame_phys_addr,
500 u32 frame_count,
501 struct megasas_register_set __iomem *regs)
502 {
503 unsigned long flags;
504
505 spin_lock_irqsave(&instance->hba_lock, flags);
506 writel((frame_phys_addr >> 3)|(frame_count),
507 &(regs)->inbound_queue_port);
508 spin_unlock_irqrestore(&instance->hba_lock, flags);
509 }
510
511 /**
512 * megasas_adp_reset_xscale - For controller reset
513 * @regs: MFI register set
514 */
515 static int
516 megasas_adp_reset_xscale(struct megasas_instance *instance,
517 struct megasas_register_set __iomem *regs)
518 {
519 u32 i;
520 u32 pcidata;
521
522 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
523
524 for (i = 0; i < 3; i++)
525 msleep(1000); /* sleep for 3 secs */
526 pcidata = 0;
527 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
528 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
529 if (pcidata & 0x2) {
530 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
531 pcidata &= ~0x2;
532 pci_write_config_dword(instance->pdev,
533 MFI_1068_PCSR_OFFSET, pcidata);
534
535 for (i = 0; i < 2; i++)
536 msleep(1000); /* need to wait 2 secs again */
537
538 pcidata = 0;
539 pci_read_config_dword(instance->pdev,
540 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
541 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
542 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
543 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
544 pcidata = 0;
545 pci_write_config_dword(instance->pdev,
546 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
547 }
548 }
549 return 0;
550 }
551
552 /**
553 * megasas_check_reset_xscale - For controller reset check
554 * @regs: MFI register set
555 */
556 static int
557 megasas_check_reset_xscale(struct megasas_instance *instance,
558 struct megasas_register_set __iomem *regs)
559 {
560 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
561 (le32_to_cpu(*instance->consumer) ==
562 MEGASAS_ADPRESET_INPROG_SIGN))
563 return 1;
564 return 0;
565 }
566
567 static struct megasas_instance_template megasas_instance_template_xscale = {
568
569 .fire_cmd = megasas_fire_cmd_xscale,
570 .enable_intr = megasas_enable_intr_xscale,
571 .disable_intr = megasas_disable_intr_xscale,
572 .clear_intr = megasas_clear_intr_xscale,
573 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
574 .adp_reset = megasas_adp_reset_xscale,
575 .check_reset = megasas_check_reset_xscale,
576 .service_isr = megasas_isr,
577 .tasklet = megasas_complete_cmd_dpc,
578 .init_adapter = megasas_init_adapter_mfi,
579 .build_and_issue_cmd = megasas_build_and_issue_cmd,
580 .issue_dcmd = megasas_issue_dcmd,
581 };
582
583 /**
584 * This is the end of set of functions & definitions specific
585 * to xscale (deviceid : 1064R, PERC5) controllers
586 */
587
588 /**
589 * The following functions are defined for ppc (deviceid : 0x60)
590 * controllers
591 */
592
593 /**
594 * megasas_enable_intr_ppc - Enables interrupts
595 * @regs: MFI register set
596 */
597 static inline void
598 megasas_enable_intr_ppc(struct megasas_instance *instance)
599 {
600 struct megasas_register_set __iomem *regs;
601
602 regs = instance->reg_set;
603 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
604
605 writel(~0x80000000, &(regs)->outbound_intr_mask);
606
607 /* Dummy readl to force pci flush */
608 readl(&regs->outbound_intr_mask);
609 }
610
611 /**
612 * megasas_disable_intr_ppc - Disable interrupt
613 * @regs: MFI register set
614 */
615 static inline void
616 megasas_disable_intr_ppc(struct megasas_instance *instance)
617 {
618 struct megasas_register_set __iomem *regs;
619 u32 mask = 0xFFFFFFFF;
620
621 regs = instance->reg_set;
622 writel(mask, &regs->outbound_intr_mask);
623 /* Dummy readl to force pci flush */
624 readl(&regs->outbound_intr_mask);
625 }
626
627 /**
628 * megasas_read_fw_status_reg_ppc - returns the current FW status value
629 * @regs: MFI register set
630 */
631 static u32
632 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
633 {
634 return readl(&instance->reg_set->outbound_scratch_pad_0);
635 }
636
637 /**
638 * megasas_clear_interrupt_ppc - Check & clear interrupt
639 * @regs: MFI register set
640 */
641 static int
642 megasas_clear_intr_ppc(struct megasas_instance *instance)
643 {
644 u32 status, mfiStatus = 0;
645 struct megasas_register_set __iomem *regs;
646 regs = instance->reg_set;
647
648 /*
649 * Check if it is our interrupt
650 */
651 status = readl(&regs->outbound_intr_status);
652
653 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
654 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
655
656 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
657 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
658
659 /*
660 * Clear the interrupt by writing back the same value
661 */
662 writel(status, &regs->outbound_doorbell_clear);
663
664 /* Dummy readl to force pci flush */
665 readl(&regs->outbound_doorbell_clear);
666
667 return mfiStatus;
668 }
669
670 /**
671 * megasas_fire_cmd_ppc - Sends command to the FW
672 * @frame_phys_addr : Physical address of cmd
673 * @frame_count : Number of frames for the command
674 * @regs : MFI register set
675 */
676 static inline void
677 megasas_fire_cmd_ppc(struct megasas_instance *instance,
678 dma_addr_t frame_phys_addr,
679 u32 frame_count,
680 struct megasas_register_set __iomem *regs)
681 {
682 unsigned long flags;
683
684 spin_lock_irqsave(&instance->hba_lock, flags);
685 writel((frame_phys_addr | (frame_count<<1))|1,
686 &(regs)->inbound_queue_port);
687 spin_unlock_irqrestore(&instance->hba_lock, flags);
688 }
689
690 /**
691 * megasas_check_reset_ppc - For controller reset check
692 * @regs: MFI register set
693 */
694 static int
695 megasas_check_reset_ppc(struct megasas_instance *instance,
696 struct megasas_register_set __iomem *regs)
697 {
698 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
699 return 1;
700
701 return 0;
702 }
703
704 static struct megasas_instance_template megasas_instance_template_ppc = {
705
706 .fire_cmd = megasas_fire_cmd_ppc,
707 .enable_intr = megasas_enable_intr_ppc,
708 .disable_intr = megasas_disable_intr_ppc,
709 .clear_intr = megasas_clear_intr_ppc,
710 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
711 .adp_reset = megasas_adp_reset_xscale,
712 .check_reset = megasas_check_reset_ppc,
713 .service_isr = megasas_isr,
714 .tasklet = megasas_complete_cmd_dpc,
715 .init_adapter = megasas_init_adapter_mfi,
716 .build_and_issue_cmd = megasas_build_and_issue_cmd,
717 .issue_dcmd = megasas_issue_dcmd,
718 };
719
720 /**
721 * megasas_enable_intr_skinny - Enables interrupts
722 * @regs: MFI register set
723 */
724 static inline void
725 megasas_enable_intr_skinny(struct megasas_instance *instance)
726 {
727 struct megasas_register_set __iomem *regs;
728
729 regs = instance->reg_set;
730 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
731
732 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
733
734 /* Dummy readl to force pci flush */
735 readl(&regs->outbound_intr_mask);
736 }
737
738 /**
739 * megasas_disable_intr_skinny - Disables interrupt
740 * @regs: MFI register set
741 */
742 static inline void
743 megasas_disable_intr_skinny(struct megasas_instance *instance)
744 {
745 struct megasas_register_set __iomem *regs;
746 u32 mask = 0xFFFFFFFF;
747
748 regs = instance->reg_set;
749 writel(mask, &regs->outbound_intr_mask);
750 /* Dummy readl to force pci flush */
751 readl(&regs->outbound_intr_mask);
752 }
753
754 /**
755 * megasas_read_fw_status_reg_skinny - returns the current FW status value
756 * @regs: MFI register set
757 */
758 static u32
759 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
760 {
761 return readl(&instance->reg_set->outbound_scratch_pad_0);
762 }
763
764 /**
765 * megasas_clear_interrupt_skinny - Check & clear interrupt
766 * @regs: MFI register set
767 */
768 static int
769 megasas_clear_intr_skinny(struct megasas_instance *instance)
770 {
771 u32 status;
772 u32 mfiStatus = 0;
773 struct megasas_register_set __iomem *regs;
774 regs = instance->reg_set;
775
776 /*
777 * Check if it is our interrupt
778 */
779 status = readl(&regs->outbound_intr_status);
780
781 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
782 return 0;
783 }
784
785 /*
786 * Check if it is our interrupt
787 */
788 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
789 MFI_STATE_FAULT) {
790 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
791 } else
792 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
793
794 /*
795 * Clear the interrupt by writing back the same value
796 */
797 writel(status, &regs->outbound_intr_status);
798
799 /*
800 * dummy read to flush PCI
801 */
802 readl(&regs->outbound_intr_status);
803
804 return mfiStatus;
805 }
806
807 /**
808 * megasas_fire_cmd_skinny - Sends command to the FW
809 * @frame_phys_addr : Physical address of cmd
810 * @frame_count : Number of frames for the command
811 * @regs : MFI register set
812 */
813 static inline void
814 megasas_fire_cmd_skinny(struct megasas_instance *instance,
815 dma_addr_t frame_phys_addr,
816 u32 frame_count,
817 struct megasas_register_set __iomem *regs)
818 {
819 unsigned long flags;
820
821 spin_lock_irqsave(&instance->hba_lock, flags);
822 writel(upper_32_bits(frame_phys_addr),
823 &(regs)->inbound_high_queue_port);
824 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
825 &(regs)->inbound_low_queue_port);
826 spin_unlock_irqrestore(&instance->hba_lock, flags);
827 }
828
829 /**
830 * megasas_check_reset_skinny - For controller reset check
831 * @regs: MFI register set
832 */
833 static int
834 megasas_check_reset_skinny(struct megasas_instance *instance,
835 struct megasas_register_set __iomem *regs)
836 {
837 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
838 return 1;
839
840 return 0;
841 }
842
843 static struct megasas_instance_template megasas_instance_template_skinny = {
844
845 .fire_cmd = megasas_fire_cmd_skinny,
846 .enable_intr = megasas_enable_intr_skinny,
847 .disable_intr = megasas_disable_intr_skinny,
848 .clear_intr = megasas_clear_intr_skinny,
849 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
850 .adp_reset = megasas_adp_reset_gen2,
851 .check_reset = megasas_check_reset_skinny,
852 .service_isr = megasas_isr,
853 .tasklet = megasas_complete_cmd_dpc,
854 .init_adapter = megasas_init_adapter_mfi,
855 .build_and_issue_cmd = megasas_build_and_issue_cmd,
856 .issue_dcmd = megasas_issue_dcmd,
857 };
858
859
860 /**
861 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
862 * controllers
863 */
864
865 /**
866 * megasas_enable_intr_gen2 - Enables interrupts
867 * @regs: MFI register set
868 */
869 static inline void
870 megasas_enable_intr_gen2(struct megasas_instance *instance)
871 {
872 struct megasas_register_set __iomem *regs;
873
874 regs = instance->reg_set;
875 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
876
877 /* write ~0x00000005 (4 & 1) to the intr mask*/
878 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
879
880 /* Dummy readl to force pci flush */
881 readl(&regs->outbound_intr_mask);
882 }
883
884 /**
885 * megasas_disable_intr_gen2 - Disables interrupt
886 * @regs: MFI register set
887 */
888 static inline void
889 megasas_disable_intr_gen2(struct megasas_instance *instance)
890 {
891 struct megasas_register_set __iomem *regs;
892 u32 mask = 0xFFFFFFFF;
893
894 regs = instance->reg_set;
895 writel(mask, &regs->outbound_intr_mask);
896 /* Dummy readl to force pci flush */
897 readl(&regs->outbound_intr_mask);
898 }
899
900 /**
901 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
902 * @regs: MFI register set
903 */
904 static u32
905 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
906 {
907 return readl(&instance->reg_set->outbound_scratch_pad_0);
908 }
909
910 /**
911 * megasas_clear_interrupt_gen2 - Check & clear interrupt
912 * @regs: MFI register set
913 */
914 static int
915 megasas_clear_intr_gen2(struct megasas_instance *instance)
916 {
917 u32 status;
918 u32 mfiStatus = 0;
919 struct megasas_register_set __iomem *regs;
920 regs = instance->reg_set;
921
922 /*
923 * Check if it is our interrupt
924 */
925 status = readl(&regs->outbound_intr_status);
926
927 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
928 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
929 }
930 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
931 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
932 }
933
934 /*
935 * Clear the interrupt by writing back the same value
936 */
937 if (mfiStatus)
938 writel(status, &regs->outbound_doorbell_clear);
939
940 /* Dummy readl to force pci flush */
941 readl(&regs->outbound_intr_status);
942
943 return mfiStatus;
944 }
945 /**
946 * megasas_fire_cmd_gen2 - Sends command to the FW
947 * @frame_phys_addr : Physical address of cmd
948 * @frame_count : Number of frames for the command
949 * @regs : MFI register set
950 */
951 static inline void
952 megasas_fire_cmd_gen2(struct megasas_instance *instance,
953 dma_addr_t frame_phys_addr,
954 u32 frame_count,
955 struct megasas_register_set __iomem *regs)
956 {
957 unsigned long flags;
958
959 spin_lock_irqsave(&instance->hba_lock, flags);
960 writel((frame_phys_addr | (frame_count<<1))|1,
961 &(regs)->inbound_queue_port);
962 spin_unlock_irqrestore(&instance->hba_lock, flags);
963 }
964
965 /**
966 * megasas_adp_reset_gen2 - For controller reset
967 * @regs: MFI register set
968 */
969 static int
970 megasas_adp_reset_gen2(struct megasas_instance *instance,
971 struct megasas_register_set __iomem *reg_set)
972 {
973 u32 retry = 0 ;
974 u32 HostDiag;
975 u32 __iomem *seq_offset = &reg_set->seq_offset;
976 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
977
978 if (instance->instancet == &megasas_instance_template_skinny) {
979 seq_offset = &reg_set->fusion_seq_offset;
980 hostdiag_offset = &reg_set->fusion_host_diag;
981 }
982
983 writel(0, seq_offset);
984 writel(4, seq_offset);
985 writel(0xb, seq_offset);
986 writel(2, seq_offset);
987 writel(7, seq_offset);
988 writel(0xd, seq_offset);
989
990 msleep(1000);
991
992 HostDiag = (u32)readl(hostdiag_offset);
993
994 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
995 msleep(100);
996 HostDiag = (u32)readl(hostdiag_offset);
997 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
998 retry, HostDiag);
999
1000 if (retry++ >= 100)
1001 return 1;
1002
1003 }
1004
1005 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1006
1007 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1008
1009 ssleep(10);
1010
1011 HostDiag = (u32)readl(hostdiag_offset);
1012 while (HostDiag & DIAG_RESET_ADAPTER) {
1013 msleep(100);
1014 HostDiag = (u32)readl(hostdiag_offset);
1015 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1016 retry, HostDiag);
1017
1018 if (retry++ >= 1000)
1019 return 1;
1020
1021 }
1022 return 0;
1023 }
1024
1025 /**
1026 * megasas_check_reset_gen2 - For controller reset check
1027 * @regs: MFI register set
1028 */
1029 static int
1030 megasas_check_reset_gen2(struct megasas_instance *instance,
1031 struct megasas_register_set __iomem *regs)
1032 {
1033 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1034 return 1;
1035
1036 return 0;
1037 }
1038
1039 static struct megasas_instance_template megasas_instance_template_gen2 = {
1040
1041 .fire_cmd = megasas_fire_cmd_gen2,
1042 .enable_intr = megasas_enable_intr_gen2,
1043 .disable_intr = megasas_disable_intr_gen2,
1044 .clear_intr = megasas_clear_intr_gen2,
1045 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1046 .adp_reset = megasas_adp_reset_gen2,
1047 .check_reset = megasas_check_reset_gen2,
1048 .service_isr = megasas_isr,
1049 .tasklet = megasas_complete_cmd_dpc,
1050 .init_adapter = megasas_init_adapter_mfi,
1051 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1052 .issue_dcmd = megasas_issue_dcmd,
1053 };
1054
1055 /**
1056 * This is the end of set of functions & definitions
1057 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1058 */
1059
1060 /*
1061 * Template added for TB (Fusion)
1062 */
1063 extern struct megasas_instance_template megasas_instance_template_fusion;
1064
1065 /**
1066 * megasas_issue_polled - Issues a polling command
1067 * @instance: Adapter soft state
1068 * @cmd: Command packet to be issued
1069 *
1070 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1071 */
1072 int
1073 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1074 {
1075 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1076
1077 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1078 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1079
1080 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1081 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1082 __func__, __LINE__);
1083 return DCMD_NOT_FIRED;
1084 }
1085
1086 instance->instancet->issue_dcmd(instance, cmd);
1087
1088 return wait_and_poll(instance, cmd, instance->requestorId ?
1089 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1090 }
1091
1092 /**
1093 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1094 * @instance: Adapter soft state
1095 * @cmd: Command to be issued
1096 * @timeout: Timeout in seconds
1097 *
1098 * This function waits on an event for the command to be returned from ISR.
1099 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1100 * Used to issue ioctl commands.
1101 */
1102 int
1103 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1104 struct megasas_cmd *cmd, int timeout)
1105 {
1106 int ret = 0;
1107 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1108
1109 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1110 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1111 __func__, __LINE__);
1112 return DCMD_NOT_FIRED;
1113 }
1114
1115 instance->instancet->issue_dcmd(instance, cmd);
1116
1117 if (timeout) {
1118 ret = wait_event_timeout(instance->int_cmd_wait_q,
1119 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1120 if (!ret) {
1121 dev_err(&instance->pdev->dev,
1122 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1123 cmd->frame->dcmd.opcode, __func__);
1124 return DCMD_TIMEOUT;
1125 }
1126 } else
1127 wait_event(instance->int_cmd_wait_q,
1128 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1129
1130 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1131 DCMD_SUCCESS : DCMD_FAILED;
1132 }
1133
1134 /**
1135 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1136 * @instance: Adapter soft state
1137 * @cmd_to_abort: Previously issued cmd to be aborted
1138 * @timeout: Timeout in seconds
1139 *
1140 * MFI firmware can abort previously issued AEN comamnd (automatic event
1141 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1142 * cmd and waits for return status.
1143 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1144 */
1145 static int
1146 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1147 struct megasas_cmd *cmd_to_abort, int timeout)
1148 {
1149 struct megasas_cmd *cmd;
1150 struct megasas_abort_frame *abort_fr;
1151 int ret = 0;
1152 u32 opcode;
1153
1154 cmd = megasas_get_cmd(instance);
1155
1156 if (!cmd)
1157 return -1;
1158
1159 abort_fr = &cmd->frame->abort;
1160
1161 /*
1162 * Prepare and issue the abort frame
1163 */
1164 abort_fr->cmd = MFI_CMD_ABORT;
1165 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1166 abort_fr->flags = cpu_to_le16(0);
1167 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1168 abort_fr->abort_mfi_phys_addr_lo =
1169 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1170 abort_fr->abort_mfi_phys_addr_hi =
1171 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1172
1173 cmd->sync_cmd = 1;
1174 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1175
1176 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1177 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1178 __func__, __LINE__);
1179 return DCMD_NOT_FIRED;
1180 }
1181
1182 instance->instancet->issue_dcmd(instance, cmd);
1183
1184 if (timeout) {
1185 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1186 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1187 if (!ret) {
1188 opcode = cmd_to_abort->frame->dcmd.opcode;
1189 dev_err(&instance->pdev->dev,
1190 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1191 opcode, __func__);
1192 return DCMD_TIMEOUT;
1193 }
1194 } else
1195 wait_event(instance->abort_cmd_wait_q,
1196 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1197
1198 cmd->sync_cmd = 0;
1199
1200 megasas_return_cmd(instance, cmd);
1201 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1202 DCMD_SUCCESS : DCMD_FAILED;
1203 }
1204
1205 /**
1206 * megasas_make_sgl32 - Prepares 32-bit SGL
1207 * @instance: Adapter soft state
1208 * @scp: SCSI command from the mid-layer
1209 * @mfi_sgl: SGL to be filled in
1210 *
1211 * If successful, this function returns the number of SG elements. Otherwise,
1212 * it returnes -1.
1213 */
1214 static int
1215 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1216 union megasas_sgl *mfi_sgl)
1217 {
1218 int i;
1219 int sge_count;
1220 struct scatterlist *os_sgl;
1221
1222 sge_count = scsi_dma_map(scp);
1223 BUG_ON(sge_count < 0);
1224
1225 if (sge_count) {
1226 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1227 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1228 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1229 }
1230 }
1231 return sge_count;
1232 }
1233
1234 /**
1235 * megasas_make_sgl64 - Prepares 64-bit SGL
1236 * @instance: Adapter soft state
1237 * @scp: SCSI command from the mid-layer
1238 * @mfi_sgl: SGL to be filled in
1239 *
1240 * If successful, this function returns the number of SG elements. Otherwise,
1241 * it returnes -1.
1242 */
1243 static int
1244 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1245 union megasas_sgl *mfi_sgl)
1246 {
1247 int i;
1248 int sge_count;
1249 struct scatterlist *os_sgl;
1250
1251 sge_count = scsi_dma_map(scp);
1252 BUG_ON(sge_count < 0);
1253
1254 if (sge_count) {
1255 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1256 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1257 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1258 }
1259 }
1260 return sge_count;
1261 }
1262
1263 /**
1264 * megasas_make_sgl_skinny - Prepares IEEE SGL
1265 * @instance: Adapter soft state
1266 * @scp: SCSI command from the mid-layer
1267 * @mfi_sgl: SGL to be filled in
1268 *
1269 * If successful, this function returns the number of SG elements. Otherwise,
1270 * it returnes -1.
1271 */
1272 static int
1273 megasas_make_sgl_skinny(struct megasas_instance *instance,
1274 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1275 {
1276 int i;
1277 int sge_count;
1278 struct scatterlist *os_sgl;
1279
1280 sge_count = scsi_dma_map(scp);
1281
1282 if (sge_count) {
1283 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1284 mfi_sgl->sge_skinny[i].length =
1285 cpu_to_le32(sg_dma_len(os_sgl));
1286 mfi_sgl->sge_skinny[i].phys_addr =
1287 cpu_to_le64(sg_dma_address(os_sgl));
1288 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1289 }
1290 }
1291 return sge_count;
1292 }
1293
1294 /**
1295 * megasas_get_frame_count - Computes the number of frames
1296 * @frame_type : type of frame- io or pthru frame
1297 * @sge_count : number of sg elements
1298 *
1299 * Returns the number of frames required for numnber of sge's (sge_count)
1300 */
1301
1302 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1303 u8 sge_count, u8 frame_type)
1304 {
1305 int num_cnt;
1306 int sge_bytes;
1307 u32 sge_sz;
1308 u32 frame_count = 0;
1309
1310 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1311 sizeof(struct megasas_sge32);
1312
1313 if (instance->flag_ieee) {
1314 sge_sz = sizeof(struct megasas_sge_skinny);
1315 }
1316
1317 /*
1318 * Main frame can contain 2 SGEs for 64-bit SGLs and
1319 * 3 SGEs for 32-bit SGLs for ldio &
1320 * 1 SGEs for 64-bit SGLs and
1321 * 2 SGEs for 32-bit SGLs for pthru frame
1322 */
1323 if (unlikely(frame_type == PTHRU_FRAME)) {
1324 if (instance->flag_ieee == 1) {
1325 num_cnt = sge_count - 1;
1326 } else if (IS_DMA64)
1327 num_cnt = sge_count - 1;
1328 else
1329 num_cnt = sge_count - 2;
1330 } else {
1331 if (instance->flag_ieee == 1) {
1332 num_cnt = sge_count - 1;
1333 } else if (IS_DMA64)
1334 num_cnt = sge_count - 2;
1335 else
1336 num_cnt = sge_count - 3;
1337 }
1338
1339 if (num_cnt > 0) {
1340 sge_bytes = sge_sz * num_cnt;
1341
1342 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1343 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1344 }
1345 /* Main frame */
1346 frame_count += 1;
1347
1348 if (frame_count > 7)
1349 frame_count = 8;
1350 return frame_count;
1351 }
1352
1353 /**
1354 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1355 * @instance: Adapter soft state
1356 * @scp: SCSI command
1357 * @cmd: Command to be prepared in
1358 *
1359 * This function prepares CDB commands. These are typcially pass-through
1360 * commands to the devices.
1361 */
1362 static int
1363 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1364 struct megasas_cmd *cmd)
1365 {
1366 u32 is_logical;
1367 u32 device_id;
1368 u16 flags = 0;
1369 struct megasas_pthru_frame *pthru;
1370
1371 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1372 device_id = MEGASAS_DEV_INDEX(scp);
1373 pthru = (struct megasas_pthru_frame *)cmd->frame;
1374
1375 if (scp->sc_data_direction == DMA_TO_DEVICE)
1376 flags = MFI_FRAME_DIR_WRITE;
1377 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1378 flags = MFI_FRAME_DIR_READ;
1379 else if (scp->sc_data_direction == DMA_NONE)
1380 flags = MFI_FRAME_DIR_NONE;
1381
1382 if (instance->flag_ieee == 1) {
1383 flags |= MFI_FRAME_IEEE;
1384 }
1385
1386 /*
1387 * Prepare the DCDB frame
1388 */
1389 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1390 pthru->cmd_status = 0x0;
1391 pthru->scsi_status = 0x0;
1392 pthru->target_id = device_id;
1393 pthru->lun = scp->device->lun;
1394 pthru->cdb_len = scp->cmd_len;
1395 pthru->timeout = 0;
1396 pthru->pad_0 = 0;
1397 pthru->flags = cpu_to_le16(flags);
1398 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1399
1400 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1401
1402 /*
1403 * If the command is for the tape device, set the
1404 * pthru timeout to the os layer timeout value.
1405 */
1406 if (scp->device->type == TYPE_TAPE) {
1407 if ((scp->request->timeout / HZ) > 0xFFFF)
1408 pthru->timeout = cpu_to_le16(0xFFFF);
1409 else
1410 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1411 }
1412
1413 /*
1414 * Construct SGL
1415 */
1416 if (instance->flag_ieee == 1) {
1417 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1418 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1419 &pthru->sgl);
1420 } else if (IS_DMA64) {
1421 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1422 pthru->sge_count = megasas_make_sgl64(instance, scp,
1423 &pthru->sgl);
1424 } else
1425 pthru->sge_count = megasas_make_sgl32(instance, scp,
1426 &pthru->sgl);
1427
1428 if (pthru->sge_count > instance->max_num_sge) {
1429 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1430 pthru->sge_count);
1431 return 0;
1432 }
1433
1434 /*
1435 * Sense info specific
1436 */
1437 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1438 pthru->sense_buf_phys_addr_hi =
1439 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1440 pthru->sense_buf_phys_addr_lo =
1441 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1442
1443 /*
1444 * Compute the total number of frames this command consumes. FW uses
1445 * this number to pull sufficient number of frames from host memory.
1446 */
1447 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1448 PTHRU_FRAME);
1449
1450 return cmd->frame_count;
1451 }
1452
1453 /**
1454 * megasas_build_ldio - Prepares IOs to logical devices
1455 * @instance: Adapter soft state
1456 * @scp: SCSI command
1457 * @cmd: Command to be prepared
1458 *
1459 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1460 */
1461 static int
1462 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1463 struct megasas_cmd *cmd)
1464 {
1465 u32 device_id;
1466 u8 sc = scp->cmnd[0];
1467 u16 flags = 0;
1468 struct megasas_io_frame *ldio;
1469
1470 device_id = MEGASAS_DEV_INDEX(scp);
1471 ldio = (struct megasas_io_frame *)cmd->frame;
1472
1473 if (scp->sc_data_direction == DMA_TO_DEVICE)
1474 flags = MFI_FRAME_DIR_WRITE;
1475 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1476 flags = MFI_FRAME_DIR_READ;
1477
1478 if (instance->flag_ieee == 1) {
1479 flags |= MFI_FRAME_IEEE;
1480 }
1481
1482 /*
1483 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1484 */
1485 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1486 ldio->cmd_status = 0x0;
1487 ldio->scsi_status = 0x0;
1488 ldio->target_id = device_id;
1489 ldio->timeout = 0;
1490 ldio->reserved_0 = 0;
1491 ldio->pad_0 = 0;
1492 ldio->flags = cpu_to_le16(flags);
1493 ldio->start_lba_hi = 0;
1494 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1495
1496 /*
1497 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1498 */
1499 if (scp->cmd_len == 6) {
1500 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1501 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1502 ((u32) scp->cmnd[2] << 8) |
1503 (u32) scp->cmnd[3]);
1504
1505 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1506 }
1507
1508 /*
1509 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1510 */
1511 else if (scp->cmd_len == 10) {
1512 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1513 ((u32) scp->cmnd[7] << 8));
1514 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1515 ((u32) scp->cmnd[3] << 16) |
1516 ((u32) scp->cmnd[4] << 8) |
1517 (u32) scp->cmnd[5]);
1518 }
1519
1520 /*
1521 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1522 */
1523 else if (scp->cmd_len == 12) {
1524 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1525 ((u32) scp->cmnd[7] << 16) |
1526 ((u32) scp->cmnd[8] << 8) |
1527 (u32) scp->cmnd[9]);
1528
1529 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1530 ((u32) scp->cmnd[3] << 16) |
1531 ((u32) scp->cmnd[4] << 8) |
1532 (u32) scp->cmnd[5]);
1533 }
1534
1535 /*
1536 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1537 */
1538 else if (scp->cmd_len == 16) {
1539 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1540 ((u32) scp->cmnd[11] << 16) |
1541 ((u32) scp->cmnd[12] << 8) |
1542 (u32) scp->cmnd[13]);
1543
1544 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1545 ((u32) scp->cmnd[7] << 16) |
1546 ((u32) scp->cmnd[8] << 8) |
1547 (u32) scp->cmnd[9]);
1548
1549 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1550 ((u32) scp->cmnd[3] << 16) |
1551 ((u32) scp->cmnd[4] << 8) |
1552 (u32) scp->cmnd[5]);
1553
1554 }
1555
1556 /*
1557 * Construct SGL
1558 */
1559 if (instance->flag_ieee) {
1560 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1561 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1562 &ldio->sgl);
1563 } else if (IS_DMA64) {
1564 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1565 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1566 } else
1567 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1568
1569 if (ldio->sge_count > instance->max_num_sge) {
1570 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1571 ldio->sge_count);
1572 return 0;
1573 }
1574
1575 /*
1576 * Sense info specific
1577 */
1578 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1579 ldio->sense_buf_phys_addr_hi = 0;
1580 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1581
1582 /*
1583 * Compute the total number of frames this command consumes. FW uses
1584 * this number to pull sufficient number of frames from host memory.
1585 */
1586 cmd->frame_count = megasas_get_frame_count(instance,
1587 ldio->sge_count, IO_FRAME);
1588
1589 return cmd->frame_count;
1590 }
1591
1592 /**
1593 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1594 * and whether it's RW or non RW
1595 * @scmd: SCSI command
1596 *
1597 */
1598 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1599 {
1600 int ret;
1601
1602 switch (cmd->cmnd[0]) {
1603 case READ_10:
1604 case WRITE_10:
1605 case READ_12:
1606 case WRITE_12:
1607 case READ_6:
1608 case WRITE_6:
1609 case READ_16:
1610 case WRITE_16:
1611 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1612 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1613 break;
1614 default:
1615 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1616 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1617 }
1618 return ret;
1619 }
1620
1621 /**
1622 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1623 * in FW
1624 * @instance: Adapter soft state
1625 */
1626 static inline void
1627 megasas_dump_pending_frames(struct megasas_instance *instance)
1628 {
1629 struct megasas_cmd *cmd;
1630 int i,n;
1631 union megasas_sgl *mfi_sgl;
1632 struct megasas_io_frame *ldio;
1633 struct megasas_pthru_frame *pthru;
1634 u32 sgcount;
1635 u16 max_cmd = instance->max_fw_cmds;
1636
1637 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1638 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1639 if (IS_DMA64)
1640 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1641 else
1642 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1643
1644 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1645 for (i = 0; i < max_cmd; i++) {
1646 cmd = instance->cmd_list[i];
1647 if (!cmd->scmd)
1648 continue;
1649 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1650 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1651 ldio = (struct megasas_io_frame *)cmd->frame;
1652 mfi_sgl = &ldio->sgl;
1653 sgcount = ldio->sge_count;
1654 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1655 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1656 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1657 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1658 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1659 } else {
1660 pthru = (struct megasas_pthru_frame *) cmd->frame;
1661 mfi_sgl = &pthru->sgl;
1662 sgcount = pthru->sge_count;
1663 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1664 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1665 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1666 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1667 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1668 }
1669 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1670 for (n = 0; n < sgcount; n++) {
1671 if (IS_DMA64)
1672 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1673 le32_to_cpu(mfi_sgl->sge64[n].length),
1674 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1675 else
1676 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1677 le32_to_cpu(mfi_sgl->sge32[n].length),
1678 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1679 }
1680 }
1681 } /*for max_cmd*/
1682 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1683 for (i = 0; i < max_cmd; i++) {
1684
1685 cmd = instance->cmd_list[i];
1686
1687 if (cmd->sync_cmd == 1)
1688 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1689 }
1690 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1691 }
1692
1693 u32
1694 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1695 struct scsi_cmnd *scmd)
1696 {
1697 struct megasas_cmd *cmd;
1698 u32 frame_count;
1699
1700 cmd = megasas_get_cmd(instance);
1701 if (!cmd)
1702 return SCSI_MLQUEUE_HOST_BUSY;
1703
1704 /*
1705 * Logical drive command
1706 */
1707 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1708 frame_count = megasas_build_ldio(instance, scmd, cmd);
1709 else
1710 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1711
1712 if (!frame_count)
1713 goto out_return_cmd;
1714
1715 cmd->scmd = scmd;
1716 scmd->SCp.ptr = (char *)cmd;
1717
1718 /*
1719 * Issue the command to the FW
1720 */
1721 atomic_inc(&instance->fw_outstanding);
1722
1723 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1724 cmd->frame_count-1, instance->reg_set);
1725
1726 return 0;
1727 out_return_cmd:
1728 megasas_return_cmd(instance, cmd);
1729 return SCSI_MLQUEUE_HOST_BUSY;
1730 }
1731
1732
1733 /**
1734 * megasas_queue_command - Queue entry point
1735 * @scmd: SCSI command to be queued
1736 * @done: Callback entry point
1737 */
1738 static int
1739 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1740 {
1741 struct megasas_instance *instance;
1742 struct MR_PRIV_DEVICE *mr_device_priv_data;
1743
1744 instance = (struct megasas_instance *)
1745 scmd->device->host->hostdata;
1746
1747 if (instance->unload == 1) {
1748 scmd->result = DID_NO_CONNECT << 16;
1749 scmd->scsi_done(scmd);
1750 return 0;
1751 }
1752
1753 if (instance->issuepend_done == 0)
1754 return SCSI_MLQUEUE_HOST_BUSY;
1755
1756
1757 /* Check for an mpio path and adjust behavior */
1758 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1759 if (megasas_check_mpio_paths(instance, scmd) ==
1760 (DID_REQUEUE << 16)) {
1761 return SCSI_MLQUEUE_HOST_BUSY;
1762 } else {
1763 scmd->result = DID_NO_CONNECT << 16;
1764 scmd->scsi_done(scmd);
1765 return 0;
1766 }
1767 }
1768
1769 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1770 scmd->result = DID_NO_CONNECT << 16;
1771 scmd->scsi_done(scmd);
1772 return 0;
1773 }
1774
1775 mr_device_priv_data = scmd->device->hostdata;
1776 if (!mr_device_priv_data) {
1777 scmd->result = DID_NO_CONNECT << 16;
1778 scmd->scsi_done(scmd);
1779 return 0;
1780 }
1781
1782 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1783 return SCSI_MLQUEUE_HOST_BUSY;
1784
1785 if (mr_device_priv_data->tm_busy)
1786 return SCSI_MLQUEUE_DEVICE_BUSY;
1787
1788
1789 scmd->result = 0;
1790
1791 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1792 (scmd->device->id >= instance->fw_supported_vd_count ||
1793 scmd->device->lun)) {
1794 scmd->result = DID_BAD_TARGET << 16;
1795 goto out_done;
1796 }
1797
1798 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1799 MEGASAS_IS_LOGICAL(scmd->device) &&
1800 (!instance->fw_sync_cache_support)) {
1801 scmd->result = DID_OK << 16;
1802 goto out_done;
1803 }
1804
1805 return instance->instancet->build_and_issue_cmd(instance, scmd);
1806
1807 out_done:
1808 scmd->scsi_done(scmd);
1809 return 0;
1810 }
1811
1812 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1813 {
1814 int i;
1815
1816 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1817
1818 if ((megasas_mgmt_info.instance[i]) &&
1819 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1820 return megasas_mgmt_info.instance[i];
1821 }
1822
1823 return NULL;
1824 }
1825
1826 /*
1827 * megasas_set_dynamic_target_properties -
1828 * Device property set by driver may not be static and it is required to be
1829 * updated after OCR
1830 *
1831 * set tm_capable.
1832 * set dma alignment (only for eedp protection enable vd).
1833 *
1834 * @sdev: OS provided scsi device
1835 *
1836 * Returns void
1837 */
1838 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1839 bool is_target_prop)
1840 {
1841 u16 pd_index = 0, ld;
1842 u32 device_id;
1843 struct megasas_instance *instance;
1844 struct fusion_context *fusion;
1845 struct MR_PRIV_DEVICE *mr_device_priv_data;
1846 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1847 struct MR_LD_RAID *raid;
1848 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1849
1850 instance = megasas_lookup_instance(sdev->host->host_no);
1851 fusion = instance->ctrl_context;
1852 mr_device_priv_data = sdev->hostdata;
1853
1854 if (!fusion || !mr_device_priv_data)
1855 return;
1856
1857 if (MEGASAS_IS_LOGICAL(sdev)) {
1858 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1859 + sdev->id;
1860 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1861 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1862 if (ld >= instance->fw_supported_vd_count)
1863 return;
1864 raid = MR_LdRaidGet(ld, local_map_ptr);
1865
1866 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1867 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1868
1869 mr_device_priv_data->is_tm_capable =
1870 raid->capability.tmCapable;
1871 } else if (instance->use_seqnum_jbod_fp) {
1872 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1873 sdev->id;
1874 pd_sync = (void *)fusion->pd_seq_sync
1875 [(instance->pd_seq_map_id - 1) & 1];
1876 mr_device_priv_data->is_tm_capable =
1877 pd_sync->seq[pd_index].capability.tmCapable;
1878 }
1879
1880 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1881 /*
1882 * If FW provides a target reset timeout value, driver will use
1883 * it. If not set, fallback to default values.
1884 */
1885 mr_device_priv_data->target_reset_tmo =
1886 min_t(u8, instance->max_reset_tmo,
1887 instance->tgt_prop->reset_tmo);
1888 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1889 } else {
1890 mr_device_priv_data->target_reset_tmo =
1891 MEGASAS_DEFAULT_TM_TIMEOUT;
1892 mr_device_priv_data->task_abort_tmo =
1893 MEGASAS_DEFAULT_TM_TIMEOUT;
1894 }
1895 }
1896
1897 /*
1898 * megasas_set_nvme_device_properties -
1899 * set nomerges=2
1900 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1901 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1902 *
1903 * MR firmware provides value in KB. Caller of this function converts
1904 * kb into bytes.
1905 *
1906 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1907 * MR firmware provides value 128 as (32 * 4K) = 128K.
1908 *
1909 * @sdev: scsi device
1910 * @max_io_size: maximum io transfer size
1911 *
1912 */
1913 static inline void
1914 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1915 {
1916 struct megasas_instance *instance;
1917 u32 mr_nvme_pg_size;
1918
1919 instance = (struct megasas_instance *)sdev->host->hostdata;
1920 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1921 MR_DEFAULT_NVME_PAGE_SIZE);
1922
1923 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1924
1925 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1926 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1927 }
1928
1929
1930 /*
1931 * megasas_set_static_target_properties -
1932 * Device property set by driver are static and it is not required to be
1933 * updated after OCR.
1934 *
1935 * set io timeout
1936 * set device queue depth
1937 * set nvme device properties. see - megasas_set_nvme_device_properties
1938 *
1939 * @sdev: scsi device
1940 * @is_target_prop true, if fw provided target properties.
1941 */
1942 static void megasas_set_static_target_properties(struct scsi_device *sdev,
1943 bool is_target_prop)
1944 {
1945 u8 interface_type;
1946 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1947 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
1948 u32 tgt_device_qd;
1949 struct megasas_instance *instance;
1950 struct MR_PRIV_DEVICE *mr_device_priv_data;
1951
1952 instance = megasas_lookup_instance(sdev->host->host_no);
1953 mr_device_priv_data = sdev->hostdata;
1954 interface_type = mr_device_priv_data->interface_type;
1955
1956 /*
1957 * The RAID firmware may require extended timeouts.
1958 */
1959 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
1960
1961 switch (interface_type) {
1962 case SAS_PD:
1963 device_qd = MEGASAS_SAS_QD;
1964 break;
1965 case SATA_PD:
1966 device_qd = MEGASAS_SATA_QD;
1967 break;
1968 case NVME_PD:
1969 device_qd = MEGASAS_NVME_QD;
1970 break;
1971 }
1972
1973 if (is_target_prop) {
1974 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1975 if (tgt_device_qd &&
1976 (tgt_device_qd <= instance->host->can_queue))
1977 device_qd = tgt_device_qd;
1978
1979 /* max_io_size_kb will be set to non zero for
1980 * nvme based vd and syspd.
1981 */
1982 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1983 }
1984
1985 if (instance->nvme_page_size && max_io_size_kb)
1986 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1987
1988 scsi_change_queue_depth(sdev, device_qd);
1989
1990 }
1991
1992
1993 static int megasas_slave_configure(struct scsi_device *sdev)
1994 {
1995 u16 pd_index = 0;
1996 struct megasas_instance *instance;
1997 int ret_target_prop = DCMD_FAILED;
1998 bool is_target_prop = false;
1999
2000 instance = megasas_lookup_instance(sdev->host->host_no);
2001 if (instance->pd_list_not_supported) {
2002 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2003 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2004 sdev->id;
2005 if (instance->pd_list[pd_index].driveState !=
2006 MR_PD_STATE_SYSTEM)
2007 return -ENXIO;
2008 }
2009 }
2010
2011 mutex_lock(&instance->reset_mutex);
2012 /* Send DCMD to Firmware and cache the information */
2013 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2014 megasas_get_pd_info(instance, sdev);
2015
2016 /* Some ventura firmware may not have instance->nvme_page_size set.
2017 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2018 */
2019 if ((instance->tgt_prop) && (instance->nvme_page_size))
2020 ret_target_prop = megasas_get_target_prop(instance, sdev);
2021
2022 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2023 megasas_set_static_target_properties(sdev, is_target_prop);
2024
2025 /* This sdev property may change post OCR */
2026 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2027
2028 mutex_unlock(&instance->reset_mutex);
2029
2030 return 0;
2031 }
2032
2033 static int megasas_slave_alloc(struct scsi_device *sdev)
2034 {
2035 u16 pd_index = 0;
2036 struct megasas_instance *instance ;
2037 struct MR_PRIV_DEVICE *mr_device_priv_data;
2038
2039 instance = megasas_lookup_instance(sdev->host->host_no);
2040 if (!MEGASAS_IS_LOGICAL(sdev)) {
2041 /*
2042 * Open the OS scan to the SYSTEM PD
2043 */
2044 pd_index =
2045 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2046 sdev->id;
2047 if ((instance->pd_list_not_supported ||
2048 instance->pd_list[pd_index].driveState ==
2049 MR_PD_STATE_SYSTEM)) {
2050 goto scan_target;
2051 }
2052 return -ENXIO;
2053 }
2054
2055 scan_target:
2056 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2057 GFP_KERNEL);
2058 if (!mr_device_priv_data)
2059 return -ENOMEM;
2060 sdev->hostdata = mr_device_priv_data;
2061
2062 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2063 instance->r1_ldio_hint_default);
2064 return 0;
2065 }
2066
2067 static void megasas_slave_destroy(struct scsi_device *sdev)
2068 {
2069 kfree(sdev->hostdata);
2070 sdev->hostdata = NULL;
2071 }
2072
2073 /*
2074 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2075 * kill adapter
2076 * @instance: Adapter soft state
2077 *
2078 */
2079 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2080 {
2081 int i;
2082 struct megasas_cmd *cmd_mfi;
2083 struct megasas_cmd_fusion *cmd_fusion;
2084 struct fusion_context *fusion = instance->ctrl_context;
2085
2086 /* Find all outstanding ioctls */
2087 if (fusion) {
2088 for (i = 0; i < instance->max_fw_cmds; i++) {
2089 cmd_fusion = fusion->cmd_list[i];
2090 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2091 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2092 if (cmd_mfi->sync_cmd &&
2093 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2094 cmd_mfi->frame->hdr.cmd_status =
2095 MFI_STAT_WRONG_STATE;
2096 megasas_complete_cmd(instance,
2097 cmd_mfi, DID_OK);
2098 }
2099 }
2100 }
2101 } else {
2102 for (i = 0; i < instance->max_fw_cmds; i++) {
2103 cmd_mfi = instance->cmd_list[i];
2104 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2105 MFI_CMD_ABORT)
2106 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2107 }
2108 }
2109 }
2110
2111
2112 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2113 {
2114 /* Set critical error to block I/O & ioctls in case caller didn't */
2115 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2116 /* Wait 1 second to ensure IO or ioctls in build have posted */
2117 msleep(1000);
2118 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2119 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2120 (instance->adapter_type != MFI_SERIES)) {
2121 if (!instance->requestorId) {
2122 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2123 /* Flush */
2124 readl(&instance->reg_set->doorbell);
2125 }
2126 if (instance->requestorId && instance->peerIsPresent)
2127 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2128 } else {
2129 writel(MFI_STOP_ADP,
2130 &instance->reg_set->inbound_doorbell);
2131 }
2132 /* Complete outstanding ioctls when adapter is killed */
2133 megasas_complete_outstanding_ioctls(instance);
2134 }
2135
2136 /**
2137 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2138 * restored to max value
2139 * @instance: Adapter soft state
2140 *
2141 */
2142 void
2143 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2144 {
2145 unsigned long flags;
2146
2147 if (instance->flag & MEGASAS_FW_BUSY
2148 && time_after(jiffies, instance->last_time + 5 * HZ)
2149 && atomic_read(&instance->fw_outstanding) <
2150 instance->throttlequeuedepth + 1) {
2151
2152 spin_lock_irqsave(instance->host->host_lock, flags);
2153 instance->flag &= ~MEGASAS_FW_BUSY;
2154
2155 instance->host->can_queue = instance->cur_can_queue;
2156 spin_unlock_irqrestore(instance->host->host_lock, flags);
2157 }
2158 }
2159
2160 /**
2161 * megasas_complete_cmd_dpc - Returns FW's controller structure
2162 * @instance_addr: Address of adapter soft state
2163 *
2164 * Tasklet to complete cmds
2165 */
2166 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2167 {
2168 u32 producer;
2169 u32 consumer;
2170 u32 context;
2171 struct megasas_cmd *cmd;
2172 struct megasas_instance *instance =
2173 (struct megasas_instance *)instance_addr;
2174 unsigned long flags;
2175
2176 /* If we have already declared adapter dead, donot complete cmds */
2177 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2178 return;
2179
2180 spin_lock_irqsave(&instance->completion_lock, flags);
2181
2182 producer = le32_to_cpu(*instance->producer);
2183 consumer = le32_to_cpu(*instance->consumer);
2184
2185 while (consumer != producer) {
2186 context = le32_to_cpu(instance->reply_queue[consumer]);
2187 if (context >= instance->max_fw_cmds) {
2188 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2189 context);
2190 BUG();
2191 }
2192
2193 cmd = instance->cmd_list[context];
2194
2195 megasas_complete_cmd(instance, cmd, DID_OK);
2196
2197 consumer++;
2198 if (consumer == (instance->max_fw_cmds + 1)) {
2199 consumer = 0;
2200 }
2201 }
2202
2203 *instance->consumer = cpu_to_le32(producer);
2204
2205 spin_unlock_irqrestore(&instance->completion_lock, flags);
2206
2207 /*
2208 * Check if we can restore can_queue
2209 */
2210 megasas_check_and_restore_queue_depth(instance);
2211 }
2212
2213 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2214
2215 /**
2216 * megasas_start_timer - Initializes sriov heartbeat timer object
2217 * @instance: Adapter soft state
2218 *
2219 */
2220 void megasas_start_timer(struct megasas_instance *instance)
2221 {
2222 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2223
2224 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2225 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2226 add_timer(timer);
2227 }
2228
2229 static void
2230 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2231
2232 static void
2233 process_fw_state_change_wq(struct work_struct *work);
2234
2235 void megasas_do_ocr(struct megasas_instance *instance)
2236 {
2237 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2238 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2239 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2240 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2241 }
2242 instance->instancet->disable_intr(instance);
2243 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2244 instance->issuepend_done = 0;
2245
2246 atomic_set(&instance->fw_outstanding, 0);
2247 megasas_internal_reset_defer_cmds(instance);
2248 process_fw_state_change_wq(&instance->work_init);
2249 }
2250
2251 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2252 int initial)
2253 {
2254 struct megasas_cmd *cmd;
2255 struct megasas_dcmd_frame *dcmd;
2256 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2257 dma_addr_t new_affiliation_111_h;
2258 int ld, retval = 0;
2259 u8 thisVf;
2260
2261 cmd = megasas_get_cmd(instance);
2262
2263 if (!cmd) {
2264 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2265 "Failed to get cmd for scsi%d\n",
2266 instance->host->host_no);
2267 return -ENOMEM;
2268 }
2269
2270 dcmd = &cmd->frame->dcmd;
2271
2272 if (!instance->vf_affiliation_111) {
2273 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2274 "affiliation for scsi%d\n", instance->host->host_no);
2275 megasas_return_cmd(instance, cmd);
2276 return -ENOMEM;
2277 }
2278
2279 if (initial)
2280 memset(instance->vf_affiliation_111, 0,
2281 sizeof(struct MR_LD_VF_AFFILIATION_111));
2282 else {
2283 new_affiliation_111 =
2284 dma_alloc_coherent(&instance->pdev->dev,
2285 sizeof(struct MR_LD_VF_AFFILIATION_111),
2286 &new_affiliation_111_h, GFP_KERNEL);
2287 if (!new_affiliation_111) {
2288 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2289 "memory for new affiliation for scsi%d\n",
2290 instance->host->host_no);
2291 megasas_return_cmd(instance, cmd);
2292 return -ENOMEM;
2293 }
2294 }
2295
2296 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2297
2298 dcmd->cmd = MFI_CMD_DCMD;
2299 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2300 dcmd->sge_count = 1;
2301 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2302 dcmd->timeout = 0;
2303 dcmd->pad_0 = 0;
2304 dcmd->data_xfer_len =
2305 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2306 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2307
2308 if (initial)
2309 dcmd->sgl.sge32[0].phys_addr =
2310 cpu_to_le32(instance->vf_affiliation_111_h);
2311 else
2312 dcmd->sgl.sge32[0].phys_addr =
2313 cpu_to_le32(new_affiliation_111_h);
2314
2315 dcmd->sgl.sge32[0].length = cpu_to_le32(
2316 sizeof(struct MR_LD_VF_AFFILIATION_111));
2317
2318 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2319 "scsi%d\n", instance->host->host_no);
2320
2321 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2322 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2323 " failed with status 0x%x for scsi%d\n",
2324 dcmd->cmd_status, instance->host->host_no);
2325 retval = 1; /* Do a scan if we couldn't get affiliation */
2326 goto out;
2327 }
2328
2329 if (!initial) {
2330 thisVf = new_affiliation_111->thisVf;
2331 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2332 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2333 new_affiliation_111->map[ld].policy[thisVf]) {
2334 dev_warn(&instance->pdev->dev, "SR-IOV: "
2335 "Got new LD/VF affiliation for scsi%d\n",
2336 instance->host->host_no);
2337 memcpy(instance->vf_affiliation_111,
2338 new_affiliation_111,
2339 sizeof(struct MR_LD_VF_AFFILIATION_111));
2340 retval = 1;
2341 goto out;
2342 }
2343 }
2344 out:
2345 if (new_affiliation_111) {
2346 dma_free_coherent(&instance->pdev->dev,
2347 sizeof(struct MR_LD_VF_AFFILIATION_111),
2348 new_affiliation_111,
2349 new_affiliation_111_h);
2350 }
2351
2352 megasas_return_cmd(instance, cmd);
2353
2354 return retval;
2355 }
2356
2357 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2358 int initial)
2359 {
2360 struct megasas_cmd *cmd;
2361 struct megasas_dcmd_frame *dcmd;
2362 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2363 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2364 dma_addr_t new_affiliation_h;
2365 int i, j, retval = 0, found = 0, doscan = 0;
2366 u8 thisVf;
2367
2368 cmd = megasas_get_cmd(instance);
2369
2370 if (!cmd) {
2371 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2372 "Failed to get cmd for scsi%d\n",
2373 instance->host->host_no);
2374 return -ENOMEM;
2375 }
2376
2377 dcmd = &cmd->frame->dcmd;
2378
2379 if (!instance->vf_affiliation) {
2380 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2381 "affiliation for scsi%d\n", instance->host->host_no);
2382 megasas_return_cmd(instance, cmd);
2383 return -ENOMEM;
2384 }
2385
2386 if (initial)
2387 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2388 sizeof(struct MR_LD_VF_AFFILIATION));
2389 else {
2390 new_affiliation =
2391 dma_alloc_coherent(&instance->pdev->dev,
2392 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2393 &new_affiliation_h, GFP_KERNEL);
2394 if (!new_affiliation) {
2395 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2396 "memory for new affiliation for scsi%d\n",
2397 instance->host->host_no);
2398 megasas_return_cmd(instance, cmd);
2399 return -ENOMEM;
2400 }
2401 }
2402
2403 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2404
2405 dcmd->cmd = MFI_CMD_DCMD;
2406 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2407 dcmd->sge_count = 1;
2408 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2409 dcmd->timeout = 0;
2410 dcmd->pad_0 = 0;
2411 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2412 sizeof(struct MR_LD_VF_AFFILIATION));
2413 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2414
2415 if (initial)
2416 dcmd->sgl.sge32[0].phys_addr =
2417 cpu_to_le32(instance->vf_affiliation_h);
2418 else
2419 dcmd->sgl.sge32[0].phys_addr =
2420 cpu_to_le32(new_affiliation_h);
2421
2422 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2423 sizeof(struct MR_LD_VF_AFFILIATION));
2424
2425 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2426 "scsi%d\n", instance->host->host_no);
2427
2428
2429 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2430 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2431 " failed with status 0x%x for scsi%d\n",
2432 dcmd->cmd_status, instance->host->host_no);
2433 retval = 1; /* Do a scan if we couldn't get affiliation */
2434 goto out;
2435 }
2436
2437 if (!initial) {
2438 if (!new_affiliation->ldCount) {
2439 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2440 "affiliation for passive path for scsi%d\n",
2441 instance->host->host_no);
2442 retval = 1;
2443 goto out;
2444 }
2445 newmap = new_affiliation->map;
2446 savedmap = instance->vf_affiliation->map;
2447 thisVf = new_affiliation->thisVf;
2448 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2449 found = 0;
2450 for (j = 0; j < instance->vf_affiliation->ldCount;
2451 j++) {
2452 if (newmap->ref.targetId ==
2453 savedmap->ref.targetId) {
2454 found = 1;
2455 if (newmap->policy[thisVf] !=
2456 savedmap->policy[thisVf]) {
2457 doscan = 1;
2458 goto out;
2459 }
2460 }
2461 savedmap = (struct MR_LD_VF_MAP *)
2462 ((unsigned char *)savedmap +
2463 savedmap->size);
2464 }
2465 if (!found && newmap->policy[thisVf] !=
2466 MR_LD_ACCESS_HIDDEN) {
2467 doscan = 1;
2468 goto out;
2469 }
2470 newmap = (struct MR_LD_VF_MAP *)
2471 ((unsigned char *)newmap + newmap->size);
2472 }
2473
2474 newmap = new_affiliation->map;
2475 savedmap = instance->vf_affiliation->map;
2476
2477 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2478 found = 0;
2479 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2480 if (savedmap->ref.targetId ==
2481 newmap->ref.targetId) {
2482 found = 1;
2483 if (savedmap->policy[thisVf] !=
2484 newmap->policy[thisVf]) {
2485 doscan = 1;
2486 goto out;
2487 }
2488 }
2489 newmap = (struct MR_LD_VF_MAP *)
2490 ((unsigned char *)newmap +
2491 newmap->size);
2492 }
2493 if (!found && savedmap->policy[thisVf] !=
2494 MR_LD_ACCESS_HIDDEN) {
2495 doscan = 1;
2496 goto out;
2497 }
2498 savedmap = (struct MR_LD_VF_MAP *)
2499 ((unsigned char *)savedmap +
2500 savedmap->size);
2501 }
2502 }
2503 out:
2504 if (doscan) {
2505 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2506 "affiliation for scsi%d\n", instance->host->host_no);
2507 memcpy(instance->vf_affiliation, new_affiliation,
2508 new_affiliation->size);
2509 retval = 1;
2510 }
2511
2512 if (new_affiliation)
2513 dma_free_coherent(&instance->pdev->dev,
2514 (MAX_LOGICAL_DRIVES + 1) *
2515 sizeof(struct MR_LD_VF_AFFILIATION),
2516 new_affiliation, new_affiliation_h);
2517 megasas_return_cmd(instance, cmd);
2518
2519 return retval;
2520 }
2521
2522 /* This function will get the current SR-IOV LD/VF affiliation */
2523 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2524 int initial)
2525 {
2526 int retval;
2527
2528 if (instance->PlasmaFW111)
2529 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2530 else
2531 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2532 return retval;
2533 }
2534
2535 /* This function will tell FW to start the SR-IOV heartbeat */
2536 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2537 int initial)
2538 {
2539 struct megasas_cmd *cmd;
2540 struct megasas_dcmd_frame *dcmd;
2541 int retval = 0;
2542
2543 cmd = megasas_get_cmd(instance);
2544
2545 if (!cmd) {
2546 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2547 "Failed to get cmd for scsi%d\n",
2548 instance->host->host_no);
2549 return -ENOMEM;
2550 }
2551
2552 dcmd = &cmd->frame->dcmd;
2553
2554 if (initial) {
2555 instance->hb_host_mem =
2556 dma_alloc_coherent(&instance->pdev->dev,
2557 sizeof(struct MR_CTRL_HB_HOST_MEM),
2558 &instance->hb_host_mem_h,
2559 GFP_KERNEL);
2560 if (!instance->hb_host_mem) {
2561 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2562 " memory for heartbeat host memory for scsi%d\n",
2563 instance->host->host_no);
2564 retval = -ENOMEM;
2565 goto out;
2566 }
2567 }
2568
2569 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2570
2571 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2572 dcmd->cmd = MFI_CMD_DCMD;
2573 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2574 dcmd->sge_count = 1;
2575 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2576 dcmd->timeout = 0;
2577 dcmd->pad_0 = 0;
2578 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2579 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2580
2581 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2582 sizeof(struct MR_CTRL_HB_HOST_MEM));
2583
2584 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2585 instance->host->host_no);
2586
2587 if ((instance->adapter_type != MFI_SERIES) &&
2588 !instance->mask_interrupts)
2589 retval = megasas_issue_blocked_cmd(instance, cmd,
2590 MEGASAS_ROUTINE_WAIT_TIME_VF);
2591 else
2592 retval = megasas_issue_polled(instance, cmd);
2593
2594 if (retval) {
2595 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2596 "_MEM_ALLOC DCMD %s for scsi%d\n",
2597 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2598 "timed out" : "failed", instance->host->host_no);
2599 retval = 1;
2600 }
2601
2602 out:
2603 megasas_return_cmd(instance, cmd);
2604
2605 return retval;
2606 }
2607
2608 /* Handler for SR-IOV heartbeat */
2609 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2610 {
2611 struct megasas_instance *instance =
2612 from_timer(instance, t, sriov_heartbeat_timer);
2613
2614 if (instance->hb_host_mem->HB.fwCounter !=
2615 instance->hb_host_mem->HB.driverCounter) {
2616 instance->hb_host_mem->HB.driverCounter =
2617 instance->hb_host_mem->HB.fwCounter;
2618 mod_timer(&instance->sriov_heartbeat_timer,
2619 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2620 } else {
2621 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2622 "completed for scsi%d\n", instance->host->host_no);
2623 schedule_work(&instance->work_init);
2624 }
2625 }
2626
2627 /**
2628 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2629 * @instance: Adapter soft state
2630 *
2631 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2632 * complete all its outstanding commands. Returns error if one or more IOs
2633 * are pending after this time period. It also marks the controller dead.
2634 */
2635 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2636 {
2637 int i, sl, outstanding;
2638 u32 reset_index;
2639 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2640 unsigned long flags;
2641 struct list_head clist_local;
2642 struct megasas_cmd *reset_cmd;
2643 u32 fw_state;
2644
2645 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2646 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2647 __func__, __LINE__);
2648 return FAILED;
2649 }
2650
2651 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2652
2653 INIT_LIST_HEAD(&clist_local);
2654 spin_lock_irqsave(&instance->hba_lock, flags);
2655 list_splice_init(&instance->internal_reset_pending_q,
2656 &clist_local);
2657 spin_unlock_irqrestore(&instance->hba_lock, flags);
2658
2659 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2660 for (i = 0; i < wait_time; i++) {
2661 msleep(1000);
2662 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2663 break;
2664 }
2665
2666 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2667 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2668 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2669 return FAILED;
2670 }
2671
2672 reset_index = 0;
2673 while (!list_empty(&clist_local)) {
2674 reset_cmd = list_entry((&clist_local)->next,
2675 struct megasas_cmd, list);
2676 list_del_init(&reset_cmd->list);
2677 if (reset_cmd->scmd) {
2678 reset_cmd->scmd->result = DID_REQUEUE << 16;
2679 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2680 reset_index, reset_cmd,
2681 reset_cmd->scmd->cmnd[0]);
2682
2683 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2684 megasas_return_cmd(instance, reset_cmd);
2685 } else if (reset_cmd->sync_cmd) {
2686 dev_notice(&instance->pdev->dev, "%p synch cmds"
2687 "reset queue\n",
2688 reset_cmd);
2689
2690 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2691 instance->instancet->fire_cmd(instance,
2692 reset_cmd->frame_phys_addr,
2693 0, instance->reg_set);
2694 } else {
2695 dev_notice(&instance->pdev->dev, "%p unexpected"
2696 "cmds lst\n",
2697 reset_cmd);
2698 }
2699 reset_index++;
2700 }
2701
2702 return SUCCESS;
2703 }
2704
2705 for (i = 0; i < resetwaittime; i++) {
2706 outstanding = atomic_read(&instance->fw_outstanding);
2707
2708 if (!outstanding)
2709 break;
2710
2711 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2712 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2713 "commands to complete\n",i,outstanding);
2714 /*
2715 * Call cmd completion routine. Cmd to be
2716 * be completed directly without depending on isr.
2717 */
2718 megasas_complete_cmd_dpc((unsigned long)instance);
2719 }
2720
2721 msleep(1000);
2722 }
2723
2724 i = 0;
2725 outstanding = atomic_read(&instance->fw_outstanding);
2726 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2727
2728 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2729 goto no_outstanding;
2730
2731 if (instance->disableOnlineCtrlReset)
2732 goto kill_hba_and_failed;
2733 do {
2734 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2735 dev_info(&instance->pdev->dev,
2736 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2737 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2738 if (i == 3)
2739 goto kill_hba_and_failed;
2740 megasas_do_ocr(instance);
2741
2742 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2743 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2744 __func__, __LINE__);
2745 return FAILED;
2746 }
2747 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2748 __func__, __LINE__);
2749
2750 for (sl = 0; sl < 10; sl++)
2751 msleep(500);
2752
2753 outstanding = atomic_read(&instance->fw_outstanding);
2754
2755 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2756 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2757 goto no_outstanding;
2758 }
2759 i++;
2760 } while (i <= 3);
2761
2762 no_outstanding:
2763
2764 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2765 __func__, __LINE__);
2766 return SUCCESS;
2767
2768 kill_hba_and_failed:
2769
2770 /* Reset not supported, kill adapter */
2771 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2772 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2773 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2774 atomic_read(&instance->fw_outstanding));
2775 megasas_dump_pending_frames(instance);
2776 megaraid_sas_kill_hba(instance);
2777
2778 return FAILED;
2779 }
2780
2781 /**
2782 * megasas_generic_reset - Generic reset routine
2783 * @scmd: Mid-layer SCSI command
2784 *
2785 * This routine implements a generic reset handler for device, bus and host
2786 * reset requests. Device, bus and host specific reset handlers can use this
2787 * function after they do their specific tasks.
2788 */
2789 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2790 {
2791 int ret_val;
2792 struct megasas_instance *instance;
2793
2794 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2795
2796 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2797 scmd->cmnd[0], scmd->retries);
2798
2799 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2800 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2801 return FAILED;
2802 }
2803
2804 ret_val = megasas_wait_for_outstanding(instance);
2805 if (ret_val == SUCCESS)
2806 dev_notice(&instance->pdev->dev, "reset successful\n");
2807 else
2808 dev_err(&instance->pdev->dev, "failed to do reset\n");
2809
2810 return ret_val;
2811 }
2812
2813 /**
2814 * megasas_reset_timer - quiesce the adapter if required
2815 * @scmd: scsi cmnd
2816 *
2817 * Sets the FW busy flag and reduces the host->can_queue if the
2818 * cmd has not been completed within the timeout period.
2819 */
2820 static enum
2821 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2822 {
2823 struct megasas_instance *instance;
2824 unsigned long flags;
2825
2826 if (time_after(jiffies, scmd->jiffies_at_alloc +
2827 (scmd_timeout * 2) * HZ)) {
2828 return BLK_EH_DONE;
2829 }
2830
2831 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2832 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2833 /* FW is busy, throttle IO */
2834 spin_lock_irqsave(instance->host->host_lock, flags);
2835
2836 instance->host->can_queue = instance->throttlequeuedepth;
2837 instance->last_time = jiffies;
2838 instance->flag |= MEGASAS_FW_BUSY;
2839
2840 spin_unlock_irqrestore(instance->host->host_lock, flags);
2841 }
2842 return BLK_EH_RESET_TIMER;
2843 }
2844
2845 /**
2846 * megasas_dump - This function will print hexdump of provided buffer.
2847 * @buf: Buffer to be dumped
2848 * @sz: Size in bytes
2849 * @format: Different formats of dumping e.g. format=n will
2850 * cause only 'n' 32 bit words to be dumped in a single
2851 * line.
2852 */
2853 inline void
2854 megasas_dump(void *buf, int sz, int format)
2855 {
2856 int i;
2857 __le32 *buf_loc = (__le32 *)buf;
2858
2859 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2860 if ((i % format) == 0) {
2861 if (i != 0)
2862 printk(KERN_CONT "\n");
2863 printk(KERN_CONT "%08x: ", (i * 4));
2864 }
2865 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2866 }
2867 printk(KERN_CONT "\n");
2868 }
2869
2870 /**
2871 * megasas_dump_reg_set - This function will print hexdump of register set
2872 * @buf: Buffer to be dumped
2873 * @sz: Size in bytes
2874 * @format: Different formats of dumping e.g. format=n will
2875 * cause only 'n' 32 bit words to be dumped in a
2876 * single line.
2877 */
2878 inline void
2879 megasas_dump_reg_set(void __iomem *reg_set)
2880 {
2881 unsigned int i, sz = 256;
2882 u32 __iomem *reg = (u32 __iomem *)reg_set;
2883
2884 for (i = 0; i < (sz / sizeof(u32)); i++)
2885 printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2886 }
2887
2888 /**
2889 * megasas_dump_fusion_io - This function will print key details
2890 * of SCSI IO
2891 * @scmd: SCSI command pointer of SCSI IO
2892 */
2893 void
2894 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2895 {
2896 struct megasas_cmd_fusion *cmd;
2897 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2898 struct megasas_instance *instance;
2899
2900 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2901 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2902
2903 scmd_printk(KERN_INFO, scmd,
2904 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2905 scmd, scmd->retries, scmd->allowed);
2906 scsi_print_command(scmd);
2907
2908 if (cmd) {
2909 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2910 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2911 scmd_printk(KERN_INFO, scmd,
2912 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
2913 req_desc->SCSIIO.RequestFlags,
2914 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2915 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2916
2917 printk(KERN_INFO "IO request frame:\n");
2918 megasas_dump(cmd->io_request,
2919 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
2920 printk(KERN_INFO "Chain frame:\n");
2921 megasas_dump(cmd->sg_frame,
2922 instance->max_chain_frame_sz, 8);
2923 }
2924
2925 }
2926
2927 /*
2928 * megasas_dump_sys_regs - This function will dump system registers through
2929 * sysfs.
2930 * @reg_set: Pointer to System register set.
2931 * @buf: Buffer to which output is to be written.
2932 * @return: Number of bytes written to buffer.
2933 */
2934 static inline ssize_t
2935 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2936 {
2937 unsigned int i, sz = 256;
2938 int bytes_wrote = 0;
2939 char *loc = (char *)buf;
2940 u32 __iomem *reg = (u32 __iomem *)reg_set;
2941
2942 for (i = 0; i < sz / sizeof(u32); i++) {
2943 bytes_wrote += snprintf(loc + bytes_wrote, PAGE_SIZE,
2944 "%08x: %08x\n", (i * 4),
2945 readl(&reg[i]));
2946 }
2947 return bytes_wrote;
2948 }
2949
2950 /**
2951 * megasas_reset_bus_host - Bus & host reset handler entry point
2952 */
2953 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2954 {
2955 int ret;
2956 struct megasas_instance *instance;
2957
2958 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2959
2960 scmd_printk(KERN_INFO, scmd,
2961 "OCR is requested due to IO timeout!!\n");
2962
2963 scmd_printk(KERN_INFO, scmd,
2964 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
2965 scmd->device->host->shost_state,
2966 scsi_host_busy(scmd->device->host),
2967 atomic_read(&instance->fw_outstanding));
2968 /*
2969 * First wait for all commands to complete
2970 */
2971 if (instance->adapter_type == MFI_SERIES) {
2972 ret = megasas_generic_reset(scmd);
2973 } else {
2974 megasas_dump_fusion_io(scmd);
2975 ret = megasas_reset_fusion(scmd->device->host,
2976 SCSIIO_TIMEOUT_OCR);
2977 }
2978
2979 return ret;
2980 }
2981
2982 /**
2983 * megasas_task_abort - Issues task abort request to firmware
2984 * (supported only for fusion adapters)
2985 * @scmd: SCSI command pointer
2986 */
2987 static int megasas_task_abort(struct scsi_cmnd *scmd)
2988 {
2989 int ret;
2990 struct megasas_instance *instance;
2991
2992 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2993
2994 if (instance->adapter_type != MFI_SERIES)
2995 ret = megasas_task_abort_fusion(scmd);
2996 else {
2997 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2998 ret = FAILED;
2999 }
3000
3001 return ret;
3002 }
3003
3004 /**
3005 * megasas_reset_target: Issues target reset request to firmware
3006 * (supported only for fusion adapters)
3007 * @scmd: SCSI command pointer
3008 */
3009 static int megasas_reset_target(struct scsi_cmnd *scmd)
3010 {
3011 int ret;
3012 struct megasas_instance *instance;
3013
3014 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3015
3016 if (instance->adapter_type != MFI_SERIES)
3017 ret = megasas_reset_target_fusion(scmd);
3018 else {
3019 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3020 ret = FAILED;
3021 }
3022
3023 return ret;
3024 }
3025
3026 /**
3027 * megasas_bios_param - Returns disk geometry for a disk
3028 * @sdev: device handle
3029 * @bdev: block device
3030 * @capacity: drive capacity
3031 * @geom: geometry parameters
3032 */
3033 static int
3034 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3035 sector_t capacity, int geom[])
3036 {
3037 int heads;
3038 int sectors;
3039 sector_t cylinders;
3040 unsigned long tmp;
3041
3042 /* Default heads (64) & sectors (32) */
3043 heads = 64;
3044 sectors = 32;
3045
3046 tmp = heads * sectors;
3047 cylinders = capacity;
3048
3049 sector_div(cylinders, tmp);
3050
3051 /*
3052 * Handle extended translation size for logical drives > 1Gb
3053 */
3054
3055 if (capacity >= 0x200000) {
3056 heads = 255;
3057 sectors = 63;
3058 tmp = heads*sectors;
3059 cylinders = capacity;
3060 sector_div(cylinders, tmp);
3061 }
3062
3063 geom[0] = heads;
3064 geom[1] = sectors;
3065 geom[2] = cylinders;
3066
3067 return 0;
3068 }
3069
3070 static void megasas_aen_polling(struct work_struct *work);
3071
3072 /**
3073 * megasas_service_aen - Processes an event notification
3074 * @instance: Adapter soft state
3075 * @cmd: AEN command completed by the ISR
3076 *
3077 * For AEN, driver sends a command down to FW that is held by the FW till an
3078 * event occurs. When an event of interest occurs, FW completes the command
3079 * that it was previously holding.
3080 *
3081 * This routines sends SIGIO signal to processes that have registered with the
3082 * driver for AEN.
3083 */
3084 static void
3085 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3086 {
3087 unsigned long flags;
3088
3089 /*
3090 * Don't signal app if it is just an aborted previously registered aen
3091 */
3092 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3093 spin_lock_irqsave(&poll_aen_lock, flags);
3094 megasas_poll_wait_aen = 1;
3095 spin_unlock_irqrestore(&poll_aen_lock, flags);
3096 wake_up(&megasas_poll_wait);
3097 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3098 }
3099 else
3100 cmd->abort_aen = 0;
3101
3102 instance->aen_cmd = NULL;
3103
3104 megasas_return_cmd(instance, cmd);
3105
3106 if ((instance->unload == 0) &&
3107 ((instance->issuepend_done == 1))) {
3108 struct megasas_aen_event *ev;
3109
3110 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3111 if (!ev) {
3112 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3113 } else {
3114 ev->instance = instance;
3115 instance->ev = ev;
3116 INIT_DELAYED_WORK(&ev->hotplug_work,
3117 megasas_aen_polling);
3118 schedule_delayed_work(&ev->hotplug_work, 0);
3119 }
3120 }
3121 }
3122
3123 static ssize_t
3124 megasas_fw_crash_buffer_store(struct device *cdev,
3125 struct device_attribute *attr, const char *buf, size_t count)
3126 {
3127 struct Scsi_Host *shost = class_to_shost(cdev);
3128 struct megasas_instance *instance =
3129 (struct megasas_instance *) shost->hostdata;
3130 int val = 0;
3131 unsigned long flags;
3132
3133 if (kstrtoint(buf, 0, &val) != 0)
3134 return -EINVAL;
3135
3136 spin_lock_irqsave(&instance->crashdump_lock, flags);
3137 instance->fw_crash_buffer_offset = val;
3138 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3139 return strlen(buf);
3140 }
3141
3142 static ssize_t
3143 megasas_fw_crash_buffer_show(struct device *cdev,
3144 struct device_attribute *attr, char *buf)
3145 {
3146 struct Scsi_Host *shost = class_to_shost(cdev);
3147 struct megasas_instance *instance =
3148 (struct megasas_instance *) shost->hostdata;
3149 u32 size;
3150 unsigned long buff_addr;
3151 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3152 unsigned long src_addr;
3153 unsigned long flags;
3154 u32 buff_offset;
3155
3156 spin_lock_irqsave(&instance->crashdump_lock, flags);
3157 buff_offset = instance->fw_crash_buffer_offset;
3158 if (!instance->crash_dump_buf &&
3159 !((instance->fw_crash_state == AVAILABLE) ||
3160 (instance->fw_crash_state == COPYING))) {
3161 dev_err(&instance->pdev->dev,
3162 "Firmware crash dump is not available\n");
3163 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3164 return -EINVAL;
3165 }
3166
3167 buff_addr = (unsigned long) buf;
3168
3169 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3170 dev_err(&instance->pdev->dev,
3171 "Firmware crash dump offset is out of range\n");
3172 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3173 return 0;
3174 }
3175
3176 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3177 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3178
3179 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3180 (buff_offset % dmachunk);
3181 memcpy(buf, (void *)src_addr, size);
3182 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3183
3184 return size;
3185 }
3186
3187 static ssize_t
3188 megasas_fw_crash_buffer_size_show(struct device *cdev,
3189 struct device_attribute *attr, char *buf)
3190 {
3191 struct Scsi_Host *shost = class_to_shost(cdev);
3192 struct megasas_instance *instance =
3193 (struct megasas_instance *) shost->hostdata;
3194
3195 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3196 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3197 }
3198
3199 static ssize_t
3200 megasas_fw_crash_state_store(struct device *cdev,
3201 struct device_attribute *attr, const char *buf, size_t count)
3202 {
3203 struct Scsi_Host *shost = class_to_shost(cdev);
3204 struct megasas_instance *instance =
3205 (struct megasas_instance *) shost->hostdata;
3206 int val = 0;
3207 unsigned long flags;
3208
3209 if (kstrtoint(buf, 0, &val) != 0)
3210 return -EINVAL;
3211
3212 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3213 dev_err(&instance->pdev->dev, "application updates invalid "
3214 "firmware crash state\n");
3215 return -EINVAL;
3216 }
3217
3218 instance->fw_crash_state = val;
3219
3220 if ((val == COPIED) || (val == COPY_ERROR)) {
3221 spin_lock_irqsave(&instance->crashdump_lock, flags);
3222 megasas_free_host_crash_buffer(instance);
3223 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3224 if (val == COPY_ERROR)
3225 dev_info(&instance->pdev->dev, "application failed to "
3226 "copy Firmware crash dump\n");
3227 else
3228 dev_info(&instance->pdev->dev, "Firmware crash dump "
3229 "copied successfully\n");
3230 }
3231 return strlen(buf);
3232 }
3233
3234 static ssize_t
3235 megasas_fw_crash_state_show(struct device *cdev,
3236 struct device_attribute *attr, char *buf)
3237 {
3238 struct Scsi_Host *shost = class_to_shost(cdev);
3239 struct megasas_instance *instance =
3240 (struct megasas_instance *) shost->hostdata;
3241
3242 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3243 }
3244
3245 static ssize_t
3246 megasas_page_size_show(struct device *cdev,
3247 struct device_attribute *attr, char *buf)
3248 {
3249 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3250 }
3251
3252 static ssize_t
3253 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3254 char *buf)
3255 {
3256 struct Scsi_Host *shost = class_to_shost(cdev);
3257 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3258
3259 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3260 }
3261
3262 static ssize_t
3263 megasas_fw_cmds_outstanding_show(struct device *cdev,
3264 struct device_attribute *attr, char *buf)
3265 {
3266 struct Scsi_Host *shost = class_to_shost(cdev);
3267 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3268
3269 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3270 }
3271
3272 static ssize_t
3273 megasas_dump_system_regs_show(struct device *cdev,
3274 struct device_attribute *attr, char *buf)
3275 {
3276 struct Scsi_Host *shost = class_to_shost(cdev);
3277 struct megasas_instance *instance =
3278 (struct megasas_instance *)shost->hostdata;
3279
3280 return megasas_dump_sys_regs(instance->reg_set, buf);
3281 }
3282
3283 static ssize_t
3284 megasas_raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3285 char *buf)
3286 {
3287 struct Scsi_Host *shost = class_to_shost(cdev);
3288 struct megasas_instance *instance =
3289 (struct megasas_instance *)shost->hostdata;
3290
3291 return snprintf(buf, PAGE_SIZE, "%ld\n",
3292 (unsigned long)instance->map_id);
3293 }
3294
3295 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3296 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3297 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3298 megasas_fw_crash_buffer_size_show, NULL);
3299 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3300 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3301 static DEVICE_ATTR(page_size, S_IRUGO,
3302 megasas_page_size_show, NULL);
3303 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3304 megasas_ldio_outstanding_show, NULL);
3305 static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3306 megasas_fw_cmds_outstanding_show, NULL);
3307 static DEVICE_ATTR(dump_system_regs, S_IRUGO,
3308 megasas_dump_system_regs_show, NULL);
3309 static DEVICE_ATTR(raid_map_id, S_IRUGO,
3310 megasas_raid_map_id_show, NULL);
3311
3312 struct device_attribute *megaraid_host_attrs[] = {
3313 &dev_attr_fw_crash_buffer_size,
3314 &dev_attr_fw_crash_buffer,
3315 &dev_attr_fw_crash_state,
3316 &dev_attr_page_size,
3317 &dev_attr_ldio_outstanding,
3318 &dev_attr_fw_cmds_outstanding,
3319 &dev_attr_dump_system_regs,
3320 &dev_attr_raid_map_id,
3321 NULL,
3322 };
3323
3324 /*
3325 * Scsi host template for megaraid_sas driver
3326 */
3327 static struct scsi_host_template megasas_template = {
3328
3329 .module = THIS_MODULE,
3330 .name = "Avago SAS based MegaRAID driver",
3331 .proc_name = "megaraid_sas",
3332 .slave_configure = megasas_slave_configure,
3333 .slave_alloc = megasas_slave_alloc,
3334 .slave_destroy = megasas_slave_destroy,
3335 .queuecommand = megasas_queue_command,
3336 .eh_target_reset_handler = megasas_reset_target,
3337 .eh_abort_handler = megasas_task_abort,
3338 .eh_host_reset_handler = megasas_reset_bus_host,
3339 .eh_timed_out = megasas_reset_timer,
3340 .shost_attrs = megaraid_host_attrs,
3341 .bios_param = megasas_bios_param,
3342 .change_queue_depth = scsi_change_queue_depth,
3343 .no_write_same = 1,
3344 };
3345
3346 /**
3347 * megasas_complete_int_cmd - Completes an internal command
3348 * @instance: Adapter soft state
3349 * @cmd: Command to be completed
3350 *
3351 * The megasas_issue_blocked_cmd() function waits for a command to complete
3352 * after it issues a command. This function wakes up that waiting routine by
3353 * calling wake_up() on the wait queue.
3354 */
3355 static void
3356 megasas_complete_int_cmd(struct megasas_instance *instance,
3357 struct megasas_cmd *cmd)
3358 {
3359 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3360 wake_up(&instance->int_cmd_wait_q);
3361 }
3362
3363 /**
3364 * megasas_complete_abort - Completes aborting a command
3365 * @instance: Adapter soft state
3366 * @cmd: Cmd that was issued to abort another cmd
3367 *
3368 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3369 * after it issues an abort on a previously issued command. This function
3370 * wakes up all functions waiting on the same wait queue.
3371 */
3372 static void
3373 megasas_complete_abort(struct megasas_instance *instance,
3374 struct megasas_cmd *cmd)
3375 {
3376 if (cmd->sync_cmd) {
3377 cmd->sync_cmd = 0;
3378 cmd->cmd_status_drv = 0;
3379 wake_up(&instance->abort_cmd_wait_q);
3380 }
3381 }
3382
3383 /**
3384 * megasas_complete_cmd - Completes a command
3385 * @instance: Adapter soft state
3386 * @cmd: Command to be completed
3387 * @alt_status: If non-zero, use this value as status to
3388 * SCSI mid-layer instead of the value returned
3389 * by the FW. This should be used if caller wants
3390 * an alternate status (as in the case of aborted
3391 * commands)
3392 */
3393 void
3394 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3395 u8 alt_status)
3396 {
3397 int exception = 0;
3398 struct megasas_header *hdr = &cmd->frame->hdr;
3399 unsigned long flags;
3400 struct fusion_context *fusion = instance->ctrl_context;
3401 u32 opcode, status;
3402
3403 /* flag for the retry reset */
3404 cmd->retry_for_fw_reset = 0;
3405
3406 if (cmd->scmd)
3407 cmd->scmd->SCp.ptr = NULL;
3408
3409 switch (hdr->cmd) {
3410 case MFI_CMD_INVALID:
3411 /* Some older 1068 controller FW may keep a pended
3412 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3413 when booting the kdump kernel. Ignore this command to
3414 prevent a kernel panic on shutdown of the kdump kernel. */
3415 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3416 "completed\n");
3417 dev_warn(&instance->pdev->dev, "If you have a controller "
3418 "other than PERC5, please upgrade your firmware\n");
3419 break;
3420 case MFI_CMD_PD_SCSI_IO:
3421 case MFI_CMD_LD_SCSI_IO:
3422
3423 /*
3424 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3425 * issued either through an IO path or an IOCTL path. If it
3426 * was via IOCTL, we will send it to internal completion.
3427 */
3428 if (cmd->sync_cmd) {
3429 cmd->sync_cmd = 0;
3430 megasas_complete_int_cmd(instance, cmd);
3431 break;
3432 }
3433 /* fall through */
3434
3435 case MFI_CMD_LD_READ:
3436 case MFI_CMD_LD_WRITE:
3437
3438 if (alt_status) {
3439 cmd->scmd->result = alt_status << 16;
3440 exception = 1;
3441 }
3442
3443 if (exception) {
3444
3445 atomic_dec(&instance->fw_outstanding);
3446
3447 scsi_dma_unmap(cmd->scmd);
3448 cmd->scmd->scsi_done(cmd->scmd);
3449 megasas_return_cmd(instance, cmd);
3450
3451 break;
3452 }
3453
3454 switch (hdr->cmd_status) {
3455
3456 case MFI_STAT_OK:
3457 cmd->scmd->result = DID_OK << 16;
3458 break;
3459
3460 case MFI_STAT_SCSI_IO_FAILED:
3461 case MFI_STAT_LD_INIT_IN_PROGRESS:
3462 cmd->scmd->result =
3463 (DID_ERROR << 16) | hdr->scsi_status;
3464 break;
3465
3466 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3467
3468 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3469
3470 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3471 memset(cmd->scmd->sense_buffer, 0,
3472 SCSI_SENSE_BUFFERSIZE);
3473 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3474 hdr->sense_len);
3475
3476 cmd->scmd->result |= DRIVER_SENSE << 24;
3477 }
3478
3479 break;
3480
3481 case MFI_STAT_LD_OFFLINE:
3482 case MFI_STAT_DEVICE_NOT_FOUND:
3483 cmd->scmd->result = DID_BAD_TARGET << 16;
3484 break;
3485
3486 default:
3487 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3488 hdr->cmd_status);
3489 cmd->scmd->result = DID_ERROR << 16;
3490 break;
3491 }
3492
3493 atomic_dec(&instance->fw_outstanding);
3494
3495 scsi_dma_unmap(cmd->scmd);
3496 cmd->scmd->scsi_done(cmd->scmd);
3497 megasas_return_cmd(instance, cmd);
3498
3499 break;
3500
3501 case MFI_CMD_SMP:
3502 case MFI_CMD_STP:
3503 case MFI_CMD_NVME:
3504 megasas_complete_int_cmd(instance, cmd);
3505 break;
3506
3507 case MFI_CMD_DCMD:
3508 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3509 /* Check for LD map update */
3510 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3511 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3512 fusion->fast_path_io = 0;
3513 spin_lock_irqsave(instance->host->host_lock, flags);
3514 status = cmd->frame->hdr.cmd_status;
3515 instance->map_update_cmd = NULL;
3516 if (status != MFI_STAT_OK) {
3517 if (status != MFI_STAT_NOT_FOUND)
3518 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3519 cmd->frame->hdr.cmd_status);
3520 else {
3521 megasas_return_cmd(instance, cmd);
3522 spin_unlock_irqrestore(
3523 instance->host->host_lock,
3524 flags);
3525 break;
3526 }
3527 }
3528
3529 megasas_return_cmd(instance, cmd);
3530
3531 /*
3532 * Set fast path IO to ZERO.
3533 * Validate Map will set proper value.
3534 * Meanwhile all IOs will go as LD IO.
3535 */
3536 if (status == MFI_STAT_OK &&
3537 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3538 instance->map_id++;
3539 fusion->fast_path_io = 1;
3540 } else {
3541 fusion->fast_path_io = 0;
3542 }
3543
3544 megasas_sync_map_info(instance);
3545 spin_unlock_irqrestore(instance->host->host_lock,
3546 flags);
3547 break;
3548 }
3549 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3550 opcode == MR_DCMD_CTRL_EVENT_GET) {
3551 spin_lock_irqsave(&poll_aen_lock, flags);
3552 megasas_poll_wait_aen = 0;
3553 spin_unlock_irqrestore(&poll_aen_lock, flags);
3554 }
3555
3556 /* FW has an updated PD sequence */
3557 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3558 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3559
3560 spin_lock_irqsave(instance->host->host_lock, flags);
3561 status = cmd->frame->hdr.cmd_status;
3562 instance->jbod_seq_cmd = NULL;
3563 megasas_return_cmd(instance, cmd);
3564
3565 if (status == MFI_STAT_OK) {
3566 instance->pd_seq_map_id++;
3567 /* Re-register a pd sync seq num cmd */
3568 if (megasas_sync_pd_seq_num(instance, true))
3569 instance->use_seqnum_jbod_fp = false;
3570 } else
3571 instance->use_seqnum_jbod_fp = false;
3572
3573 spin_unlock_irqrestore(instance->host->host_lock, flags);
3574 break;
3575 }
3576
3577 /*
3578 * See if got an event notification
3579 */
3580 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3581 megasas_service_aen(instance, cmd);
3582 else
3583 megasas_complete_int_cmd(instance, cmd);
3584
3585 break;
3586
3587 case MFI_CMD_ABORT:
3588 /*
3589 * Cmd issued to abort another cmd returned
3590 */
3591 megasas_complete_abort(instance, cmd);
3592 break;
3593
3594 default:
3595 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3596 hdr->cmd);
3597 megasas_complete_int_cmd(instance, cmd);
3598 break;
3599 }
3600 }
3601
3602 /**
3603 * megasas_issue_pending_cmds_again - issue all pending cmds
3604 * in FW again because of the fw reset
3605 * @instance: Adapter soft state
3606 */
3607 static inline void
3608 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3609 {
3610 struct megasas_cmd *cmd;
3611 struct list_head clist_local;
3612 union megasas_evt_class_locale class_locale;
3613 unsigned long flags;
3614 u32 seq_num;
3615
3616 INIT_LIST_HEAD(&clist_local);
3617 spin_lock_irqsave(&instance->hba_lock, flags);
3618 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3619 spin_unlock_irqrestore(&instance->hba_lock, flags);
3620
3621 while (!list_empty(&clist_local)) {
3622 cmd = list_entry((&clist_local)->next,
3623 struct megasas_cmd, list);
3624 list_del_init(&cmd->list);
3625
3626 if (cmd->sync_cmd || cmd->scmd) {
3627 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3628 "detected to be pending while HBA reset\n",
3629 cmd, cmd->scmd, cmd->sync_cmd);
3630
3631 cmd->retry_for_fw_reset++;
3632
3633 if (cmd->retry_for_fw_reset == 3) {
3634 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3635 "was tried multiple times during reset."
3636 "Shutting down the HBA\n",
3637 cmd, cmd->scmd, cmd->sync_cmd);
3638 instance->instancet->disable_intr(instance);
3639 atomic_set(&instance->fw_reset_no_pci_access, 1);
3640 megaraid_sas_kill_hba(instance);
3641 return;
3642 }
3643 }
3644
3645 if (cmd->sync_cmd == 1) {
3646 if (cmd->scmd) {
3647 dev_notice(&instance->pdev->dev, "unexpected"
3648 "cmd attached to internal command!\n");
3649 }
3650 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3651 "on the internal reset queue,"
3652 "issue it again.\n", cmd);
3653 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3654 instance->instancet->fire_cmd(instance,
3655 cmd->frame_phys_addr,
3656 0, instance->reg_set);
3657 } else if (cmd->scmd) {
3658 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3659 "detected on the internal queue, issue again.\n",
3660 cmd, cmd->scmd->cmnd[0]);
3661
3662 atomic_inc(&instance->fw_outstanding);
3663 instance->instancet->fire_cmd(instance,
3664 cmd->frame_phys_addr,
3665 cmd->frame_count-1, instance->reg_set);
3666 } else {
3667 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3668 "internal reset defer list while re-issue!!\n",
3669 cmd);
3670 }
3671 }
3672
3673 if (instance->aen_cmd) {
3674 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3675 megasas_return_cmd(instance, instance->aen_cmd);
3676
3677 instance->aen_cmd = NULL;
3678 }
3679
3680 /*
3681 * Initiate AEN (Asynchronous Event Notification)
3682 */
3683 seq_num = instance->last_seq_num;
3684 class_locale.members.reserved = 0;
3685 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3686 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3687
3688 megasas_register_aen(instance, seq_num, class_locale.word);
3689 }
3690
3691 /**
3692 * Move the internal reset pending commands to a deferred queue.
3693 *
3694 * We move the commands pending at internal reset time to a
3695 * pending queue. This queue would be flushed after successful
3696 * completion of the internal reset sequence. if the internal reset
3697 * did not complete in time, the kernel reset handler would flush
3698 * these commands.
3699 **/
3700 static void
3701 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3702 {
3703 struct megasas_cmd *cmd;
3704 int i;
3705 u16 max_cmd = instance->max_fw_cmds;
3706 u32 defer_index;
3707 unsigned long flags;
3708
3709 defer_index = 0;
3710 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3711 for (i = 0; i < max_cmd; i++) {
3712 cmd = instance->cmd_list[i];
3713 if (cmd->sync_cmd == 1 || cmd->scmd) {
3714 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3715 "on the defer queue as internal\n",
3716 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3717
3718 if (!list_empty(&cmd->list)) {
3719 dev_notice(&instance->pdev->dev, "ERROR while"
3720 " moving this cmd:%p, %d %p, it was"
3721 "discovered on some list?\n",
3722 cmd, cmd->sync_cmd, cmd->scmd);
3723
3724 list_del_init(&cmd->list);
3725 }
3726 defer_index++;
3727 list_add_tail(&cmd->list,
3728 &instance->internal_reset_pending_q);
3729 }
3730 }
3731 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3732 }
3733
3734
3735 static void
3736 process_fw_state_change_wq(struct work_struct *work)
3737 {
3738 struct megasas_instance *instance =
3739 container_of(work, struct megasas_instance, work_init);
3740 u32 wait;
3741 unsigned long flags;
3742
3743 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3744 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3745 atomic_read(&instance->adprecovery));
3746 return ;
3747 }
3748
3749 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3750 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3751 "state, restarting it...\n");
3752
3753 instance->instancet->disable_intr(instance);
3754 atomic_set(&instance->fw_outstanding, 0);
3755
3756 atomic_set(&instance->fw_reset_no_pci_access, 1);
3757 instance->instancet->adp_reset(instance, instance->reg_set);
3758 atomic_set(&instance->fw_reset_no_pci_access, 0);
3759
3760 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3761 "initiating next stage...\n");
3762
3763 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3764 "state 2 starting...\n");
3765
3766 /* waiting for about 20 second before start the second init */
3767 for (wait = 0; wait < 30; wait++) {
3768 msleep(1000);
3769 }
3770
3771 if (megasas_transition_to_ready(instance, 1)) {
3772 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3773
3774 atomic_set(&instance->fw_reset_no_pci_access, 1);
3775 megaraid_sas_kill_hba(instance);
3776 return ;
3777 }
3778
3779 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3780 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3781 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3782 ) {
3783 *instance->consumer = *instance->producer;
3784 } else {
3785 *instance->consumer = 0;
3786 *instance->producer = 0;
3787 }
3788
3789 megasas_issue_init_mfi(instance);
3790
3791 spin_lock_irqsave(&instance->hba_lock, flags);
3792 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3793 spin_unlock_irqrestore(&instance->hba_lock, flags);
3794 instance->instancet->enable_intr(instance);
3795
3796 megasas_issue_pending_cmds_again(instance);
3797 instance->issuepend_done = 1;
3798 }
3799 }
3800
3801 /**
3802 * megasas_deplete_reply_queue - Processes all completed commands
3803 * @instance: Adapter soft state
3804 * @alt_status: Alternate status to be returned to
3805 * SCSI mid-layer instead of the status
3806 * returned by the FW
3807 * Note: this must be called with hba lock held
3808 */
3809 static int
3810 megasas_deplete_reply_queue(struct megasas_instance *instance,
3811 u8 alt_status)
3812 {
3813 u32 mfiStatus;
3814 u32 fw_state;
3815
3816 if ((mfiStatus = instance->instancet->check_reset(instance,
3817 instance->reg_set)) == 1) {
3818 return IRQ_HANDLED;
3819 }
3820
3821 mfiStatus = instance->instancet->clear_intr(instance);
3822 if (mfiStatus == 0) {
3823 /* Hardware may not set outbound_intr_status in MSI-X mode */
3824 if (!instance->msix_vectors)
3825 return IRQ_NONE;
3826 }
3827
3828 instance->mfiStatus = mfiStatus;
3829
3830 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3831 fw_state = instance->instancet->read_fw_status_reg(
3832 instance) & MFI_STATE_MASK;
3833
3834 if (fw_state != MFI_STATE_FAULT) {
3835 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3836 fw_state);
3837 }
3838
3839 if ((fw_state == MFI_STATE_FAULT) &&
3840 (instance->disableOnlineCtrlReset == 0)) {
3841 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3842
3843 if ((instance->pdev->device ==
3844 PCI_DEVICE_ID_LSI_SAS1064R) ||
3845 (instance->pdev->device ==
3846 PCI_DEVICE_ID_DELL_PERC5) ||
3847 (instance->pdev->device ==
3848 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3849
3850 *instance->consumer =
3851 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3852 }
3853
3854
3855 instance->instancet->disable_intr(instance);
3856 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3857 instance->issuepend_done = 0;
3858
3859 atomic_set(&instance->fw_outstanding, 0);
3860 megasas_internal_reset_defer_cmds(instance);
3861
3862 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3863 fw_state, atomic_read(&instance->adprecovery));
3864
3865 schedule_work(&instance->work_init);
3866 return IRQ_HANDLED;
3867
3868 } else {
3869 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3870 fw_state, instance->disableOnlineCtrlReset);
3871 }
3872 }
3873
3874 tasklet_schedule(&instance->isr_tasklet);
3875 return IRQ_HANDLED;
3876 }
3877 /**
3878 * megasas_isr - isr entry point
3879 */
3880 static irqreturn_t megasas_isr(int irq, void *devp)
3881 {
3882 struct megasas_irq_context *irq_context = devp;
3883 struct megasas_instance *instance = irq_context->instance;
3884 unsigned long flags;
3885 irqreturn_t rc;
3886
3887 if (atomic_read(&instance->fw_reset_no_pci_access))
3888 return IRQ_HANDLED;
3889
3890 spin_lock_irqsave(&instance->hba_lock, flags);
3891 rc = megasas_deplete_reply_queue(instance, DID_OK);
3892 spin_unlock_irqrestore(&instance->hba_lock, flags);
3893
3894 return rc;
3895 }
3896
3897 /**
3898 * megasas_transition_to_ready - Move the FW to READY state
3899 * @instance: Adapter soft state
3900 *
3901 * During the initialization, FW passes can potentially be in any one of
3902 * several possible states. If the FW in operational, waiting-for-handshake
3903 * states, driver must take steps to bring it to ready state. Otherwise, it
3904 * has to wait for the ready state.
3905 */
3906 int
3907 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3908 {
3909 int i;
3910 u8 max_wait;
3911 u32 fw_state;
3912 u32 abs_state, curr_abs_state;
3913
3914 abs_state = instance->instancet->read_fw_status_reg(instance);
3915 fw_state = abs_state & MFI_STATE_MASK;
3916
3917 if (fw_state != MFI_STATE_READY)
3918 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3919 " state\n");
3920
3921 while (fw_state != MFI_STATE_READY) {
3922
3923 switch (fw_state) {
3924
3925 case MFI_STATE_FAULT:
3926 dev_printk(KERN_ERR, &instance->pdev->dev,
3927 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
3928 abs_state & MFI_STATE_FAULT_CODE,
3929 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
3930 if (ocr) {
3931 max_wait = MEGASAS_RESET_WAIT_TIME;
3932 break;
3933 } else {
3934 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
3935 megasas_dump_reg_set(instance->reg_set);
3936 return -ENODEV;
3937 }
3938
3939 case MFI_STATE_WAIT_HANDSHAKE:
3940 /*
3941 * Set the CLR bit in inbound doorbell
3942 */
3943 if ((instance->pdev->device ==
3944 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3945 (instance->pdev->device ==
3946 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3947 (instance->adapter_type != MFI_SERIES))
3948 writel(
3949 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3950 &instance->reg_set->doorbell);
3951 else
3952 writel(
3953 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3954 &instance->reg_set->inbound_doorbell);
3955
3956 max_wait = MEGASAS_RESET_WAIT_TIME;
3957 break;
3958
3959 case MFI_STATE_BOOT_MESSAGE_PENDING:
3960 if ((instance->pdev->device ==
3961 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3962 (instance->pdev->device ==
3963 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3964 (instance->adapter_type != MFI_SERIES))
3965 writel(MFI_INIT_HOTPLUG,
3966 &instance->reg_set->doorbell);
3967 else
3968 writel(MFI_INIT_HOTPLUG,
3969 &instance->reg_set->inbound_doorbell);
3970
3971 max_wait = MEGASAS_RESET_WAIT_TIME;
3972 break;
3973
3974 case MFI_STATE_OPERATIONAL:
3975 /*
3976 * Bring it to READY state; assuming max wait 10 secs
3977 */
3978 instance->instancet->disable_intr(instance);
3979 if ((instance->pdev->device ==
3980 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3981 (instance->pdev->device ==
3982 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3983 (instance->adapter_type != MFI_SERIES)) {
3984 writel(MFI_RESET_FLAGS,
3985 &instance->reg_set->doorbell);
3986
3987 if (instance->adapter_type != MFI_SERIES) {
3988 for (i = 0; i < (10 * 1000); i += 20) {
3989 if (megasas_readl(
3990 instance,
3991 &instance->
3992 reg_set->
3993 doorbell) & 1)
3994 msleep(20);
3995 else
3996 break;
3997 }
3998 }
3999 } else
4000 writel(MFI_RESET_FLAGS,
4001 &instance->reg_set->inbound_doorbell);
4002
4003 max_wait = MEGASAS_RESET_WAIT_TIME;
4004 break;
4005
4006 case MFI_STATE_UNDEFINED:
4007 /*
4008 * This state should not last for more than 2 seconds
4009 */
4010 max_wait = MEGASAS_RESET_WAIT_TIME;
4011 break;
4012
4013 case MFI_STATE_BB_INIT:
4014 max_wait = MEGASAS_RESET_WAIT_TIME;
4015 break;
4016
4017 case MFI_STATE_FW_INIT:
4018 max_wait = MEGASAS_RESET_WAIT_TIME;
4019 break;
4020
4021 case MFI_STATE_FW_INIT_2:
4022 max_wait = MEGASAS_RESET_WAIT_TIME;
4023 break;
4024
4025 case MFI_STATE_DEVICE_SCAN:
4026 max_wait = MEGASAS_RESET_WAIT_TIME;
4027 break;
4028
4029 case MFI_STATE_FLUSH_CACHE:
4030 max_wait = MEGASAS_RESET_WAIT_TIME;
4031 break;
4032
4033 default:
4034 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4035 fw_state);
4036 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4037 megasas_dump_reg_set(instance->reg_set);
4038 return -ENODEV;
4039 }
4040
4041 /*
4042 * The cur_state should not last for more than max_wait secs
4043 */
4044 for (i = 0; i < max_wait * 50; i++) {
4045 curr_abs_state = instance->instancet->
4046 read_fw_status_reg(instance);
4047
4048 if (abs_state == curr_abs_state) {
4049 msleep(20);
4050 } else
4051 break;
4052 }
4053
4054 /*
4055 * Return error if fw_state hasn't changed after max_wait
4056 */
4057 if (curr_abs_state == abs_state) {
4058 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4059 "in %d secs\n", fw_state, max_wait);
4060 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4061 megasas_dump_reg_set(instance->reg_set);
4062 return -ENODEV;
4063 }
4064
4065 abs_state = curr_abs_state;
4066 fw_state = curr_abs_state & MFI_STATE_MASK;
4067 }
4068 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4069
4070 return 0;
4071 }
4072
4073 /**
4074 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4075 * @instance: Adapter soft state
4076 */
4077 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4078 {
4079 int i;
4080 u16 max_cmd = instance->max_mfi_cmds;
4081 struct megasas_cmd *cmd;
4082
4083 if (!instance->frame_dma_pool)
4084 return;
4085
4086 /*
4087 * Return all frames to pool
4088 */
4089 for (i = 0; i < max_cmd; i++) {
4090
4091 cmd = instance->cmd_list[i];
4092
4093 if (cmd->frame)
4094 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4095 cmd->frame_phys_addr);
4096
4097 if (cmd->sense)
4098 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4099 cmd->sense_phys_addr);
4100 }
4101
4102 /*
4103 * Now destroy the pool itself
4104 */
4105 dma_pool_destroy(instance->frame_dma_pool);
4106 dma_pool_destroy(instance->sense_dma_pool);
4107
4108 instance->frame_dma_pool = NULL;
4109 instance->sense_dma_pool = NULL;
4110 }
4111
4112 /**
4113 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4114 * @instance: Adapter soft state
4115 *
4116 * Each command packet has an embedded DMA memory buffer that is used for
4117 * filling MFI frame and the SG list that immediately follows the frame. This
4118 * function creates those DMA memory buffers for each command packet by using
4119 * PCI pool facility.
4120 */
4121 static int megasas_create_frame_pool(struct megasas_instance *instance)
4122 {
4123 int i;
4124 u16 max_cmd;
4125 u32 sge_sz;
4126 u32 frame_count;
4127 struct megasas_cmd *cmd;
4128
4129 max_cmd = instance->max_mfi_cmds;
4130
4131 /*
4132 * Size of our frame is 64 bytes for MFI frame, followed by max SG
4133 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4134 */
4135 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4136 sizeof(struct megasas_sge32);
4137
4138 if (instance->flag_ieee)
4139 sge_sz = sizeof(struct megasas_sge_skinny);
4140
4141 /*
4142 * For MFI controllers.
4143 * max_num_sge = 60
4144 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4145 * Total 960 byte (15 MFI frame of 64 byte)
4146 *
4147 * Fusion adapter require only 3 extra frame.
4148 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4149 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4150 * Total 192 byte (3 MFI frame of 64 byte)
4151 */
4152 frame_count = (instance->adapter_type == MFI_SERIES) ?
4153 (15 + 1) : (3 + 1);
4154 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4155 /*
4156 * Use DMA pool facility provided by PCI layer
4157 */
4158 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4159 &instance->pdev->dev,
4160 instance->mfi_frame_size, 256, 0);
4161
4162 if (!instance->frame_dma_pool) {
4163 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4164 return -ENOMEM;
4165 }
4166
4167 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4168 &instance->pdev->dev, 128,
4169 4, 0);
4170
4171 if (!instance->sense_dma_pool) {
4172 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4173
4174 dma_pool_destroy(instance->frame_dma_pool);
4175 instance->frame_dma_pool = NULL;
4176
4177 return -ENOMEM;
4178 }
4179
4180 /*
4181 * Allocate and attach a frame to each of the commands in cmd_list.
4182 * By making cmd->index as the context instead of the &cmd, we can
4183 * always use 32bit context regardless of the architecture
4184 */
4185 for (i = 0; i < max_cmd; i++) {
4186
4187 cmd = instance->cmd_list[i];
4188
4189 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4190 GFP_KERNEL, &cmd->frame_phys_addr);
4191
4192 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4193 GFP_KERNEL, &cmd->sense_phys_addr);
4194
4195 /*
4196 * megasas_teardown_frame_pool() takes care of freeing
4197 * whatever has been allocated
4198 */
4199 if (!cmd->frame || !cmd->sense) {
4200 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4201 megasas_teardown_frame_pool(instance);
4202 return -ENOMEM;
4203 }
4204
4205 cmd->frame->io.context = cpu_to_le32(cmd->index);
4206 cmd->frame->io.pad_0 = 0;
4207 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4208 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4209 }
4210
4211 return 0;
4212 }
4213
4214 /**
4215 * megasas_free_cmds - Free all the cmds in the free cmd pool
4216 * @instance: Adapter soft state
4217 */
4218 void megasas_free_cmds(struct megasas_instance *instance)
4219 {
4220 int i;
4221
4222 /* First free the MFI frame pool */
4223 megasas_teardown_frame_pool(instance);
4224
4225 /* Free all the commands in the cmd_list */
4226 for (i = 0; i < instance->max_mfi_cmds; i++)
4227
4228 kfree(instance->cmd_list[i]);
4229
4230 /* Free the cmd_list buffer itself */
4231 kfree(instance->cmd_list);
4232 instance->cmd_list = NULL;
4233
4234 INIT_LIST_HEAD(&instance->cmd_pool);
4235 }
4236
4237 /**
4238 * megasas_alloc_cmds - Allocates the command packets
4239 * @instance: Adapter soft state
4240 *
4241 * Each command that is issued to the FW, whether IO commands from the OS or
4242 * internal commands like IOCTLs, are wrapped in local data structure called
4243 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4244 * the FW.
4245 *
4246 * Each frame has a 32-bit field called context (tag). This context is used
4247 * to get back the megasas_cmd from the frame when a frame gets completed in
4248 * the ISR. Typically the address of the megasas_cmd itself would be used as
4249 * the context. But we wanted to keep the differences between 32 and 64 bit
4250 * systems to the mininum. We always use 32 bit integers for the context. In
4251 * this driver, the 32 bit values are the indices into an array cmd_list.
4252 * This array is used only to look up the megasas_cmd given the context. The
4253 * free commands themselves are maintained in a linked list called cmd_pool.
4254 */
4255 int megasas_alloc_cmds(struct megasas_instance *instance)
4256 {
4257 int i;
4258 int j;
4259 u16 max_cmd;
4260 struct megasas_cmd *cmd;
4261
4262 max_cmd = instance->max_mfi_cmds;
4263
4264 /*
4265 * instance->cmd_list is an array of struct megasas_cmd pointers.
4266 * Allocate the dynamic array first and then allocate individual
4267 * commands.
4268 */
4269 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4270
4271 if (!instance->cmd_list) {
4272 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4273 return -ENOMEM;
4274 }
4275
4276 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4277
4278 for (i = 0; i < max_cmd; i++) {
4279 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4280 GFP_KERNEL);
4281
4282 if (!instance->cmd_list[i]) {
4283
4284 for (j = 0; j < i; j++)
4285 kfree(instance->cmd_list[j]);
4286
4287 kfree(instance->cmd_list);
4288 instance->cmd_list = NULL;
4289
4290 return -ENOMEM;
4291 }
4292 }
4293
4294 for (i = 0; i < max_cmd; i++) {
4295 cmd = instance->cmd_list[i];
4296 memset(cmd, 0, sizeof(struct megasas_cmd));
4297 cmd->index = i;
4298 cmd->scmd = NULL;
4299 cmd->instance = instance;
4300
4301 list_add_tail(&cmd->list, &instance->cmd_pool);
4302 }
4303
4304 /*
4305 * Create a frame pool and assign one frame to each cmd
4306 */
4307 if (megasas_create_frame_pool(instance)) {
4308 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4309 megasas_free_cmds(instance);
4310 return -ENOMEM;
4311 }
4312
4313 return 0;
4314 }
4315
4316 /*
4317 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4318 * @instance: Adapter soft state
4319 *
4320 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4321 * or FW is not under OCR.
4322 */
4323 inline int
4324 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4325
4326 if (instance->adapter_type == MFI_SERIES)
4327 return KILL_ADAPTER;
4328 else if (instance->unload ||
4329 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4330 return IGNORE_TIMEOUT;
4331 else
4332 return INITIATE_OCR;
4333 }
4334
4335 static void
4336 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4337 {
4338 int ret;
4339 struct megasas_cmd *cmd;
4340 struct megasas_dcmd_frame *dcmd;
4341
4342 struct MR_PRIV_DEVICE *mr_device_priv_data;
4343 u16 device_id = 0;
4344
4345 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4346 cmd = megasas_get_cmd(instance);
4347
4348 if (!cmd) {
4349 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4350 return;
4351 }
4352
4353 dcmd = &cmd->frame->dcmd;
4354
4355 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4356 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4357
4358 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4359 dcmd->cmd = MFI_CMD_DCMD;
4360 dcmd->cmd_status = 0xFF;
4361 dcmd->sge_count = 1;
4362 dcmd->flags = MFI_FRAME_DIR_READ;
4363 dcmd->timeout = 0;
4364 dcmd->pad_0 = 0;
4365 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4366 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4367
4368 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4369 sizeof(struct MR_PD_INFO));
4370
4371 if ((instance->adapter_type != MFI_SERIES) &&
4372 !instance->mask_interrupts)
4373 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4374 else
4375 ret = megasas_issue_polled(instance, cmd);
4376
4377 switch (ret) {
4378 case DCMD_SUCCESS:
4379 mr_device_priv_data = sdev->hostdata;
4380 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4381 mr_device_priv_data->interface_type =
4382 instance->pd_info->state.ddf.pdType.intf;
4383 break;
4384
4385 case DCMD_TIMEOUT:
4386
4387 switch (dcmd_timeout_ocr_possible(instance)) {
4388 case INITIATE_OCR:
4389 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4390 megasas_reset_fusion(instance->host,
4391 MFI_IO_TIMEOUT_OCR);
4392 break;
4393 case KILL_ADAPTER:
4394 megaraid_sas_kill_hba(instance);
4395 break;
4396 case IGNORE_TIMEOUT:
4397 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4398 __func__, __LINE__);
4399 break;
4400 }
4401
4402 break;
4403 }
4404
4405 if (ret != DCMD_TIMEOUT)
4406 megasas_return_cmd(instance, cmd);
4407
4408 return;
4409 }
4410 /*
4411 * megasas_get_pd_list_info - Returns FW's pd_list structure
4412 * @instance: Adapter soft state
4413 * @pd_list: pd_list structure
4414 *
4415 * Issues an internal command (DCMD) to get the FW's controller PD
4416 * list structure. This information is mainly used to find out SYSTEM
4417 * supported by the FW.
4418 */
4419 static int
4420 megasas_get_pd_list(struct megasas_instance *instance)
4421 {
4422 int ret = 0, pd_index = 0;
4423 struct megasas_cmd *cmd;
4424 struct megasas_dcmd_frame *dcmd;
4425 struct MR_PD_LIST *ci;
4426 struct MR_PD_ADDRESS *pd_addr;
4427 dma_addr_t ci_h = 0;
4428
4429 if (instance->pd_list_not_supported) {
4430 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4431 "not supported by firmware\n");
4432 return ret;
4433 }
4434
4435 ci = instance->pd_list_buf;
4436 ci_h = instance->pd_list_buf_h;
4437
4438 cmd = megasas_get_cmd(instance);
4439
4440 if (!cmd) {
4441 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4442 return -ENOMEM;
4443 }
4444
4445 dcmd = &cmd->frame->dcmd;
4446
4447 memset(ci, 0, sizeof(*ci));
4448 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4449
4450 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4451 dcmd->mbox.b[1] = 0;
4452 dcmd->cmd = MFI_CMD_DCMD;
4453 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4454 dcmd->sge_count = 1;
4455 dcmd->flags = MFI_FRAME_DIR_READ;
4456 dcmd->timeout = 0;
4457 dcmd->pad_0 = 0;
4458 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4459 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4460
4461 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4462 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4463
4464 if ((instance->adapter_type != MFI_SERIES) &&
4465 !instance->mask_interrupts)
4466 ret = megasas_issue_blocked_cmd(instance, cmd,
4467 MFI_IO_TIMEOUT_SECS);
4468 else
4469 ret = megasas_issue_polled(instance, cmd);
4470
4471 switch (ret) {
4472 case DCMD_FAILED:
4473 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4474 "failed/not supported by firmware\n");
4475
4476 if (instance->adapter_type != MFI_SERIES)
4477 megaraid_sas_kill_hba(instance);
4478 else
4479 instance->pd_list_not_supported = 1;
4480 break;
4481 case DCMD_TIMEOUT:
4482
4483 switch (dcmd_timeout_ocr_possible(instance)) {
4484 case INITIATE_OCR:
4485 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4486 /*
4487 * DCMD failed from AEN path.
4488 * AEN path already hold reset_mutex to avoid PCI access
4489 * while OCR is in progress.
4490 */
4491 mutex_unlock(&instance->reset_mutex);
4492 megasas_reset_fusion(instance->host,
4493 MFI_IO_TIMEOUT_OCR);
4494 mutex_lock(&instance->reset_mutex);
4495 break;
4496 case KILL_ADAPTER:
4497 megaraid_sas_kill_hba(instance);
4498 break;
4499 case IGNORE_TIMEOUT:
4500 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4501 __func__, __LINE__);
4502 break;
4503 }
4504
4505 break;
4506
4507 case DCMD_SUCCESS:
4508 pd_addr = ci->addr;
4509 if (megasas_dbg_lvl & LD_PD_DEBUG)
4510 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4511 __func__, le32_to_cpu(ci->count));
4512
4513 if ((le32_to_cpu(ci->count) >
4514 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4515 break;
4516
4517 memset(instance->local_pd_list, 0,
4518 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4519
4520 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4521 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4522 le16_to_cpu(pd_addr->deviceId);
4523 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4524 pd_addr->scsiDevType;
4525 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4526 MR_PD_STATE_SYSTEM;
4527 if (megasas_dbg_lvl & LD_PD_DEBUG)
4528 dev_info(&instance->pdev->dev,
4529 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4530 pd_index, le16_to_cpu(pd_addr->deviceId),
4531 pd_addr->scsiDevType);
4532 pd_addr++;
4533 }
4534
4535 memcpy(instance->pd_list, instance->local_pd_list,
4536 sizeof(instance->pd_list));
4537 break;
4538
4539 }
4540
4541 if (ret != DCMD_TIMEOUT)
4542 megasas_return_cmd(instance, cmd);
4543
4544 return ret;
4545 }
4546
4547 /*
4548 * megasas_get_ld_list_info - Returns FW's ld_list structure
4549 * @instance: Adapter soft state
4550 * @ld_list: ld_list structure
4551 *
4552 * Issues an internal command (DCMD) to get the FW's controller PD
4553 * list structure. This information is mainly used to find out SYSTEM
4554 * supported by the FW.
4555 */
4556 static int
4557 megasas_get_ld_list(struct megasas_instance *instance)
4558 {
4559 int ret = 0, ld_index = 0, ids = 0;
4560 struct megasas_cmd *cmd;
4561 struct megasas_dcmd_frame *dcmd;
4562 struct MR_LD_LIST *ci;
4563 dma_addr_t ci_h = 0;
4564 u32 ld_count;
4565
4566 ci = instance->ld_list_buf;
4567 ci_h = instance->ld_list_buf_h;
4568
4569 cmd = megasas_get_cmd(instance);
4570
4571 if (!cmd) {
4572 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4573 return -ENOMEM;
4574 }
4575
4576 dcmd = &cmd->frame->dcmd;
4577
4578 memset(ci, 0, sizeof(*ci));
4579 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4580
4581 if (instance->supportmax256vd)
4582 dcmd->mbox.b[0] = 1;
4583 dcmd->cmd = MFI_CMD_DCMD;
4584 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4585 dcmd->sge_count = 1;
4586 dcmd->flags = MFI_FRAME_DIR_READ;
4587 dcmd->timeout = 0;
4588 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4589 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4590 dcmd->pad_0 = 0;
4591
4592 megasas_set_dma_settings(instance, dcmd, ci_h,
4593 sizeof(struct MR_LD_LIST));
4594
4595 if ((instance->adapter_type != MFI_SERIES) &&
4596 !instance->mask_interrupts)
4597 ret = megasas_issue_blocked_cmd(instance, cmd,
4598 MFI_IO_TIMEOUT_SECS);
4599 else
4600 ret = megasas_issue_polled(instance, cmd);
4601
4602 ld_count = le32_to_cpu(ci->ldCount);
4603
4604 switch (ret) {
4605 case DCMD_FAILED:
4606 megaraid_sas_kill_hba(instance);
4607 break;
4608 case DCMD_TIMEOUT:
4609
4610 switch (dcmd_timeout_ocr_possible(instance)) {
4611 case INITIATE_OCR:
4612 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4613 /*
4614 * DCMD failed from AEN path.
4615 * AEN path already hold reset_mutex to avoid PCI access
4616 * while OCR is in progress.
4617 */
4618 mutex_unlock(&instance->reset_mutex);
4619 megasas_reset_fusion(instance->host,
4620 MFI_IO_TIMEOUT_OCR);
4621 mutex_lock(&instance->reset_mutex);
4622 break;
4623 case KILL_ADAPTER:
4624 megaraid_sas_kill_hba(instance);
4625 break;
4626 case IGNORE_TIMEOUT:
4627 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4628 __func__, __LINE__);
4629 break;
4630 }
4631
4632 break;
4633
4634 case DCMD_SUCCESS:
4635 if (megasas_dbg_lvl & LD_PD_DEBUG)
4636 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4637 __func__, ld_count);
4638
4639 if (ld_count > instance->fw_supported_vd_count)
4640 break;
4641
4642 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4643
4644 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4645 if (ci->ldList[ld_index].state != 0) {
4646 ids = ci->ldList[ld_index].ref.targetId;
4647 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4648 if (megasas_dbg_lvl & LD_PD_DEBUG)
4649 dev_info(&instance->pdev->dev,
4650 "LD%d: targetID: 0x%03x\n",
4651 ld_index, ids);
4652 }
4653 }
4654
4655 break;
4656 }
4657
4658 if (ret != DCMD_TIMEOUT)
4659 megasas_return_cmd(instance, cmd);
4660
4661 return ret;
4662 }
4663
4664 /**
4665 * megasas_ld_list_query - Returns FW's ld_list structure
4666 * @instance: Adapter soft state
4667 * @ld_list: ld_list structure
4668 *
4669 * Issues an internal command (DCMD) to get the FW's controller PD
4670 * list structure. This information is mainly used to find out SYSTEM
4671 * supported by the FW.
4672 */
4673 static int
4674 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4675 {
4676 int ret = 0, ld_index = 0, ids = 0;
4677 struct megasas_cmd *cmd;
4678 struct megasas_dcmd_frame *dcmd;
4679 struct MR_LD_TARGETID_LIST *ci;
4680 dma_addr_t ci_h = 0;
4681 u32 tgtid_count;
4682
4683 ci = instance->ld_targetid_list_buf;
4684 ci_h = instance->ld_targetid_list_buf_h;
4685
4686 cmd = megasas_get_cmd(instance);
4687
4688 if (!cmd) {
4689 dev_warn(&instance->pdev->dev,
4690 "megasas_ld_list_query: Failed to get cmd\n");
4691 return -ENOMEM;
4692 }
4693
4694 dcmd = &cmd->frame->dcmd;
4695
4696 memset(ci, 0, sizeof(*ci));
4697 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4698
4699 dcmd->mbox.b[0] = query_type;
4700 if (instance->supportmax256vd)
4701 dcmd->mbox.b[2] = 1;
4702
4703 dcmd->cmd = MFI_CMD_DCMD;
4704 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4705 dcmd->sge_count = 1;
4706 dcmd->flags = MFI_FRAME_DIR_READ;
4707 dcmd->timeout = 0;
4708 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4709 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4710 dcmd->pad_0 = 0;
4711
4712 megasas_set_dma_settings(instance, dcmd, ci_h,
4713 sizeof(struct MR_LD_TARGETID_LIST));
4714
4715 if ((instance->adapter_type != MFI_SERIES) &&
4716 !instance->mask_interrupts)
4717 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4718 else
4719 ret = megasas_issue_polled(instance, cmd);
4720
4721 switch (ret) {
4722 case DCMD_FAILED:
4723 dev_info(&instance->pdev->dev,
4724 "DCMD not supported by firmware - %s %d\n",
4725 __func__, __LINE__);
4726 ret = megasas_get_ld_list(instance);
4727 break;
4728 case DCMD_TIMEOUT:
4729 switch (dcmd_timeout_ocr_possible(instance)) {
4730 case INITIATE_OCR:
4731 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4732 /*
4733 * DCMD failed from AEN path.
4734 * AEN path already hold reset_mutex to avoid PCI access
4735 * while OCR is in progress.
4736 */
4737 mutex_unlock(&instance->reset_mutex);
4738 megasas_reset_fusion(instance->host,
4739 MFI_IO_TIMEOUT_OCR);
4740 mutex_lock(&instance->reset_mutex);
4741 break;
4742 case KILL_ADAPTER:
4743 megaraid_sas_kill_hba(instance);
4744 break;
4745 case IGNORE_TIMEOUT:
4746 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4747 __func__, __LINE__);
4748 break;
4749 }
4750
4751 break;
4752 case DCMD_SUCCESS:
4753 tgtid_count = le32_to_cpu(ci->count);
4754
4755 if (megasas_dbg_lvl & LD_PD_DEBUG)
4756 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4757 __func__, tgtid_count);
4758
4759 if ((tgtid_count > (instance->fw_supported_vd_count)))
4760 break;
4761
4762 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4763 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4764 ids = ci->targetId[ld_index];
4765 instance->ld_ids[ids] = ci->targetId[ld_index];
4766 if (megasas_dbg_lvl & LD_PD_DEBUG)
4767 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4768 ld_index, ci->targetId[ld_index]);
4769 }
4770
4771 break;
4772 }
4773
4774 if (ret != DCMD_TIMEOUT)
4775 megasas_return_cmd(instance, cmd);
4776
4777 return ret;
4778 }
4779
4780 /**
4781 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4782 * dcmd.mbox - reserved
4783 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4784 * Desc: This DCMD will return the combined device list
4785 * Status: MFI_STAT_OK - List returned successfully
4786 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4787 * disabled
4788 * @instance: Adapter soft state
4789 * @is_probe: Driver probe check
4790 * Return: 0 if DCMD succeeded
4791 * non-zero if failed
4792 */
4793 static int
4794 megasas_host_device_list_query(struct megasas_instance *instance,
4795 bool is_probe)
4796 {
4797 int ret, i, target_id;
4798 struct megasas_cmd *cmd;
4799 struct megasas_dcmd_frame *dcmd;
4800 struct MR_HOST_DEVICE_LIST *ci;
4801 u32 count;
4802 dma_addr_t ci_h;
4803
4804 ci = instance->host_device_list_buf;
4805 ci_h = instance->host_device_list_buf_h;
4806
4807 cmd = megasas_get_cmd(instance);
4808
4809 if (!cmd) {
4810 dev_warn(&instance->pdev->dev,
4811 "%s: failed to get cmd\n",
4812 __func__);
4813 return -ENOMEM;
4814 }
4815
4816 dcmd = &cmd->frame->dcmd;
4817
4818 memset(ci, 0, sizeof(*ci));
4819 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4820
4821 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4822 dcmd->cmd = MFI_CMD_DCMD;
4823 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4824 dcmd->sge_count = 1;
4825 dcmd->flags = MFI_FRAME_DIR_READ;
4826 dcmd->timeout = 0;
4827 dcmd->pad_0 = 0;
4828 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4829 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4830
4831 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4832
4833 if (!instance->mask_interrupts) {
4834 ret = megasas_issue_blocked_cmd(instance, cmd,
4835 MFI_IO_TIMEOUT_SECS);
4836 } else {
4837 ret = megasas_issue_polled(instance, cmd);
4838 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4839 }
4840
4841 switch (ret) {
4842 case DCMD_SUCCESS:
4843 /* Fill the internal pd_list and ld_ids array based on
4844 * targetIds returned by FW
4845 */
4846 count = le32_to_cpu(ci->count);
4847
4848 if (megasas_dbg_lvl & LD_PD_DEBUG)
4849 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
4850 __func__, count);
4851
4852 memset(instance->local_pd_list, 0,
4853 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4854 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4855 for (i = 0; i < count; i++) {
4856 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4857 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4858 instance->local_pd_list[target_id].tid = target_id;
4859 instance->local_pd_list[target_id].driveType =
4860 ci->host_device_list[i].scsi_type;
4861 instance->local_pd_list[target_id].driveState =
4862 MR_PD_STATE_SYSTEM;
4863 if (megasas_dbg_lvl & LD_PD_DEBUG)
4864 dev_info(&instance->pdev->dev,
4865 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
4866 i, target_id, ci->host_device_list[i].scsi_type);
4867 } else {
4868 instance->ld_ids[target_id] = target_id;
4869 if (megasas_dbg_lvl & LD_PD_DEBUG)
4870 dev_info(&instance->pdev->dev,
4871 "Device %d: LD targetID: 0x%03x\n",
4872 i, target_id);
4873 }
4874 }
4875
4876 memcpy(instance->pd_list, instance->local_pd_list,
4877 sizeof(instance->pd_list));
4878 break;
4879
4880 case DCMD_TIMEOUT:
4881 switch (dcmd_timeout_ocr_possible(instance)) {
4882 case INITIATE_OCR:
4883 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4884 megasas_reset_fusion(instance->host,
4885 MFI_IO_TIMEOUT_OCR);
4886 break;
4887 case KILL_ADAPTER:
4888 megaraid_sas_kill_hba(instance);
4889 break;
4890 case IGNORE_TIMEOUT:
4891 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4892 __func__, __LINE__);
4893 break;
4894 }
4895 break;
4896 case DCMD_FAILED:
4897 dev_err(&instance->pdev->dev,
4898 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4899 __func__);
4900 break;
4901 }
4902
4903 if (ret != DCMD_TIMEOUT)
4904 megasas_return_cmd(instance, cmd);
4905
4906 return ret;
4907 }
4908
4909 /*
4910 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4911 * instance : Controller's instance
4912 */
4913 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4914 {
4915 struct fusion_context *fusion;
4916 u32 ventura_map_sz = 0;
4917
4918 fusion = instance->ctrl_context;
4919 /* For MFI based controllers return dummy success */
4920 if (!fusion)
4921 return;
4922
4923 instance->supportmax256vd =
4924 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
4925 /* Below is additional check to address future FW enhancement */
4926 if (instance->ctrl_info_buf->max_lds > 64)
4927 instance->supportmax256vd = 1;
4928
4929 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4930 * MEGASAS_MAX_DEV_PER_CHANNEL;
4931 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4932 * MEGASAS_MAX_DEV_PER_CHANNEL;
4933 if (instance->supportmax256vd) {
4934 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4935 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4936 } else {
4937 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4938 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4939 }
4940
4941 dev_info(&instance->pdev->dev,
4942 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4943 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4944 instance->ctrl_info_buf->max_lds);
4945
4946 if (instance->max_raid_mapsize) {
4947 ventura_map_sz = instance->max_raid_mapsize *
4948 MR_MIN_MAP_SIZE; /* 64k */
4949 fusion->current_map_sz = ventura_map_sz;
4950 fusion->max_map_sz = ventura_map_sz;
4951 } else {
4952 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4953 (sizeof(struct MR_LD_SPAN_MAP) *
4954 (instance->fw_supported_vd_count - 1));
4955 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4956
4957 fusion->max_map_sz =
4958 max(fusion->old_map_sz, fusion->new_map_sz);
4959
4960 if (instance->supportmax256vd)
4961 fusion->current_map_sz = fusion->new_map_sz;
4962 else
4963 fusion->current_map_sz = fusion->old_map_sz;
4964 }
4965 /* irrespective of FW raid maps, driver raid map is constant */
4966 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
4967 }
4968
4969 /*
4970 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4971 * dcmd.hdr.length - number of bytes to read
4972 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4973 * Desc: Fill in snapdump properties
4974 * Status: MFI_STAT_OK- Command successful
4975 */
4976 void megasas_get_snapdump_properties(struct megasas_instance *instance)
4977 {
4978 int ret = 0;
4979 struct megasas_cmd *cmd;
4980 struct megasas_dcmd_frame *dcmd;
4981 struct MR_SNAPDUMP_PROPERTIES *ci;
4982 dma_addr_t ci_h = 0;
4983
4984 ci = instance->snapdump_prop;
4985 ci_h = instance->snapdump_prop_h;
4986
4987 if (!ci)
4988 return;
4989
4990 cmd = megasas_get_cmd(instance);
4991
4992 if (!cmd) {
4993 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4994 return;
4995 }
4996
4997 dcmd = &cmd->frame->dcmd;
4998
4999 memset(ci, 0, sizeof(*ci));
5000 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5001
5002 dcmd->cmd = MFI_CMD_DCMD;
5003 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5004 dcmd->sge_count = 1;
5005 dcmd->flags = MFI_FRAME_DIR_READ;
5006 dcmd->timeout = 0;
5007 dcmd->pad_0 = 0;
5008 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5009 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5010
5011 megasas_set_dma_settings(instance, dcmd, ci_h,
5012 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5013
5014 if (!instance->mask_interrupts) {
5015 ret = megasas_issue_blocked_cmd(instance, cmd,
5016 MFI_IO_TIMEOUT_SECS);
5017 } else {
5018 ret = megasas_issue_polled(instance, cmd);
5019 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5020 }
5021
5022 switch (ret) {
5023 case DCMD_SUCCESS:
5024 instance->snapdump_wait_time =
5025 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5026 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5027 break;
5028
5029 case DCMD_TIMEOUT:
5030 switch (dcmd_timeout_ocr_possible(instance)) {
5031 case INITIATE_OCR:
5032 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5033 megasas_reset_fusion(instance->host,
5034 MFI_IO_TIMEOUT_OCR);
5035 break;
5036 case KILL_ADAPTER:
5037 megaraid_sas_kill_hba(instance);
5038 break;
5039 case IGNORE_TIMEOUT:
5040 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5041 __func__, __LINE__);
5042 break;
5043 }
5044 }
5045
5046 if (ret != DCMD_TIMEOUT)
5047 megasas_return_cmd(instance, cmd);
5048 }
5049
5050 /**
5051 * megasas_get_controller_info - Returns FW's controller structure
5052 * @instance: Adapter soft state
5053 *
5054 * Issues an internal command (DCMD) to get the FW's controller structure.
5055 * This information is mainly used to find out the maximum IO transfer per
5056 * command supported by the FW.
5057 */
5058 int
5059 megasas_get_ctrl_info(struct megasas_instance *instance)
5060 {
5061 int ret = 0;
5062 struct megasas_cmd *cmd;
5063 struct megasas_dcmd_frame *dcmd;
5064 struct megasas_ctrl_info *ci;
5065 dma_addr_t ci_h = 0;
5066
5067 ci = instance->ctrl_info_buf;
5068 ci_h = instance->ctrl_info_buf_h;
5069
5070 cmd = megasas_get_cmd(instance);
5071
5072 if (!cmd) {
5073 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5074 return -ENOMEM;
5075 }
5076
5077 dcmd = &cmd->frame->dcmd;
5078
5079 memset(ci, 0, sizeof(*ci));
5080 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5081
5082 dcmd->cmd = MFI_CMD_DCMD;
5083 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5084 dcmd->sge_count = 1;
5085 dcmd->flags = MFI_FRAME_DIR_READ;
5086 dcmd->timeout = 0;
5087 dcmd->pad_0 = 0;
5088 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5089 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5090 dcmd->mbox.b[0] = 1;
5091
5092 megasas_set_dma_settings(instance, dcmd, ci_h,
5093 sizeof(struct megasas_ctrl_info));
5094
5095 if ((instance->adapter_type != MFI_SERIES) &&
5096 !instance->mask_interrupts) {
5097 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5098 } else {
5099 ret = megasas_issue_polled(instance, cmd);
5100 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5101 }
5102
5103 switch (ret) {
5104 case DCMD_SUCCESS:
5105 /* Save required controller information in
5106 * CPU endianness format.
5107 */
5108 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5109 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5110 le32_to_cpus((u32 *)&ci->adapterOperations2);
5111 le32_to_cpus((u32 *)&ci->adapterOperations3);
5112 le16_to_cpus((u16 *)&ci->adapter_operations4);
5113
5114 /* Update the latest Ext VD info.
5115 * From Init path, store current firmware details.
5116 * From OCR path, detect any firmware properties changes.
5117 * in case of Firmware upgrade without system reboot.
5118 */
5119 megasas_update_ext_vd_details(instance);
5120 instance->use_seqnum_jbod_fp =
5121 ci->adapterOperations3.useSeqNumJbodFP;
5122 instance->support_morethan256jbod =
5123 ci->adapter_operations4.support_pd_map_target_id;
5124 instance->support_nvme_passthru =
5125 ci->adapter_operations4.support_nvme_passthru;
5126 instance->task_abort_tmo = ci->TaskAbortTO;
5127 instance->max_reset_tmo = ci->MaxResetTO;
5128
5129 /*Check whether controller is iMR or MR */
5130 instance->is_imr = (ci->memory_size ? 0 : 1);
5131
5132 instance->snapdump_wait_time =
5133 (ci->properties.on_off_properties2.enable_snap_dump ?
5134 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5135
5136 instance->enable_fw_dev_list =
5137 ci->properties.on_off_properties2.enable_fw_dev_list;
5138
5139 dev_info(&instance->pdev->dev,
5140 "controller type\t: %s(%dMB)\n",
5141 instance->is_imr ? "iMR" : "MR",
5142 le16_to_cpu(ci->memory_size));
5143
5144 instance->disableOnlineCtrlReset =
5145 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5146 instance->secure_jbod_support =
5147 ci->adapterOperations3.supportSecurityonJBOD;
5148 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5149 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5150 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5151 instance->secure_jbod_support ? "Yes" : "No");
5152 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5153 instance->support_nvme_passthru ? "Yes" : "No");
5154 dev_info(&instance->pdev->dev,
5155 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5156 instance->task_abort_tmo, instance->max_reset_tmo);
5157
5158 break;
5159
5160 case DCMD_TIMEOUT:
5161 switch (dcmd_timeout_ocr_possible(instance)) {
5162 case INITIATE_OCR:
5163 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5164 megasas_reset_fusion(instance->host,
5165 MFI_IO_TIMEOUT_OCR);
5166 break;
5167 case KILL_ADAPTER:
5168 megaraid_sas_kill_hba(instance);
5169 break;
5170 case IGNORE_TIMEOUT:
5171 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5172 __func__, __LINE__);
5173 break;
5174 }
5175 break;
5176 case DCMD_FAILED:
5177 megaraid_sas_kill_hba(instance);
5178 break;
5179
5180 }
5181
5182 if (ret != DCMD_TIMEOUT)
5183 megasas_return_cmd(instance, cmd);
5184
5185 return ret;
5186 }
5187
5188 /*
5189 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5190 * to firmware
5191 *
5192 * @instance: Adapter soft state
5193 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5194 MR_CRASH_BUF_TURN_OFF = 0
5195 MR_CRASH_BUF_TURN_ON = 1
5196 * @return 0 on success non-zero on failure.
5197 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5198 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5199 * that driver supports crash dump feature. This DCMD will be sent only if
5200 * crash dump feature is supported by the FW.
5201 *
5202 */
5203 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5204 u8 crash_buf_state)
5205 {
5206 int ret = 0;
5207 struct megasas_cmd *cmd;
5208 struct megasas_dcmd_frame *dcmd;
5209
5210 cmd = megasas_get_cmd(instance);
5211
5212 if (!cmd) {
5213 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5214 return -ENOMEM;
5215 }
5216
5217
5218 dcmd = &cmd->frame->dcmd;
5219
5220 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5221 dcmd->mbox.b[0] = crash_buf_state;
5222 dcmd->cmd = MFI_CMD_DCMD;
5223 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5224 dcmd->sge_count = 1;
5225 dcmd->flags = MFI_FRAME_DIR_NONE;
5226 dcmd->timeout = 0;
5227 dcmd->pad_0 = 0;
5228 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5229 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5230
5231 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5232 CRASH_DMA_BUF_SIZE);
5233
5234 if ((instance->adapter_type != MFI_SERIES) &&
5235 !instance->mask_interrupts)
5236 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5237 else
5238 ret = megasas_issue_polled(instance, cmd);
5239
5240 if (ret == DCMD_TIMEOUT) {
5241 switch (dcmd_timeout_ocr_possible(instance)) {
5242 case INITIATE_OCR:
5243 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5244 megasas_reset_fusion(instance->host,
5245 MFI_IO_TIMEOUT_OCR);
5246 break;
5247 case KILL_ADAPTER:
5248 megaraid_sas_kill_hba(instance);
5249 break;
5250 case IGNORE_TIMEOUT:
5251 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5252 __func__, __LINE__);
5253 break;
5254 }
5255 } else
5256 megasas_return_cmd(instance, cmd);
5257
5258 return ret;
5259 }
5260
5261 /**
5262 * megasas_issue_init_mfi - Initializes the FW
5263 * @instance: Adapter soft state
5264 *
5265 * Issues the INIT MFI cmd
5266 */
5267 static int
5268 megasas_issue_init_mfi(struct megasas_instance *instance)
5269 {
5270 __le32 context;
5271 struct megasas_cmd *cmd;
5272 struct megasas_init_frame *init_frame;
5273 struct megasas_init_queue_info *initq_info;
5274 dma_addr_t init_frame_h;
5275 dma_addr_t initq_info_h;
5276
5277 /*
5278 * Prepare a init frame. Note the init frame points to queue info
5279 * structure. Each frame has SGL allocated after first 64 bytes. For
5280 * this frame - since we don't need any SGL - we use SGL's space as
5281 * queue info structure
5282 *
5283 * We will not get a NULL command below. We just created the pool.
5284 */
5285 cmd = megasas_get_cmd(instance);
5286
5287 init_frame = (struct megasas_init_frame *)cmd->frame;
5288 initq_info = (struct megasas_init_queue_info *)
5289 ((unsigned long)init_frame + 64);
5290
5291 init_frame_h = cmd->frame_phys_addr;
5292 initq_info_h = init_frame_h + 64;
5293
5294 context = init_frame->context;
5295 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5296 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5297 init_frame->context = context;
5298
5299 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5300 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5301
5302 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5303 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5304
5305 init_frame->cmd = MFI_CMD_INIT;
5306 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5307 init_frame->queue_info_new_phys_addr_lo =
5308 cpu_to_le32(lower_32_bits(initq_info_h));
5309 init_frame->queue_info_new_phys_addr_hi =
5310 cpu_to_le32(upper_32_bits(initq_info_h));
5311
5312 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5313
5314 /*
5315 * disable the intr before firing the init frame to FW
5316 */
5317 instance->instancet->disable_intr(instance);
5318
5319 /*
5320 * Issue the init frame in polled mode
5321 */
5322
5323 if (megasas_issue_polled(instance, cmd)) {
5324 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5325 megasas_return_cmd(instance, cmd);
5326 goto fail_fw_init;
5327 }
5328
5329 megasas_return_cmd(instance, cmd);
5330
5331 return 0;
5332
5333 fail_fw_init:
5334 return -EINVAL;
5335 }
5336
5337 static u32
5338 megasas_init_adapter_mfi(struct megasas_instance *instance)
5339 {
5340 u32 context_sz;
5341 u32 reply_q_sz;
5342
5343 /*
5344 * Get various operational parameters from status register
5345 */
5346 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5347 /*
5348 * Reduce the max supported cmds by 1. This is to ensure that the
5349 * reply_q_sz (1 more than the max cmd that driver may send)
5350 * does not exceed max cmds that the FW can support
5351 */
5352 instance->max_fw_cmds = instance->max_fw_cmds-1;
5353 instance->max_mfi_cmds = instance->max_fw_cmds;
5354 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5355 0x10;
5356 /*
5357 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5358 * are reserved for IOCTL + driver's internal DCMDs.
5359 */
5360 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5361 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5362 instance->max_scsi_cmds = (instance->max_fw_cmds -
5363 MEGASAS_SKINNY_INT_CMDS);
5364 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5365 } else {
5366 instance->max_scsi_cmds = (instance->max_fw_cmds -
5367 MEGASAS_INT_CMDS);
5368 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5369 }
5370
5371 instance->cur_can_queue = instance->max_scsi_cmds;
5372 /*
5373 * Create a pool of commands
5374 */
5375 if (megasas_alloc_cmds(instance))
5376 goto fail_alloc_cmds;
5377
5378 /*
5379 * Allocate memory for reply queue. Length of reply queue should
5380 * be _one_ more than the maximum commands handled by the firmware.
5381 *
5382 * Note: When FW completes commands, it places corresponding contex
5383 * values in this circular reply queue. This circular queue is a fairly
5384 * typical producer-consumer queue. FW is the producer (of completed
5385 * commands) and the driver is the consumer.
5386 */
5387 context_sz = sizeof(u32);
5388 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5389
5390 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5391 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5392
5393 if (!instance->reply_queue) {
5394 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5395 goto fail_reply_queue;
5396 }
5397
5398 if (megasas_issue_init_mfi(instance))
5399 goto fail_fw_init;
5400
5401 if (megasas_get_ctrl_info(instance)) {
5402 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5403 "Fail from %s %d\n", instance->unique_id,
5404 __func__, __LINE__);
5405 goto fail_fw_init;
5406 }
5407
5408 instance->fw_support_ieee = 0;
5409 instance->fw_support_ieee =
5410 (instance->instancet->read_fw_status_reg(instance) &
5411 0x04000000);
5412
5413 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5414 instance->fw_support_ieee);
5415
5416 if (instance->fw_support_ieee)
5417 instance->flag_ieee = 1;
5418
5419 return 0;
5420
5421 fail_fw_init:
5422
5423 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5424 instance->reply_queue, instance->reply_queue_h);
5425 fail_reply_queue:
5426 megasas_free_cmds(instance);
5427
5428 fail_alloc_cmds:
5429 return 1;
5430 }
5431
5432 static
5433 void megasas_setup_irq_poll(struct megasas_instance *instance)
5434 {
5435 struct megasas_irq_context *irq_ctx;
5436 u32 count, i;
5437
5438 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5439
5440 /* Initialize IRQ poll */
5441 for (i = 0; i < count; i++) {
5442 irq_ctx = &instance->irq_context[i];
5443 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5444 irq_ctx->irq_poll_scheduled = false;
5445 irq_poll_init(&irq_ctx->irqpoll,
5446 instance->threshold_reply_count,
5447 megasas_irqpoll);
5448 }
5449 }
5450
5451 /*
5452 * megasas_setup_irqs_ioapic - register legacy interrupts.
5453 * @instance: Adapter soft state
5454 *
5455 * Do not enable interrupt, only setup ISRs.
5456 *
5457 * Return 0 on success.
5458 */
5459 static int
5460 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5461 {
5462 struct pci_dev *pdev;
5463
5464 pdev = instance->pdev;
5465 instance->irq_context[0].instance = instance;
5466 instance->irq_context[0].MSIxIndex = 0;
5467 if (request_irq(pci_irq_vector(pdev, 0),
5468 instance->instancet->service_isr, IRQF_SHARED,
5469 "megasas", &instance->irq_context[0])) {
5470 dev_err(&instance->pdev->dev,
5471 "Failed to register IRQ from %s %d\n",
5472 __func__, __LINE__);
5473 return -1;
5474 }
5475 return 0;
5476 }
5477
5478 /**
5479 * megasas_setup_irqs_msix - register MSI-x interrupts.
5480 * @instance: Adapter soft state
5481 * @is_probe: Driver probe check
5482 *
5483 * Do not enable interrupt, only setup ISRs.
5484 *
5485 * Return 0 on success.
5486 */
5487 static int
5488 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5489 {
5490 int i, j;
5491 struct pci_dev *pdev;
5492
5493 pdev = instance->pdev;
5494
5495 /* Try MSI-x */
5496 for (i = 0; i < instance->msix_vectors; i++) {
5497 instance->irq_context[i].instance = instance;
5498 instance->irq_context[i].MSIxIndex = i;
5499 if (request_irq(pci_irq_vector(pdev, i),
5500 instance->instancet->service_isr, 0, "megasas",
5501 &instance->irq_context[i])) {
5502 dev_err(&instance->pdev->dev,
5503 "Failed to register IRQ for vector %d.\n", i);
5504 for (j = 0; j < i; j++)
5505 free_irq(pci_irq_vector(pdev, j),
5506 &instance->irq_context[j]);
5507 /* Retry irq register for IO_APIC*/
5508 instance->msix_vectors = 0;
5509 instance->msix_load_balance = false;
5510 if (is_probe) {
5511 pci_free_irq_vectors(instance->pdev);
5512 return megasas_setup_irqs_ioapic(instance);
5513 } else {
5514 return -1;
5515 }
5516 }
5517 }
5518
5519 return 0;
5520 }
5521
5522 /*
5523 * megasas_destroy_irqs- unregister interrupts.
5524 * @instance: Adapter soft state
5525 * return: void
5526 */
5527 static void
5528 megasas_destroy_irqs(struct megasas_instance *instance) {
5529
5530 int i;
5531 int count;
5532 struct megasas_irq_context *irq_ctx;
5533
5534 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5535 if (instance->adapter_type != MFI_SERIES) {
5536 for (i = 0; i < count; i++) {
5537 irq_ctx = &instance->irq_context[i];
5538 irq_poll_disable(&irq_ctx->irqpoll);
5539 }
5540 }
5541
5542 if (instance->msix_vectors)
5543 for (i = 0; i < instance->msix_vectors; i++) {
5544 free_irq(pci_irq_vector(instance->pdev, i),
5545 &instance->irq_context[i]);
5546 }
5547 else
5548 free_irq(pci_irq_vector(instance->pdev, 0),
5549 &instance->irq_context[0]);
5550 }
5551
5552 /**
5553 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5554 * @instance: Adapter soft state
5555 * @is_probe: Driver probe check
5556 *
5557 * Return 0 on success.
5558 */
5559 void
5560 megasas_setup_jbod_map(struct megasas_instance *instance)
5561 {
5562 int i;
5563 struct fusion_context *fusion = instance->ctrl_context;
5564 u32 pd_seq_map_sz;
5565
5566 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5567 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5568
5569 if (reset_devices || !fusion ||
5570 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
5571 dev_info(&instance->pdev->dev,
5572 "Jbod map is not supported %s %d\n",
5573 __func__, __LINE__);
5574 instance->use_seqnum_jbod_fp = false;
5575 return;
5576 }
5577
5578 if (fusion->pd_seq_sync[0])
5579 goto skip_alloc;
5580
5581 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5582 fusion->pd_seq_sync[i] = dma_alloc_coherent
5583 (&instance->pdev->dev, pd_seq_map_sz,
5584 &fusion->pd_seq_phys[i], GFP_KERNEL);
5585 if (!fusion->pd_seq_sync[i]) {
5586 dev_err(&instance->pdev->dev,
5587 "Failed to allocate memory from %s %d\n",
5588 __func__, __LINE__);
5589 if (i == 1) {
5590 dma_free_coherent(&instance->pdev->dev,
5591 pd_seq_map_sz, fusion->pd_seq_sync[0],
5592 fusion->pd_seq_phys[0]);
5593 fusion->pd_seq_sync[0] = NULL;
5594 }
5595 instance->use_seqnum_jbod_fp = false;
5596 return;
5597 }
5598 }
5599
5600 skip_alloc:
5601 if (!megasas_sync_pd_seq_num(instance, false) &&
5602 !megasas_sync_pd_seq_num(instance, true))
5603 instance->use_seqnum_jbod_fp = true;
5604 else
5605 instance->use_seqnum_jbod_fp = false;
5606 }
5607
5608 static void megasas_setup_reply_map(struct megasas_instance *instance)
5609 {
5610 const struct cpumask *mask;
5611 unsigned int queue, cpu;
5612
5613 for (queue = 0; queue < instance->msix_vectors; queue++) {
5614 mask = pci_irq_get_affinity(instance->pdev, queue);
5615 if (!mask)
5616 goto fallback;
5617
5618 for_each_cpu(cpu, mask)
5619 instance->reply_map[cpu] = queue;
5620 }
5621 return;
5622
5623 fallback:
5624 for_each_possible_cpu(cpu)
5625 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5626 }
5627
5628 /**
5629 * megasas_get_device_list - Get the PD and LD device list from FW.
5630 * @instance: Adapter soft state
5631 * @return: Success or failure
5632 *
5633 * Issue DCMDs to Firmware to get the PD and LD list.
5634 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5635 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5636 */
5637 static
5638 int megasas_get_device_list(struct megasas_instance *instance)
5639 {
5640 memset(instance->pd_list, 0,
5641 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5642 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5643
5644 if (instance->enable_fw_dev_list) {
5645 if (megasas_host_device_list_query(instance, true))
5646 return FAILED;
5647 } else {
5648 if (megasas_get_pd_list(instance) < 0) {
5649 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5650 return FAILED;
5651 }
5652
5653 if (megasas_ld_list_query(instance,
5654 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5655 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5656 return FAILED;
5657 }
5658 }
5659
5660 return SUCCESS;
5661 }
5662 /**
5663 * megasas_init_fw - Initializes the FW
5664 * @instance: Adapter soft state
5665 *
5666 * This is the main function for initializing firmware
5667 */
5668
5669 static int megasas_init_fw(struct megasas_instance *instance)
5670 {
5671 u32 max_sectors_1;
5672 u32 max_sectors_2, tmp_sectors, msix_enable;
5673 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5674 resource_size_t base_addr;
5675 void *base_addr_phys;
5676 struct megasas_ctrl_info *ctrl_info = NULL;
5677 unsigned long bar_list;
5678 int i, j, loop;
5679 struct IOV_111 *iovPtr;
5680 struct fusion_context *fusion;
5681
5682 fusion = instance->ctrl_context;
5683
5684 /* Find first memory bar */
5685 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5686 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5687 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5688 "megasas: LSI")) {
5689 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5690 return -EBUSY;
5691 }
5692
5693 base_addr = pci_resource_start(instance->pdev, instance->bar);
5694 instance->reg_set = ioremap_nocache(base_addr, 8192);
5695
5696 if (!instance->reg_set) {
5697 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5698 goto fail_ioremap;
5699 }
5700
5701 base_addr_phys = &base_addr;
5702 dev_printk(KERN_DEBUG, &instance->pdev->dev,
5703 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
5704 instance->bar, base_addr_phys, instance->reg_set);
5705
5706 if (instance->adapter_type != MFI_SERIES)
5707 instance->instancet = &megasas_instance_template_fusion;
5708 else {
5709 switch (instance->pdev->device) {
5710 case PCI_DEVICE_ID_LSI_SAS1078R:
5711 case PCI_DEVICE_ID_LSI_SAS1078DE:
5712 instance->instancet = &megasas_instance_template_ppc;
5713 break;
5714 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5715 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5716 instance->instancet = &megasas_instance_template_gen2;
5717 break;
5718 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5719 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5720 instance->instancet = &megasas_instance_template_skinny;
5721 break;
5722 case PCI_DEVICE_ID_LSI_SAS1064R:
5723 case PCI_DEVICE_ID_DELL_PERC5:
5724 default:
5725 instance->instancet = &megasas_instance_template_xscale;
5726 instance->pd_list_not_supported = 1;
5727 break;
5728 }
5729 }
5730
5731 if (megasas_transition_to_ready(instance, 0)) {
5732 dev_info(&instance->pdev->dev,
5733 "Failed to transition controller to ready from %s!\n",
5734 __func__);
5735 if (instance->adapter_type != MFI_SERIES) {
5736 status_reg = instance->instancet->read_fw_status_reg(
5737 instance);
5738 if (status_reg & MFI_RESET_ADAPTER) {
5739 if (megasas_adp_reset_wait_for_ready
5740 (instance, true, 0) == FAILED)
5741 goto fail_ready_state;
5742 } else {
5743 goto fail_ready_state;
5744 }
5745 } else {
5746 atomic_set(&instance->fw_reset_no_pci_access, 1);
5747 instance->instancet->adp_reset
5748 (instance, instance->reg_set);
5749 atomic_set(&instance->fw_reset_no_pci_access, 0);
5750
5751 /*waiting for about 30 second before retry*/
5752 ssleep(30);
5753
5754 if (megasas_transition_to_ready(instance, 0))
5755 goto fail_ready_state;
5756 }
5757
5758 dev_info(&instance->pdev->dev,
5759 "FW restarted successfully from %s!\n",
5760 __func__);
5761 }
5762
5763 megasas_init_ctrl_params(instance);
5764
5765 if (megasas_set_dma_mask(instance))
5766 goto fail_ready_state;
5767
5768 if (megasas_alloc_ctrl_mem(instance))
5769 goto fail_alloc_dma_buf;
5770
5771 if (megasas_alloc_ctrl_dma_buffers(instance))
5772 goto fail_alloc_dma_buf;
5773
5774 fusion = instance->ctrl_context;
5775
5776 if (instance->adapter_type >= VENTURA_SERIES) {
5777 scratch_pad_2 =
5778 megasas_readl(instance,
5779 &instance->reg_set->outbound_scratch_pad_2);
5780 instance->max_raid_mapsize = ((scratch_pad_2 >>
5781 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5782 MR_MAX_RAID_MAP_SIZE_MASK);
5783 }
5784
5785 /* Check if MSI-X is supported while in ready state */
5786 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
5787 0x4000000) >> 0x1a;
5788 if (msix_enable && !msix_disable) {
5789 int irq_flags = PCI_IRQ_MSIX;
5790
5791 scratch_pad_1 = megasas_readl
5792 (instance, &instance->reg_set->outbound_scratch_pad_1);
5793 /* Check max MSI-X vectors */
5794 if (fusion) {
5795 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5796 /* Thunderbolt Series*/
5797 instance->msix_vectors = (scratch_pad_1
5798 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5799 } else {
5800 instance->msix_vectors = ((scratch_pad_1
5801 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5802 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5803
5804 /*
5805 * For Invader series, > 8 MSI-x vectors
5806 * supported by FW/HW implies combined
5807 * reply queue mode is enabled.
5808 * For Ventura series, > 16 MSI-x vectors
5809 * supported by FW/HW implies combined
5810 * reply queue mode is enabled.
5811 */
5812 switch (instance->adapter_type) {
5813 case INVADER_SERIES:
5814 if (instance->msix_vectors > 8)
5815 instance->msix_combined = true;
5816 break;
5817 case AERO_SERIES:
5818 case VENTURA_SERIES:
5819 if (instance->msix_vectors > 16)
5820 instance->msix_combined = true;
5821 break;
5822 }
5823
5824 if (rdpq_enable)
5825 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
5826 1 : 0;
5827
5828 if (!instance->msix_combined) {
5829 instance->msix_load_balance = true;
5830 instance->smp_affinity_enable = false;
5831 }
5832
5833 /* Save 1-15 reply post index address to local memory
5834 * Index 0 is already saved from reg offset
5835 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5836 */
5837 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5838 instance->reply_post_host_index_addr[loop] =
5839 (u32 __iomem *)
5840 ((u8 __iomem *)instance->reg_set +
5841 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5842 + (loop * 0x10));
5843 }
5844 }
5845
5846 dev_info(&instance->pdev->dev,
5847 "firmware supports msix\t: (%d)",
5848 instance->msix_vectors);
5849 if (msix_vectors)
5850 instance->msix_vectors = min(msix_vectors,
5851 instance->msix_vectors);
5852 } else /* MFI adapters */
5853 instance->msix_vectors = 1;
5854
5855 /* Don't bother allocating more MSI-X vectors than cpus */
5856 instance->msix_vectors = min(instance->msix_vectors,
5857 (unsigned int)num_online_cpus());
5858 if (instance->smp_affinity_enable)
5859 irq_flags |= PCI_IRQ_AFFINITY;
5860 i = pci_alloc_irq_vectors(instance->pdev, 1,
5861 instance->msix_vectors, irq_flags);
5862 if (i > 0) {
5863 instance->msix_vectors = i;
5864 } else {
5865 instance->msix_vectors = 0;
5866 instance->msix_load_balance = false;
5867 }
5868 }
5869 /*
5870 * MSI-X host index 0 is common for all adapter.
5871 * It is used for all MPT based Adapters.
5872 */
5873 if (instance->msix_combined) {
5874 instance->reply_post_host_index_addr[0] =
5875 (u32 *)((u8 *)instance->reg_set +
5876 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5877 } else {
5878 instance->reply_post_host_index_addr[0] =
5879 (u32 *)((u8 *)instance->reg_set +
5880 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5881 }
5882
5883 if (!instance->msix_vectors) {
5884 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5885 if (i < 0)
5886 goto fail_init_adapter;
5887 }
5888
5889 megasas_setup_reply_map(instance);
5890
5891 dev_info(&instance->pdev->dev,
5892 "current msix/online cpus\t: (%d/%d)\n",
5893 instance->msix_vectors, (unsigned int)num_online_cpus());
5894 dev_info(&instance->pdev->dev,
5895 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5896
5897 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5898 (unsigned long)instance);
5899
5900 /*
5901 * Below are default value for legacy Firmware.
5902 * non-fusion based controllers
5903 */
5904 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5905 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5906 /* Get operational params, sge flags, send init cmd to controller */
5907 if (instance->instancet->init_adapter(instance))
5908 goto fail_init_adapter;
5909
5910 if (instance->adapter_type >= VENTURA_SERIES) {
5911 scratch_pad_3 =
5912 megasas_readl(instance,
5913 &instance->reg_set->outbound_scratch_pad_3);
5914 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
5915 MR_DEFAULT_NVME_PAGE_SHIFT)
5916 instance->nvme_page_size =
5917 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
5918
5919 dev_info(&instance->pdev->dev,
5920 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5921 }
5922
5923 if (instance->msix_vectors ?
5924 megasas_setup_irqs_msix(instance, 1) :
5925 megasas_setup_irqs_ioapic(instance))
5926 goto fail_init_adapter;
5927
5928 if (instance->adapter_type != MFI_SERIES)
5929 megasas_setup_irq_poll(instance);
5930
5931 instance->instancet->enable_intr(instance);
5932
5933 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5934
5935 megasas_setup_jbod_map(instance);
5936
5937 if (megasas_get_device_list(instance) != SUCCESS) {
5938 dev_err(&instance->pdev->dev,
5939 "%s: megasas_get_device_list failed\n",
5940 __func__);
5941 goto fail_get_ld_pd_list;
5942 }
5943
5944 /* stream detection initialization */
5945 if (instance->adapter_type >= VENTURA_SERIES) {
5946 fusion->stream_detect_by_ld =
5947 kcalloc(MAX_LOGICAL_DRIVES_EXT,
5948 sizeof(struct LD_STREAM_DETECT *),
5949 GFP_KERNEL);
5950 if (!fusion->stream_detect_by_ld) {
5951 dev_err(&instance->pdev->dev,
5952 "unable to allocate stream detection for pool of LDs\n");
5953 goto fail_get_ld_pd_list;
5954 }
5955 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5956 fusion->stream_detect_by_ld[i] =
5957 kzalloc(sizeof(struct LD_STREAM_DETECT),
5958 GFP_KERNEL);
5959 if (!fusion->stream_detect_by_ld[i]) {
5960 dev_err(&instance->pdev->dev,
5961 "unable to allocate stream detect by LD\n ");
5962 for (j = 0; j < i; ++j)
5963 kfree(fusion->stream_detect_by_ld[j]);
5964 kfree(fusion->stream_detect_by_ld);
5965 fusion->stream_detect_by_ld = NULL;
5966 goto fail_get_ld_pd_list;
5967 }
5968 fusion->stream_detect_by_ld[i]->mru_bit_map
5969 = MR_STREAM_BITMAP;
5970 }
5971 }
5972
5973 /*
5974 * Compute the max allowed sectors per IO: The controller info has two
5975 * limits on max sectors. Driver should use the minimum of these two.
5976 *
5977 * 1 << stripe_sz_ops.min = max sectors per strip
5978 *
5979 * Note that older firmwares ( < FW ver 30) didn't report information
5980 * to calculate max_sectors_1. So the number ended up as zero always.
5981 */
5982 tmp_sectors = 0;
5983 ctrl_info = instance->ctrl_info_buf;
5984
5985 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5986 le16_to_cpu(ctrl_info->max_strips_per_io);
5987 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5988
5989 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5990
5991 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5992 instance->passive = ctrl_info->cluster.passive;
5993 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5994 instance->UnevenSpanSupport =
5995 ctrl_info->adapterOperations2.supportUnevenSpans;
5996 if (instance->UnevenSpanSupport) {
5997 struct fusion_context *fusion = instance->ctrl_context;
5998 if (MR_ValidateMapInfo(instance, instance->map_id))
5999 fusion->fast_path_io = 1;
6000 else
6001 fusion->fast_path_io = 0;
6002
6003 }
6004 if (ctrl_info->host_interface.SRIOV) {
6005 instance->requestorId = ctrl_info->iov.requestorId;
6006 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6007 if (!ctrl_info->adapterOperations2.activePassive)
6008 instance->PlasmaFW111 = 1;
6009
6010 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6011 instance->PlasmaFW111 ? "1.11" : "new");
6012
6013 if (instance->PlasmaFW111) {
6014 iovPtr = (struct IOV_111 *)
6015 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6016 instance->requestorId = iovPtr->requestorId;
6017 }
6018 }
6019 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6020 instance->requestorId);
6021 }
6022
6023 instance->crash_dump_fw_support =
6024 ctrl_info->adapterOperations3.supportCrashDump;
6025 instance->crash_dump_drv_support =
6026 (instance->crash_dump_fw_support &&
6027 instance->crash_dump_buf);
6028 if (instance->crash_dump_drv_support)
6029 megasas_set_crash_dump_params(instance,
6030 MR_CRASH_BUF_TURN_OFF);
6031
6032 else {
6033 if (instance->crash_dump_buf)
6034 dma_free_coherent(&instance->pdev->dev,
6035 CRASH_DMA_BUF_SIZE,
6036 instance->crash_dump_buf,
6037 instance->crash_dump_h);
6038 instance->crash_dump_buf = NULL;
6039 }
6040
6041 if (instance->snapdump_wait_time) {
6042 megasas_get_snapdump_properties(instance);
6043 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6044 instance->snapdump_wait_time);
6045 }
6046
6047 dev_info(&instance->pdev->dev,
6048 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6049 le16_to_cpu(ctrl_info->pci.vendor_id),
6050 le16_to_cpu(ctrl_info->pci.device_id),
6051 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6052 le16_to_cpu(ctrl_info->pci.sub_device_id));
6053 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6054 instance->UnevenSpanSupport ? "yes" : "no");
6055 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6056 instance->crash_dump_drv_support ? "yes" : "no");
6057 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
6058 instance->use_seqnum_jbod_fp ? "yes" : "no");
6059
6060 instance->max_sectors_per_req = instance->max_num_sge *
6061 SGE_BUFFER_SIZE / 512;
6062 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6063 instance->max_sectors_per_req = tmp_sectors;
6064
6065 /* Check for valid throttlequeuedepth module parameter */
6066 if (throttlequeuedepth &&
6067 throttlequeuedepth <= instance->max_scsi_cmds)
6068 instance->throttlequeuedepth = throttlequeuedepth;
6069 else
6070 instance->throttlequeuedepth =
6071 MEGASAS_THROTTLE_QUEUE_DEPTH;
6072
6073 if ((resetwaittime < 1) ||
6074 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6075 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6076
6077 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6078 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6079
6080 /* Launch SR-IOV heartbeat timer */
6081 if (instance->requestorId) {
6082 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6083 megasas_start_timer(instance);
6084 } else {
6085 instance->skip_heartbeat_timer_del = 1;
6086 goto fail_get_ld_pd_list;
6087 }
6088 }
6089
6090 /*
6091 * Create and start watchdog thread which will monitor
6092 * controller state every 1 sec and trigger OCR when
6093 * it enters fault state
6094 */
6095 if (instance->adapter_type != MFI_SERIES)
6096 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6097 goto fail_start_watchdog;
6098
6099 return 0;
6100
6101 fail_start_watchdog:
6102 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6103 del_timer_sync(&instance->sriov_heartbeat_timer);
6104 fail_get_ld_pd_list:
6105 instance->instancet->disable_intr(instance);
6106 megasas_destroy_irqs(instance);
6107 fail_init_adapter:
6108 if (instance->msix_vectors)
6109 pci_free_irq_vectors(instance->pdev);
6110 instance->msix_vectors = 0;
6111 fail_alloc_dma_buf:
6112 megasas_free_ctrl_dma_buffers(instance);
6113 megasas_free_ctrl_mem(instance);
6114 fail_ready_state:
6115 iounmap(instance->reg_set);
6116
6117 fail_ioremap:
6118 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6119
6120 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6121 __func__, __LINE__);
6122 return -EINVAL;
6123 }
6124
6125 /**
6126 * megasas_release_mfi - Reverses the FW initialization
6127 * @instance: Adapter soft state
6128 */
6129 static void megasas_release_mfi(struct megasas_instance *instance)
6130 {
6131 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6132
6133 if (instance->reply_queue)
6134 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6135 instance->reply_queue, instance->reply_queue_h);
6136
6137 megasas_free_cmds(instance);
6138
6139 iounmap(instance->reg_set);
6140
6141 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6142 }
6143
6144 /**
6145 * megasas_get_seq_num - Gets latest event sequence numbers
6146 * @instance: Adapter soft state
6147 * @eli: FW event log sequence numbers information
6148 *
6149 * FW maintains a log of all events in a non-volatile area. Upper layers would
6150 * usually find out the latest sequence number of the events, the seq number at
6151 * the boot etc. They would "read" all the events below the latest seq number
6152 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6153 * number), they would subsribe to AEN (asynchronous event notification) and
6154 * wait for the events to happen.
6155 */
6156 static int
6157 megasas_get_seq_num(struct megasas_instance *instance,
6158 struct megasas_evt_log_info *eli)
6159 {
6160 struct megasas_cmd *cmd;
6161 struct megasas_dcmd_frame *dcmd;
6162 struct megasas_evt_log_info *el_info;
6163 dma_addr_t el_info_h = 0;
6164 int ret;
6165
6166 cmd = megasas_get_cmd(instance);
6167
6168 if (!cmd) {
6169 return -ENOMEM;
6170 }
6171
6172 dcmd = &cmd->frame->dcmd;
6173 el_info = dma_alloc_coherent(&instance->pdev->dev,
6174 sizeof(struct megasas_evt_log_info),
6175 &el_info_h, GFP_KERNEL);
6176 if (!el_info) {
6177 megasas_return_cmd(instance, cmd);
6178 return -ENOMEM;
6179 }
6180
6181 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6182
6183 dcmd->cmd = MFI_CMD_DCMD;
6184 dcmd->cmd_status = 0x0;
6185 dcmd->sge_count = 1;
6186 dcmd->flags = MFI_FRAME_DIR_READ;
6187 dcmd->timeout = 0;
6188 dcmd->pad_0 = 0;
6189 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6190 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6191
6192 megasas_set_dma_settings(instance, dcmd, el_info_h,
6193 sizeof(struct megasas_evt_log_info));
6194
6195 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6196 if (ret != DCMD_SUCCESS) {
6197 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6198 __func__, __LINE__);
6199 goto dcmd_failed;
6200 }
6201
6202 /*
6203 * Copy the data back into callers buffer
6204 */
6205 eli->newest_seq_num = el_info->newest_seq_num;
6206 eli->oldest_seq_num = el_info->oldest_seq_num;
6207 eli->clear_seq_num = el_info->clear_seq_num;
6208 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6209 eli->boot_seq_num = el_info->boot_seq_num;
6210
6211 dcmd_failed:
6212 dma_free_coherent(&instance->pdev->dev,
6213 sizeof(struct megasas_evt_log_info),
6214 el_info, el_info_h);
6215
6216 megasas_return_cmd(instance, cmd);
6217
6218 return ret;
6219 }
6220
6221 /**
6222 * megasas_register_aen - Registers for asynchronous event notification
6223 * @instance: Adapter soft state
6224 * @seq_num: The starting sequence number
6225 * @class_locale: Class of the event
6226 *
6227 * This function subscribes for AEN for events beyond the @seq_num. It requests
6228 * to be notified if and only if the event is of type @class_locale
6229 */
6230 static int
6231 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6232 u32 class_locale_word)
6233 {
6234 int ret_val;
6235 struct megasas_cmd *cmd;
6236 struct megasas_dcmd_frame *dcmd;
6237 union megasas_evt_class_locale curr_aen;
6238 union megasas_evt_class_locale prev_aen;
6239
6240 /*
6241 * If there an AEN pending already (aen_cmd), check if the
6242 * class_locale of that pending AEN is inclusive of the new
6243 * AEN request we currently have. If it is, then we don't have
6244 * to do anything. In other words, whichever events the current
6245 * AEN request is subscribing to, have already been subscribed
6246 * to.
6247 *
6248 * If the old_cmd is _not_ inclusive, then we have to abort
6249 * that command, form a class_locale that is superset of both
6250 * old and current and re-issue to the FW
6251 */
6252
6253 curr_aen.word = class_locale_word;
6254
6255 if (instance->aen_cmd) {
6256
6257 prev_aen.word =
6258 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6259
6260 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6261 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6262 dev_info(&instance->pdev->dev,
6263 "%s %d out of range class %d send by application\n",
6264 __func__, __LINE__, curr_aen.members.class);
6265 return 0;
6266 }
6267
6268 /*
6269 * A class whose enum value is smaller is inclusive of all
6270 * higher values. If a PROGRESS (= -1) was previously
6271 * registered, then a new registration requests for higher
6272 * classes need not be sent to FW. They are automatically
6273 * included.
6274 *
6275 * Locale numbers don't have such hierarchy. They are bitmap
6276 * values
6277 */
6278 if ((prev_aen.members.class <= curr_aen.members.class) &&
6279 !((prev_aen.members.locale & curr_aen.members.locale) ^
6280 curr_aen.members.locale)) {
6281 /*
6282 * Previously issued event registration includes
6283 * current request. Nothing to do.
6284 */
6285 return 0;
6286 } else {
6287 curr_aen.members.locale |= prev_aen.members.locale;
6288
6289 if (prev_aen.members.class < curr_aen.members.class)
6290 curr_aen.members.class = prev_aen.members.class;
6291
6292 instance->aen_cmd->abort_aen = 1;
6293 ret_val = megasas_issue_blocked_abort_cmd(instance,
6294 instance->
6295 aen_cmd, 30);
6296
6297 if (ret_val) {
6298 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6299 "previous AEN command\n");
6300 return ret_val;
6301 }
6302 }
6303 }
6304
6305 cmd = megasas_get_cmd(instance);
6306
6307 if (!cmd)
6308 return -ENOMEM;
6309
6310 dcmd = &cmd->frame->dcmd;
6311
6312 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6313
6314 /*
6315 * Prepare DCMD for aen registration
6316 */
6317 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6318
6319 dcmd->cmd = MFI_CMD_DCMD;
6320 dcmd->cmd_status = 0x0;
6321 dcmd->sge_count = 1;
6322 dcmd->flags = MFI_FRAME_DIR_READ;
6323 dcmd->timeout = 0;
6324 dcmd->pad_0 = 0;
6325 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6326 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6327 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6328 instance->last_seq_num = seq_num;
6329 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6330
6331 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6332 sizeof(struct megasas_evt_detail));
6333
6334 if (instance->aen_cmd != NULL) {
6335 megasas_return_cmd(instance, cmd);
6336 return 0;
6337 }
6338
6339 /*
6340 * Store reference to the cmd used to register for AEN. When an
6341 * application wants us to register for AEN, we have to abort this
6342 * cmd and re-register with a new EVENT LOCALE supplied by that app
6343 */
6344 instance->aen_cmd = cmd;
6345
6346 /*
6347 * Issue the aen registration frame
6348 */
6349 instance->instancet->issue_dcmd(instance, cmd);
6350
6351 return 0;
6352 }
6353
6354 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6355 *
6356 * This DCMD will fetch few properties of LD/system PD defined
6357 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6358 *
6359 * DCMD send by drivers whenever new target is added to the OS.
6360 *
6361 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6362 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6363 * 0 = system PD, 1 = LD.
6364 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6365 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6366 *
6367 * @instance: Adapter soft state
6368 * @sdev: OS provided scsi device
6369 *
6370 * Returns 0 on success non-zero on failure.
6371 */
6372 int
6373 megasas_get_target_prop(struct megasas_instance *instance,
6374 struct scsi_device *sdev)
6375 {
6376 int ret;
6377 struct megasas_cmd *cmd;
6378 struct megasas_dcmd_frame *dcmd;
6379 u16 targetId = (sdev->channel % 2) + sdev->id;
6380
6381 cmd = megasas_get_cmd(instance);
6382
6383 if (!cmd) {
6384 dev_err(&instance->pdev->dev,
6385 "Failed to get cmd %s\n", __func__);
6386 return -ENOMEM;
6387 }
6388
6389 dcmd = &cmd->frame->dcmd;
6390
6391 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6392 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6393 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6394
6395 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6396 dcmd->cmd = MFI_CMD_DCMD;
6397 dcmd->cmd_status = 0xFF;
6398 dcmd->sge_count = 1;
6399 dcmd->flags = MFI_FRAME_DIR_READ;
6400 dcmd->timeout = 0;
6401 dcmd->pad_0 = 0;
6402 dcmd->data_xfer_len =
6403 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6404 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6405
6406 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6407 sizeof(struct MR_TARGET_PROPERTIES));
6408
6409 if ((instance->adapter_type != MFI_SERIES) &&
6410 !instance->mask_interrupts)
6411 ret = megasas_issue_blocked_cmd(instance,
6412 cmd, MFI_IO_TIMEOUT_SECS);
6413 else
6414 ret = megasas_issue_polled(instance, cmd);
6415
6416 switch (ret) {
6417 case DCMD_TIMEOUT:
6418 switch (dcmd_timeout_ocr_possible(instance)) {
6419 case INITIATE_OCR:
6420 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6421 megasas_reset_fusion(instance->host,
6422 MFI_IO_TIMEOUT_OCR);
6423 break;
6424 case KILL_ADAPTER:
6425 megaraid_sas_kill_hba(instance);
6426 break;
6427 case IGNORE_TIMEOUT:
6428 dev_info(&instance->pdev->dev,
6429 "Ignore DCMD timeout: %s %d\n",
6430 __func__, __LINE__);
6431 break;
6432 }
6433 break;
6434
6435 default:
6436 megasas_return_cmd(instance, cmd);
6437 }
6438 if (ret != DCMD_SUCCESS)
6439 dev_err(&instance->pdev->dev,
6440 "return from %s %d return value %d\n",
6441 __func__, __LINE__, ret);
6442
6443 return ret;
6444 }
6445
6446 /**
6447 * megasas_start_aen - Subscribes to AEN during driver load time
6448 * @instance: Adapter soft state
6449 */
6450 static int megasas_start_aen(struct megasas_instance *instance)
6451 {
6452 struct megasas_evt_log_info eli;
6453 union megasas_evt_class_locale class_locale;
6454
6455 /*
6456 * Get the latest sequence number from FW
6457 */
6458 memset(&eli, 0, sizeof(eli));
6459
6460 if (megasas_get_seq_num(instance, &eli))
6461 return -1;
6462
6463 /*
6464 * Register AEN with FW for latest sequence number plus 1
6465 */
6466 class_locale.members.reserved = 0;
6467 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6468 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6469
6470 return megasas_register_aen(instance,
6471 le32_to_cpu(eli.newest_seq_num) + 1,
6472 class_locale.word);
6473 }
6474
6475 /**
6476 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6477 * @instance: Adapter soft state
6478 */
6479 static int megasas_io_attach(struct megasas_instance *instance)
6480 {
6481 struct Scsi_Host *host = instance->host;
6482
6483 /*
6484 * Export parameters required by SCSI mid-layer
6485 */
6486 host->unique_id = instance->unique_id;
6487 host->can_queue = instance->max_scsi_cmds;
6488 host->this_id = instance->init_id;
6489 host->sg_tablesize = instance->max_num_sge;
6490
6491 if (instance->fw_support_ieee)
6492 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6493
6494 /*
6495 * Check if the module parameter value for max_sectors can be used
6496 */
6497 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6498 instance->max_sectors_per_req = max_sectors;
6499 else {
6500 if (max_sectors) {
6501 if (((instance->pdev->device ==
6502 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6503 (instance->pdev->device ==
6504 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6505 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6506 instance->max_sectors_per_req = max_sectors;
6507 } else {
6508 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6509 "and <= %d (or < 1MB for GEN2 controller)\n",
6510 instance->max_sectors_per_req);
6511 }
6512 }
6513 }
6514
6515 host->max_sectors = instance->max_sectors_per_req;
6516 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6517 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6518 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6519 host->max_lun = MEGASAS_MAX_LUN;
6520 host->max_cmd_len = 16;
6521
6522 /*
6523 * Notify the mid-layer about the new controller
6524 */
6525 if (scsi_add_host(host, &instance->pdev->dev)) {
6526 dev_err(&instance->pdev->dev,
6527 "Failed to add host from %s %d\n",
6528 __func__, __LINE__);
6529 return -ENODEV;
6530 }
6531
6532 return 0;
6533 }
6534
6535 /**
6536 * megasas_set_dma_mask - Set DMA mask for supported controllers
6537 *
6538 * @instance: Adapter soft state
6539 * Description:
6540 *
6541 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6542 *
6543 * For invader-
6544 * By default, driver/FW will operate in 32bit DMA addresses
6545 * for consistent DMA mapping but if 32 bit consistent
6546 * DMA mask fails, driver will try with 63 bit consistent
6547 * mask provided FW is true 63bit DMA capable
6548 *
6549 * For older controllers(Thunderbolt and MFI based adapters)-
6550 * driver/FW will operate in 32 bit consistent DMA addresses.
6551 */
6552 static int
6553 megasas_set_dma_mask(struct megasas_instance *instance)
6554 {
6555 u64 consistent_mask;
6556 struct pci_dev *pdev;
6557 u32 scratch_pad_1;
6558
6559 pdev = instance->pdev;
6560 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6561 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6562
6563 if (IS_DMA64) {
6564 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6565 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6566 goto fail_set_dma_mask;
6567
6568 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6569 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6570 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6571 /*
6572 * If 32 bit DMA mask fails, then try for 64 bit mask
6573 * for FW capable of handling 64 bit DMA.
6574 */
6575 scratch_pad_1 = megasas_readl
6576 (instance, &instance->reg_set->outbound_scratch_pad_1);
6577
6578 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6579 goto fail_set_dma_mask;
6580 else if (dma_set_mask_and_coherent(&pdev->dev,
6581 DMA_BIT_MASK(63)))
6582 goto fail_set_dma_mask;
6583 }
6584 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6585 goto fail_set_dma_mask;
6586
6587 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6588 instance->consistent_mask_64bit = false;
6589 else
6590 instance->consistent_mask_64bit = true;
6591
6592 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6593 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6594 (instance->consistent_mask_64bit ? "63" : "32"));
6595
6596 return 0;
6597
6598 fail_set_dma_mask:
6599 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6600 return -1;
6601
6602 }
6603
6604 /*
6605 * megasas_set_adapter_type - Set adapter type.
6606 * Supported controllers can be divided in
6607 * different categories-
6608 * enum MR_ADAPTER_TYPE {
6609 * MFI_SERIES = 1,
6610 * THUNDERBOLT_SERIES = 2,
6611 * INVADER_SERIES = 3,
6612 * VENTURA_SERIES = 4,
6613 * AERO_SERIES = 5,
6614 * };
6615 * @instance: Adapter soft state
6616 * return: void
6617 */
6618 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6619 {
6620 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6621 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6622 instance->adapter_type = MFI_SERIES;
6623 } else {
6624 switch (instance->pdev->device) {
6625 case PCI_DEVICE_ID_LSI_AERO_10E1:
6626 case PCI_DEVICE_ID_LSI_AERO_10E2:
6627 case PCI_DEVICE_ID_LSI_AERO_10E5:
6628 case PCI_DEVICE_ID_LSI_AERO_10E6:
6629 instance->adapter_type = AERO_SERIES;
6630 break;
6631 case PCI_DEVICE_ID_LSI_VENTURA:
6632 case PCI_DEVICE_ID_LSI_CRUSADER:
6633 case PCI_DEVICE_ID_LSI_HARPOON:
6634 case PCI_DEVICE_ID_LSI_TOMCAT:
6635 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6636 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6637 instance->adapter_type = VENTURA_SERIES;
6638 break;
6639 case PCI_DEVICE_ID_LSI_FUSION:
6640 case PCI_DEVICE_ID_LSI_PLASMA:
6641 instance->adapter_type = THUNDERBOLT_SERIES;
6642 break;
6643 case PCI_DEVICE_ID_LSI_INVADER:
6644 case PCI_DEVICE_ID_LSI_INTRUDER:
6645 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6646 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6647 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6648 case PCI_DEVICE_ID_LSI_FURY:
6649 instance->adapter_type = INVADER_SERIES;
6650 break;
6651 default: /* For all other supported controllers */
6652 instance->adapter_type = MFI_SERIES;
6653 break;
6654 }
6655 }
6656 }
6657
6658 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6659 {
6660 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6661 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6662 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6663 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
6664
6665 if (!instance->producer || !instance->consumer) {
6666 dev_err(&instance->pdev->dev,
6667 "Failed to allocate memory for producer, consumer\n");
6668 return -1;
6669 }
6670
6671 *instance->producer = 0;
6672 *instance->consumer = 0;
6673 return 0;
6674 }
6675
6676 /**
6677 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6678 * structures which are not common across MFI
6679 * adapters and fusion adapters.
6680 * For MFI based adapters, allocate producer and
6681 * consumer buffers. For fusion adapters, allocate
6682 * memory for fusion context.
6683 * @instance: Adapter soft state
6684 * return: 0 for SUCCESS
6685 */
6686 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6687 {
6688 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
6689 GFP_KERNEL);
6690 if (!instance->reply_map)
6691 return -ENOMEM;
6692
6693 switch (instance->adapter_type) {
6694 case MFI_SERIES:
6695 if (megasas_alloc_mfi_ctrl_mem(instance))
6696 goto fail;
6697 break;
6698 case AERO_SERIES:
6699 case VENTURA_SERIES:
6700 case THUNDERBOLT_SERIES:
6701 case INVADER_SERIES:
6702 if (megasas_alloc_fusion_context(instance))
6703 goto fail;
6704 break;
6705 }
6706
6707 return 0;
6708 fail:
6709 kfree(instance->reply_map);
6710 instance->reply_map = NULL;
6711 return -ENOMEM;
6712 }
6713
6714 /*
6715 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6716 * producer, consumer buffers for MFI adapters
6717 *
6718 * @instance - Adapter soft instance
6719 *
6720 */
6721 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6722 {
6723 kfree(instance->reply_map);
6724 if (instance->adapter_type == MFI_SERIES) {
6725 if (instance->producer)
6726 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6727 instance->producer,
6728 instance->producer_h);
6729 if (instance->consumer)
6730 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6731 instance->consumer,
6732 instance->consumer_h);
6733 } else {
6734 megasas_free_fusion_context(instance);
6735 }
6736 }
6737
6738 /**
6739 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6740 * driver load time
6741 *
6742 * @instance- Adapter soft instance
6743 * @return- O for SUCCESS
6744 */
6745 static inline
6746 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6747 {
6748 struct pci_dev *pdev = instance->pdev;
6749 struct fusion_context *fusion = instance->ctrl_context;
6750
6751 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6752 sizeof(struct megasas_evt_detail),
6753 &instance->evt_detail_h, GFP_KERNEL);
6754
6755 if (!instance->evt_detail) {
6756 dev_err(&instance->pdev->dev,
6757 "Failed to allocate event detail buffer\n");
6758 return -ENOMEM;
6759 }
6760
6761 if (fusion) {
6762 fusion->ioc_init_request =
6763 dma_alloc_coherent(&pdev->dev,
6764 sizeof(struct MPI2_IOC_INIT_REQUEST),
6765 &fusion->ioc_init_request_phys,
6766 GFP_KERNEL);
6767
6768 if (!fusion->ioc_init_request) {
6769 dev_err(&pdev->dev,
6770 "Failed to allocate PD list buffer\n");
6771 return -ENOMEM;
6772 }
6773
6774 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6775 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6776 &instance->snapdump_prop_h, GFP_KERNEL);
6777
6778 if (!instance->snapdump_prop)
6779 dev_err(&pdev->dev,
6780 "Failed to allocate snapdump properties buffer\n");
6781
6782 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6783 HOST_DEVICE_LIST_SZ,
6784 &instance->host_device_list_buf_h,
6785 GFP_KERNEL);
6786
6787 if (!instance->host_device_list_buf) {
6788 dev_err(&pdev->dev,
6789 "Failed to allocate targetid list buffer\n");
6790 return -ENOMEM;
6791 }
6792
6793 }
6794
6795 instance->pd_list_buf =
6796 dma_alloc_coherent(&pdev->dev,
6797 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6798 &instance->pd_list_buf_h, GFP_KERNEL);
6799
6800 if (!instance->pd_list_buf) {
6801 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6802 return -ENOMEM;
6803 }
6804
6805 instance->ctrl_info_buf =
6806 dma_alloc_coherent(&pdev->dev,
6807 sizeof(struct megasas_ctrl_info),
6808 &instance->ctrl_info_buf_h, GFP_KERNEL);
6809
6810 if (!instance->ctrl_info_buf) {
6811 dev_err(&pdev->dev,
6812 "Failed to allocate controller info buffer\n");
6813 return -ENOMEM;
6814 }
6815
6816 instance->ld_list_buf =
6817 dma_alloc_coherent(&pdev->dev,
6818 sizeof(struct MR_LD_LIST),
6819 &instance->ld_list_buf_h, GFP_KERNEL);
6820
6821 if (!instance->ld_list_buf) {
6822 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6823 return -ENOMEM;
6824 }
6825
6826 instance->ld_targetid_list_buf =
6827 dma_alloc_coherent(&pdev->dev,
6828 sizeof(struct MR_LD_TARGETID_LIST),
6829 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
6830
6831 if (!instance->ld_targetid_list_buf) {
6832 dev_err(&pdev->dev,
6833 "Failed to allocate LD targetid list buffer\n");
6834 return -ENOMEM;
6835 }
6836
6837 if (!reset_devices) {
6838 instance->system_info_buf =
6839 dma_alloc_coherent(&pdev->dev,
6840 sizeof(struct MR_DRV_SYSTEM_INFO),
6841 &instance->system_info_h, GFP_KERNEL);
6842 instance->pd_info =
6843 dma_alloc_coherent(&pdev->dev,
6844 sizeof(struct MR_PD_INFO),
6845 &instance->pd_info_h, GFP_KERNEL);
6846 instance->tgt_prop =
6847 dma_alloc_coherent(&pdev->dev,
6848 sizeof(struct MR_TARGET_PROPERTIES),
6849 &instance->tgt_prop_h, GFP_KERNEL);
6850 instance->crash_dump_buf =
6851 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6852 &instance->crash_dump_h, GFP_KERNEL);
6853
6854 if (!instance->system_info_buf)
6855 dev_err(&instance->pdev->dev,
6856 "Failed to allocate system info buffer\n");
6857
6858 if (!instance->pd_info)
6859 dev_err(&instance->pdev->dev,
6860 "Failed to allocate pd_info buffer\n");
6861
6862 if (!instance->tgt_prop)
6863 dev_err(&instance->pdev->dev,
6864 "Failed to allocate tgt_prop buffer\n");
6865
6866 if (!instance->crash_dump_buf)
6867 dev_err(&instance->pdev->dev,
6868 "Failed to allocate crash dump buffer\n");
6869 }
6870
6871 return 0;
6872 }
6873
6874 /*
6875 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6876 * during driver load time
6877 *
6878 * @instance- Adapter soft instance
6879 *
6880 */
6881 static inline
6882 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6883 {
6884 struct pci_dev *pdev = instance->pdev;
6885 struct fusion_context *fusion = instance->ctrl_context;
6886
6887 if (instance->evt_detail)
6888 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
6889 instance->evt_detail,
6890 instance->evt_detail_h);
6891
6892 if (fusion && fusion->ioc_init_request)
6893 dma_free_coherent(&pdev->dev,
6894 sizeof(struct MPI2_IOC_INIT_REQUEST),
6895 fusion->ioc_init_request,
6896 fusion->ioc_init_request_phys);
6897
6898 if (instance->pd_list_buf)
6899 dma_free_coherent(&pdev->dev,
6900 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6901 instance->pd_list_buf,
6902 instance->pd_list_buf_h);
6903
6904 if (instance->ld_list_buf)
6905 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
6906 instance->ld_list_buf,
6907 instance->ld_list_buf_h);
6908
6909 if (instance->ld_targetid_list_buf)
6910 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
6911 instance->ld_targetid_list_buf,
6912 instance->ld_targetid_list_buf_h);
6913
6914 if (instance->ctrl_info_buf)
6915 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
6916 instance->ctrl_info_buf,
6917 instance->ctrl_info_buf_h);
6918
6919 if (instance->system_info_buf)
6920 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
6921 instance->system_info_buf,
6922 instance->system_info_h);
6923
6924 if (instance->pd_info)
6925 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
6926 instance->pd_info, instance->pd_info_h);
6927
6928 if (instance->tgt_prop)
6929 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
6930 instance->tgt_prop, instance->tgt_prop_h);
6931
6932 if (instance->crash_dump_buf)
6933 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6934 instance->crash_dump_buf,
6935 instance->crash_dump_h);
6936
6937 if (instance->snapdump_prop)
6938 dma_free_coherent(&pdev->dev,
6939 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6940 instance->snapdump_prop,
6941 instance->snapdump_prop_h);
6942
6943 if (instance->host_device_list_buf)
6944 dma_free_coherent(&pdev->dev,
6945 HOST_DEVICE_LIST_SZ,
6946 instance->host_device_list_buf,
6947 instance->host_device_list_buf_h);
6948
6949 }
6950
6951 /*
6952 * megasas_init_ctrl_params - Initialize controller's instance
6953 * parameters before FW init
6954 * @instance - Adapter soft instance
6955 * @return - void
6956 */
6957 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6958 {
6959 instance->fw_crash_state = UNAVAILABLE;
6960
6961 megasas_poll_wait_aen = 0;
6962 instance->issuepend_done = 1;
6963 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6964
6965 /*
6966 * Initialize locks and queues
6967 */
6968 INIT_LIST_HEAD(&instance->cmd_pool);
6969 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6970
6971 atomic_set(&instance->fw_outstanding, 0);
6972 atomic64_set(&instance->total_io_count, 0);
6973
6974 init_waitqueue_head(&instance->int_cmd_wait_q);
6975 init_waitqueue_head(&instance->abort_cmd_wait_q);
6976
6977 spin_lock_init(&instance->crashdump_lock);
6978 spin_lock_init(&instance->mfi_pool_lock);
6979 spin_lock_init(&instance->hba_lock);
6980 spin_lock_init(&instance->stream_lock);
6981 spin_lock_init(&instance->completion_lock);
6982
6983 mutex_init(&instance->reset_mutex);
6984
6985 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6986 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6987 instance->flag_ieee = 1;
6988
6989 megasas_dbg_lvl = 0;
6990 instance->flag = 0;
6991 instance->unload = 1;
6992 instance->last_time = 0;
6993 instance->disableOnlineCtrlReset = 1;
6994 instance->UnevenSpanSupport = 0;
6995 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
6996 instance->msix_load_balance = false;
6997
6998 if (instance->adapter_type != MFI_SERIES)
6999 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7000 else
7001 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7002 }
7003
7004 /**
7005 * megasas_probe_one - PCI hotplug entry point
7006 * @pdev: PCI device structure
7007 * @id: PCI ids of supported hotplugged adapter
7008 */
7009 static int megasas_probe_one(struct pci_dev *pdev,
7010 const struct pci_device_id *id)
7011 {
7012 int rval, pos;
7013 struct Scsi_Host *host;
7014 struct megasas_instance *instance;
7015 u16 control = 0;
7016
7017 switch (pdev->device) {
7018 case PCI_DEVICE_ID_LSI_AERO_10E1:
7019 case PCI_DEVICE_ID_LSI_AERO_10E5:
7020 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7021 break;
7022 }
7023
7024 /* Reset MSI-X in the kdump kernel */
7025 if (reset_devices) {
7026 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7027 if (pos) {
7028 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7029 &control);
7030 if (control & PCI_MSIX_FLAGS_ENABLE) {
7031 dev_info(&pdev->dev, "resetting MSI-X\n");
7032 pci_write_config_word(pdev,
7033 pos + PCI_MSIX_FLAGS,
7034 control &
7035 ~PCI_MSIX_FLAGS_ENABLE);
7036 }
7037 }
7038 }
7039
7040 /*
7041 * PCI prepping: enable device set bus mastering and dma mask
7042 */
7043 rval = pci_enable_device_mem(pdev);
7044
7045 if (rval) {
7046 return rval;
7047 }
7048
7049 pci_set_master(pdev);
7050
7051 host = scsi_host_alloc(&megasas_template,
7052 sizeof(struct megasas_instance));
7053
7054 if (!host) {
7055 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7056 goto fail_alloc_instance;
7057 }
7058
7059 instance = (struct megasas_instance *)host->hostdata;
7060 memset(instance, 0, sizeof(*instance));
7061 atomic_set(&instance->fw_reset_no_pci_access, 0);
7062
7063 /*
7064 * Initialize PCI related and misc parameters
7065 */
7066 instance->pdev = pdev;
7067 instance->host = host;
7068 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7069 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7070
7071 megasas_set_adapter_type(instance);
7072
7073 /*
7074 * Initialize MFI Firmware
7075 */
7076 if (megasas_init_fw(instance))
7077 goto fail_init_mfi;
7078
7079 if (instance->requestorId) {
7080 if (instance->PlasmaFW111) {
7081 instance->vf_affiliation_111 =
7082 dma_alloc_coherent(&pdev->dev,
7083 sizeof(struct MR_LD_VF_AFFILIATION_111),
7084 &instance->vf_affiliation_111_h,
7085 GFP_KERNEL);
7086 if (!instance->vf_affiliation_111)
7087 dev_warn(&pdev->dev, "Can't allocate "
7088 "memory for VF affiliation buffer\n");
7089 } else {
7090 instance->vf_affiliation =
7091 dma_alloc_coherent(&pdev->dev,
7092 (MAX_LOGICAL_DRIVES + 1) *
7093 sizeof(struct MR_LD_VF_AFFILIATION),
7094 &instance->vf_affiliation_h,
7095 GFP_KERNEL);
7096 if (!instance->vf_affiliation)
7097 dev_warn(&pdev->dev, "Can't allocate "
7098 "memory for VF affiliation buffer\n");
7099 }
7100 }
7101
7102 /*
7103 * Store instance in PCI softstate
7104 */
7105 pci_set_drvdata(pdev, instance);
7106
7107 /*
7108 * Add this controller to megasas_mgmt_info structure so that it
7109 * can be exported to management applications
7110 */
7111 megasas_mgmt_info.count++;
7112 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7113 megasas_mgmt_info.max_index++;
7114
7115 /*
7116 * Register with SCSI mid-layer
7117 */
7118 if (megasas_io_attach(instance))
7119 goto fail_io_attach;
7120
7121 instance->unload = 0;
7122 /*
7123 * Trigger SCSI to scan our drives
7124 */
7125 if (!instance->enable_fw_dev_list ||
7126 (instance->host_device_list_buf->count > 0))
7127 scsi_scan_host(host);
7128
7129 /*
7130 * Initiate AEN (Asynchronous Event Notification)
7131 */
7132 if (megasas_start_aen(instance)) {
7133 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7134 goto fail_start_aen;
7135 }
7136
7137 megasas_setup_debugfs(instance);
7138
7139 /* Get current SR-IOV LD/VF affiliation */
7140 if (instance->requestorId)
7141 megasas_get_ld_vf_affiliation(instance, 1);
7142
7143 return 0;
7144
7145 fail_start_aen:
7146 fail_io_attach:
7147 megasas_mgmt_info.count--;
7148 megasas_mgmt_info.max_index--;
7149 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7150
7151 instance->instancet->disable_intr(instance);
7152 megasas_destroy_irqs(instance);
7153
7154 if (instance->adapter_type != MFI_SERIES)
7155 megasas_release_fusion(instance);
7156 else
7157 megasas_release_mfi(instance);
7158 if (instance->msix_vectors)
7159 pci_free_irq_vectors(instance->pdev);
7160 fail_init_mfi:
7161 scsi_host_put(host);
7162 fail_alloc_instance:
7163 pci_disable_device(pdev);
7164
7165 return -ENODEV;
7166 }
7167
7168 /**
7169 * megasas_flush_cache - Requests FW to flush all its caches
7170 * @instance: Adapter soft state
7171 */
7172 static void megasas_flush_cache(struct megasas_instance *instance)
7173 {
7174 struct megasas_cmd *cmd;
7175 struct megasas_dcmd_frame *dcmd;
7176
7177 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7178 return;
7179
7180 cmd = megasas_get_cmd(instance);
7181
7182 if (!cmd)
7183 return;
7184
7185 dcmd = &cmd->frame->dcmd;
7186
7187 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7188
7189 dcmd->cmd = MFI_CMD_DCMD;
7190 dcmd->cmd_status = 0x0;
7191 dcmd->sge_count = 0;
7192 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7193 dcmd->timeout = 0;
7194 dcmd->pad_0 = 0;
7195 dcmd->data_xfer_len = 0;
7196 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7197 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7198
7199 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7200 != DCMD_SUCCESS) {
7201 dev_err(&instance->pdev->dev,
7202 "return from %s %d\n", __func__, __LINE__);
7203 return;
7204 }
7205
7206 megasas_return_cmd(instance, cmd);
7207 }
7208
7209 /**
7210 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7211 * @instance: Adapter soft state
7212 * @opcode: Shutdown/Hibernate
7213 */
7214 static void megasas_shutdown_controller(struct megasas_instance *instance,
7215 u32 opcode)
7216 {
7217 struct megasas_cmd *cmd;
7218 struct megasas_dcmd_frame *dcmd;
7219
7220 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7221 return;
7222
7223 cmd = megasas_get_cmd(instance);
7224
7225 if (!cmd)
7226 return;
7227
7228 if (instance->aen_cmd)
7229 megasas_issue_blocked_abort_cmd(instance,
7230 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7231 if (instance->map_update_cmd)
7232 megasas_issue_blocked_abort_cmd(instance,
7233 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7234 if (instance->jbod_seq_cmd)
7235 megasas_issue_blocked_abort_cmd(instance,
7236 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7237
7238 dcmd = &cmd->frame->dcmd;
7239
7240 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7241
7242 dcmd->cmd = MFI_CMD_DCMD;
7243 dcmd->cmd_status = 0x0;
7244 dcmd->sge_count = 0;
7245 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7246 dcmd->timeout = 0;
7247 dcmd->pad_0 = 0;
7248 dcmd->data_xfer_len = 0;
7249 dcmd->opcode = cpu_to_le32(opcode);
7250
7251 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7252 != DCMD_SUCCESS) {
7253 dev_err(&instance->pdev->dev,
7254 "return from %s %d\n", __func__, __LINE__);
7255 return;
7256 }
7257
7258 megasas_return_cmd(instance, cmd);
7259 }
7260
7261 #ifdef CONFIG_PM
7262 /**
7263 * megasas_suspend - driver suspend entry point
7264 * @pdev: PCI device structure
7265 * @state: PCI power state to suspend routine
7266 */
7267 static int
7268 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7269 {
7270 struct Scsi_Host *host;
7271 struct megasas_instance *instance;
7272
7273 instance = pci_get_drvdata(pdev);
7274 host = instance->host;
7275 instance->unload = 1;
7276
7277 dev_info(&pdev->dev, "%s is called\n", __func__);
7278
7279 /* Shutdown SR-IOV heartbeat timer */
7280 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7281 del_timer_sync(&instance->sriov_heartbeat_timer);
7282
7283 /* Stop the FW fault detection watchdog */
7284 if (instance->adapter_type != MFI_SERIES)
7285 megasas_fusion_stop_watchdog(instance);
7286
7287 megasas_flush_cache(instance);
7288 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7289
7290 /* cancel the delayed work if this work still in queue */
7291 if (instance->ev != NULL) {
7292 struct megasas_aen_event *ev = instance->ev;
7293 cancel_delayed_work_sync(&ev->hotplug_work);
7294 instance->ev = NULL;
7295 }
7296
7297 tasklet_kill(&instance->isr_tasklet);
7298
7299 pci_set_drvdata(instance->pdev, instance);
7300 instance->instancet->disable_intr(instance);
7301
7302 megasas_destroy_irqs(instance);
7303
7304 if (instance->msix_vectors)
7305 pci_free_irq_vectors(instance->pdev);
7306
7307 pci_save_state(pdev);
7308 pci_disable_device(pdev);
7309
7310 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7311
7312 return 0;
7313 }
7314
7315 /**
7316 * megasas_resume- driver resume entry point
7317 * @pdev: PCI device structure
7318 */
7319 static int
7320 megasas_resume(struct pci_dev *pdev)
7321 {
7322 int rval;
7323 struct Scsi_Host *host;
7324 struct megasas_instance *instance;
7325 int irq_flags = PCI_IRQ_LEGACY;
7326
7327 instance = pci_get_drvdata(pdev);
7328 host = instance->host;
7329 pci_set_power_state(pdev, PCI_D0);
7330 pci_enable_wake(pdev, PCI_D0, 0);
7331 pci_restore_state(pdev);
7332
7333 dev_info(&pdev->dev, "%s is called\n", __func__);
7334 /*
7335 * PCI prepping: enable device set bus mastering and dma mask
7336 */
7337 rval = pci_enable_device_mem(pdev);
7338
7339 if (rval) {
7340 dev_err(&pdev->dev, "Enable device failed\n");
7341 return rval;
7342 }
7343
7344 pci_set_master(pdev);
7345
7346 /*
7347 * We expect the FW state to be READY
7348 */
7349 if (megasas_transition_to_ready(instance, 0))
7350 goto fail_ready_state;
7351
7352 if (megasas_set_dma_mask(instance))
7353 goto fail_set_dma_mask;
7354
7355 /*
7356 * Initialize MFI Firmware
7357 */
7358
7359 atomic_set(&instance->fw_outstanding, 0);
7360 atomic_set(&instance->ldio_outstanding, 0);
7361
7362 /* Now re-enable MSI-X */
7363 if (instance->msix_vectors) {
7364 irq_flags = PCI_IRQ_MSIX;
7365 if (instance->smp_affinity_enable)
7366 irq_flags |= PCI_IRQ_AFFINITY;
7367 }
7368 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7369 instance->msix_vectors ?
7370 instance->msix_vectors : 1, irq_flags);
7371 if (rval < 0)
7372 goto fail_reenable_msix;
7373
7374 megasas_setup_reply_map(instance);
7375
7376 if (instance->adapter_type != MFI_SERIES) {
7377 megasas_reset_reply_desc(instance);
7378 if (megasas_ioc_init_fusion(instance)) {
7379 megasas_free_cmds(instance);
7380 megasas_free_cmds_fusion(instance);
7381 goto fail_init_mfi;
7382 }
7383 if (!megasas_get_map_info(instance))
7384 megasas_sync_map_info(instance);
7385 } else {
7386 *instance->producer = 0;
7387 *instance->consumer = 0;
7388 if (megasas_issue_init_mfi(instance))
7389 goto fail_init_mfi;
7390 }
7391
7392 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7393 goto fail_init_mfi;
7394
7395 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7396 (unsigned long)instance);
7397
7398 if (instance->msix_vectors ?
7399 megasas_setup_irqs_msix(instance, 0) :
7400 megasas_setup_irqs_ioapic(instance))
7401 goto fail_init_mfi;
7402
7403 if (instance->adapter_type != MFI_SERIES)
7404 megasas_setup_irq_poll(instance);
7405
7406 /* Re-launch SR-IOV heartbeat timer */
7407 if (instance->requestorId) {
7408 if (!megasas_sriov_start_heartbeat(instance, 0))
7409 megasas_start_timer(instance);
7410 else {
7411 instance->skip_heartbeat_timer_del = 1;
7412 goto fail_init_mfi;
7413 }
7414 }
7415
7416 instance->instancet->enable_intr(instance);
7417 megasas_setup_jbod_map(instance);
7418 instance->unload = 0;
7419
7420 /*
7421 * Initiate AEN (Asynchronous Event Notification)
7422 */
7423 if (megasas_start_aen(instance))
7424 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7425
7426 /* Re-launch FW fault watchdog */
7427 if (instance->adapter_type != MFI_SERIES)
7428 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7429 goto fail_start_watchdog;
7430
7431 return 0;
7432
7433 fail_start_watchdog:
7434 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7435 del_timer_sync(&instance->sriov_heartbeat_timer);
7436 fail_init_mfi:
7437 megasas_free_ctrl_dma_buffers(instance);
7438 megasas_free_ctrl_mem(instance);
7439 scsi_host_put(host);
7440
7441 fail_reenable_msix:
7442 fail_set_dma_mask:
7443 fail_ready_state:
7444
7445 pci_disable_device(pdev);
7446
7447 return -ENODEV;
7448 }
7449 #else
7450 #define megasas_suspend NULL
7451 #define megasas_resume NULL
7452 #endif
7453
7454 static inline int
7455 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7456 {
7457 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7458 int i;
7459 u8 adp_state;
7460
7461 for (i = 0; i < wait_time; i++) {
7462 adp_state = atomic_read(&instance->adprecovery);
7463 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7464 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7465 break;
7466
7467 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7468 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7469
7470 msleep(1000);
7471 }
7472
7473 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7474 dev_info(&instance->pdev->dev,
7475 "%s HBA failed to become operational, adp_state %d\n",
7476 __func__, adp_state);
7477 return 1;
7478 }
7479
7480 return 0;
7481 }
7482
7483 /**
7484 * megasas_detach_one - PCI hot"un"plug entry point
7485 * @pdev: PCI device structure
7486 */
7487 static void megasas_detach_one(struct pci_dev *pdev)
7488 {
7489 int i;
7490 struct Scsi_Host *host;
7491 struct megasas_instance *instance;
7492 struct fusion_context *fusion;
7493 u32 pd_seq_map_sz;
7494
7495 instance = pci_get_drvdata(pdev);
7496 host = instance->host;
7497 fusion = instance->ctrl_context;
7498
7499 /* Shutdown SR-IOV heartbeat timer */
7500 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7501 del_timer_sync(&instance->sriov_heartbeat_timer);
7502
7503 /* Stop the FW fault detection watchdog */
7504 if (instance->adapter_type != MFI_SERIES)
7505 megasas_fusion_stop_watchdog(instance);
7506
7507 if (instance->fw_crash_state != UNAVAILABLE)
7508 megasas_free_host_crash_buffer(instance);
7509 scsi_remove_host(instance->host);
7510 instance->unload = 1;
7511
7512 if (megasas_wait_for_adapter_operational(instance))
7513 goto skip_firing_dcmds;
7514
7515 megasas_flush_cache(instance);
7516 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7517
7518 skip_firing_dcmds:
7519 /* cancel the delayed work if this work still in queue*/
7520 if (instance->ev != NULL) {
7521 struct megasas_aen_event *ev = instance->ev;
7522 cancel_delayed_work_sync(&ev->hotplug_work);
7523 instance->ev = NULL;
7524 }
7525
7526 /* cancel all wait events */
7527 wake_up_all(&instance->int_cmd_wait_q);
7528
7529 tasklet_kill(&instance->isr_tasklet);
7530
7531 /*
7532 * Take the instance off the instance array. Note that we will not
7533 * decrement the max_index. We let this array be sparse array
7534 */
7535 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7536 if (megasas_mgmt_info.instance[i] == instance) {
7537 megasas_mgmt_info.count--;
7538 megasas_mgmt_info.instance[i] = NULL;
7539
7540 break;
7541 }
7542 }
7543
7544 instance->instancet->disable_intr(instance);
7545
7546 megasas_destroy_irqs(instance);
7547
7548 if (instance->msix_vectors)
7549 pci_free_irq_vectors(instance->pdev);
7550
7551 if (instance->adapter_type >= VENTURA_SERIES) {
7552 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7553 kfree(fusion->stream_detect_by_ld[i]);
7554 kfree(fusion->stream_detect_by_ld);
7555 fusion->stream_detect_by_ld = NULL;
7556 }
7557
7558
7559 if (instance->adapter_type != MFI_SERIES) {
7560 megasas_release_fusion(instance);
7561 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7562 (sizeof(struct MR_PD_CFG_SEQ) *
7563 (MAX_PHYSICAL_DEVICES - 1));
7564 for (i = 0; i < 2 ; i++) {
7565 if (fusion->ld_map[i])
7566 dma_free_coherent(&instance->pdev->dev,
7567 fusion->max_map_sz,
7568 fusion->ld_map[i],
7569 fusion->ld_map_phys[i]);
7570 if (fusion->ld_drv_map[i]) {
7571 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7572 vfree(fusion->ld_drv_map[i]);
7573 else
7574 free_pages((ulong)fusion->ld_drv_map[i],
7575 fusion->drv_map_pages);
7576 }
7577
7578 if (fusion->pd_seq_sync[i])
7579 dma_free_coherent(&instance->pdev->dev,
7580 pd_seq_map_sz,
7581 fusion->pd_seq_sync[i],
7582 fusion->pd_seq_phys[i]);
7583 }
7584 } else {
7585 megasas_release_mfi(instance);
7586 }
7587
7588 if (instance->vf_affiliation)
7589 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7590 sizeof(struct MR_LD_VF_AFFILIATION),
7591 instance->vf_affiliation,
7592 instance->vf_affiliation_h);
7593
7594 if (instance->vf_affiliation_111)
7595 dma_free_coherent(&pdev->dev,
7596 sizeof(struct MR_LD_VF_AFFILIATION_111),
7597 instance->vf_affiliation_111,
7598 instance->vf_affiliation_111_h);
7599
7600 if (instance->hb_host_mem)
7601 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
7602 instance->hb_host_mem,
7603 instance->hb_host_mem_h);
7604
7605 megasas_free_ctrl_dma_buffers(instance);
7606
7607 megasas_free_ctrl_mem(instance);
7608
7609 megasas_destroy_debugfs(instance);
7610
7611 scsi_host_put(host);
7612
7613 pci_disable_device(pdev);
7614 }
7615
7616 /**
7617 * megasas_shutdown - Shutdown entry point
7618 * @device: Generic device structure
7619 */
7620 static void megasas_shutdown(struct pci_dev *pdev)
7621 {
7622 struct megasas_instance *instance = pci_get_drvdata(pdev);
7623
7624 instance->unload = 1;
7625
7626 if (megasas_wait_for_adapter_operational(instance))
7627 goto skip_firing_dcmds;
7628
7629 megasas_flush_cache(instance);
7630 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7631
7632 skip_firing_dcmds:
7633 instance->instancet->disable_intr(instance);
7634 megasas_destroy_irqs(instance);
7635
7636 if (instance->msix_vectors)
7637 pci_free_irq_vectors(instance->pdev);
7638 }
7639
7640 /**
7641 * megasas_mgmt_open - char node "open" entry point
7642 */
7643 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7644 {
7645 /*
7646 * Allow only those users with admin rights
7647 */
7648 if (!capable(CAP_SYS_ADMIN))
7649 return -EACCES;
7650
7651 return 0;
7652 }
7653
7654 /**
7655 * megasas_mgmt_fasync - Async notifier registration from applications
7656 *
7657 * This function adds the calling process to a driver global queue. When an
7658 * event occurs, SIGIO will be sent to all processes in this queue.
7659 */
7660 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7661 {
7662 int rc;
7663
7664 mutex_lock(&megasas_async_queue_mutex);
7665
7666 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7667
7668 mutex_unlock(&megasas_async_queue_mutex);
7669
7670 if (rc >= 0) {
7671 /* For sanity check when we get ioctl */
7672 filep->private_data = filep;
7673 return 0;
7674 }
7675
7676 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7677
7678 return rc;
7679 }
7680
7681 /**
7682 * megasas_mgmt_poll - char node "poll" entry point
7683 * */
7684 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
7685 {
7686 __poll_t mask;
7687 unsigned long flags;
7688
7689 poll_wait(file, &megasas_poll_wait, wait);
7690 spin_lock_irqsave(&poll_aen_lock, flags);
7691 if (megasas_poll_wait_aen)
7692 mask = (EPOLLIN | EPOLLRDNORM);
7693 else
7694 mask = 0;
7695 megasas_poll_wait_aen = 0;
7696 spin_unlock_irqrestore(&poll_aen_lock, flags);
7697 return mask;
7698 }
7699
7700 /*
7701 * megasas_set_crash_dump_params_ioctl:
7702 * Send CRASH_DUMP_MODE DCMD to all controllers
7703 * @cmd: MFI command frame
7704 */
7705
7706 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
7707 {
7708 struct megasas_instance *local_instance;
7709 int i, error = 0;
7710 int crash_support;
7711
7712 crash_support = cmd->frame->dcmd.mbox.w[0];
7713
7714 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7715 local_instance = megasas_mgmt_info.instance[i];
7716 if (local_instance && local_instance->crash_dump_drv_support) {
7717 if ((atomic_read(&local_instance->adprecovery) ==
7718 MEGASAS_HBA_OPERATIONAL) &&
7719 !megasas_set_crash_dump_params(local_instance,
7720 crash_support)) {
7721 local_instance->crash_dump_app_support =
7722 crash_support;
7723 dev_info(&local_instance->pdev->dev,
7724 "Application firmware crash "
7725 "dump mode set success\n");
7726 error = 0;
7727 } else {
7728 dev_info(&local_instance->pdev->dev,
7729 "Application firmware crash "
7730 "dump mode set failed\n");
7731 error = -1;
7732 }
7733 }
7734 }
7735 return error;
7736 }
7737
7738 /**
7739 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7740 * @instance: Adapter soft state
7741 * @argp: User's ioctl packet
7742 */
7743 static int
7744 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7745 struct megasas_iocpacket __user * user_ioc,
7746 struct megasas_iocpacket *ioc)
7747 {
7748 struct megasas_sge64 *kern_sge64 = NULL;
7749 struct megasas_sge32 *kern_sge32 = NULL;
7750 struct megasas_cmd *cmd;
7751 void *kbuff_arr[MAX_IOCTL_SGE];
7752 dma_addr_t buf_handle = 0;
7753 int error = 0, i;
7754 void *sense = NULL;
7755 dma_addr_t sense_handle;
7756 unsigned long *sense_ptr;
7757 u32 opcode = 0;
7758
7759 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7760
7761 if (ioc->sge_count > MAX_IOCTL_SGE) {
7762 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
7763 ioc->sge_count, MAX_IOCTL_SGE);
7764 return -EINVAL;
7765 }
7766
7767 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7768 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7769 !instance->support_nvme_passthru)) {
7770 dev_err(&instance->pdev->dev,
7771 "Received invalid ioctl command 0x%x\n",
7772 ioc->frame.hdr.cmd);
7773 return -ENOTSUPP;
7774 }
7775
7776 cmd = megasas_get_cmd(instance);
7777 if (!cmd) {
7778 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
7779 return -ENOMEM;
7780 }
7781
7782 /*
7783 * User's IOCTL packet has 2 frames (maximum). Copy those two
7784 * frames into our cmd's frames. cmd->frame's context will get
7785 * overwritten when we copy from user's frames. So set that value
7786 * alone separately
7787 */
7788 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
7789 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
7790 cmd->frame->hdr.pad_0 = 0;
7791
7792 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7793
7794 if (instance->consistent_mask_64bit)
7795 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7796 MFI_FRAME_SENSE64));
7797 else
7798 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
7799 MFI_FRAME_SENSE64));
7800
7801 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7802 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
7803
7804 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
7805 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7806 megasas_return_cmd(instance, cmd);
7807 return -1;
7808 }
7809 }
7810
7811 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
7812 error = megasas_set_crash_dump_params_ioctl(cmd);
7813 megasas_return_cmd(instance, cmd);
7814 return error;
7815 }
7816
7817 /*
7818 * The management interface between applications and the fw uses
7819 * MFI frames. E.g, RAID configuration changes, LD property changes
7820 * etc are accomplishes through different kinds of MFI frames. The
7821 * driver needs to care only about substituting user buffers with
7822 * kernel buffers in SGLs. The location of SGL is embedded in the
7823 * struct iocpacket itself.
7824 */
7825 if (instance->consistent_mask_64bit)
7826 kern_sge64 = (struct megasas_sge64 *)
7827 ((unsigned long)cmd->frame + ioc->sgl_off);
7828 else
7829 kern_sge32 = (struct megasas_sge32 *)
7830 ((unsigned long)cmd->frame + ioc->sgl_off);
7831
7832 /*
7833 * For each user buffer, create a mirror buffer and copy in
7834 */
7835 for (i = 0; i < ioc->sge_count; i++) {
7836 if (!ioc->sgl[i].iov_len)
7837 continue;
7838
7839 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
7840 ioc->sgl[i].iov_len,
7841 &buf_handle, GFP_KERNEL);
7842 if (!kbuff_arr[i]) {
7843 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7844 "kernel SGL buffer for IOCTL\n");
7845 error = -ENOMEM;
7846 goto out;
7847 }
7848
7849 /*
7850 * We don't change the dma_coherent_mask, so
7851 * dma_alloc_coherent only returns 32bit addresses
7852 */
7853 if (instance->consistent_mask_64bit) {
7854 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7855 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7856 } else {
7857 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7858 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7859 }
7860
7861 /*
7862 * We created a kernel buffer corresponding to the
7863 * user buffer. Now copy in from the user buffer
7864 */
7865 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7866 (u32) (ioc->sgl[i].iov_len))) {
7867 error = -EFAULT;
7868 goto out;
7869 }
7870 }
7871
7872 if (ioc->sense_len) {
7873 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7874 &sense_handle, GFP_KERNEL);
7875 if (!sense) {
7876 error = -ENOMEM;
7877 goto out;
7878 }
7879
7880 sense_ptr =
7881 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
7882 if (instance->consistent_mask_64bit)
7883 *sense_ptr = cpu_to_le64(sense_handle);
7884 else
7885 *sense_ptr = cpu_to_le32(sense_handle);
7886 }
7887
7888 /*
7889 * Set the sync_cmd flag so that the ISR knows not to complete this
7890 * cmd to the SCSI mid-layer
7891 */
7892 cmd->sync_cmd = 1;
7893 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7894 cmd->sync_cmd = 0;
7895 dev_err(&instance->pdev->dev,
7896 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7897 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7898 cmd->cmd_status_drv);
7899 return -EBUSY;
7900 }
7901
7902 cmd->sync_cmd = 0;
7903
7904 if (instance->unload == 1) {
7905 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7906 "don't submit data to application\n");
7907 goto out;
7908 }
7909 /*
7910 * copy out the kernel buffers to user buffers
7911 */
7912 for (i = 0; i < ioc->sge_count; i++) {
7913 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7914 ioc->sgl[i].iov_len)) {
7915 error = -EFAULT;
7916 goto out;
7917 }
7918 }
7919
7920 /*
7921 * copy out the sense
7922 */
7923 if (ioc->sense_len) {
7924 /*
7925 * sense_ptr points to the location that has the user
7926 * sense buffer address
7927 */
7928 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7929 ioc->sense_off);
7930
7931 if (copy_to_user((void __user *)((unsigned long)
7932 get_unaligned((unsigned long *)sense_ptr)),
7933 sense, ioc->sense_len)) {
7934 dev_err(&instance->pdev->dev, "Failed to copy out to user "
7935 "sense data\n");
7936 error = -EFAULT;
7937 goto out;
7938 }
7939 }
7940
7941 /*
7942 * copy the status codes returned by the fw
7943 */
7944 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7945 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
7946 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
7947 error = -EFAULT;
7948 }
7949
7950 out:
7951 if (sense) {
7952 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
7953 sense, sense_handle);
7954 }
7955
7956 for (i = 0; i < ioc->sge_count; i++) {
7957 if (kbuff_arr[i]) {
7958 if (instance->consistent_mask_64bit)
7959 dma_free_coherent(&instance->pdev->dev,
7960 le32_to_cpu(kern_sge64[i].length),
7961 kbuff_arr[i],
7962 le64_to_cpu(kern_sge64[i].phys_addr));
7963 else
7964 dma_free_coherent(&instance->pdev->dev,
7965 le32_to_cpu(kern_sge32[i].length),
7966 kbuff_arr[i],
7967 le32_to_cpu(kern_sge32[i].phys_addr));
7968 kbuff_arr[i] = NULL;
7969 }
7970 }
7971
7972 megasas_return_cmd(instance, cmd);
7973 return error;
7974 }
7975
7976 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7977 {
7978 struct megasas_iocpacket __user *user_ioc =
7979 (struct megasas_iocpacket __user *)arg;
7980 struct megasas_iocpacket *ioc;
7981 struct megasas_instance *instance;
7982 int error;
7983
7984 ioc = memdup_user(user_ioc, sizeof(*ioc));
7985 if (IS_ERR(ioc))
7986 return PTR_ERR(ioc);
7987
7988 instance = megasas_lookup_instance(ioc->host_no);
7989 if (!instance) {
7990 error = -ENODEV;
7991 goto out_kfree_ioc;
7992 }
7993
7994 /* Block ioctls in VF mode */
7995 if (instance->requestorId && !allow_vf_ioctls) {
7996 error = -ENODEV;
7997 goto out_kfree_ioc;
7998 }
7999
8000 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8001 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8002 error = -ENODEV;
8003 goto out_kfree_ioc;
8004 }
8005
8006 if (instance->unload == 1) {
8007 error = -ENODEV;
8008 goto out_kfree_ioc;
8009 }
8010
8011 if (down_interruptible(&instance->ioctl_sem)) {
8012 error = -ERESTARTSYS;
8013 goto out_kfree_ioc;
8014 }
8015
8016 if (megasas_wait_for_adapter_operational(instance)) {
8017 error = -ENODEV;
8018 goto out_up;
8019 }
8020
8021 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8022 out_up:
8023 up(&instance->ioctl_sem);
8024
8025 out_kfree_ioc:
8026 kfree(ioc);
8027 return error;
8028 }
8029
8030 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8031 {
8032 struct megasas_instance *instance;
8033 struct megasas_aen aen;
8034 int error;
8035
8036 if (file->private_data != file) {
8037 printk(KERN_DEBUG "megasas: fasync_helper was not "
8038 "called first\n");
8039 return -EINVAL;
8040 }
8041
8042 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8043 return -EFAULT;
8044
8045 instance = megasas_lookup_instance(aen.host_no);
8046
8047 if (!instance)
8048 return -ENODEV;
8049
8050 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8051 return -ENODEV;
8052 }
8053
8054 if (instance->unload == 1) {
8055 return -ENODEV;
8056 }
8057
8058 if (megasas_wait_for_adapter_operational(instance))
8059 return -ENODEV;
8060
8061 mutex_lock(&instance->reset_mutex);
8062 error = megasas_register_aen(instance, aen.seq_num,
8063 aen.class_locale_word);
8064 mutex_unlock(&instance->reset_mutex);
8065 return error;
8066 }
8067
8068 /**
8069 * megasas_mgmt_ioctl - char node ioctl entry point
8070 */
8071 static long
8072 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8073 {
8074 switch (cmd) {
8075 case MEGASAS_IOC_FIRMWARE:
8076 return megasas_mgmt_ioctl_fw(file, arg);
8077
8078 case MEGASAS_IOC_GET_AEN:
8079 return megasas_mgmt_ioctl_aen(file, arg);
8080 }
8081
8082 return -ENOTTY;
8083 }
8084
8085 #ifdef CONFIG_COMPAT
8086 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8087 {
8088 struct compat_megasas_iocpacket __user *cioc =
8089 (struct compat_megasas_iocpacket __user *)arg;
8090 struct megasas_iocpacket __user *ioc =
8091 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8092 int i;
8093 int error = 0;
8094 compat_uptr_t ptr;
8095 u32 local_sense_off;
8096 u32 local_sense_len;
8097 u32 user_sense_off;
8098
8099 if (clear_user(ioc, sizeof(*ioc)))
8100 return -EFAULT;
8101
8102 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8103 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8104 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8105 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8106 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8107 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8108 return -EFAULT;
8109
8110 /*
8111 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8112 * sense_len is not null, so prepare the 64bit value under
8113 * the same condition.
8114 */
8115 if (get_user(local_sense_off, &ioc->sense_off) ||
8116 get_user(local_sense_len, &ioc->sense_len) ||
8117 get_user(user_sense_off, &cioc->sense_off))
8118 return -EFAULT;
8119
8120 if (local_sense_off != user_sense_off)
8121 return -EINVAL;
8122
8123 if (local_sense_len) {
8124 void __user **sense_ioc_ptr =
8125 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
8126 compat_uptr_t *sense_cioc_ptr =
8127 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
8128 if (get_user(ptr, sense_cioc_ptr) ||
8129 put_user(compat_ptr(ptr), sense_ioc_ptr))
8130 return -EFAULT;
8131 }
8132
8133 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8134 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8135 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8136 copy_in_user(&ioc->sgl[i].iov_len,
8137 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8138 return -EFAULT;
8139 }
8140
8141 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8142
8143 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8144 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8145 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8146 return -EFAULT;
8147 }
8148 return error;
8149 }
8150
8151 static long
8152 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8153 unsigned long arg)
8154 {
8155 switch (cmd) {
8156 case MEGASAS_IOC_FIRMWARE32:
8157 return megasas_mgmt_compat_ioctl_fw(file, arg);
8158 case MEGASAS_IOC_GET_AEN:
8159 return megasas_mgmt_ioctl_aen(file, arg);
8160 }
8161
8162 return -ENOTTY;
8163 }
8164 #endif
8165
8166 /*
8167 * File operations structure for management interface
8168 */
8169 static const struct file_operations megasas_mgmt_fops = {
8170 .owner = THIS_MODULE,
8171 .open = megasas_mgmt_open,
8172 .fasync = megasas_mgmt_fasync,
8173 .unlocked_ioctl = megasas_mgmt_ioctl,
8174 .poll = megasas_mgmt_poll,
8175 #ifdef CONFIG_COMPAT
8176 .compat_ioctl = megasas_mgmt_compat_ioctl,
8177 #endif
8178 .llseek = noop_llseek,
8179 };
8180
8181 /*
8182 * PCI hotplug support registration structure
8183 */
8184 static struct pci_driver megasas_pci_driver = {
8185
8186 .name = "megaraid_sas",
8187 .id_table = megasas_pci_table,
8188 .probe = megasas_probe_one,
8189 .remove = megasas_detach_one,
8190 .suspend = megasas_suspend,
8191 .resume = megasas_resume,
8192 .shutdown = megasas_shutdown,
8193 };
8194
8195 /*
8196 * Sysfs driver attributes
8197 */
8198 static ssize_t version_show(struct device_driver *dd, char *buf)
8199 {
8200 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8201 MEGASAS_VERSION);
8202 }
8203 static DRIVER_ATTR_RO(version);
8204
8205 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8206 {
8207 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8208 MEGASAS_RELDATE);
8209 }
8210 static DRIVER_ATTR_RO(release_date);
8211
8212 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8213 {
8214 return sprintf(buf, "%u\n", support_poll_for_event);
8215 }
8216 static DRIVER_ATTR_RO(support_poll_for_event);
8217
8218 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8219 {
8220 return sprintf(buf, "%u\n", support_device_change);
8221 }
8222 static DRIVER_ATTR_RO(support_device_change);
8223
8224 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8225 {
8226 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8227 }
8228
8229 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8230 size_t count)
8231 {
8232 int retval = count;
8233
8234 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8235 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8236 retval = -EINVAL;
8237 }
8238 return retval;
8239 }
8240 static DRIVER_ATTR_RW(dbg_lvl);
8241
8242 static ssize_t
8243 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8244 {
8245 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8246 }
8247
8248 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8249
8250 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8251 {
8252 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8253 scsi_remove_device(sdev);
8254 scsi_device_put(sdev);
8255 }
8256
8257 /**
8258 * megasas_update_device_list - Update the PD and LD device list from FW
8259 * after an AEN event notification
8260 * @instance: Adapter soft state
8261 * @event_type: Indicates type of event (PD or LD event)
8262 *
8263 * @return: Success or failure
8264 *
8265 * Issue DCMDs to Firmware to update the internal device list in driver.
8266 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8267 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8268 */
8269 static
8270 int megasas_update_device_list(struct megasas_instance *instance,
8271 int event_type)
8272 {
8273 int dcmd_ret = DCMD_SUCCESS;
8274
8275 if (instance->enable_fw_dev_list) {
8276 dcmd_ret = megasas_host_device_list_query(instance, false);
8277 if (dcmd_ret != DCMD_SUCCESS)
8278 goto out;
8279 } else {
8280 if (event_type & SCAN_PD_CHANNEL) {
8281 dcmd_ret = megasas_get_pd_list(instance);
8282
8283 if (dcmd_ret != DCMD_SUCCESS)
8284 goto out;
8285 }
8286
8287 if (event_type & SCAN_VD_CHANNEL) {
8288 if (!instance->requestorId ||
8289 (instance->requestorId &&
8290 megasas_get_ld_vf_affiliation(instance, 0))) {
8291 dcmd_ret = megasas_ld_list_query(instance,
8292 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8293 if (dcmd_ret != DCMD_SUCCESS)
8294 goto out;
8295 }
8296 }
8297 }
8298
8299 out:
8300 return dcmd_ret;
8301 }
8302
8303 /**
8304 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8305 * after an AEN event notification
8306 * @instance: Adapter soft state
8307 * @scan_type: Indicates type of devices (PD/LD) to add
8308 * @return void
8309 */
8310 static
8311 void megasas_add_remove_devices(struct megasas_instance *instance,
8312 int scan_type)
8313 {
8314 int i, j;
8315 u16 pd_index = 0;
8316 u16 ld_index = 0;
8317 u16 channel = 0, id = 0;
8318 struct Scsi_Host *host;
8319 struct scsi_device *sdev1;
8320 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8321 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8322
8323 host = instance->host;
8324
8325 if (instance->enable_fw_dev_list) {
8326 targetid_list = instance->host_device_list_buf;
8327 for (i = 0; i < targetid_list->count; i++) {
8328 targetid_entry = &targetid_list->host_device_list[i];
8329 if (targetid_entry->flags.u.bits.is_sys_pd) {
8330 channel = le16_to_cpu(targetid_entry->target_id) /
8331 MEGASAS_MAX_DEV_PER_CHANNEL;
8332 id = le16_to_cpu(targetid_entry->target_id) %
8333 MEGASAS_MAX_DEV_PER_CHANNEL;
8334 } else {
8335 channel = MEGASAS_MAX_PD_CHANNELS +
8336 (le16_to_cpu(targetid_entry->target_id) /
8337 MEGASAS_MAX_DEV_PER_CHANNEL);
8338 id = le16_to_cpu(targetid_entry->target_id) %
8339 MEGASAS_MAX_DEV_PER_CHANNEL;
8340 }
8341 sdev1 = scsi_device_lookup(host, channel, id, 0);
8342 if (!sdev1) {
8343 scsi_add_device(host, channel, id, 0);
8344 } else {
8345 scsi_device_put(sdev1);
8346 }
8347 }
8348 }
8349
8350 if (scan_type & SCAN_PD_CHANNEL) {
8351 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8352 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8353 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8354 sdev1 = scsi_device_lookup(host, i, j, 0);
8355 if (instance->pd_list[pd_index].driveState ==
8356 MR_PD_STATE_SYSTEM) {
8357 if (!sdev1)
8358 scsi_add_device(host, i, j, 0);
8359 else
8360 scsi_device_put(sdev1);
8361 } else {
8362 if (sdev1)
8363 megasas_remove_scsi_device(sdev1);
8364 }
8365 }
8366 }
8367 }
8368
8369 if (scan_type & SCAN_VD_CHANNEL) {
8370 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8371 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8372 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8373 sdev1 = scsi_device_lookup(host,
8374 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8375 if (instance->ld_ids[ld_index] != 0xff) {
8376 if (!sdev1)
8377 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8378 else
8379 scsi_device_put(sdev1);
8380 } else {
8381 if (sdev1)
8382 megasas_remove_scsi_device(sdev1);
8383 }
8384 }
8385 }
8386 }
8387
8388 }
8389
8390 static void
8391 megasas_aen_polling(struct work_struct *work)
8392 {
8393 struct megasas_aen_event *ev =
8394 container_of(work, struct megasas_aen_event, hotplug_work.work);
8395 struct megasas_instance *instance = ev->instance;
8396 union megasas_evt_class_locale class_locale;
8397 int event_type = 0;
8398 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
8399 int error;
8400 u8 dcmd_ret = DCMD_SUCCESS;
8401
8402 if (!instance) {
8403 printk(KERN_ERR "invalid instance!\n");
8404 kfree(ev);
8405 return;
8406 }
8407
8408 /* Adjust event workqueue thread wait time for VF mode */
8409 if (instance->requestorId)
8410 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8411
8412 /* Don't run the event workqueue thread if OCR is running */
8413 mutex_lock(&instance->reset_mutex);
8414
8415 instance->ev = NULL;
8416 if (instance->evt_detail) {
8417 megasas_decode_evt(instance);
8418
8419 switch (le32_to_cpu(instance->evt_detail->code)) {
8420
8421 case MR_EVT_PD_INSERTED:
8422 case MR_EVT_PD_REMOVED:
8423 event_type = SCAN_PD_CHANNEL;
8424 break;
8425
8426 case MR_EVT_LD_OFFLINE:
8427 case MR_EVT_CFG_CLEARED:
8428 case MR_EVT_LD_DELETED:
8429 case MR_EVT_LD_CREATED:
8430 event_type = SCAN_VD_CHANNEL;
8431 break;
8432
8433 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8434 case MR_EVT_FOREIGN_CFG_IMPORTED:
8435 case MR_EVT_LD_STATE_CHANGE:
8436 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8437 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8438 instance->host->host_no);
8439 break;
8440
8441 case MR_EVT_CTRL_PROP_CHANGED:
8442 dcmd_ret = megasas_get_ctrl_info(instance);
8443 if (dcmd_ret == DCMD_SUCCESS &&
8444 instance->snapdump_wait_time) {
8445 megasas_get_snapdump_properties(instance);
8446 dev_info(&instance->pdev->dev,
8447 "Snap dump wait time\t: %d\n",
8448 instance->snapdump_wait_time);
8449 }
8450 break;
8451 default:
8452 event_type = 0;
8453 break;
8454 }
8455 } else {
8456 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8457 mutex_unlock(&instance->reset_mutex);
8458 kfree(ev);
8459 return;
8460 }
8461
8462 if (event_type)
8463 dcmd_ret = megasas_update_device_list(instance, event_type);
8464
8465 mutex_unlock(&instance->reset_mutex);
8466
8467 if (event_type && dcmd_ret == DCMD_SUCCESS)
8468 megasas_add_remove_devices(instance, event_type);
8469
8470 if (dcmd_ret == DCMD_SUCCESS)
8471 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8472 else
8473 seq_num = instance->last_seq_num;
8474
8475 /* Register AEN with FW for latest sequence number plus 1 */
8476 class_locale.members.reserved = 0;
8477 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8478 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8479
8480 if (instance->aen_cmd != NULL) {
8481 kfree(ev);
8482 return;
8483 }
8484
8485 mutex_lock(&instance->reset_mutex);
8486 error = megasas_register_aen(instance, seq_num,
8487 class_locale.word);
8488 if (error)
8489 dev_err(&instance->pdev->dev,
8490 "register aen failed error %x\n", error);
8491
8492 mutex_unlock(&instance->reset_mutex);
8493 kfree(ev);
8494 }
8495
8496 /**
8497 * megasas_init - Driver load entry point
8498 */
8499 static int __init megasas_init(void)
8500 {
8501 int rval;
8502
8503 /*
8504 * Booted in kdump kernel, minimize memory footprints by
8505 * disabling few features
8506 */
8507 if (reset_devices) {
8508 msix_vectors = 1;
8509 rdpq_enable = 0;
8510 dual_qdepth_disable = 1;
8511 }
8512
8513 /*
8514 * Announce driver version and other information
8515 */
8516 pr_info("megasas: %s\n", MEGASAS_VERSION);
8517
8518 spin_lock_init(&poll_aen_lock);
8519
8520 support_poll_for_event = 2;
8521 support_device_change = 1;
8522 support_nvme_encapsulation = true;
8523
8524 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8525
8526 /*
8527 * Register character device node
8528 */
8529 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8530
8531 if (rval < 0) {
8532 printk(KERN_DEBUG "megasas: failed to open device node\n");
8533 return rval;
8534 }
8535
8536 megasas_mgmt_majorno = rval;
8537
8538 megasas_init_debugfs();
8539
8540 /*
8541 * Register ourselves as PCI hotplug module
8542 */
8543 rval = pci_register_driver(&megasas_pci_driver);
8544
8545 if (rval) {
8546 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8547 goto err_pcidrv;
8548 }
8549
8550 rval = driver_create_file(&megasas_pci_driver.driver,
8551 &driver_attr_version);
8552 if (rval)
8553 goto err_dcf_attr_ver;
8554
8555 rval = driver_create_file(&megasas_pci_driver.driver,
8556 &driver_attr_release_date);
8557 if (rval)
8558 goto err_dcf_rel_date;
8559
8560 rval = driver_create_file(&megasas_pci_driver.driver,
8561 &driver_attr_support_poll_for_event);
8562 if (rval)
8563 goto err_dcf_support_poll_for_event;
8564
8565 rval = driver_create_file(&megasas_pci_driver.driver,
8566 &driver_attr_dbg_lvl);
8567 if (rval)
8568 goto err_dcf_dbg_lvl;
8569 rval = driver_create_file(&megasas_pci_driver.driver,
8570 &driver_attr_support_device_change);
8571 if (rval)
8572 goto err_dcf_support_device_change;
8573
8574 rval = driver_create_file(&megasas_pci_driver.driver,
8575 &driver_attr_support_nvme_encapsulation);
8576 if (rval)
8577 goto err_dcf_support_nvme_encapsulation;
8578
8579 return rval;
8580
8581 err_dcf_support_nvme_encapsulation:
8582 driver_remove_file(&megasas_pci_driver.driver,
8583 &driver_attr_support_device_change);
8584
8585 err_dcf_support_device_change:
8586 driver_remove_file(&megasas_pci_driver.driver,
8587 &driver_attr_dbg_lvl);
8588 err_dcf_dbg_lvl:
8589 driver_remove_file(&megasas_pci_driver.driver,
8590 &driver_attr_support_poll_for_event);
8591 err_dcf_support_poll_for_event:
8592 driver_remove_file(&megasas_pci_driver.driver,
8593 &driver_attr_release_date);
8594 err_dcf_rel_date:
8595 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8596 err_dcf_attr_ver:
8597 pci_unregister_driver(&megasas_pci_driver);
8598 err_pcidrv:
8599 megasas_exit_debugfs();
8600 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8601 return rval;
8602 }
8603
8604 /**
8605 * megasas_exit - Driver unload entry point
8606 */
8607 static void __exit megasas_exit(void)
8608 {
8609 driver_remove_file(&megasas_pci_driver.driver,
8610 &driver_attr_dbg_lvl);
8611 driver_remove_file(&megasas_pci_driver.driver,
8612 &driver_attr_support_poll_for_event);
8613 driver_remove_file(&megasas_pci_driver.driver,
8614 &driver_attr_support_device_change);
8615 driver_remove_file(&megasas_pci_driver.driver,
8616 &driver_attr_release_date);
8617 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8618 driver_remove_file(&megasas_pci_driver.driver,
8619 &driver_attr_support_nvme_encapsulation);
8620
8621 pci_unregister_driver(&megasas_pci_driver);
8622 megasas_exit_debugfs();
8623 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8624 }
8625
8626 module_init(megasas_init);
8627 module_exit(megasas_exit);