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