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