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