blob: b2339d04a700263d1164931505c19bf1c2f45bf5 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002/*
adam radford3f1530c2010-12-14 18:51:48 -08003 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05305 * Copyright (c) 2003-2013 LSI Corporation
Shivasharan S365597c2018-10-16 23:37:43 -07006 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008 *
Shivasharan S365597c2018-10-16 23:37:43 -07009 * Authors: Broadcom Inc.
adam radford3f1530c2010-12-14 18:51:48 -080010 * Sreenivas Bagalkote
11 * Sumant Patro
12 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053013 * Adam Radford
Shivasharan S365597c2018-10-16 23:37:43 -070014 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040016 *
Shivasharan S365597c2018-10-16 23:37:43 -070017 * Send feedback to: megaraidlinux.pdl@broadcom.com
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040018 */
19
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/pci.h>
23#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040024#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Shivasharan S318aaef2017-02-10 00:59:22 -080032#include <asm/unaligned.h>
Al Viro43399232005-10-04 17:36:04 +010033#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040034#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080035#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010036#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060037#include <linux/poll.h>
Shivasharan Sdef3e8d2017-08-23 04:47:03 -070038#include <linux/vmalloc.h>
Shivasharan S62a04f82019-05-07 10:05:35 -070039#include <linux/irq_poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040040
41#include <scsi/scsi.h>
42#include <scsi/scsi_cmnd.h>
43#include <scsi/scsi_device.h>
44#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070045#include <scsi/scsi_tcq.h>
Shivasharan S96c96032019-05-07 10:05:37 -070046#include <scsi/scsi_dbg.h>
adam radford9c915a82010-12-21 13:34:31 -080047#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040048#include "megaraid_sas.h"
49
bo yangad84db22007-11-09 04:40:16 -050050/*
Yang, Bo1fd10682010-10-12 07:18:50 -060051 * Number of sectors per IO command
52 * Will be set in megasas_init_mfi if user does not provide
53 */
54static unsigned int max_sectors;
Tomas Henzlea14e462019-05-29 18:00:40 +020055module_param_named(max_sectors, max_sectors, int, 0444);
Yang, Bo1fd10682010-10-12 07:18:50 -060056MODULE_PARM_DESC(max_sectors,
57 "Maximum number of sectors per IO command");
58
adam radford80d9da92010-12-21 10:17:40 -080059static int msix_disable;
Tomas Henzlea14e462019-05-29 18:00:40 +020060module_param(msix_disable, int, 0444);
adam radford80d9da92010-12-21 10:17:40 -080061MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
62
adam radford079eadd2012-10-01 19:26:59 -070063static unsigned int msix_vectors;
Tomas Henzlea14e462019-05-29 18:00:40 +020064module_param(msix_vectors, int, 0444);
adam radford079eadd2012-10-01 19:26:59 -070065MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
66
adam radford229fe472014-03-10 02:51:56 -070067static int allow_vf_ioctls;
Tomas Henzlea14e462019-05-29 18:00:40 +020068module_param(allow_vf_ioctls, int, 0444);
adam radford229fe472014-03-10 02:51:56 -070069MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
70
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053071static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
Tomas Henzlea14e462019-05-29 18:00:40 +020072module_param(throttlequeuedepth, int, 0444);
adam radfordc5daa6a2012-07-17 18:20:03 -070073MODULE_PARM_DESC(throttlequeuedepth,
74 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
75
Sumit Saxenae3d178c2016-01-28 21:04:34 +053076unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
Tomas Henzlea14e462019-05-29 18:00:40 +020077module_param(resetwaittime, int, 0444);
Shivasharan S14013712018-10-16 23:37:45 -070078MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
adam radfordc007b8b2012-07-17 18:20:24 -070079
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053080int smp_affinity_enable = 1;
Tomas Henzlea14e462019-05-29 18:00:40 +020081module_param(smp_affinity_enable, int, 0444);
Colin Ian King55d9a1d2018-04-29 13:25:32 +010082MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053083
Sumit Saxena179ac142016-01-28 21:04:28 +053084int rdpq_enable = 1;
Tomas Henzlea14e462019-05-29 18:00:40 +020085module_param(rdpq_enable, int, 0444);
Shivasharan S14013712018-10-16 23:37:45 -070086MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
Sumit Saxena179ac142016-01-28 21:04:28 +053087
Sumit Saxena308ec452016-01-28 21:04:30 +053088unsigned int dual_qdepth_disable;
Tomas Henzlea14e462019-05-29 18:00:40 +020089module_param(dual_qdepth_disable, int, 0444);
Sumit Saxena308ec452016-01-28 21:04:30 +053090MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
91
Sumit Saxenae3d178c2016-01-28 21:04:34 +053092unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
Tomas Henzlea14e462019-05-29 18:00:40 +020093module_param(scmd_timeout, int, 0444);
Sumit Saxenae3d178c2016-01-28 21:04:34 +053094MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
95
Chandrakanth Patil299ee422019-06-25 16:34:35 +053096int perf_mode = -1;
97module_param(perf_mode, int, 0444);
98MODULE_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
Shivasharan Sd956a112019-06-28 02:50:40 -0700108int event_log_level = MFI_EVT_CLASS_CRITICAL;
109module_param(event_log_level, int, 0644);
110MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
111
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400112MODULE_LICENSE("GPL");
113MODULE_VERSION(MEGASAS_VERSION);
Shivasharan S365597c2018-10-16 23:37:43 -0700114MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
115MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400116
adam radford058a8fa2011-10-08 18:14:27 -0700117int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400118static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700119static int megasas_ld_list_query(struct megasas_instance *instance,
120 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400121static int megasas_issue_init_mfi(struct megasas_instance *instance);
122static int megasas_register_aen(struct megasas_instance *instance,
123 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800124static void megasas_get_pd_info(struct megasas_instance *instance,
125 struct scsi_device *sdev);
Shivasharan Se9495e22018-06-04 03:45:12 -0700126
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400127/*
128 * PCI ID table for all supported controllers
129 */
130static struct pci_device_id megasas_pci_table[] = {
131
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
133 /* xscale IOP */
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
135 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
137 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
139 /* gen2*/
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
141 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
143 /* skinny*/
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
145 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
147 /* xscale IOP, vega */
148 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
149 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
151 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
153 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
155 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
157 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
159 /* Intruder */
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
161 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500164 /* VENTURA */
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Shivasharan S469f72d2018-11-09 09:47:20 -0800171 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
172 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
173 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
174 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
Chandrakanth Patildd807692019-06-25 16:34:20 +0530175 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
176 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
177 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
178 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200179 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400180};
181
182MODULE_DEVICE_TABLE(pci, megasas_pci_table);
183
184static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700185struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400186static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100187static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400188
Yang, Boc3518832009-10-06 14:18:02 -0600189static int megasas_poll_wait_aen;
190static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600191static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800192u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600193static u32 support_device_change;
Shivasharan Sf870bcb2018-01-05 05:33:04 -0800194static bool support_nvme_encapsulation;
Chandrakanth Patil58136852019-06-25 16:34:30 +0530195static bool support_pci_lane_margining;
Sumant Patro658dced2006-10-03 13:09:14 -0700196
Yang, Boc3518832009-10-06 14:18:02 -0600197/* define lock for aen poll */
198spinlock_t poll_aen_lock;
199
Shivasharan Sba535722019-05-07 10:05:49 -0700200extern struct dentry *megasas_debugfs_root;
201extern void megasas_init_debugfs(void);
202extern void megasas_exit_debugfs(void);
203extern void megasas_setup_debugfs(struct megasas_instance *instance);
204extern void megasas_destroy_debugfs(struct megasas_instance *instance);
205
adam radford9c915a82010-12-21 13:34:31 -0800206void
bo yang7343eb62007-11-09 04:35:44 -0500207megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
208 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800209static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800210megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
adam radfordebf054b2011-02-24 20:57:15 -0800211static int
212megasas_adp_reset_gen2(struct megasas_instance *instance,
213 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800214static irqreturn_t megasas_isr(int irq, void *devp);
215static u32
216megasas_init_adapter_mfi(struct megasas_instance *instance);
217u32
218megasas_build_and_issue_cmd(struct megasas_instance *instance,
219 struct scsi_cmnd *scmd);
220static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800221int
adam radford229fe472014-03-10 02:51:56 -0700222wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
223 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800224void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700225static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
226 int initial);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700227static int
Shivasharan S107a60d2017-10-19 02:49:05 -0700228megasas_set_dma_mask(struct megasas_instance *instance);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700229static int
230megasas_alloc_ctrl_mem(struct megasas_instance *instance);
231static inline void
232megasas_free_ctrl_mem(struct megasas_instance *instance);
233static inline int
234megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
235static inline void
236megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
237static inline void
238megasas_init_ctrl_params(struct megasas_instance *instance);
adam radfordcd50ba82010-12-21 10:23:23 -0800239
Shivasharan S272652f2018-12-17 00:47:40 -0800240u32 megasas_readl(struct megasas_instance *instance,
241 const volatile void __iomem *addr)
242{
243 u32 i = 0, ret_val;
244 /*
245 * Due to a HW errata in Aero controllers, reads to certain
246 * Fusion registers could intermittently return all zeroes.
247 * This behavior is transient in nature and subsequent reads will
248 * return valid value. As a workaround in driver, retry readl for
249 * upto three times until a non-zero value is read.
250 */
251 if (instance->adapter_type == AERO_SERIES) {
252 do {
253 ret_val = readl(addr);
254 i++;
255 } while (ret_val == 0 && i < 3);
256 return ret_val;
257 } else {
258 return readl(addr);
259 }
260}
261
Shivasharan S107a60d2017-10-19 02:49:05 -0700262/**
263 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
264 * @instance: Adapter soft state
265 * @dcmd: DCMD frame inside MFI command
266 * @dma_addr: DMA address of buffer to be passed to FW
267 * @dma_len: Length of DMA buffer to be passed to FW
268 * @return: void
269 */
270void megasas_set_dma_settings(struct megasas_instance *instance,
271 struct megasas_dcmd_frame *dcmd,
272 dma_addr_t dma_addr, u32 dma_len)
273{
274 if (instance->consistent_mask_64bit) {
275 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
276 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
277 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
278
279 } else {
280 dcmd->sgl.sge32[0].phys_addr =
281 cpu_to_le32(lower_32_bits(dma_addr));
282 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
283 dcmd->flags = cpu_to_le16(dcmd->flags);
284 }
285}
adam radfordcd50ba82010-12-21 10:23:23 -0800286
YueHaibing6764f512019-07-02 21:01:14 +0800287static void
adam radfordcd50ba82010-12-21 10:23:23 -0800288megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
289{
290 instance->instancet->fire_cmd(instance,
291 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800292 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800293}
bo yang7343eb62007-11-09 04:35:44 -0500294
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400295/**
296 * megasas_get_cmd - Get a command from the free pool
297 * @instance: Adapter soft state
298 *
299 * Returns a free command from the pool
300 */
adam radford9c915a82010-12-21 13:34:31 -0800301struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400302 *instance)
303{
304 unsigned long flags;
305 struct megasas_cmd *cmd = NULL;
306
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530307 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400308
309 if (!list_empty(&instance->cmd_pool)) {
310 cmd = list_entry((&instance->cmd_pool)->next,
311 struct megasas_cmd, list);
312 list_del_init(&cmd->list);
313 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500314 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400315 }
316
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530317 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400318 return cmd;
319}
320
321/**
322 * megasas_return_cmd - Return a cmd to free command pool
323 * @instance: Adapter soft state
324 * @cmd: Command packet to be returned to free command pool
325 */
Andi Kleen68b43742017-05-08 15:58:53 -0700326void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400327megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
328{
329 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530330 u32 blk_tags;
331 struct megasas_cmd_fusion *cmd_fusion;
332 struct fusion_context *fusion = instance->ctrl_context;
333
334 /* This flag is used only for fusion adapter.
335 * Wait for Interrupt for Polled mode DCMD
336 */
337 if (cmd->flags & DRV_DCMD_POLLED_MODE)
338 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400339
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530340 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400341
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530342 if (fusion) {
343 blk_tags = instance->max_scsi_cmds + cmd->index;
344 cmd_fusion = fusion->cmd_list[blk_tags];
345 megasas_return_cmd_fusion(instance, cmd_fusion);
346 }
347 cmd->scmd = NULL;
348 cmd->frame_count = 0;
349 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800350 memset(cmd->frame, 0, instance->mfi_frame_size);
351 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530352 if (!fusion && reset_devices)
353 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
354 list_add(&cmd->list, (&instance->cmd_pool)->next);
355
356 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
357
358}
Sumant Patro1341c932006-01-25 12:02:40 -0800359
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530360static const char *
361format_timestamp(uint32_t timestamp)
362{
363 static char buffer[32];
364
365 if ((timestamp & 0xff000000) == 0xff000000)
366 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
367 0x00ffffff);
368 else
369 snprintf(buffer, sizeof(buffer), "%us", timestamp);
370 return buffer;
371}
372
373static const char *
374format_class(int8_t class)
375{
376 static char buffer[6];
377
378 switch (class) {
379 case MFI_EVT_CLASS_DEBUG:
380 return "debug";
381 case MFI_EVT_CLASS_PROGRESS:
382 return "progress";
383 case MFI_EVT_CLASS_INFO:
384 return "info";
385 case MFI_EVT_CLASS_WARNING:
386 return "WARN";
387 case MFI_EVT_CLASS_CRITICAL:
388 return "CRIT";
389 case MFI_EVT_CLASS_FATAL:
390 return "FATAL";
391 case MFI_EVT_CLASS_DEAD:
392 return "DEAD";
393 default:
394 snprintf(buffer, sizeof(buffer), "%d", class);
395 return buffer;
396 }
397}
398
399/**
400 * megasas_decode_evt: Decode FW AEN event and print critical event
401 * for information.
402 * @instance: Adapter soft state
403 */
404static void
405megasas_decode_evt(struct megasas_instance *instance)
406{
407 struct megasas_evt_detail *evt_detail = instance->evt_detail;
408 union megasas_evt_class_locale class_locale;
409 class_locale.word = le32_to_cpu(evt_detail->cl.word);
410
Shivasharan Sd956a112019-06-28 02:50:40 -0700411 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
412 (event_log_level > MFI_EVT_CLASS_DEAD)) {
413 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");
414 event_log_level = MFI_EVT_CLASS_CRITICAL;
415 }
416
417 if (class_locale.members.class >= event_log_level)
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530418 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
419 le32_to_cpu(evt_detail->seq_num),
420 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
421 (class_locale.members.locale),
422 format_class(class_locale.members.class),
423 evt_detail->description);
424}
425
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400426/**
adam radford0d490162010-12-14 19:17:17 -0800427* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800428* (deviceid : 1064R, PERC5) controllers
429*/
430
431/**
432 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400433 * @regs: MFI register set
434 */
435static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530436megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400437{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530438 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500439
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530440 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400441 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400442
443 /* Dummy readl to force pci flush */
444 readl(&regs->outbound_intr_mask);
445}
446
447/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700448 * megasas_disable_intr_xscale -Disables interrupt
449 * @regs: MFI register set
450 */
451static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530452megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700453{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530454 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700455 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500456
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530457 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700458 writel(mask, &regs->outbound_intr_mask);
459 /* Dummy readl to force pci flush */
460 readl(&regs->outbound_intr_mask);
461}
462
463/**
Sumant Patro1341c932006-01-25 12:02:40 -0800464 * megasas_read_fw_status_reg_xscale - returns the current FW status value
465 * @regs: MFI register set
466 */
467static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800468megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800469{
Shivasharan Sde516372018-12-17 00:47:39 -0800470 return readl(&instance->reg_set->outbound_msg_0);
Sumant Patro1341c932006-01-25 12:02:40 -0800471}
472/**
473 * megasas_clear_interrupt_xscale - Check & clear interrupt
474 * @regs: MFI register set
475 */
adam radford0d490162010-12-14 19:17:17 -0800476static int
Shivasharan Sde516372018-12-17 00:47:39 -0800477megasas_clear_intr_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800478{
479 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400480 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800481 struct megasas_register_set __iomem *regs;
482 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500483
Sumant Patro1341c932006-01-25 12:02:40 -0800484 /*
485 * Check if it is our interrupt
486 */
487 status = readl(&regs->outbound_intr_status);
488
bo yang39a98552010-09-22 22:36:29 -0400489 if (status & MFI_OB_INTR_STATUS_MASK)
490 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
491 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
492 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800493
494 /*
495 * Clear the interrupt by writing back the same value
496 */
bo yang39a98552010-09-22 22:36:29 -0400497 if (mfiStatus)
498 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800499
Yang, Bo06f579d2008-08-10 12:42:37 -0700500 /* Dummy readl to force pci flush */
501 readl(&regs->outbound_intr_status);
502
bo yang39a98552010-09-22 22:36:29 -0400503 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800504}
505
506/**
507 * megasas_fire_cmd_xscale - Sends command to the FW
508 * @frame_phys_addr : Physical address of cmd
509 * @frame_count : Number of frames for the command
510 * @regs : MFI register set
511 */
adam radford0d490162010-12-14 19:17:17 -0800512static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600513megasas_fire_cmd_xscale(struct megasas_instance *instance,
514 dma_addr_t frame_phys_addr,
515 u32 frame_count,
516 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800517{
bo yang39a98552010-09-22 22:36:29 -0400518 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500519
bo yang39a98552010-09-22 22:36:29 -0400520 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800521 writel((frame_phys_addr >> 3)|(frame_count),
522 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400523 spin_unlock_irqrestore(&instance->hba_lock, flags);
524}
525
526/**
527 * megasas_adp_reset_xscale - For controller reset
528 * @regs: MFI register set
529 */
530static int
531megasas_adp_reset_xscale(struct megasas_instance *instance,
532 struct megasas_register_set __iomem *regs)
533{
534 u32 i;
535 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500536
bo yang39a98552010-09-22 22:36:29 -0400537 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
538
539 for (i = 0; i < 3; i++)
540 msleep(1000); /* sleep for 3 secs */
541 pcidata = 0;
542 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500543 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400544 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500545 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400546 pcidata &= ~0x2;
547 pci_write_config_dword(instance->pdev,
548 MFI_1068_PCSR_OFFSET, pcidata);
549
550 for (i = 0; i < 2; i++)
551 msleep(1000); /* need to wait 2 secs again */
552
553 pcidata = 0;
554 pci_read_config_dword(instance->pdev,
555 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500556 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400557 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500558 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400559 pcidata = 0;
560 pci_write_config_dword(instance->pdev,
561 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
562 }
563 }
564 return 0;
565}
566
567/**
568 * megasas_check_reset_xscale - For controller reset check
569 * @regs: MFI register set
570 */
571static int
572megasas_check_reset_xscale(struct megasas_instance *instance,
573 struct megasas_register_set __iomem *regs)
574{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530575 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530576 (le32_to_cpu(*instance->consumer) ==
577 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400578 return 1;
bo yang39a98552010-09-22 22:36:29 -0400579 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800580}
581
582static struct megasas_instance_template megasas_instance_template_xscale = {
583
584 .fire_cmd = megasas_fire_cmd_xscale,
585 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700586 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800587 .clear_intr = megasas_clear_intr_xscale,
588 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400589 .adp_reset = megasas_adp_reset_xscale,
590 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800591 .service_isr = megasas_isr,
592 .tasklet = megasas_complete_cmd_dpc,
593 .init_adapter = megasas_init_adapter_mfi,
594 .build_and_issue_cmd = megasas_build_and_issue_cmd,
595 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800596};
597
598/**
adam radford0d490162010-12-14 19:17:17 -0800599* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800600* to xscale (deviceid : 1064R, PERC5) controllers
601*/
602
603/**
adam radford0d490162010-12-14 19:17:17 -0800604* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500605* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800606*/
607
608/**
609 * megasas_enable_intr_ppc - Enables interrupts
610 * @regs: MFI register set
611 */
612static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530613megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800614{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530615 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500616
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530617 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800618 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800619
bo yang39a98552010-09-22 22:36:29 -0400620 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800621
622 /* Dummy readl to force pci flush */
623 readl(&regs->outbound_intr_mask);
624}
625
626/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700627 * megasas_disable_intr_ppc - Disable interrupt
628 * @regs: MFI register set
629 */
630static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530631megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700632{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530633 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700634 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500635
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530636 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700637 writel(mask, &regs->outbound_intr_mask);
638 /* Dummy readl to force pci flush */
639 readl(&regs->outbound_intr_mask);
640}
641
642/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800643 * megasas_read_fw_status_reg_ppc - returns the current FW status value
644 * @regs: MFI register set
645 */
646static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800647megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800648{
Shivasharan Sde516372018-12-17 00:47:39 -0800649 return readl(&instance->reg_set->outbound_scratch_pad_0);
Sumant Patrof9876f02006-02-03 15:34:35 -0800650}
651
652/**
653 * megasas_clear_interrupt_ppc - Check & clear interrupt
654 * @regs: MFI register set
655 */
adam radford0d490162010-12-14 19:17:17 -0800656static int
Shivasharan Sde516372018-12-17 00:47:39 -0800657megasas_clear_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800658{
adam radford3cc6851f92011-05-11 18:34:52 -0700659 u32 status, mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800660 struct megasas_register_set __iomem *regs;
661 regs = instance->reg_set;
adam radford3cc6851f92011-05-11 18:34:52 -0700662
Sumant Patrof9876f02006-02-03 15:34:35 -0800663 /*
664 * Check if it is our interrupt
665 */
666 status = readl(&regs->outbound_intr_status);
667
adam radford3cc6851f92011-05-11 18:34:52 -0700668 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
669 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
670
671 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
672 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800673
674 /*
675 * Clear the interrupt by writing back the same value
676 */
677 writel(status, &regs->outbound_doorbell_clear);
678
Yang, Bo06f579d2008-08-10 12:42:37 -0700679 /* Dummy readl to force pci flush */
680 readl(&regs->outbound_doorbell_clear);
681
adam radford3cc6851f92011-05-11 18:34:52 -0700682 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800683}
adam radford3cc6851f92011-05-11 18:34:52 -0700684
Sumant Patrof9876f02006-02-03 15:34:35 -0800685/**
686 * megasas_fire_cmd_ppc - Sends command to the FW
687 * @frame_phys_addr : Physical address of cmd
688 * @frame_count : Number of frames for the command
689 * @regs : MFI register set
690 */
adam radford0d490162010-12-14 19:17:17 -0800691static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600692megasas_fire_cmd_ppc(struct megasas_instance *instance,
693 dma_addr_t frame_phys_addr,
694 u32 frame_count,
695 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800696{
bo yang39a98552010-09-22 22:36:29 -0400697 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500698
bo yang39a98552010-09-22 22:36:29 -0400699 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800700 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800701 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400702 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800703}
704
bo yang39a98552010-09-22 22:36:29 -0400705/**
bo yang39a98552010-09-22 22:36:29 -0400706 * megasas_check_reset_ppc - For controller reset check
707 * @regs: MFI register set
708 */
709static int
710megasas_check_reset_ppc(struct megasas_instance *instance,
711 struct megasas_register_set __iomem *regs)
712{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530713 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700714 return 1;
715
bo yang39a98552010-09-22 22:36:29 -0400716 return 0;
717}
adam radford3cc6851f92011-05-11 18:34:52 -0700718
Sumant Patrof9876f02006-02-03 15:34:35 -0800719static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800720
Sumant Patrof9876f02006-02-03 15:34:35 -0800721 .fire_cmd = megasas_fire_cmd_ppc,
722 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700723 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800724 .clear_intr = megasas_clear_intr_ppc,
725 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700726 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400727 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800728 .service_isr = megasas_isr,
729 .tasklet = megasas_complete_cmd_dpc,
730 .init_adapter = megasas_init_adapter_mfi,
731 .build_and_issue_cmd = megasas_build_and_issue_cmd,
732 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800733};
734
735/**
Yang, Bo87911122009-10-06 14:31:54 -0600736 * megasas_enable_intr_skinny - Enables interrupts
737 * @regs: MFI register set
738 */
739static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530740megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600741{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530742 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500743
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530744 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600745 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
746
747 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
748
749 /* Dummy readl to force pci flush */
750 readl(&regs->outbound_intr_mask);
751}
752
753/**
754 * megasas_disable_intr_skinny - Disables interrupt
755 * @regs: MFI register set
756 */
757static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530758megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600759{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530760 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600761 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500762
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530763 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600764 writel(mask, &regs->outbound_intr_mask);
765 /* Dummy readl to force pci flush */
766 readl(&regs->outbound_intr_mask);
767}
768
769/**
770 * megasas_read_fw_status_reg_skinny - returns the current FW status value
771 * @regs: MFI register set
772 */
773static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800774megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600775{
Shivasharan Sde516372018-12-17 00:47:39 -0800776 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo87911122009-10-06 14:31:54 -0600777}
778
779/**
780 * megasas_clear_interrupt_skinny - Check & clear interrupt
781 * @regs: MFI register set
782 */
783static int
Shivasharan Sde516372018-12-17 00:47:39 -0800784megasas_clear_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600785{
786 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800787 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800788 struct megasas_register_set __iomem *regs;
789 regs = instance->reg_set;
adam radfordebf054b2011-02-24 20:57:15 -0800790
Yang, Bo87911122009-10-06 14:31:54 -0600791 /*
792 * Check if it is our interrupt
793 */
794 status = readl(&regs->outbound_intr_status);
795
796 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400797 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600798 }
799
800 /*
adam radfordebf054b2011-02-24 20:57:15 -0800801 * Check if it is our interrupt
802 */
Shivasharan Sde516372018-12-17 00:47:39 -0800803 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800804 MFI_STATE_FAULT) {
805 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
806 } else
807 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
808
809 /*
Yang, Bo87911122009-10-06 14:31:54 -0600810 * Clear the interrupt by writing back the same value
811 */
812 writel(status, &regs->outbound_intr_status);
813
814 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500815 * dummy read to flush PCI
816 */
Yang, Bo87911122009-10-06 14:31:54 -0600817 readl(&regs->outbound_intr_status);
818
adam radfordebf054b2011-02-24 20:57:15 -0800819 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600820}
821
822/**
823 * megasas_fire_cmd_skinny - Sends command to the FW
824 * @frame_phys_addr : Physical address of cmd
825 * @frame_count : Number of frames for the command
826 * @regs : MFI register set
827 */
828static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600829megasas_fire_cmd_skinny(struct megasas_instance *instance,
830 dma_addr_t frame_phys_addr,
831 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600832 struct megasas_register_set __iomem *regs)
833{
Yang, Bo0c79e682009-10-06 14:47:35 -0600834 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500835
bo yang39a98552010-09-22 22:36:29 -0400836 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530837 writel(upper_32_bits(frame_phys_addr),
838 &(regs)->inbound_high_queue_port);
839 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
840 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400841 spin_unlock_irqrestore(&instance->hba_lock, flags);
842}
843
844/**
bo yang39a98552010-09-22 22:36:29 -0400845 * megasas_check_reset_skinny - For controller reset check
846 * @regs: MFI register set
847 */
848static int
849megasas_check_reset_skinny(struct megasas_instance *instance,
850 struct megasas_register_set __iomem *regs)
851{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530852 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700853 return 1;
854
bo yang39a98552010-09-22 22:36:29 -0400855 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600856}
857
858static struct megasas_instance_template megasas_instance_template_skinny = {
859
860 .fire_cmd = megasas_fire_cmd_skinny,
861 .enable_intr = megasas_enable_intr_skinny,
862 .disable_intr = megasas_disable_intr_skinny,
863 .clear_intr = megasas_clear_intr_skinny,
864 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800865 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400866 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800867 .service_isr = megasas_isr,
868 .tasklet = megasas_complete_cmd_dpc,
869 .init_adapter = megasas_init_adapter_mfi,
870 .build_and_issue_cmd = megasas_build_and_issue_cmd,
871 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600872};
873
874
875/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700876* The following functions are defined for gen2 (deviceid : 0x78 0x79)
877* controllers
878*/
879
880/**
881 * megasas_enable_intr_gen2 - Enables interrupts
882 * @regs: MFI register set
883 */
884static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530885megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700886{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530887 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500888
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530889 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700890 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
891
892 /* write ~0x00000005 (4 & 1) to the intr mask*/
893 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
894
895 /* Dummy readl to force pci flush */
896 readl(&regs->outbound_intr_mask);
897}
898
899/**
900 * megasas_disable_intr_gen2 - Disables interrupt
901 * @regs: MFI register set
902 */
903static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530904megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700905{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530906 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700907 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500908
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530909 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700910 writel(mask, &regs->outbound_intr_mask);
911 /* Dummy readl to force pci flush */
912 readl(&regs->outbound_intr_mask);
913}
914
915/**
916 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
917 * @regs: MFI register set
918 */
919static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800920megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700921{
Shivasharan Sde516372018-12-17 00:47:39 -0800922 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700923}
924
925/**
926 * megasas_clear_interrupt_gen2 - Check & clear interrupt
927 * @regs: MFI register set
928 */
929static int
Shivasharan Sde516372018-12-17 00:47:39 -0800930megasas_clear_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700931{
932 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400933 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800934 struct megasas_register_set __iomem *regs;
935 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500936
Yang, Bo6610a6b2008-08-10 12:42:38 -0700937 /*
938 * Check if it is our interrupt
939 */
940 status = readl(&regs->outbound_intr_status);
941
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530942 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400943 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
944 }
945 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
946 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
947 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700948
949 /*
950 * Clear the interrupt by writing back the same value
951 */
bo yang39a98552010-09-22 22:36:29 -0400952 if (mfiStatus)
953 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700954
955 /* Dummy readl to force pci flush */
956 readl(&regs->outbound_intr_status);
957
bo yang39a98552010-09-22 22:36:29 -0400958 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700959}
960/**
961 * megasas_fire_cmd_gen2 - Sends command to the FW
962 * @frame_phys_addr : Physical address of cmd
963 * @frame_count : Number of frames for the command
964 * @regs : MFI register set
965 */
966static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600967megasas_fire_cmd_gen2(struct megasas_instance *instance,
968 dma_addr_t frame_phys_addr,
969 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700970 struct megasas_register_set __iomem *regs)
971{
bo yang39a98552010-09-22 22:36:29 -0400972 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500973
bo yang39a98552010-09-22 22:36:29 -0400974 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700975 writel((frame_phys_addr | (frame_count<<1))|1,
976 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400977 spin_unlock_irqrestore(&instance->hba_lock, flags);
978}
979
980/**
981 * megasas_adp_reset_gen2 - For controller reset
982 * @regs: MFI register set
983 */
984static int
985megasas_adp_reset_gen2(struct megasas_instance *instance,
986 struct megasas_register_set __iomem *reg_set)
987{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500988 u32 retry = 0 ;
989 u32 HostDiag;
990 u32 __iomem *seq_offset = &reg_set->seq_offset;
991 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400992
adam radfordebf054b2011-02-24 20:57:15 -0800993 if (instance->instancet == &megasas_instance_template_skinny) {
994 seq_offset = &reg_set->fusion_seq_offset;
995 hostdiag_offset = &reg_set->fusion_host_diag;
996 }
997
998 writel(0, seq_offset);
999 writel(4, seq_offset);
1000 writel(0xb, seq_offset);
1001 writel(2, seq_offset);
1002 writel(7, seq_offset);
1003 writel(0xd, seq_offset);
1004
bo yang39a98552010-09-22 22:36:29 -04001005 msleep(1000);
1006
adam radfordebf054b2011-02-24 20:57:15 -08001007 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -04001008
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001009 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -04001010 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -08001011 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001012 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -04001013 retry, HostDiag);
1014
1015 if (retry++ >= 100)
1016 return 1;
1017
1018 }
1019
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001020 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -04001021
adam radfordebf054b2011-02-24 20:57:15 -08001022 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -04001023
1024 ssleep(10);
1025
adam radfordebf054b2011-02-24 20:57:15 -08001026 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001027 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -04001028 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -08001029 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001030 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -04001031 retry, HostDiag);
1032
1033 if (retry++ >= 1000)
1034 return 1;
1035
1036 }
1037 return 0;
1038}
1039
1040/**
1041 * megasas_check_reset_gen2 - For controller reset check
1042 * @regs: MFI register set
1043 */
1044static int
1045megasas_check_reset_gen2(struct megasas_instance *instance,
1046 struct megasas_register_set __iomem *regs)
1047{
Sumit Saxena8a01a412016-01-28 21:04:32 +05301048 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -06001049 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -06001050
bo yang39a98552010-09-22 22:36:29 -04001051 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -07001052}
1053
1054static struct megasas_instance_template megasas_instance_template_gen2 = {
1055
1056 .fire_cmd = megasas_fire_cmd_gen2,
1057 .enable_intr = megasas_enable_intr_gen2,
1058 .disable_intr = megasas_disable_intr_gen2,
1059 .clear_intr = megasas_clear_intr_gen2,
1060 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -04001061 .adp_reset = megasas_adp_reset_gen2,
1062 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -08001063 .service_isr = megasas_isr,
1064 .tasklet = megasas_complete_cmd_dpc,
1065 .init_adapter = megasas_init_adapter_mfi,
1066 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1067 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -07001068};
1069
1070/**
Sumant Patrof9876f02006-02-03 15:34:35 -08001071* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -04001072* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -08001073*/
1074
adam radford9c915a82010-12-21 13:34:31 -08001075/*
1076 * Template added for TB (Fusion)
1077 */
1078extern struct megasas_instance_template megasas_instance_template_fusion;
1079
Sumant Patrof9876f02006-02-03 15:34:35 -08001080/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001081 * megasas_issue_polled - Issues a polling command
1082 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -08001083 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001084 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301085 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001086 */
adam radford9c915a82010-12-21 13:34:31 -08001087int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001088megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1089{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1091
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301092 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301093 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001094
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001095 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301096 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1097 __func__, __LINE__);
1098 return DCMD_NOT_FIRED;
1099 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001100
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001101 instance->instancet->issue_dcmd(instance, cmd);
1102
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301103 return wait_and_poll(instance, cmd, instance->requestorId ?
1104 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001105}
1106
1107/**
1108 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1109 * @instance: Adapter soft state
1110 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301111 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001112 *
1113 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001114 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001115 * Used to issue ioctl commands.
1116 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301117int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001118megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301119 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301121 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301122 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001123
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001124 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301125 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1126 __func__, __LINE__);
1127 return DCMD_NOT_FIRED;
1128 }
1129
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001130 instance->instancet->issue_dcmd(instance, cmd);
1131
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301132 if (timeout) {
1133 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301134 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301135 if (!ret) {
Shivasharan S2ce43502019-05-07 10:05:38 -07001136 dev_err(&instance->pdev->dev,
1137 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1138 cmd->frame->dcmd.opcode, __func__);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301139 return DCMD_TIMEOUT;
1140 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301141 } else
1142 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301143 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001144
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301145 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301146 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001147}
1148
1149/**
1150 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1151 * @instance: Adapter soft state
1152 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301153 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001154 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301155 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001156 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001157 * cmd and waits for return status.
1158 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001159 */
1160static int
1161megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301162 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163{
1164 struct megasas_cmd *cmd;
1165 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301166 int ret = 0;
Shivasharan S2ce43502019-05-07 10:05:38 -07001167 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001168
1169 cmd = megasas_get_cmd(instance);
1170
1171 if (!cmd)
1172 return -1;
1173
1174 abort_fr = &cmd->frame->abort;
1175
1176 /*
1177 * Prepare and issue the abort frame
1178 */
1179 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301180 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301181 abort_fr->flags = cpu_to_le16(0);
1182 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1183 abort_fr->abort_mfi_phys_addr_lo =
1184 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1185 abort_fr->abort_mfi_phys_addr_hi =
1186 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001187
1188 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301189 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001190
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001191 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301192 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1193 __func__, __LINE__);
1194 return DCMD_NOT_FIRED;
1195 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001196
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001197 instance->instancet->issue_dcmd(instance, cmd);
1198
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301199 if (timeout) {
1200 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301201 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301202 if (!ret) {
Shivasharan S2ce43502019-05-07 10:05:38 -07001203 opcode = cmd_to_abort->frame->dcmd.opcode;
1204 dev_err(&instance->pdev->dev,
1205 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1206 opcode, __func__);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301207 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301208 }
1209 } else
1210 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301211 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301212
bo yang39a98552010-09-22 22:36:29 -04001213 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001214
1215 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301216 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1217 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001218}
1219
1220/**
1221 * megasas_make_sgl32 - Prepares 32-bit SGL
1222 * @instance: Adapter soft state
1223 * @scp: SCSI command from the mid-layer
1224 * @mfi_sgl: SGL to be filled in
1225 *
1226 * If successful, this function returns the number of SG elements. Otherwise,
1227 * it returnes -1.
1228 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001229static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001230megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1231 union megasas_sgl *mfi_sgl)
1232{
1233 int i;
1234 int sge_count;
1235 struct scatterlist *os_sgl;
1236
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001237 sge_count = scsi_dma_map(scp);
1238 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001239
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001240 if (sge_count) {
1241 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301242 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1243 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001244 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001245 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001246 return sge_count;
1247}
1248
1249/**
1250 * megasas_make_sgl64 - Prepares 64-bit SGL
1251 * @instance: Adapter soft state
1252 * @scp: SCSI command from the mid-layer
1253 * @mfi_sgl: SGL to be filled in
1254 *
1255 * If successful, this function returns the number of SG elements. Otherwise,
1256 * it returnes -1.
1257 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001258static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001259megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1260 union megasas_sgl *mfi_sgl)
1261{
1262 int i;
1263 int sge_count;
1264 struct scatterlist *os_sgl;
1265
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001266 sge_count = scsi_dma_map(scp);
1267 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001268
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001269 if (sge_count) {
1270 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301271 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1272 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001273 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001274 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001275 return sge_count;
1276}
1277
Yang, Bof4c9a132009-10-06 14:43:28 -06001278/**
1279 * megasas_make_sgl_skinny - Prepares IEEE SGL
1280 * @instance: Adapter soft state
1281 * @scp: SCSI command from the mid-layer
1282 * @mfi_sgl: SGL to be filled in
1283 *
1284 * If successful, this function returns the number of SG elements. Otherwise,
1285 * it returnes -1.
1286 */
1287static int
1288megasas_make_sgl_skinny(struct megasas_instance *instance,
1289 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1290{
1291 int i;
1292 int sge_count;
1293 struct scatterlist *os_sgl;
1294
1295 sge_count = scsi_dma_map(scp);
1296
1297 if (sge_count) {
1298 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301299 mfi_sgl->sge_skinny[i].length =
1300 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001301 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301302 cpu_to_le64(sg_dma_address(os_sgl));
1303 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001304 }
1305 }
1306 return sge_count;
1307}
1308
Sumant Patrob1df99d2006-10-03 12:40:47 -07001309 /**
1310 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001311 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001312 * @sge_count : number of sg elements
1313 *
1314 * Returns the number of frames required for numnber of sge's (sge_count)
1315 */
1316
Yang, Bof4c9a132009-10-06 14:43:28 -06001317static u32 megasas_get_frame_count(struct megasas_instance *instance,
1318 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001319{
1320 int num_cnt;
1321 int sge_bytes;
1322 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001323 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001324
1325 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1326 sizeof(struct megasas_sge32);
1327
Yang, Bof4c9a132009-10-06 14:43:28 -06001328 if (instance->flag_ieee) {
1329 sge_sz = sizeof(struct megasas_sge_skinny);
1330 }
1331
Sumant Patrob1df99d2006-10-03 12:40:47 -07001332 /*
bo yangd532dbe2008-03-17 03:36:43 -04001333 * Main frame can contain 2 SGEs for 64-bit SGLs and
1334 * 3 SGEs for 32-bit SGLs for ldio &
1335 * 1 SGEs for 64-bit SGLs and
1336 * 2 SGEs for 32-bit SGLs for pthru frame
1337 */
1338 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001339 if (instance->flag_ieee == 1) {
1340 num_cnt = sge_count - 1;
1341 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001342 num_cnt = sge_count - 1;
1343 else
1344 num_cnt = sge_count - 2;
1345 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001346 if (instance->flag_ieee == 1) {
1347 num_cnt = sge_count - 1;
1348 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001349 num_cnt = sge_count - 2;
1350 else
1351 num_cnt = sge_count - 3;
1352 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001353
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001354 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001355 sge_bytes = sge_sz * num_cnt;
1356
1357 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1358 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1359 }
1360 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001361 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001362
1363 if (frame_count > 7)
1364 frame_count = 8;
1365 return frame_count;
1366}
1367
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001368/**
1369 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1370 * @instance: Adapter soft state
1371 * @scp: SCSI command
1372 * @cmd: Command to be prepared in
1373 *
1374 * This function prepares CDB commands. These are typcially pass-through
1375 * commands to the devices.
1376 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001377static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001378megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1379 struct megasas_cmd *cmd)
1380{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001381 u32 is_logical;
1382 u32 device_id;
1383 u16 flags = 0;
1384 struct megasas_pthru_frame *pthru;
1385
Shivasharan S3cabd162017-02-10 00:59:05 -08001386 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301387 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388 pthru = (struct megasas_pthru_frame *)cmd->frame;
1389
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001390 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001391 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001392 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393 flags = MFI_FRAME_DIR_READ;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001394 else if (scp->sc_data_direction == DMA_NONE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001395 flags = MFI_FRAME_DIR_NONE;
1396
Yang, Bof4c9a132009-10-06 14:43:28 -06001397 if (instance->flag_ieee == 1) {
1398 flags |= MFI_FRAME_IEEE;
1399 }
1400
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401 /*
1402 * Prepare the DCDB frame
1403 */
1404 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1405 pthru->cmd_status = 0x0;
1406 pthru->scsi_status = 0x0;
1407 pthru->target_id = device_id;
1408 pthru->lun = scp->device->lun;
1409 pthru->cdb_len = scp->cmd_len;
1410 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001411 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301412 pthru->flags = cpu_to_le16(flags);
1413 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001414
1415 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1416
1417 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001418 * If the command is for the tape device, set the
1419 * pthru timeout to the os layer timeout value.
1420 */
Yang, Bo8d568252009-10-06 14:12:21 -06001421 if (scp->device->type == TYPE_TAPE) {
1422 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301423 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001424 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301425 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001426 }
1427
1428 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001429 * Construct SGL
1430 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001431 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301432 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001433 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1434 &pthru->sgl);
1435 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301436 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001437 pthru->sge_count = megasas_make_sgl64(instance, scp,
1438 &pthru->sgl);
1439 } else
1440 pthru->sge_count = megasas_make_sgl32(instance, scp,
1441 &pthru->sgl);
1442
Yang, Bobdc6fb82009-12-06 08:30:19 -07001443 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001444 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001445 pthru->sge_count);
1446 return 0;
1447 }
1448
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001449 /*
1450 * Sense info specific
1451 */
1452 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301453 pthru->sense_buf_phys_addr_hi =
1454 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1455 pthru->sense_buf_phys_addr_lo =
1456 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001457
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001458 /*
1459 * Compute the total number of frames this command consumes. FW uses
1460 * this number to pull sufficient number of frames from host memory.
1461 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001462 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001463 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464
1465 return cmd->frame_count;
1466}
1467
1468/**
1469 * megasas_build_ldio - Prepares IOs to logical devices
1470 * @instance: Adapter soft state
1471 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001472 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001473 *
1474 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1475 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001476static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001477megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1478 struct megasas_cmd *cmd)
1479{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001480 u32 device_id;
1481 u8 sc = scp->cmnd[0];
1482 u16 flags = 0;
1483 struct megasas_io_frame *ldio;
1484
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301485 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001486 ldio = (struct megasas_io_frame *)cmd->frame;
1487
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001488 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001489 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001490 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001491 flags = MFI_FRAME_DIR_READ;
1492
Yang, Bof4c9a132009-10-06 14:43:28 -06001493 if (instance->flag_ieee == 1) {
1494 flags |= MFI_FRAME_IEEE;
1495 }
1496
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001497 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001498 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001499 */
1500 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1501 ldio->cmd_status = 0x0;
1502 ldio->scsi_status = 0x0;
1503 ldio->target_id = device_id;
1504 ldio->timeout = 0;
1505 ldio->reserved_0 = 0;
1506 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301507 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001508 ldio->start_lba_hi = 0;
1509 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1510
1511 /*
1512 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1513 */
1514 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301515 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1516 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1517 ((u32) scp->cmnd[2] << 8) |
1518 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001519
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301520 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001521 }
1522
1523 /*
1524 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1525 */
1526 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301527 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1528 ((u32) scp->cmnd[7] << 8));
1529 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1530 ((u32) scp->cmnd[3] << 16) |
1531 ((u32) scp->cmnd[4] << 8) |
1532 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001533 }
1534
1535 /*
1536 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1537 */
1538 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301539 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1540 ((u32) scp->cmnd[7] << 16) |
1541 ((u32) scp->cmnd[8] << 8) |
1542 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001543
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301544 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1545 ((u32) scp->cmnd[3] << 16) |
1546 ((u32) scp->cmnd[4] << 8) |
1547 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001548 }
1549
1550 /*
1551 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1552 */
1553 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301554 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1555 ((u32) scp->cmnd[11] << 16) |
1556 ((u32) scp->cmnd[12] << 8) |
1557 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001558
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301559 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1560 ((u32) scp->cmnd[7] << 16) |
1561 ((u32) scp->cmnd[8] << 8) |
1562 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001563
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301564 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1565 ((u32) scp->cmnd[3] << 16) |
1566 ((u32) scp->cmnd[4] << 8) |
1567 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001568
1569 }
1570
1571 /*
1572 * Construct SGL
1573 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001574 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301575 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001576 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1577 &ldio->sgl);
1578 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301579 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001580 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1581 } else
1582 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1583
Yang, Bobdc6fb82009-12-06 08:30:19 -07001584 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001585 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001586 ldio->sge_count);
1587 return 0;
1588 }
1589
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001590 /*
1591 * Sense info specific
1592 */
1593 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1594 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301595 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001596
Sumant Patrob1df99d2006-10-03 12:40:47 -07001597 /*
1598 * Compute the total number of frames this command consumes. FW uses
1599 * this number to pull sufficient number of frames from host memory.
1600 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001601 cmd->frame_count = megasas_get_frame_count(instance,
1602 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001603
1604 return cmd->frame_count;
1605}
1606
1607/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301608 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1609 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001610 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001611 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001612 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301613inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001614{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301615 int ret;
1616
Sumant Patrocb59aa62006-01-25 11:53:25 -08001617 switch (cmd->cmnd[0]) {
1618 case READ_10:
1619 case WRITE_10:
1620 case READ_12:
1621 case WRITE_12:
1622 case READ_6:
1623 case WRITE_6:
1624 case READ_16:
1625 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001626 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301627 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1628 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001629 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001630 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301631 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001632 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301633 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001634}
1635
Sumant Patro658dced2006-10-03 13:09:14 -07001636 /**
1637 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001638 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001639 * @instance: Adapter soft state
1640 */
1641static inline void
1642megasas_dump_pending_frames(struct megasas_instance *instance)
1643{
1644 struct megasas_cmd *cmd;
1645 int i,n;
1646 union megasas_sgl *mfi_sgl;
1647 struct megasas_io_frame *ldio;
1648 struct megasas_pthru_frame *pthru;
1649 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001650 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001651
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001652 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1653 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
Sumant Patro658dced2006-10-03 13:09:14 -07001654 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001655 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001656 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001657 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001658
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001659 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001660 for (i = 0; i < max_cmd; i++) {
1661 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001662 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001663 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001664 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301665 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001666 ldio = (struct megasas_io_frame *)cmd->frame;
1667 mfi_sgl = &ldio->sgl;
1668 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001669 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301670 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1671 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1672 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1673 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001674 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001675 pthru = (struct megasas_pthru_frame *) cmd->frame;
1676 mfi_sgl = &pthru->sgl;
1677 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001678 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301679 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1680 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1681 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1682 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001683 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001684 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1685 for (n = 0; n < sgcount; n++) {
1686 if (IS_DMA64)
1687 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1688 le32_to_cpu(mfi_sgl->sge64[n].length),
1689 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1690 else
1691 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1692 le32_to_cpu(mfi_sgl->sge32[n].length),
1693 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001694 }
1695 }
Sumant Patro658dced2006-10-03 13:09:14 -07001696 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001697 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001698 for (i = 0; i < max_cmd; i++) {
1699
1700 cmd = instance->cmd_list[i];
1701
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001702 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001703 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001704 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001705 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001706}
1707
adam radfordcd50ba82010-12-21 10:23:23 -08001708u32
1709megasas_build_and_issue_cmd(struct megasas_instance *instance,
1710 struct scsi_cmnd *scmd)
1711{
1712 struct megasas_cmd *cmd;
1713 u32 frame_count;
1714
1715 cmd = megasas_get_cmd(instance);
1716 if (!cmd)
1717 return SCSI_MLQUEUE_HOST_BUSY;
1718
1719 /*
1720 * Logical drive command
1721 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301722 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001723 frame_count = megasas_build_ldio(instance, scmd, cmd);
1724 else
1725 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1726
1727 if (!frame_count)
1728 goto out_return_cmd;
1729
1730 cmd->scmd = scmd;
1731 scmd->SCp.ptr = (char *)cmd;
1732
1733 /*
1734 * Issue the command to the FW
1735 */
1736 atomic_inc(&instance->fw_outstanding);
1737
1738 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1739 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001740
1741 return 0;
1742out_return_cmd:
1743 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301744 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001745}
1746
1747
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001748/**
1749 * megasas_queue_command - Queue entry point
1750 * @scmd: SCSI command to be queued
1751 * @done: Callback entry point
1752 */
1753static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301754megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001755{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001756 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301757 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001758
1759 instance = (struct megasas_instance *)
1760 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001761
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301762 if (instance->unload == 1) {
1763 scmd->result = DID_NO_CONNECT << 16;
1764 scmd->scsi_done(scmd);
1765 return 0;
1766 }
1767
bo yang39a98552010-09-22 22:36:29 -04001768 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001769 return SCSI_MLQUEUE_HOST_BUSY;
1770
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301771
adam radford229fe472014-03-10 02:51:56 -07001772 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301773 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001774 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001775 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001776 return SCSI_MLQUEUE_HOST_BUSY;
1777 } else {
adam radford229fe472014-03-10 02:51:56 -07001778 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301779 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001780 return 0;
1781 }
1782 }
1783
Sumit Saxena8a01a412016-01-28 21:04:32 +05301784 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001785 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301786 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301787 return 0;
1788 }
1789
Sumit Saxena18365b12016-01-28 21:04:25 +05301790 mr_device_priv_data = scmd->device->hostdata;
1791 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301792 scmd->result = DID_NO_CONNECT << 16;
1793 scmd->scsi_done(scmd);
1794 return 0;
1795 }
1796
Sumit Saxena8a01a412016-01-28 21:04:32 +05301797 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001798 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001799
Sumit Saxena8a01a412016-01-28 21:04:32 +05301800 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301801 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301802
bo yang39a98552010-09-22 22:36:29 -04001803
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001804 scmd->result = 0;
1805
Shivasharan S3cabd162017-02-10 00:59:05 -08001806 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301807 (scmd->device->id >= instance->fw_supported_vd_count ||
1808 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001809 scmd->result = DID_BAD_TARGET << 16;
1810 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001811 }
1812
Shivasharan S3cabd162017-02-10 00:59:05 -08001813 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1814 MEGASAS_IS_LOGICAL(scmd->device) &&
1815 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001816 scmd->result = DID_OK << 16;
1817 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001818 }
1819
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301820 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001821
Sumant Patrocb59aa62006-01-25 11:53:25 -08001822 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301823 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001824 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001825}
1826
Yang, Bo044833b2009-10-06 14:33:06 -06001827static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1828{
1829 int i;
1830
1831 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1832
1833 if ((megasas_mgmt_info.instance[i]) &&
1834 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1835 return megasas_mgmt_info.instance[i];
1836 }
1837
1838 return NULL;
1839}
1840
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301841/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001842* megasas_set_dynamic_target_properties -
1843* Device property set by driver may not be static and it is required to be
1844* updated after OCR
1845*
1846* set tm_capable.
1847* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301848*
1849* @sdev: OS provided scsi device
1850*
1851* Returns void
1852*/
Shivasharan Se9495e22018-06-04 03:45:12 -07001853void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1854 bool is_target_prop)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301855{
Shivasharan S15dd0382017-02-10 00:59:10 -08001856 u16 pd_index = 0, ld;
1857 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301858 struct megasas_instance *instance;
1859 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301860 struct MR_PRIV_DEVICE *mr_device_priv_data;
1861 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301862 struct MR_LD_RAID *raid;
1863 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1864
1865 instance = megasas_lookup_instance(sdev->host->host_no);
1866 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301867 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301868
Shivasharan Sed981b82017-02-10 00:59:06 -08001869 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301870 return;
1871
Shivasharan Sed981b82017-02-10 00:59:06 -08001872 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301873 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1874 + sdev->id;
1875 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1876 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001877 if (ld >= instance->fw_supported_vd_count)
1878 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301879 raid = MR_LdRaidGet(ld, local_map_ptr);
1880
1881 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001882 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001883
Sumit Saxena18365b12016-01-28 21:04:25 +05301884 mr_device_priv_data->is_tm_capable =
1885 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001886 } else if (instance->use_seqnum_jbod_fp) {
1887 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001888 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001889 pd_sync = (void *)fusion->pd_seq_sync
1890 [(instance->pd_seq_map_id - 1) & 1];
1891 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001892 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301893 }
Shivasharan Se9495e22018-06-04 03:45:12 -07001894
1895 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1896 /*
1897 * If FW provides a target reset timeout value, driver will use
1898 * it. If not set, fallback to default values.
1899 */
1900 mr_device_priv_data->target_reset_tmo =
1901 min_t(u8, instance->max_reset_tmo,
1902 instance->tgt_prop->reset_tmo);
1903 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1904 } else {
1905 mr_device_priv_data->target_reset_tmo =
1906 MEGASAS_DEFAULT_TM_TIMEOUT;
1907 mr_device_priv_data->task_abort_tmo =
1908 MEGASAS_DEFAULT_TM_TIMEOUT;
1909 }
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301910}
1911
Shivasharan S15dd0382017-02-10 00:59:10 -08001912/*
1913 * megasas_set_nvme_device_properties -
1914 * set nomerges=2
1915 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1916 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1917 *
1918 * MR firmware provides value in KB. Caller of this function converts
1919 * kb into bytes.
1920 *
1921 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1922 * MR firmware provides value 128 as (32 * 4K) = 128K.
1923 *
1924 * @sdev: scsi device
1925 * @max_io_size: maximum io transfer size
1926 *
1927 */
1928static inline void
1929megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301930{
Sumit Saxena2216c302016-01-28 21:04:26 +05301931 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001932 u32 mr_nvme_pg_size;
1933
1934 instance = (struct megasas_instance *)sdev->host->hostdata;
1935 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1936 MR_DEFAULT_NVME_PAGE_SIZE);
1937
1938 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1939
Bart Van Assche8b904b52018-03-07 17:10:10 -08001940 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
Shivasharan S15dd0382017-02-10 00:59:10 -08001941 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1942}
1943
1944
1945/*
1946 * megasas_set_static_target_properties -
1947 * Device property set by driver are static and it is not required to be
1948 * updated after OCR.
1949 *
1950 * set io timeout
1951 * set device queue depth
1952 * set nvme device properties. see - megasas_set_nvme_device_properties
1953 *
1954 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001955 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001956 */
Shivasharan S96188a82017-02-10 00:59:11 -08001957static void megasas_set_static_target_properties(struct scsi_device *sdev,
1958 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001959{
Shivasharan S15dd0382017-02-10 00:59:10 -08001960 u8 interface_type;
1961 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1962 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001963 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001964 struct megasas_instance *instance;
1965 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301966
1967 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001968 mr_device_priv_data = sdev->hostdata;
1969 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301970
Shivasharan S15dd0382017-02-10 00:59:10 -08001971 /*
1972 * The RAID firmware may require extended timeouts.
1973 */
1974 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301975
Shivasharan S15dd0382017-02-10 00:59:10 -08001976 switch (interface_type) {
1977 case SAS_PD:
1978 device_qd = MEGASAS_SAS_QD;
1979 break;
1980 case SATA_PD:
1981 device_qd = MEGASAS_SATA_QD;
1982 break;
1983 case NVME_PD:
1984 device_qd = MEGASAS_NVME_QD;
1985 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301986 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001987
Shivasharan S96188a82017-02-10 00:59:11 -08001988 if (is_target_prop) {
1989 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1990 if (tgt_device_qd &&
1991 (tgt_device_qd <= instance->host->can_queue))
1992 device_qd = tgt_device_qd;
1993
1994 /* max_io_size_kb will be set to non zero for
1995 * nvme based vd and syspd.
1996 */
1997 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1998 }
1999
Shivasharan S15dd0382017-02-10 00:59:10 -08002000 if (instance->nvme_page_size && max_io_size_kb)
2001 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2002
2003 scsi_change_queue_depth(sdev, device_qd);
2004
Sumit Saxena2216c302016-01-28 21:04:26 +05302005}
2006
Sumit Saxena18365b12016-01-28 21:04:25 +05302007
Christoph Hellwig147aab62006-02-17 12:13:48 +01002008static int megasas_slave_configure(struct scsi_device *sdev)
2009{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302010 u16 pd_index = 0;
2011 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08002012 int ret_target_prop = DCMD_FAILED;
2013 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302014
2015 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08002016 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08002017 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302018 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2019 sdev->id;
2020 if (instance->pd_list[pd_index].driveState !=
2021 MR_PD_STATE_SYSTEM)
2022 return -ENXIO;
2023 }
2024 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302025
Shivasharan S149c5752018-01-05 05:27:42 -08002026 mutex_lock(&instance->reset_mutex);
Shivasharan S15dd0382017-02-10 00:59:10 -08002027 /* Send DCMD to Firmware and cache the information */
2028 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2029 megasas_get_pd_info(instance, sdev);
2030
Shivasharan S96188a82017-02-10 00:59:11 -08002031 /* Some ventura firmware may not have instance->nvme_page_size set.
2032 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2033 */
2034 if ((instance->tgt_prop) && (instance->nvme_page_size))
2035 ret_target_prop = megasas_get_target_prop(instance, sdev);
2036
2037 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2038 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08002039
Shivasharan S15dd0382017-02-10 00:59:10 -08002040 /* This sdev property may change post OCR */
Shivasharan Se9495e22018-06-04 03:45:12 -07002041 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2042
2043 mutex_unlock(&instance->reset_mutex);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05302044
Yang, Bo044833b2009-10-06 14:33:06 -06002045 return 0;
2046}
2047
2048static int megasas_slave_alloc(struct scsi_device *sdev)
2049{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002050 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06002051 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05302052 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002053
Yang, Bo044833b2009-10-06 14:33:06 -06002054 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08002055 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06002056 /*
2057 * Open the OS scan to the SYSTEM PD
2058 */
2059 pd_index =
2060 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2061 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08002062 if ((instance->pd_list_not_supported ||
2063 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302064 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05302065 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06002066 }
2067 return -ENXIO;
2068 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302069
2070scan_target:
2071 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2072 GFP_KERNEL);
2073 if (!mr_device_priv_data)
2074 return -ENOMEM;
2075 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08002076
2077 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2078 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01002079 return 0;
2080}
2081
Sumit Saxena18365b12016-01-28 21:04:25 +05302082static void megasas_slave_destroy(struct scsi_device *sdev)
2083{
2084 kfree(sdev->hostdata);
2085 sdev->hostdata = NULL;
2086}
2087
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302088/*
2089* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2090* kill adapter
2091* @instance: Adapter soft state
2092*
2093*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05302094static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302095{
2096 int i;
2097 struct megasas_cmd *cmd_mfi;
2098 struct megasas_cmd_fusion *cmd_fusion;
2099 struct fusion_context *fusion = instance->ctrl_context;
2100
2101 /* Find all outstanding ioctls */
2102 if (fusion) {
2103 for (i = 0; i < instance->max_fw_cmds; i++) {
2104 cmd_fusion = fusion->cmd_list[i];
2105 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2106 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2107 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002108 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2109 cmd_mfi->frame->hdr.cmd_status =
2110 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302111 megasas_complete_cmd(instance,
2112 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002113 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302114 }
2115 }
2116 } else {
2117 for (i = 0; i < instance->max_fw_cmds; i++) {
2118 cmd_mfi = instance->cmd_list[i];
2119 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2120 MFI_CMD_ABORT)
2121 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2122 }
2123 }
2124}
2125
2126
adam radford9c915a82010-12-21 13:34:31 -08002127void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002128{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302129 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302130 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302131 /* Wait 1 second to ensure IO or ioctls in build have posted */
2132 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002133 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302134 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002135 (instance->adapter_type != MFI_SERIES)) {
Shivasharan S5acad9b2018-10-16 23:37:47 -07002136 if (!instance->requestorId) {
2137 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2138 /* Flush */
2139 readl(&instance->reg_set->doorbell);
2140 }
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302141 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002142 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002143 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302144 writel(MFI_STOP_ADP,
2145 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002146 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302147 /* Complete outstanding ioctls when adapter is killed */
2148 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002149}
2150
2151 /**
2152 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2153 * restored to max value
2154 * @instance: Adapter soft state
2155 *
2156 */
2157void
2158megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2159{
2160 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302161
adam radford9c915a82010-12-21 13:34:31 -08002162 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002163 && time_after(jiffies, instance->last_time + 5 * HZ)
2164 && atomic_read(&instance->fw_outstanding) <
2165 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002166
2167 spin_lock_irqsave(instance->host->host_lock, flags);
2168 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002169
Sumit Saxena308ec452016-01-28 21:04:30 +05302170 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002171 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002172 }
2173}
2174
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002175/**
bo yang7343eb62007-11-09 04:35:44 -05002176 * megasas_complete_cmd_dpc - Returns FW's controller structure
2177 * @instance_addr: Address of adapter soft state
2178 *
2179 * Tasklet to complete cmds
2180 */
2181static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2182{
2183 u32 producer;
2184 u32 consumer;
2185 u32 context;
2186 struct megasas_cmd *cmd;
2187 struct megasas_instance *instance =
2188 (struct megasas_instance *)instance_addr;
2189 unsigned long flags;
2190
2191 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302192 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002193 return;
2194
2195 spin_lock_irqsave(&instance->completion_lock, flags);
2196
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302197 producer = le32_to_cpu(*instance->producer);
2198 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002199
2200 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302201 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002202 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002203 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002204 context);
2205 BUG();
2206 }
bo yang7343eb62007-11-09 04:35:44 -05002207
2208 cmd = instance->cmd_list[context];
2209
2210 megasas_complete_cmd(instance, cmd, DID_OK);
2211
2212 consumer++;
2213 if (consumer == (instance->max_fw_cmds + 1)) {
2214 consumer = 0;
2215 }
2216 }
2217
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302218 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002219
2220 spin_unlock_irqrestore(&instance->completion_lock, flags);
2221
2222 /*
2223 * Check if we can restore can_queue
2224 */
adam radford9c915a82010-12-21 13:34:31 -08002225 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002226}
2227
Kees Cookc251a7b2017-10-22 15:30:04 -07002228static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2229
adam radford229fe472014-03-10 02:51:56 -07002230/**
Kees Cookc251a7b2017-10-22 15:30:04 -07002231 * megasas_start_timer - Initializes sriov heartbeat timer object
adam radford229fe472014-03-10 02:51:56 -07002232 * @instance: Adapter soft state
adam radford229fe472014-03-10 02:51:56 -07002233 *
2234 */
Kees Cookc251a7b2017-10-22 15:30:04 -07002235void megasas_start_timer(struct megasas_instance *instance)
adam radford229fe472014-03-10 02:51:56 -07002236{
Kees Cookc251a7b2017-10-22 15:30:04 -07002237 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2238
2239 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2240 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
adam radford229fe472014-03-10 02:51:56 -07002241 add_timer(timer);
2242}
2243
Yang, Bo707e09b2010-10-12 07:20:27 -06002244static void
2245megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2246
2247static void
2248process_fw_state_change_wq(struct work_struct *work);
2249
YueHaibing6764f512019-07-02 21:01:14 +08002250static void megasas_do_ocr(struct megasas_instance *instance)
Yang, Bo707e09b2010-10-12 07:20:27 -06002251{
2252 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2253 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2254 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302255 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002256 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302257 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302258 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002259 instance->issuepend_done = 0;
2260
2261 atomic_set(&instance->fw_outstanding, 0);
2262 megasas_internal_reset_defer_cmds(instance);
2263 process_fw_state_change_wq(&instance->work_init);
2264}
2265
Adam Radford4cbfea82014-07-09 15:17:56 -07002266static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2267 int initial)
adam radford229fe472014-03-10 02:51:56 -07002268{
2269 struct megasas_cmd *cmd;
2270 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002271 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002272 dma_addr_t new_affiliation_111_h;
2273 int ld, retval = 0;
2274 u8 thisVf;
2275
2276 cmd = megasas_get_cmd(instance);
2277
2278 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002279 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2280 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002281 instance->host->host_no);
2282 return -ENOMEM;
2283 }
2284
2285 dcmd = &cmd->frame->dcmd;
2286
Adam Radford4cbfea82014-07-09 15:17:56 -07002287 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002288 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2289 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002290 megasas_return_cmd(instance, cmd);
2291 return -ENOMEM;
2292 }
2293
2294 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002295 memset(instance->vf_affiliation_111, 0,
2296 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002297 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002298 new_affiliation_111 =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002299 dma_alloc_coherent(&instance->pdev->dev,
2300 sizeof(struct MR_LD_VF_AFFILIATION_111),
2301 &new_affiliation_111_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002302 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002303 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2304 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002305 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002306 megasas_return_cmd(instance, cmd);
2307 return -ENOMEM;
2308 }
adam radford229fe472014-03-10 02:51:56 -07002309 }
2310
2311 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2312
2313 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302314 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002315 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302316 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002317 dcmd->timeout = 0;
2318 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302319 dcmd->data_xfer_len =
2320 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2321 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002322
Adam Radford4cbfea82014-07-09 15:17:56 -07002323 if (initial)
2324 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302325 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002326 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302327 dcmd->sgl.sge32[0].phys_addr =
2328 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002329
Christoph Hellwig2213a462015-04-23 16:33:54 +05302330 dcmd->sgl.sge32[0].length = cpu_to_le32(
2331 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002332
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002333 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002334 "scsi%d\n", instance->host->host_no);
2335
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302336 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002337 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2338 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002339 dcmd->cmd_status, instance->host->host_no);
2340 retval = 1; /* Do a scan if we couldn't get affiliation */
2341 goto out;
2342 }
2343
2344 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002345 thisVf = new_affiliation_111->thisVf;
2346 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2347 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2348 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002349 dev_warn(&instance->pdev->dev, "SR-IOV: "
2350 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002351 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002352 memcpy(instance->vf_affiliation_111,
2353 new_affiliation_111,
2354 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002355 retval = 1;
2356 goto out;
2357 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002358 }
2359out:
2360 if (new_affiliation_111) {
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002361 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002362 sizeof(struct MR_LD_VF_AFFILIATION_111),
2363 new_affiliation_111,
2364 new_affiliation_111_h);
2365 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302366
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302367 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002368
2369 return retval;
2370}
2371
2372static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2373 int initial)
2374{
2375 struct megasas_cmd *cmd;
2376 struct megasas_dcmd_frame *dcmd;
2377 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2378 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2379 dma_addr_t new_affiliation_h;
2380 int i, j, retval = 0, found = 0, doscan = 0;
2381 u8 thisVf;
2382
2383 cmd = megasas_get_cmd(instance);
2384
2385 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002386 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2387 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002388 instance->host->host_no);
2389 return -ENOMEM;
2390 }
2391
2392 dcmd = &cmd->frame->dcmd;
2393
2394 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002395 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2396 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002397 megasas_return_cmd(instance, cmd);
2398 return -ENOMEM;
2399 }
2400
2401 if (initial)
2402 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2403 sizeof(struct MR_LD_VF_AFFILIATION));
2404 else {
2405 new_affiliation =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002406 dma_alloc_coherent(&instance->pdev->dev,
2407 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2408 &new_affiliation_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002409 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002410 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2411 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002412 instance->host->host_no);
2413 megasas_return_cmd(instance, cmd);
2414 return -ENOMEM;
2415 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002416 }
2417
2418 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2419
2420 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302421 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002422 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302423 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002424 dcmd->timeout = 0;
2425 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302426 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2427 sizeof(struct MR_LD_VF_AFFILIATION));
2428 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002429
2430 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302431 dcmd->sgl.sge32[0].phys_addr =
2432 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002433 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302434 dcmd->sgl.sge32[0].phys_addr =
2435 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002436
Christoph Hellwig2213a462015-04-23 16:33:54 +05302437 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2438 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002439
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002440 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002441 "scsi%d\n", instance->host->host_no);
2442
Adam Radford4cbfea82014-07-09 15:17:56 -07002443
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302444 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002445 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2446 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002447 dcmd->cmd_status, instance->host->host_no);
2448 retval = 1; /* Do a scan if we couldn't get affiliation */
2449 goto out;
2450 }
2451
2452 if (!initial) {
2453 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002454 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2455 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002456 instance->host->host_no);
2457 retval = 1;
2458 goto out;
2459 }
2460 newmap = new_affiliation->map;
2461 savedmap = instance->vf_affiliation->map;
2462 thisVf = new_affiliation->thisVf;
2463 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2464 found = 0;
2465 for (j = 0; j < instance->vf_affiliation->ldCount;
2466 j++) {
2467 if (newmap->ref.targetId ==
2468 savedmap->ref.targetId) {
2469 found = 1;
2470 if (newmap->policy[thisVf] !=
2471 savedmap->policy[thisVf]) {
2472 doscan = 1;
2473 goto out;
2474 }
adam radford229fe472014-03-10 02:51:56 -07002475 }
2476 savedmap = (struct MR_LD_VF_MAP *)
2477 ((unsigned char *)savedmap +
2478 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002479 }
2480 if (!found && newmap->policy[thisVf] !=
2481 MR_LD_ACCESS_HIDDEN) {
2482 doscan = 1;
2483 goto out;
2484 }
2485 newmap = (struct MR_LD_VF_MAP *)
2486 ((unsigned char *)newmap + newmap->size);
2487 }
2488
2489 newmap = new_affiliation->map;
2490 savedmap = instance->vf_affiliation->map;
2491
2492 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2493 found = 0;
2494 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2495 if (savedmap->ref.targetId ==
2496 newmap->ref.targetId) {
2497 found = 1;
2498 if (savedmap->policy[thisVf] !=
2499 newmap->policy[thisVf]) {
2500 doscan = 1;
2501 goto out;
2502 }
2503 }
adam radford229fe472014-03-10 02:51:56 -07002504 newmap = (struct MR_LD_VF_MAP *)
2505 ((unsigned char *)newmap +
2506 newmap->size);
2507 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002508 if (!found && savedmap->policy[thisVf] !=
2509 MR_LD_ACCESS_HIDDEN) {
2510 doscan = 1;
2511 goto out;
2512 }
2513 savedmap = (struct MR_LD_VF_MAP *)
2514 ((unsigned char *)savedmap +
2515 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002516 }
2517 }
2518out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002519 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002520 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2521 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002522 memcpy(instance->vf_affiliation, new_affiliation,
2523 new_affiliation->size);
2524 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002525 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002526
2527 if (new_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002528 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002529 (MAX_LOGICAL_DRIVES + 1) *
2530 sizeof(struct MR_LD_VF_AFFILIATION),
2531 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302532 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002533
2534 return retval;
2535}
2536
Adam Radford4cbfea82014-07-09 15:17:56 -07002537/* This function will get the current SR-IOV LD/VF affiliation */
2538static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2539 int initial)
2540{
2541 int retval;
2542
2543 if (instance->PlasmaFW111)
2544 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2545 else
2546 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2547 return retval;
2548}
2549
adam radford229fe472014-03-10 02:51:56 -07002550/* This function will tell FW to start the SR-IOV heartbeat */
2551int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2552 int initial)
2553{
2554 struct megasas_cmd *cmd;
2555 struct megasas_dcmd_frame *dcmd;
2556 int retval = 0;
2557
2558 cmd = megasas_get_cmd(instance);
2559
2560 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002561 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2562 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002563 instance->host->host_no);
2564 return -ENOMEM;
2565 }
2566
2567 dcmd = &cmd->frame->dcmd;
2568
2569 if (initial) {
2570 instance->hb_host_mem =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002571 dma_alloc_coherent(&instance->pdev->dev,
2572 sizeof(struct MR_CTRL_HB_HOST_MEM),
2573 &instance->hb_host_mem_h,
2574 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07002575 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002576 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2577 " memory for heartbeat host memory for scsi%d\n",
2578 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002579 retval = -ENOMEM;
2580 goto out;
2581 }
adam radford229fe472014-03-10 02:51:56 -07002582 }
2583
2584 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2585
Christoph Hellwig2213a462015-04-23 16:33:54 +05302586 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002587 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302588 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002589 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302590 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002591 dcmd->timeout = 0;
2592 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302593 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2594 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
Shivasharan S107a60d2017-10-19 02:49:05 -07002595
2596 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2597 sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002598
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002599 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002600 instance->host->host_no);
2601
Shivasharan Se7d36b82017-10-19 02:48:50 -07002602 if ((instance->adapter_type != MFI_SERIES) &&
2603 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302604 retval = megasas_issue_blocked_cmd(instance, cmd,
2605 MEGASAS_ROUTINE_WAIT_TIME_VF);
2606 else
2607 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002608
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302609 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302610 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2611 "_MEM_ALLOC DCMD %s for scsi%d\n",
2612 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2613 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002614 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002615 }
2616
2617out:
2618 megasas_return_cmd(instance, cmd);
2619
2620 return retval;
2621}
2622
2623/* Handler for SR-IOV heartbeat */
Kees Cookc251a7b2017-10-22 15:30:04 -07002624static void megasas_sriov_heartbeat_handler(struct timer_list *t)
adam radford229fe472014-03-10 02:51:56 -07002625{
2626 struct megasas_instance *instance =
Kees Cookc251a7b2017-10-22 15:30:04 -07002627 from_timer(instance, t, sriov_heartbeat_timer);
adam radford229fe472014-03-10 02:51:56 -07002628
2629 if (instance->hb_host_mem->HB.fwCounter !=
2630 instance->hb_host_mem->HB.driverCounter) {
2631 instance->hb_host_mem->HB.driverCounter =
2632 instance->hb_host_mem->HB.fwCounter;
2633 mod_timer(&instance->sriov_heartbeat_timer,
2634 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2635 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002636 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002637 "completed for scsi%d\n", instance->host->host_no);
2638 schedule_work(&instance->work_init);
2639 }
2640}
2641
bo yang7343eb62007-11-09 04:35:44 -05002642/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002643 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2644 * @instance: Adapter soft state
2645 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002646 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002647 * complete all its outstanding commands. Returns error if one or more IOs
2648 * are pending after this time period. It also marks the controller dead.
2649 */
2650static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2651{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302652 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002653 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002654 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002655 unsigned long flags;
2656 struct list_head clist_local;
2657 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002658 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002659
Sumit Saxenaccc75072016-01-28 21:04:33 +05302660 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2661 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2662 __func__, __LINE__);
2663 return FAILED;
2664 }
bo yang39a98552010-09-22 22:36:29 -04002665
Sumit Saxena8a01a412016-01-28 21:04:32 +05302666 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002667
2668 INIT_LIST_HEAD(&clist_local);
2669 spin_lock_irqsave(&instance->hba_lock, flags);
2670 list_splice_init(&instance->internal_reset_pending_q,
2671 &clist_local);
2672 spin_unlock_irqrestore(&instance->hba_lock, flags);
2673
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002674 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002675 for (i = 0; i < wait_time; i++) {
2676 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302677 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002678 break;
2679 }
2680
Sumit Saxena8a01a412016-01-28 21:04:32 +05302681 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002682 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302683 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002684 return FAILED;
2685 }
2686
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002687 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002688 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002689 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002690 struct megasas_cmd, list);
2691 list_del_init(&reset_cmd->list);
2692 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002693 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002694 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002695 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002696 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002697
2698 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2699 megasas_return_cmd(instance, reset_cmd);
2700 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002701 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002702 "reset queue\n",
2703 reset_cmd);
2704
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302705 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002706 instance->instancet->fire_cmd(instance,
2707 reset_cmd->frame_phys_addr,
2708 0, instance->reg_set);
2709 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002710 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002711 "cmds lst\n",
2712 reset_cmd);
2713 }
2714 reset_index++;
2715 }
2716
2717 return SUCCESS;
2718 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002719
adam radfordc007b8b2012-07-17 18:20:24 -07002720 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302721 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002722
2723 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002724 break;
2725
2726 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002727 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002728 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002729 /*
2730 * Call cmd completion routine. Cmd to be
2731 * be completed directly without depending on isr.
2732 */
2733 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002734 }
2735
2736 msleep(1000);
2737 }
2738
Yang, Bo707e09b2010-10-12 07:20:27 -06002739 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302740 outstanding = atomic_read(&instance->fw_outstanding);
Shivasharan Sde516372018-12-17 00:47:39 -08002741 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002742
Sumit Saxenaccc75072016-01-28 21:04:33 +05302743 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2744 goto no_outstanding;
2745
2746 if (instance->disableOnlineCtrlReset)
2747 goto kill_hba_and_failed;
2748 do {
2749 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2750 dev_info(&instance->pdev->dev,
Colin Ian Kingefc372c2019-04-17 14:51:09 +01002751 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
Sumit Saxenaccc75072016-01-28 21:04:33 +05302752 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2753 if (i == 3)
2754 goto kill_hba_and_failed;
2755 megasas_do_ocr(instance);
2756
2757 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2758 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2759 __func__, __LINE__);
2760 return FAILED;
2761 }
2762 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2763 __func__, __LINE__);
2764
2765 for (sl = 0; sl < 10; sl++)
2766 msleep(500);
2767
2768 outstanding = atomic_read(&instance->fw_outstanding);
2769
Shivasharan Sde516372018-12-17 00:47:39 -08002770 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302771 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2772 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002773 }
2774 i++;
2775 } while (i <= 3);
2776
Sumit Saxenaccc75072016-01-28 21:04:33 +05302777no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002778
Sumit Saxenaccc75072016-01-28 21:04:33 +05302779 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2780 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002781 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302782
2783kill_hba_and_failed:
2784
2785 /* Reset not supported, kill adapter */
2786 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2787 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2788 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2789 atomic_read(&instance->fw_outstanding));
2790 megasas_dump_pending_frames(instance);
2791 megaraid_sas_kill_hba(instance);
2792
2793 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794}
2795
2796/**
2797 * megasas_generic_reset - Generic reset routine
2798 * @scmd: Mid-layer SCSI command
2799 *
2800 * This routine implements a generic reset handler for device, bus and host
2801 * reset requests. Device, bus and host specific reset handlers can use this
2802 * function after they do their specific tasks.
2803 */
2804static int megasas_generic_reset(struct scsi_cmnd *scmd)
2805{
2806 int ret_val;
2807 struct megasas_instance *instance;
2808
2809 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2810
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002811 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2812 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002813
Sumit Saxena8a01a412016-01-28 21:04:32 +05302814 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002815 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002816 return FAILED;
2817 }
2818
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002819 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002820 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002821 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002822 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002823 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002824
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002825 return ret_val;
2826}
2827
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002828/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002829 * megasas_reset_timer - quiesce the adapter if required
2830 * @scmd: scsi cmnd
2831 *
2832 * Sets the FW busy flag and reduces the host->can_queue if the
2833 * cmd has not been completed within the timeout period.
2834 */
2835static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002836blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002837{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002838 struct megasas_instance *instance;
2839 unsigned long flags;
2840
2841 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302842 (scmd_timeout * 2) * HZ)) {
Christoph Hellwig66005932018-05-29 15:52:29 +02002843 return BLK_EH_DONE;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002844 }
2845
adam radfordf575c5d2011-10-13 16:01:12 -07002846 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002847 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2848 /* FW is busy, throttle IO */
2849 spin_lock_irqsave(instance->host->host_lock, flags);
2850
adam radfordc5daa6a2012-07-17 18:20:03 -07002851 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002852 instance->last_time = jiffies;
2853 instance->flag |= MEGASAS_FW_BUSY;
2854
2855 spin_unlock_irqrestore(instance->host->host_lock, flags);
2856 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002857 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002858}
2859
2860/**
Shivasharan S4fe55032019-05-07 10:05:39 -07002861 * megasas_dump - This function will print hexdump of provided buffer.
2862 * @buf: Buffer to be dumped
2863 * @sz: Size in bytes
2864 * @format: Different formats of dumping e.g. format=n will
2865 * cause only 'n' 32 bit words to be dumped in a single
2866 * line.
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002867 */
Shivasharan S96c96032019-05-07 10:05:37 -07002868inline void
Shivasharan S4fe55032019-05-07 10:05:39 -07002869megasas_dump(void *buf, int sz, int format)
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002870{
2871 int i;
Shivasharan S4fe55032019-05-07 10:05:39 -07002872 __le32 *buf_loc = (__le32 *)buf;
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002873
Shivasharan S4fe55032019-05-07 10:05:39 -07002874 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2875 if ((i % format) == 0) {
2876 if (i != 0)
2877 printk(KERN_CONT "\n");
2878 printk(KERN_CONT "%08x: ", (i * 4));
2879 }
2880 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002881 }
Shivasharan S4fe55032019-05-07 10:05:39 -07002882 printk(KERN_CONT "\n");
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002883}
2884
2885/**
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07002886 * megasas_dump_reg_set - This function will print hexdump of register set
2887 * @buf: Buffer to be dumped
2888 * @sz: Size in bytes
2889 * @format: Different formats of dumping e.g. format=n will
2890 * cause only 'n' 32 bit words to be dumped in a
2891 * single line.
2892 */
2893inline void
2894megasas_dump_reg_set(void __iomem *reg_set)
2895{
2896 unsigned int i, sz = 256;
2897 u32 __iomem *reg = (u32 __iomem *)reg_set;
2898
2899 for (i = 0; i < (sz / sizeof(u32)); i++)
2900 printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2901}
2902
2903/**
Shivasharan S96c96032019-05-07 10:05:37 -07002904 * megasas_dump_fusion_io - This function will print key details
2905 * of SCSI IO
2906 * @scmd: SCSI command pointer of SCSI IO
2907 */
2908void
2909megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2910{
2911 struct megasas_cmd_fusion *cmd;
2912 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2913 struct megasas_instance *instance;
2914
2915 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2916 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2917
2918 scmd_printk(KERN_INFO, scmd,
2919 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2920 scmd, scmd->retries, scmd->allowed);
2921 scsi_print_command(scmd);
2922
2923 if (cmd) {
2924 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2925 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2926 scmd_printk(KERN_INFO, scmd,
2927 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
2928 req_desc->SCSIIO.RequestFlags,
2929 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2930 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2931
2932 printk(KERN_INFO "IO request frame:\n");
2933 megasas_dump(cmd->io_request,
Shivasharan S4fe55032019-05-07 10:05:39 -07002934 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
Shivasharan S96c96032019-05-07 10:05:37 -07002935 printk(KERN_INFO "Chain frame:\n");
2936 megasas_dump(cmd->sg_frame,
Shivasharan S4fe55032019-05-07 10:05:39 -07002937 instance->max_chain_frame_sz, 8);
Shivasharan S96c96032019-05-07 10:05:37 -07002938 }
2939
2940}
2941
Shivasharan Scfb9a302019-05-07 10:05:40 -07002942/*
2943 * megasas_dump_sys_regs - This function will dump system registers through
2944 * sysfs.
2945 * @reg_set: Pointer to System register set.
2946 * @buf: Buffer to which output is to be written.
2947 * @return: Number of bytes written to buffer.
2948 */
2949static inline ssize_t
2950megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2951{
2952 unsigned int i, sz = 256;
2953 int bytes_wrote = 0;
2954 char *loc = (char *)buf;
2955 u32 __iomem *reg = (u32 __iomem *)reg_set;
2956
2957 for (i = 0; i < sz / sizeof(u32); i++) {
2958 bytes_wrote += snprintf(loc + bytes_wrote, PAGE_SIZE,
2959 "%08x: %08x\n", (i * 4),
2960 readl(&reg[i]));
2961 }
2962 return bytes_wrote;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002963}
2964
2965/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002966 * megasas_reset_bus_host - Bus & host reset handler entry point
2967 */
2968static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2969{
2970 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002971 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002972
adam radford9c915a82010-12-21 13:34:31 -08002973 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002974
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002975 scmd_printk(KERN_INFO, scmd,
Shivasharan S96c96032019-05-07 10:05:37 -07002976 "OCR is requested due to IO timeout!!\n");
2977
2978 scmd_printk(KERN_INFO, scmd,
2979 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
2980 scmd->device->host->shost_state,
Ming Leic84b0232018-06-24 22:03:26 +08002981 scsi_host_busy(scmd->device->host),
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002982 atomic_read(&instance->fw_outstanding));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002983 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002984 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002985 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002986 if (instance->adapter_type == MFI_SERIES) {
2987 ret = megasas_generic_reset(scmd);
2988 } else {
Shivasharan S96c96032019-05-07 10:05:37 -07002989 megasas_dump_fusion_io(scmd);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002990 ret = megasas_reset_fusion(scmd->device->host,
2991 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002992 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002993
2994 return ret;
2995}
2996
2997/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002998 * megasas_task_abort - Issues task abort request to firmware
2999 * (supported only for fusion adapters)
3000 * @scmd: SCSI command pointer
3001 */
3002static int megasas_task_abort(struct scsi_cmnd *scmd)
3003{
3004 int ret;
3005 struct megasas_instance *instance;
3006
3007 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3008
Shivasharan Se7d36b82017-10-19 02:48:50 -07003009 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003010 ret = megasas_task_abort_fusion(scmd);
3011 else {
3012 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3013 ret = FAILED;
3014 }
3015
3016 return ret;
3017}
3018
3019/**
3020 * megasas_reset_target: Issues target reset request to firmware
3021 * (supported only for fusion adapters)
3022 * @scmd: SCSI command pointer
3023 */
3024static int megasas_reset_target(struct scsi_cmnd *scmd)
3025{
3026 int ret;
3027 struct megasas_instance *instance;
3028
3029 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3030
Shivasharan Se7d36b82017-10-19 02:48:50 -07003031 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003032 ret = megasas_reset_target_fusion(scmd);
3033 else {
3034 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3035 ret = FAILED;
3036 }
3037
3038 return ret;
3039}
3040
3041/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003042 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003043 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003044 * @bdev: block device
3045 * @capacity: drive capacity
3046 * @geom: geometry parameters
3047 */
3048static int
3049megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3050 sector_t capacity, int geom[])
3051{
3052 int heads;
3053 int sectors;
3054 sector_t cylinders;
3055 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003056
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003057 /* Default heads (64) & sectors (32) */
3058 heads = 64;
3059 sectors = 32;
3060
3061 tmp = heads * sectors;
3062 cylinders = capacity;
3063
3064 sector_div(cylinders, tmp);
3065
3066 /*
3067 * Handle extended translation size for logical drives > 1Gb
3068 */
3069
3070 if (capacity >= 0x200000) {
3071 heads = 255;
3072 sectors = 63;
3073 tmp = heads*sectors;
3074 cylinders = capacity;
3075 sector_div(cylinders, tmp);
3076 }
3077
3078 geom[0] = heads;
3079 geom[1] = sectors;
3080 geom[2] = cylinders;
3081
3082 return 0;
3083}
3084
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003085static void megasas_aen_polling(struct work_struct *work);
3086
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003087/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003088 * megasas_service_aen - Processes an event notification
3089 * @instance: Adapter soft state
3090 * @cmd: AEN command completed by the ISR
3091 *
3092 * For AEN, driver sends a command down to FW that is held by the FW till an
3093 * event occurs. When an event of interest occurs, FW completes the command
3094 * that it was previously holding.
3095 *
3096 * This routines sends SIGIO signal to processes that have registered with the
3097 * driver for AEN.
3098 */
3099static void
3100megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3101{
Yang, Boc3518832009-10-06 14:18:02 -06003102 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003103
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003104 /*
3105 * Don't signal app if it is just an aborted previously registered aen
3106 */
Yang, Boc3518832009-10-06 14:18:02 -06003107 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3108 spin_lock_irqsave(&poll_aen_lock, flags);
3109 megasas_poll_wait_aen = 1;
3110 spin_unlock_irqrestore(&poll_aen_lock, flags);
3111 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003112 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06003113 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003114 else
3115 cmd->abort_aen = 0;
3116
3117 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303118
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303119 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003120
bo yang39a98552010-09-22 22:36:29 -04003121 if ((instance->unload == 0) &&
3122 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003123 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003124
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003125 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3126 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003127 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003128 } else {
3129 ev->instance = instance;
3130 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08003131 INIT_DELAYED_WORK(&ev->hotplug_work,
3132 megasas_aen_polling);
3133 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003134 }
3135 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003136}
3137
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303138static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003139fw_crash_buffer_store(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303140 struct device_attribute *attr, const char *buf, size_t count)
3141{
3142 struct Scsi_Host *shost = class_to_shost(cdev);
3143 struct megasas_instance *instance =
3144 (struct megasas_instance *) shost->hostdata;
3145 int val = 0;
3146 unsigned long flags;
3147
3148 if (kstrtoint(buf, 0, &val) != 0)
3149 return -EINVAL;
3150
3151 spin_lock_irqsave(&instance->crashdump_lock, flags);
3152 instance->fw_crash_buffer_offset = val;
3153 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3154 return strlen(buf);
3155}
3156
3157static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003158fw_crash_buffer_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303159 struct device_attribute *attr, char *buf)
3160{
3161 struct Scsi_Host *shost = class_to_shost(cdev);
3162 struct megasas_instance *instance =
3163 (struct megasas_instance *) shost->hostdata;
3164 u32 size;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303165 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3166 unsigned long src_addr;
3167 unsigned long flags;
3168 u32 buff_offset;
3169
3170 spin_lock_irqsave(&instance->crashdump_lock, flags);
3171 buff_offset = instance->fw_crash_buffer_offset;
3172 if (!instance->crash_dump_buf &&
3173 !((instance->fw_crash_state == AVAILABLE) ||
3174 (instance->fw_crash_state == COPYING))) {
3175 dev_err(&instance->pdev->dev,
3176 "Firmware crash dump is not available\n");
3177 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3178 return -EINVAL;
3179 }
3180
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003181 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303182 dev_err(&instance->pdev->dev,
3183 "Firmware crash dump offset is out of range\n");
3184 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3185 return 0;
3186 }
3187
3188 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3189 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3190
3191 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3192 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003193 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303194 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3195
3196 return size;
3197}
3198
3199static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003200fw_crash_buffer_size_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303201 struct device_attribute *attr, char *buf)
3202{
3203 struct Scsi_Host *shost = class_to_shost(cdev);
3204 struct megasas_instance *instance =
3205 (struct megasas_instance *) shost->hostdata;
3206
3207 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3208 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3209}
3210
3211static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003212fw_crash_state_store(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303213 struct device_attribute *attr, const char *buf, size_t count)
3214{
3215 struct Scsi_Host *shost = class_to_shost(cdev);
3216 struct megasas_instance *instance =
3217 (struct megasas_instance *) shost->hostdata;
3218 int val = 0;
3219 unsigned long flags;
3220
3221 if (kstrtoint(buf, 0, &val) != 0)
3222 return -EINVAL;
3223
3224 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3225 dev_err(&instance->pdev->dev, "application updates invalid "
3226 "firmware crash state\n");
3227 return -EINVAL;
3228 }
3229
3230 instance->fw_crash_state = val;
3231
3232 if ((val == COPIED) || (val == COPY_ERROR)) {
3233 spin_lock_irqsave(&instance->crashdump_lock, flags);
3234 megasas_free_host_crash_buffer(instance);
3235 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3236 if (val == COPY_ERROR)
3237 dev_info(&instance->pdev->dev, "application failed to "
3238 "copy Firmware crash dump\n");
3239 else
3240 dev_info(&instance->pdev->dev, "Firmware crash dump "
3241 "copied successfully\n");
3242 }
3243 return strlen(buf);
3244}
3245
3246static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003247fw_crash_state_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303248 struct device_attribute *attr, char *buf)
3249{
3250 struct Scsi_Host *shost = class_to_shost(cdev);
3251 struct megasas_instance *instance =
3252 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003253
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303254 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3255}
3256
3257static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003258page_size_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303259 struct device_attribute *attr, char *buf)
3260{
3261 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3262}
3263
Sumit Saxena308ec452016-01-28 21:04:30 +05303264static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003265ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
Sumit Saxena308ec452016-01-28 21:04:30 +05303266 char *buf)
3267{
3268 struct Scsi_Host *shost = class_to_shost(cdev);
3269 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3270
3271 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3272}
3273
Shivasharan S88d155c2018-01-05 05:27:46 -08003274static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003275fw_cmds_outstanding_show(struct device *cdev,
Shivasharan S88d155c2018-01-05 05:27:46 -08003276 struct device_attribute *attr, char *buf)
3277{
3278 struct Scsi_Host *shost = class_to_shost(cdev);
3279 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3280
3281 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3282}
3283
Shivasharan Scfb9a302019-05-07 10:05:40 -07003284static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003285dump_system_regs_show(struct device *cdev,
Shivasharan Scfb9a302019-05-07 10:05:40 -07003286 struct device_attribute *attr, char *buf)
3287{
3288 struct Scsi_Host *shost = class_to_shost(cdev);
3289 struct megasas_instance *instance =
3290 (struct megasas_instance *)shost->hostdata;
3291
3292 return megasas_dump_sys_regs(instance->reg_set, buf);
3293}
3294
Shivasharan Sa6024a92019-05-07 10:05:43 -07003295static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003296raid_map_id_show(struct device *cdev, struct device_attribute *attr,
Shivasharan Sa6024a92019-05-07 10:05:43 -07003297 char *buf)
3298{
3299 struct Scsi_Host *shost = class_to_shost(cdev);
3300 struct megasas_instance *instance =
3301 (struct megasas_instance *)shost->hostdata;
3302
3303 return snprintf(buf, PAGE_SIZE, "%ld\n",
3304 (unsigned long)instance->map_id);
3305}
3306
Tomas Henzld6354682019-05-29 18:00:41 +02003307static DEVICE_ATTR_RW(fw_crash_buffer);
3308static DEVICE_ATTR_RO(fw_crash_buffer_size);
3309static DEVICE_ATTR_RW(fw_crash_state);
3310static DEVICE_ATTR_RO(page_size);
3311static DEVICE_ATTR_RO(ldio_outstanding);
3312static DEVICE_ATTR_RO(fw_cmds_outstanding);
3313static DEVICE_ATTR_RO(dump_system_regs);
3314static DEVICE_ATTR_RO(raid_map_id);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303315
YueHaibing6764f512019-07-02 21:01:14 +08003316static struct device_attribute *megaraid_host_attrs[] = {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303317 &dev_attr_fw_crash_buffer_size,
3318 &dev_attr_fw_crash_buffer,
3319 &dev_attr_fw_crash_state,
3320 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303321 &dev_attr_ldio_outstanding,
Shivasharan S88d155c2018-01-05 05:27:46 -08003322 &dev_attr_fw_cmds_outstanding,
Shivasharan Scfb9a302019-05-07 10:05:40 -07003323 &dev_attr_dump_system_regs,
Shivasharan Sa6024a92019-05-07 10:05:43 -07003324 &dev_attr_raid_map_id,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303325 NULL,
3326};
3327
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003328/*
3329 * Scsi host template for megaraid_sas driver
3330 */
3331static struct scsi_host_template megasas_template = {
3332
3333 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303334 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003335 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003336 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003337 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303338 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003339 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003340 .eh_target_reset_handler = megasas_reset_target,
3341 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003342 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003343 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303344 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003345 .bios_param = megasas_bios_param,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003346 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwig07d9aa12019-06-17 14:20:00 +02003347 .max_segment_size = 0xffffffff,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003348 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003349};
3350
3351/**
3352 * megasas_complete_int_cmd - Completes an internal command
3353 * @instance: Adapter soft state
3354 * @cmd: Command to be completed
3355 *
3356 * The megasas_issue_blocked_cmd() function waits for a command to complete
3357 * after it issues a command. This function wakes up that waiting routine by
3358 * calling wake_up() on the wait queue.
3359 */
3360static void
3361megasas_complete_int_cmd(struct megasas_instance *instance,
3362 struct megasas_cmd *cmd)
3363{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303364 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003365 wake_up(&instance->int_cmd_wait_q);
3366}
3367
3368/**
3369 * megasas_complete_abort - Completes aborting a command
3370 * @instance: Adapter soft state
3371 * @cmd: Cmd that was issued to abort another cmd
3372 *
adam radford0d490162010-12-14 19:17:17 -08003373 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3374 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003375 * wakes up all functions waiting on the same wait queue.
3376 */
3377static void
3378megasas_complete_abort(struct megasas_instance *instance,
3379 struct megasas_cmd *cmd)
3380{
3381 if (cmd->sync_cmd) {
3382 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303383 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003384 wake_up(&instance->abort_cmd_wait_q);
3385 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003386}
3387
3388/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003389 * megasas_complete_cmd - Completes a command
3390 * @instance: Adapter soft state
3391 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003392 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003393 * SCSI mid-layer instead of the value returned
3394 * by the FW. This should be used if caller wants
3395 * an alternate status (as in the case of aborted
3396 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003397 */
adam radford9c915a82010-12-21 13:34:31 -08003398void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3400 u8 alt_status)
3401{
3402 int exception = 0;
3403 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003404 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003405 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303406 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003407
bo yang39a98552010-09-22 22:36:29 -04003408 /* flag for the retry reset */
3409 cmd->retry_for_fw_reset = 0;
3410
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003411 if (cmd->scmd)
3412 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003413
3414 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003415 case MFI_CMD_INVALID:
3416 /* Some older 1068 controller FW may keep a pended
3417 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3418 when booting the kdump kernel. Ignore this command to
3419 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003420 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3421 "completed\n");
3422 dev_warn(&instance->pdev->dev, "If you have a controller "
3423 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003424 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003425 case MFI_CMD_PD_SCSI_IO:
3426 case MFI_CMD_LD_SCSI_IO:
3427
3428 /*
3429 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3430 * issued either through an IO path or an IOCTL path. If it
3431 * was via IOCTL, we will send it to internal completion.
3432 */
3433 if (cmd->sync_cmd) {
3434 cmd->sync_cmd = 0;
3435 megasas_complete_int_cmd(instance, cmd);
3436 break;
3437 }
Gustavo A. R. Silva8d07f7d2018-11-27 22:32:27 -06003438 /* fall through */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003439
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003440 case MFI_CMD_LD_READ:
3441 case MFI_CMD_LD_WRITE:
3442
3443 if (alt_status) {
3444 cmd->scmd->result = alt_status << 16;
3445 exception = 1;
3446 }
3447
3448 if (exception) {
3449
Sumant Patroe4a082c2006-05-30 12:03:37 -07003450 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003451
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003452 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003453 cmd->scmd->scsi_done(cmd->scmd);
3454 megasas_return_cmd(instance, cmd);
3455
3456 break;
3457 }
3458
3459 switch (hdr->cmd_status) {
3460
3461 case MFI_STAT_OK:
3462 cmd->scmd->result = DID_OK << 16;
3463 break;
3464
3465 case MFI_STAT_SCSI_IO_FAILED:
3466 case MFI_STAT_LD_INIT_IN_PROGRESS:
3467 cmd->scmd->result =
3468 (DID_ERROR << 16) | hdr->scsi_status;
3469 break;
3470
3471 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3472
3473 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3474
3475 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3476 memset(cmd->scmd->sense_buffer, 0,
3477 SCSI_SENSE_BUFFERSIZE);
3478 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3479 hdr->sense_len);
3480
3481 cmd->scmd->result |= DRIVER_SENSE << 24;
3482 }
3483
3484 break;
3485
3486 case MFI_STAT_LD_OFFLINE:
3487 case MFI_STAT_DEVICE_NOT_FOUND:
3488 cmd->scmd->result = DID_BAD_TARGET << 16;
3489 break;
3490
3491 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003492 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003493 hdr->cmd_status);
3494 cmd->scmd->result = DID_ERROR << 16;
3495 break;
3496 }
3497
Sumant Patroe4a082c2006-05-30 12:03:37 -07003498 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003499
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003500 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003501 cmd->scmd->scsi_done(cmd->scmd);
3502 megasas_return_cmd(instance, cmd);
3503
3504 break;
3505
3506 case MFI_CMD_SMP:
3507 case MFI_CMD_STP:
Shivasharan Sf870bcb2018-01-05 05:33:04 -08003508 case MFI_CMD_NVME:
Chandrakanth Patil58136852019-06-25 16:34:30 +05303509 case MFI_CMD_TOOLBOX:
Shivasharan S82add4e2017-10-19 02:49:02 -07003510 megasas_complete_int_cmd(instance, cmd);
3511 break;
3512
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003513 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303514 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003515 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303516 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3517 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303518 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003519 spin_lock_irqsave(instance->host->host_lock, flags);
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003520 status = cmd->frame->hdr.cmd_status;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303521 instance->map_update_cmd = NULL;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003522 if (status != MFI_STAT_OK) {
3523 if (status != MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003524 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003525 cmd->frame->hdr.cmd_status);
3526 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303527 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003528 spin_unlock_irqrestore(
3529 instance->host->host_lock,
3530 flags);
3531 break;
3532 }
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003533 }
3534
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303535 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303536
3537 /*
3538 * Set fast path IO to ZERO.
3539 * Validate Map will set proper value.
3540 * Meanwhile all IOs will go as LD IO.
3541 */
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003542 if (status == MFI_STAT_OK &&
3543 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3544 instance->map_id++;
adam radford9c915a82010-12-21 13:34:31 -08003545 fusion->fast_path_io = 1;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003546 } else {
adam radford9c915a82010-12-21 13:34:31 -08003547 fusion->fast_path_io = 0;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003548 }
3549
adam radford9c915a82010-12-21 13:34:31 -08003550 megasas_sync_map_info(instance);
3551 spin_unlock_irqrestore(instance->host->host_lock,
3552 flags);
3553 break;
3554 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303555 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3556 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003557 spin_lock_irqsave(&poll_aen_lock, flags);
3558 megasas_poll_wait_aen = 0;
3559 spin_unlock_irqrestore(&poll_aen_lock, flags);
3560 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003561
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303562 /* FW has an updated PD sequence */
3563 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3564 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3565
3566 spin_lock_irqsave(instance->host->host_lock, flags);
3567 status = cmd->frame->hdr.cmd_status;
3568 instance->jbod_seq_cmd = NULL;
3569 megasas_return_cmd(instance, cmd);
3570
3571 if (status == MFI_STAT_OK) {
3572 instance->pd_seq_map_id++;
3573 /* Re-register a pd sync seq num cmd */
3574 if (megasas_sync_pd_seq_num(instance, true))
3575 instance->use_seqnum_jbod_fp = false;
3576 } else
3577 instance->use_seqnum_jbod_fp = false;
3578
3579 spin_unlock_irqrestore(instance->host->host_lock, flags);
3580 break;
3581 }
3582
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003583 /*
3584 * See if got an event notification
3585 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303586 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003587 megasas_service_aen(instance, cmd);
3588 else
3589 megasas_complete_int_cmd(instance, cmd);
3590
3591 break;
3592
3593 case MFI_CMD_ABORT:
3594 /*
3595 * Cmd issued to abort another cmd returned
3596 */
3597 megasas_complete_abort(instance, cmd);
3598 break;
3599
3600 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003601 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003602 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003603 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003604 break;
3605 }
3606}
3607
3608/**
bo yang39a98552010-09-22 22:36:29 -04003609 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003610 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003611 * @instance: Adapter soft state
3612 */
3613static inline void
3614megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3615{
3616 struct megasas_cmd *cmd;
3617 struct list_head clist_local;
3618 union megasas_evt_class_locale class_locale;
3619 unsigned long flags;
3620 u32 seq_num;
3621
3622 INIT_LIST_HEAD(&clist_local);
3623 spin_lock_irqsave(&instance->hba_lock, flags);
3624 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3625 spin_unlock_irqrestore(&instance->hba_lock, flags);
3626
3627 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003628 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003629 struct megasas_cmd, list);
3630 list_del_init(&cmd->list);
3631
3632 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003633 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3634 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003635 cmd, cmd->scmd, cmd->sync_cmd);
3636
3637 cmd->retry_for_fw_reset++;
3638
3639 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003640 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003641 "was tried multiple times during reset."
3642 "Shutting down the HBA\n",
3643 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303644 instance->instancet->disable_intr(instance);
3645 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003646 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003647 return;
3648 }
3649 }
3650
3651 if (cmd->sync_cmd == 1) {
3652 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003653 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003654 "cmd attached to internal command!\n");
3655 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003656 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003657 "on the internal reset queue,"
3658 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303659 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003660 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003661 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003662 0, instance->reg_set);
3663 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003664 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003665 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003666 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003667
3668 atomic_inc(&instance->fw_outstanding);
3669 instance->instancet->fire_cmd(instance,
3670 cmd->frame_phys_addr,
3671 cmd->frame_count-1, instance->reg_set);
3672 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003673 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003674 "internal reset defer list while re-issue!!\n",
3675 cmd);
3676 }
3677 }
3678
3679 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003680 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003681 megasas_return_cmd(instance, instance->aen_cmd);
3682
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003683 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003684 }
3685
3686 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003687 * Initiate AEN (Asynchronous Event Notification)
3688 */
bo yang39a98552010-09-22 22:36:29 -04003689 seq_num = instance->last_seq_num;
3690 class_locale.members.reserved = 0;
3691 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3692 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3693
3694 megasas_register_aen(instance, seq_num, class_locale.word);
3695}
3696
3697/**
3698 * Move the internal reset pending commands to a deferred queue.
3699 *
3700 * We move the commands pending at internal reset time to a
3701 * pending queue. This queue would be flushed after successful
3702 * completion of the internal reset sequence. if the internal reset
3703 * did not complete in time, the kernel reset handler would flush
3704 * these commands.
3705 **/
3706static void
3707megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3708{
3709 struct megasas_cmd *cmd;
3710 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003711 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003712 u32 defer_index;
3713 unsigned long flags;
3714
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003715 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303716 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003717 for (i = 0; i < max_cmd; i++) {
3718 cmd = instance->cmd_list[i];
3719 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003720 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003721 "on the defer queue as internal\n",
3722 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3723
3724 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003725 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003726 " moving this cmd:%p, %d %p, it was"
3727 "discovered on some list?\n",
3728 cmd, cmd->sync_cmd, cmd->scmd);
3729
3730 list_del_init(&cmd->list);
3731 }
3732 defer_index++;
3733 list_add_tail(&cmd->list,
3734 &instance->internal_reset_pending_q);
3735 }
3736 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303737 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003738}
3739
3740
3741static void
3742process_fw_state_change_wq(struct work_struct *work)
3743{
3744 struct megasas_instance *instance =
3745 container_of(work, struct megasas_instance, work_init);
3746 u32 wait;
3747 unsigned long flags;
3748
Sumit Saxena8a01a412016-01-28 21:04:32 +05303749 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003750 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303751 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003752 return ;
3753 }
3754
Sumit Saxena8a01a412016-01-28 21:04:32 +05303755 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003756 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003757 "state, restarting it...\n");
3758
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303759 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003760 atomic_set(&instance->fw_outstanding, 0);
3761
3762 atomic_set(&instance->fw_reset_no_pci_access, 1);
3763 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003764 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003765
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003766 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003767 "initiating next stage...\n");
3768
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003769 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003770 "state 2 starting...\n");
3771
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003772 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003773 for (wait = 0; wait < 30; wait++) {
3774 msleep(1000);
3775 }
3776
adam radford058a8fa2011-10-08 18:14:27 -07003777 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003778 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003779
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303780 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003781 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003782 return ;
3783 }
3784
3785 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3786 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3787 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3788 ) {
3789 *instance->consumer = *instance->producer;
3790 } else {
3791 *instance->consumer = 0;
3792 *instance->producer = 0;
3793 }
3794
3795 megasas_issue_init_mfi(instance);
3796
3797 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303798 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003799 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303800 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003801
3802 megasas_issue_pending_cmds_again(instance);
3803 instance->issuepend_done = 1;
3804 }
bo yang39a98552010-09-22 22:36:29 -04003805}
3806
3807/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003808 * megasas_deplete_reply_queue - Processes all completed commands
3809 * @instance: Adapter soft state
3810 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003811 * SCSI mid-layer instead of the status
3812 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003813 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003814 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003815static int
bo yang39a98552010-09-22 22:36:29 -04003816megasas_deplete_reply_queue(struct megasas_instance *instance,
3817 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003818{
bo yang39a98552010-09-22 22:36:29 -04003819 u32 mfiStatus;
3820 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003821
bo yang39a98552010-09-22 22:36:29 -04003822 if ((mfiStatus = instance->instancet->check_reset(instance,
3823 instance->reg_set)) == 1) {
3824 return IRQ_HANDLED;
3825 }
3826
Shivasharan Sde516372018-12-17 00:47:39 -08003827 mfiStatus = instance->instancet->clear_intr(instance);
3828 if (mfiStatus == 0) {
adam radforde1419192011-02-24 20:56:21 -08003829 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003830 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003831 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003832 }
3833
3834 instance->mfiStatus = mfiStatus;
3835
3836 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3837 fw_state = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08003838 instance) & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003839
3840 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003841 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003842 fw_state);
3843 }
3844
3845 if ((fw_state == MFI_STATE_FAULT) &&
3846 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003847 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003848
3849 if ((instance->pdev->device ==
3850 PCI_DEVICE_ID_LSI_SAS1064R) ||
3851 (instance->pdev->device ==
3852 PCI_DEVICE_ID_DELL_PERC5) ||
3853 (instance->pdev->device ==
3854 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3855
3856 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303857 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003858 }
3859
3860
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303861 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303862 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003863 instance->issuepend_done = 0;
3864
3865 atomic_set(&instance->fw_outstanding, 0);
3866 megasas_internal_reset_defer_cmds(instance);
3867
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003868 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303869 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003870
3871 schedule_work(&instance->work_init);
3872 return IRQ_HANDLED;
3873
3874 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003875 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003876 fw_state, instance->disableOnlineCtrlReset);
3877 }
3878 }
3879
Sumant Patro5d018ad2006-10-03 13:13:18 -07003880 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003881 return IRQ_HANDLED;
3882}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003883/**
3884 * megasas_isr - isr entry point
3885 */
David Howells7d12e782006-10-05 14:55:46 +01003886static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003887{
adam radfordc8e858f2011-10-08 18:15:13 -07003888 struct megasas_irq_context *irq_context = devp;
3889 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003890 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003891 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003892
adam radfordc8e858f2011-10-08 18:15:13 -07003893 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003894 return IRQ_HANDLED;
3895
bo yang39a98552010-09-22 22:36:29 -04003896 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003897 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003898 spin_unlock_irqrestore(&instance->hba_lock, flags);
3899
3900 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003901}
3902
3903/**
3904 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003905 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 *
3907 * During the initialization, FW passes can potentially be in any one of
3908 * several possible states. If the FW in operational, waiting-for-handshake
3909 * states, driver must take steps to bring it to ready state. Otherwise, it
3910 * has to wait for the ready state.
3911 */
adam radford9c915a82010-12-21 13:34:31 -08003912int
adam radford058a8fa2011-10-08 18:14:27 -07003913megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003914{
3915 int i;
3916 u8 max_wait;
3917 u32 fw_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003918 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003919
Shivasharan Sde516372018-12-17 00:47:39 -08003920 abs_state = instance->instancet->read_fw_status_reg(instance);
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003921 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922
Sumant Patroe3bbff92006-10-03 12:28:49 -07003923 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003924 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003925 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003926
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003927 while (fw_state != MFI_STATE_READY) {
3928
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003929 switch (fw_state) {
3930
3931 case MFI_STATE_FAULT:
Shivasharan Sb6661342019-05-07 10:05:44 -07003932 dev_printk(KERN_ERR, &instance->pdev->dev,
3933 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
3934 abs_state & MFI_STATE_FAULT_CODE,
3935 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
adam radford058a8fa2011-10-08 18:14:27 -07003936 if (ocr) {
3937 max_wait = MEGASAS_RESET_WAIT_TIME;
adam radford058a8fa2011-10-08 18:14:27 -07003938 break;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003939 } else {
3940 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
3941 megasas_dump_reg_set(instance->reg_set);
adam radford058a8fa2011-10-08 18:14:27 -07003942 return -ENODEV;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003943 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003944
3945 case MFI_STATE_WAIT_HANDSHAKE:
3946 /*
3947 * Set the CLR bit in inbound doorbell
3948 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003949 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003950 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3951 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003952 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003953 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003954 writel(
3955 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003956 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303957 else
Yang, Bo87911122009-10-06 14:31:54 -06003958 writel(
3959 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3960 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003961
Yang, Bo7218df62009-10-06 14:52:20 -06003962 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003963 break;
3964
Sumant Patroe3bbff92006-10-03 12:28:49 -07003965 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003966 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003967 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3968 (instance->pdev->device ==
3969 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003970 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003971 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003972 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303973 else
Yang, Bo87911122009-10-06 14:31:54 -06003974 writel(MFI_INIT_HOTPLUG,
3975 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003976
Yang, Bo7218df62009-10-06 14:52:20 -06003977 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003978 break;
3979
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003980 case MFI_STATE_OPERATIONAL:
3981 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003982 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003983 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303984 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003985 if ((instance->pdev->device ==
3986 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3987 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003988 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003989 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003990 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003991 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303992
Shivasharan Se7d36b82017-10-19 02:48:50 -07003993 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003994 for (i = 0; i < (10 * 1000); i += 20) {
Shivasharan S272652f2018-12-17 00:47:40 -08003995 if (megasas_readl(
3996 instance,
adam radford9c915a82010-12-21 13:34:31 -08003997 &instance->
3998 reg_set->
3999 doorbell) & 1)
4000 msleep(20);
4001 else
4002 break;
4003 }
4004 }
Yang, Bo87911122009-10-06 14:31:54 -06004005 } else
4006 writel(MFI_RESET_FLAGS,
4007 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004008
Yang, Bo7218df62009-10-06 14:52:20 -06004009 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004010 break;
4011
4012 case MFI_STATE_UNDEFINED:
4013 /*
4014 * This state should not last for more than 2 seconds
4015 */
Yang, Bo7218df62009-10-06 14:52:20 -06004016 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004017 break;
4018
4019 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06004020 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004021 break;
4022
4023 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06004024 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004025 break;
4026
4027 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06004028 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004029 break;
4030
4031 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06004032 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004033 break;
4034
4035 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06004036 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004037 break;
4038
4039 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004040 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041 fw_state);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004042 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4043 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004044 return -ENODEV;
4045 }
4046
4047 /*
4048 * The cur_state should not last for more than max_wait secs
4049 */
Steve Sistare31b6a052019-03-01 06:46:28 -08004050 for (i = 0; i < max_wait * 50; i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004051 curr_abs_state = instance->instancet->
Shivasharan Sde516372018-12-17 00:47:39 -08004052 read_fw_status_reg(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004053
Yang, Bo7218df62009-10-06 14:52:20 -06004054 if (abs_state == curr_abs_state) {
Steve Sistare31b6a052019-03-01 06:46:28 -08004055 msleep(20);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004056 } else
4057 break;
4058 }
4059
4060 /*
4061 * Return error if fw_state hasn't changed after max_wait
4062 */
Yang, Bo7218df62009-10-06 14:52:20 -06004063 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004064 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004065 "in %d secs\n", fw_state, max_wait);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004066 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4067 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004068 return -ENODEV;
4069 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004070
4071 abs_state = curr_abs_state;
4072 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04004073 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004074 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004075
4076 return 0;
4077}
4078
4079/**
4080 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4081 * @instance: Adapter soft state
4082 */
4083static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4084{
4085 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004086 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004087 struct megasas_cmd *cmd;
4088
4089 if (!instance->frame_dma_pool)
4090 return;
4091
4092 /*
4093 * Return all frames to pool
4094 */
4095 for (i = 0; i < max_cmd; i++) {
4096
4097 cmd = instance->cmd_list[i];
4098
4099 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02004100 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004101 cmd->frame_phys_addr);
4102
4103 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02004104 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004105 cmd->sense_phys_addr);
4106 }
4107
4108 /*
4109 * Now destroy the pool itself
4110 */
Romain Perierfc69d862017-07-06 10:13:06 +02004111 dma_pool_destroy(instance->frame_dma_pool);
4112 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004113
4114 instance->frame_dma_pool = NULL;
4115 instance->sense_dma_pool = NULL;
4116}
4117
4118/**
4119 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4120 * @instance: Adapter soft state
4121 *
4122 * Each command packet has an embedded DMA memory buffer that is used for
4123 * filling MFI frame and the SG list that immediately follows the frame. This
4124 * function creates those DMA memory buffers for each command packet by using
4125 * PCI pool facility.
4126 */
4127static int megasas_create_frame_pool(struct megasas_instance *instance)
4128{
4129 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004130 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004131 u32 frame_count;
4132 struct megasas_cmd *cmd;
4133
adam radford9c915a82010-12-21 13:34:31 -08004134 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004135
4136 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05304137 * For MFI controllers.
4138 * max_num_sge = 60
4139 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4140 * Total 960 byte (15 MFI frame of 64 byte)
4141 *
4142 * Fusion adapter require only 3 extra frame.
4143 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4144 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4145 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004146 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07004147 frame_count = (instance->adapter_type == MFI_SERIES) ?
4148 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08004149 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004150 /*
4151 * Use DMA pool facility provided by PCI layer
4152 */
Romain Perierfc69d862017-07-06 10:13:06 +02004153 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4154 &instance->pdev->dev,
4155 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004156
4157 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004158 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004159 return -ENOMEM;
4160 }
4161
Romain Perierfc69d862017-07-06 10:13:06 +02004162 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4163 &instance->pdev->dev, 128,
4164 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004165
4166 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004167 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004168
Romain Perierfc69d862017-07-06 10:13:06 +02004169 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004170 instance->frame_dma_pool = NULL;
4171
4172 return -ENOMEM;
4173 }
4174
4175 /*
4176 * Allocate and attach a frame to each of the commands in cmd_list.
4177 * By making cmd->index as the context instead of the &cmd, we can
4178 * always use 32bit context regardless of the architecture
4179 */
4180 for (i = 0; i < max_cmd; i++) {
4181
4182 cmd = instance->cmd_list[i];
4183
Souptick Joarder61b142a2018-02-15 21:55:06 +05304184 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004185 GFP_KERNEL, &cmd->frame_phys_addr);
4186
Romain Perierfc69d862017-07-06 10:13:06 +02004187 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004188 GFP_KERNEL, &cmd->sense_phys_addr);
4189
4190 /*
4191 * megasas_teardown_frame_pool() takes care of freeing
4192 * whatever has been allocated
4193 */
4194 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004195 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004196 megasas_teardown_frame_pool(instance);
4197 return -ENOMEM;
4198 }
4199
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304200 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004201 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004202 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004203 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004204 }
4205
4206 return 0;
4207}
4208
4209/**
4210 * megasas_free_cmds - Free all the cmds in the free cmd pool
4211 * @instance: Adapter soft state
4212 */
adam radford9c915a82010-12-21 13:34:31 -08004213void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004214{
4215 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004216
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004217 /* First free the MFI frame pool */
4218 megasas_teardown_frame_pool(instance);
4219
4220 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004221 for (i = 0; i < instance->max_mfi_cmds; i++)
4222
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004223 kfree(instance->cmd_list[i]);
4224
4225 /* Free the cmd_list buffer itself */
4226 kfree(instance->cmd_list);
4227 instance->cmd_list = NULL;
4228
4229 INIT_LIST_HEAD(&instance->cmd_pool);
4230}
4231
4232/**
4233 * megasas_alloc_cmds - Allocates the command packets
4234 * @instance: Adapter soft state
4235 *
4236 * Each command that is issued to the FW, whether IO commands from the OS or
4237 * internal commands like IOCTLs, are wrapped in local data structure called
4238 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4239 * the FW.
4240 *
4241 * Each frame has a 32-bit field called context (tag). This context is used
4242 * to get back the megasas_cmd from the frame when a frame gets completed in
4243 * the ISR. Typically the address of the megasas_cmd itself would be used as
4244 * the context. But we wanted to keep the differences between 32 and 64 bit
4245 * systems to the mininum. We always use 32 bit integers for the context. In
4246 * this driver, the 32 bit values are the indices into an array cmd_list.
4247 * This array is used only to look up the megasas_cmd given the context. The
4248 * free commands themselves are maintained in a linked list called cmd_pool.
4249 */
adam radford9c915a82010-12-21 13:34:31 -08004250int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004251{
4252 int i;
4253 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004254 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004255 struct megasas_cmd *cmd;
4256
adam radford9c915a82010-12-21 13:34:31 -08004257 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004258
4259 /*
4260 * instance->cmd_list is an array of struct megasas_cmd pointers.
4261 * Allocate the dynamic array first and then allocate individual
4262 * commands.
4263 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004264 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004265
4266 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004267 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004268 return -ENOMEM;
4269 }
4270
adam radford9c915a82010-12-21 13:34:31 -08004271 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004272
4273 for (i = 0; i < max_cmd; i++) {
4274 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4275 GFP_KERNEL);
4276
4277 if (!instance->cmd_list[i]) {
4278
4279 for (j = 0; j < i; j++)
4280 kfree(instance->cmd_list[j]);
4281
4282 kfree(instance->cmd_list);
4283 instance->cmd_list = NULL;
4284
4285 return -ENOMEM;
4286 }
4287 }
4288
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004289 for (i = 0; i < max_cmd; i++) {
4290 cmd = instance->cmd_list[i];
4291 memset(cmd, 0, sizeof(struct megasas_cmd));
4292 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004293 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004294 cmd->instance = instance;
4295
4296 list_add_tail(&cmd->list, &instance->cmd_pool);
4297 }
4298
4299 /*
4300 * Create a frame pool and assign one frame to each cmd
4301 */
4302 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004303 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004304 megasas_free_cmds(instance);
Jason Yanbcf3b672019-02-15 19:50:27 +08004305 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004306 }
4307
4308 return 0;
4309}
4310
Yang, Bo81e403c2009-10-06 14:27:54 -06004311/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304312 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4313 * @instance: Adapter soft state
4314 *
4315 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4316 * or FW is not under OCR.
4317 */
4318inline int
4319dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4320
Shivasharan Se7d36b82017-10-19 02:48:50 -07004321 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304322 return KILL_ADAPTER;
4323 else if (instance->unload ||
4324 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4325 return IGNORE_TIMEOUT;
4326 else
4327 return INITIATE_OCR;
4328}
4329
Shivasharan S15dd0382017-02-10 00:59:10 -08004330static void
4331megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304332{
4333 int ret;
4334 struct megasas_cmd *cmd;
4335 struct megasas_dcmd_frame *dcmd;
4336
Shivasharan S15dd0382017-02-10 00:59:10 -08004337 struct MR_PRIV_DEVICE *mr_device_priv_data;
4338 u16 device_id = 0;
4339
4340 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304341 cmd = megasas_get_cmd(instance);
4342
4343 if (!cmd) {
4344 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004345 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304346 }
4347
4348 dcmd = &cmd->frame->dcmd;
4349
4350 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4351 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4352
4353 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4354 dcmd->cmd = MFI_CMD_DCMD;
4355 dcmd->cmd_status = 0xFF;
4356 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004357 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304358 dcmd->timeout = 0;
4359 dcmd->pad_0 = 0;
4360 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4361 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304362
Shivasharan S107a60d2017-10-19 02:49:05 -07004363 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4364 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304365
Shivasharan Se7d36b82017-10-19 02:48:50 -07004366 if ((instance->adapter_type != MFI_SERIES) &&
4367 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304368 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4369 else
4370 ret = megasas_issue_polled(instance, cmd);
4371
4372 switch (ret) {
4373 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004374 mr_device_priv_data = sdev->hostdata;
4375 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4376 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304377 instance->pd_info->state.ddf.pdType.intf;
4378 break;
4379
4380 case DCMD_TIMEOUT:
4381
4382 switch (dcmd_timeout_ocr_possible(instance)) {
4383 case INITIATE_OCR:
4384 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304385 mutex_unlock(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05304386 megasas_reset_fusion(instance->host,
4387 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304388 mutex_lock(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05304389 break;
4390 case KILL_ADAPTER:
4391 megaraid_sas_kill_hba(instance);
4392 break;
4393 case IGNORE_TIMEOUT:
4394 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4395 __func__, __LINE__);
4396 break;
4397 }
4398
4399 break;
4400 }
4401
4402 if (ret != DCMD_TIMEOUT)
4403 megasas_return_cmd(instance, cmd);
4404
Shivasharan S15dd0382017-02-10 00:59:10 -08004405 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304406}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304407/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004408 * megasas_get_pd_list_info - Returns FW's pd_list structure
4409 * @instance: Adapter soft state
4410 * @pd_list: pd_list structure
4411 *
4412 * Issues an internal command (DCMD) to get the FW's controller PD
4413 * list structure. This information is mainly used to find out SYSTEM
4414 * supported by the FW.
4415 */
4416static int
4417megasas_get_pd_list(struct megasas_instance *instance)
4418{
4419 int ret = 0, pd_index = 0;
4420 struct megasas_cmd *cmd;
4421 struct megasas_dcmd_frame *dcmd;
4422 struct MR_PD_LIST *ci;
4423 struct MR_PD_ADDRESS *pd_addr;
Yang, Bo81e403c2009-10-06 14:27:54 -06004424
Sumit Saxenad9083162016-07-08 03:30:16 -07004425 if (instance->pd_list_not_supported) {
4426 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4427 "not supported by firmware\n");
4428 return ret;
4429 }
4430
Shivasharan S9b3d0282017-10-19 02:48:56 -07004431 ci = instance->pd_list_buf;
Shivasharan S9b3d0282017-10-19 02:48:56 -07004432
Yang, Bo81e403c2009-10-06 14:27:54 -06004433 cmd = megasas_get_cmd(instance);
4434
4435 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004436 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004437 return -ENOMEM;
4438 }
4439
4440 dcmd = &cmd->frame->dcmd;
4441
Yang, Bo81e403c2009-10-06 14:27:54 -06004442 memset(ci, 0, sizeof(*ci));
4443 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4444
4445 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4446 dcmd->mbox.b[1] = 0;
4447 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304448 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004449 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004450 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004451 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004452 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304453 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4454 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004455
Shivasharan S107a60d2017-10-19 02:49:05 -07004456 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4457 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004458
Shivasharan Se7d36b82017-10-19 02:48:50 -07004459 if ((instance->adapter_type != MFI_SERIES) &&
4460 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304461 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304462 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304463 else
4464 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004465
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304466 switch (ret) {
4467 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004468 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4469 "failed/not supported by firmware\n");
4470
Shivasharan Se7d36b82017-10-19 02:48:50 -07004471 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004472 megaraid_sas_kill_hba(instance);
4473 else
4474 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304475 break;
4476 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004477
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304478 switch (dcmd_timeout_ocr_possible(instance)) {
4479 case INITIATE_OCR:
4480 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4481 /*
4482 * DCMD failed from AEN path.
4483 * AEN path already hold reset_mutex to avoid PCI access
4484 * while OCR is in progress.
4485 */
4486 mutex_unlock(&instance->reset_mutex);
4487 megasas_reset_fusion(instance->host,
4488 MFI_IO_TIMEOUT_OCR);
4489 mutex_lock(&instance->reset_mutex);
4490 break;
4491 case KILL_ADAPTER:
4492 megaraid_sas_kill_hba(instance);
4493 break;
4494 case IGNORE_TIMEOUT:
4495 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4496 __func__, __LINE__);
4497 break;
4498 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004499
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304500 break;
4501
4502 case DCMD_SUCCESS:
4503 pd_addr = ci->addr;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004504 if (megasas_dbg_lvl & LD_PD_DEBUG)
4505 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4506 __func__, le32_to_cpu(ci->count));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304507
4508 if ((le32_to_cpu(ci->count) >
4509 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4510 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004511
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304512 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304513 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004514
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304515 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304516 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304517 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304518 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304519 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304520 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304521 MR_PD_STATE_SYSTEM;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004522 if (megasas_dbg_lvl & LD_PD_DEBUG)
4523 dev_info(&instance->pdev->dev,
4524 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4525 pd_index, le16_to_cpu(pd_addr->deviceId),
4526 pd_addr->scsiDevType);
Yang, Bo81e403c2009-10-06 14:27:54 -06004527 pd_addr++;
4528 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304529
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304530 memcpy(instance->pd_list, instance->local_pd_list,
4531 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304532 break;
4533
Yang, Bo81e403c2009-10-06 14:27:54 -06004534 }
4535
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304536 if (ret != DCMD_TIMEOUT)
4537 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004538
4539 return ret;
4540}
4541
Yang, Bobdc6fb82009-12-06 08:30:19 -07004542/*
4543 * megasas_get_ld_list_info - Returns FW's ld_list structure
4544 * @instance: Adapter soft state
4545 * @ld_list: ld_list structure
4546 *
4547 * Issues an internal command (DCMD) to get the FW's controller PD
4548 * list structure. This information is mainly used to find out SYSTEM
4549 * supported by the FW.
4550 */
4551static int
4552megasas_get_ld_list(struct megasas_instance *instance)
4553{
4554 int ret = 0, ld_index = 0, ids = 0;
4555 struct megasas_cmd *cmd;
4556 struct megasas_dcmd_frame *dcmd;
4557 struct MR_LD_LIST *ci;
4558 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304559 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004560
Shivasharan S9b3d0282017-10-19 02:48:56 -07004561 ci = instance->ld_list_buf;
4562 ci_h = instance->ld_list_buf_h;
4563
Yang, Bobdc6fb82009-12-06 08:30:19 -07004564 cmd = megasas_get_cmd(instance);
4565
4566 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004567 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004568 return -ENOMEM;
4569 }
4570
4571 dcmd = &cmd->frame->dcmd;
4572
Yang, Bobdc6fb82009-12-06 08:30:19 -07004573 memset(ci, 0, sizeof(*ci));
4574 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4575
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304576 if (instance->supportmax256vd)
4577 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004578 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304579 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004580 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004581 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004582 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304583 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4584 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004585 dcmd->pad_0 = 0;
4586
Shivasharan S107a60d2017-10-19 02:49:05 -07004587 megasas_set_dma_settings(instance, dcmd, ci_h,
4588 sizeof(struct MR_LD_LIST));
4589
Shivasharan Se7d36b82017-10-19 02:48:50 -07004590 if ((instance->adapter_type != MFI_SERIES) &&
4591 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304592 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304593 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304594 else
4595 ret = megasas_issue_polled(instance, cmd);
4596
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304597 ld_count = le32_to_cpu(ci->ldCount);
4598
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304599 switch (ret) {
4600 case DCMD_FAILED:
4601 megaraid_sas_kill_hba(instance);
4602 break;
4603 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004604
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304605 switch (dcmd_timeout_ocr_possible(instance)) {
4606 case INITIATE_OCR:
4607 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4608 /*
4609 * DCMD failed from AEN path.
4610 * AEN path already hold reset_mutex to avoid PCI access
4611 * while OCR is in progress.
4612 */
4613 mutex_unlock(&instance->reset_mutex);
4614 megasas_reset_fusion(instance->host,
4615 MFI_IO_TIMEOUT_OCR);
4616 mutex_lock(&instance->reset_mutex);
4617 break;
4618 case KILL_ADAPTER:
4619 megaraid_sas_kill_hba(instance);
4620 break;
4621 case IGNORE_TIMEOUT:
4622 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4623 __func__, __LINE__);
4624 break;
4625 }
4626
4627 break;
4628
4629 case DCMD_SUCCESS:
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004630 if (megasas_dbg_lvl & LD_PD_DEBUG)
4631 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4632 __func__, ld_count);
4633
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304634 if (ld_count > instance->fw_supported_vd_count)
4635 break;
4636
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304637 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004638
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304639 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004640 if (ci->ldList[ld_index].state != 0) {
4641 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304642 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004643 if (megasas_dbg_lvl & LD_PD_DEBUG)
4644 dev_info(&instance->pdev->dev,
4645 "LD%d: targetID: 0x%03x\n",
4646 ld_index, ids);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004647 }
4648 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304649
4650 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004651 }
4652
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304653 if (ret != DCMD_TIMEOUT)
4654 megasas_return_cmd(instance, cmd);
4655
Yang, Bobdc6fb82009-12-06 08:30:19 -07004656 return ret;
4657}
4658
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004659/**
adam radford21c9e162013-09-06 15:27:14 -07004660 * megasas_ld_list_query - Returns FW's ld_list structure
4661 * @instance: Adapter soft state
4662 * @ld_list: ld_list structure
4663 *
4664 * Issues an internal command (DCMD) to get the FW's controller PD
4665 * list structure. This information is mainly used to find out SYSTEM
4666 * supported by the FW.
4667 */
4668static int
4669megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4670{
4671 int ret = 0, ld_index = 0, ids = 0;
4672 struct megasas_cmd *cmd;
4673 struct megasas_dcmd_frame *dcmd;
4674 struct MR_LD_TARGETID_LIST *ci;
4675 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304676 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004677
Shivasharan S9b3d0282017-10-19 02:48:56 -07004678 ci = instance->ld_targetid_list_buf;
4679 ci_h = instance->ld_targetid_list_buf_h;
4680
adam radford21c9e162013-09-06 15:27:14 -07004681 cmd = megasas_get_cmd(instance);
4682
4683 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004684 dev_warn(&instance->pdev->dev,
4685 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004686 return -ENOMEM;
4687 }
4688
4689 dcmd = &cmd->frame->dcmd;
4690
adam radford21c9e162013-09-06 15:27:14 -07004691 memset(ci, 0, sizeof(*ci));
4692 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4693
4694 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304695 if (instance->supportmax256vd)
4696 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004697
4698 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304699 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004700 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004701 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004702 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304703 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4704 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004705 dcmd->pad_0 = 0;
4706
Shivasharan S107a60d2017-10-19 02:49:05 -07004707 megasas_set_dma_settings(instance, dcmd, ci_h,
4708 sizeof(struct MR_LD_TARGETID_LIST));
4709
Shivasharan Se7d36b82017-10-19 02:48:50 -07004710 if ((instance->adapter_type != MFI_SERIES) &&
4711 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304712 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304713 else
4714 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004715
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304716 switch (ret) {
4717 case DCMD_FAILED:
4718 dev_info(&instance->pdev->dev,
4719 "DCMD not supported by firmware - %s %d\n",
4720 __func__, __LINE__);
4721 ret = megasas_get_ld_list(instance);
4722 break;
4723 case DCMD_TIMEOUT:
4724 switch (dcmd_timeout_ocr_possible(instance)) {
4725 case INITIATE_OCR:
4726 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4727 /*
4728 * DCMD failed from AEN path.
4729 * AEN path already hold reset_mutex to avoid PCI access
4730 * while OCR is in progress.
4731 */
4732 mutex_unlock(&instance->reset_mutex);
4733 megasas_reset_fusion(instance->host,
4734 MFI_IO_TIMEOUT_OCR);
4735 mutex_lock(&instance->reset_mutex);
4736 break;
4737 case KILL_ADAPTER:
4738 megaraid_sas_kill_hba(instance);
4739 break;
4740 case IGNORE_TIMEOUT:
4741 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4742 __func__, __LINE__);
4743 break;
4744 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304745
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304746 break;
4747 case DCMD_SUCCESS:
4748 tgtid_count = le32_to_cpu(ci->count);
4749
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004750 if (megasas_dbg_lvl & LD_PD_DEBUG)
4751 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4752 __func__, tgtid_count);
4753
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304754 if ((tgtid_count > (instance->fw_supported_vd_count)))
4755 break;
4756
adam radford21c9e162013-09-06 15:27:14 -07004757 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304758 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004759 ids = ci->targetId[ld_index];
4760 instance->ld_ids[ids] = ci->targetId[ld_index];
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004761 if (megasas_dbg_lvl & LD_PD_DEBUG)
4762 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4763 ld_index, ci->targetId[ld_index]);
adam radford21c9e162013-09-06 15:27:14 -07004764 }
4765
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304766 break;
adam radford21c9e162013-09-06 15:27:14 -07004767 }
4768
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304769 if (ret != DCMD_TIMEOUT)
4770 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004771
4772 return ret;
4773}
4774
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004775/**
4776 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4777 * dcmd.mbox - reserved
4778 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4779 * Desc: This DCMD will return the combined device list
4780 * Status: MFI_STAT_OK - List returned successfully
4781 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4782 * disabled
4783 * @instance: Adapter soft state
4784 * @is_probe: Driver probe check
4785 * Return: 0 if DCMD succeeded
4786 * non-zero if failed
4787 */
YueHaibing7c3f8ca2019-04-13 22:21:38 +08004788static int
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004789megasas_host_device_list_query(struct megasas_instance *instance,
4790 bool is_probe)
4791{
4792 int ret, i, target_id;
4793 struct megasas_cmd *cmd;
4794 struct megasas_dcmd_frame *dcmd;
4795 struct MR_HOST_DEVICE_LIST *ci;
4796 u32 count;
4797 dma_addr_t ci_h;
4798
4799 ci = instance->host_device_list_buf;
4800 ci_h = instance->host_device_list_buf_h;
4801
4802 cmd = megasas_get_cmd(instance);
4803
4804 if (!cmd) {
4805 dev_warn(&instance->pdev->dev,
4806 "%s: failed to get cmd\n",
4807 __func__);
4808 return -ENOMEM;
4809 }
4810
4811 dcmd = &cmd->frame->dcmd;
4812
4813 memset(ci, 0, sizeof(*ci));
4814 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4815
4816 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4817 dcmd->cmd = MFI_CMD_DCMD;
4818 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4819 dcmd->sge_count = 1;
4820 dcmd->flags = MFI_FRAME_DIR_READ;
4821 dcmd->timeout = 0;
4822 dcmd->pad_0 = 0;
4823 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4824 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4825
4826 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4827
4828 if (!instance->mask_interrupts) {
4829 ret = megasas_issue_blocked_cmd(instance, cmd,
4830 MFI_IO_TIMEOUT_SECS);
4831 } else {
4832 ret = megasas_issue_polled(instance, cmd);
4833 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4834 }
4835
4836 switch (ret) {
4837 case DCMD_SUCCESS:
4838 /* Fill the internal pd_list and ld_ids array based on
4839 * targetIds returned by FW
4840 */
4841 count = le32_to_cpu(ci->count);
4842
Chandrakanth Patila4413a52019-06-25 16:34:27 +05304843 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
4844 break;
4845
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004846 if (megasas_dbg_lvl & LD_PD_DEBUG)
4847 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
4848 __func__, count);
4849
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004850 memset(instance->local_pd_list, 0,
4851 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4852 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4853 for (i = 0; i < count; i++) {
4854 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4855 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4856 instance->local_pd_list[target_id].tid = target_id;
4857 instance->local_pd_list[target_id].driveType =
4858 ci->host_device_list[i].scsi_type;
4859 instance->local_pd_list[target_id].driveState =
4860 MR_PD_STATE_SYSTEM;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004861 if (megasas_dbg_lvl & LD_PD_DEBUG)
4862 dev_info(&instance->pdev->dev,
4863 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
4864 i, target_id, ci->host_device_list[i].scsi_type);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004865 } else {
4866 instance->ld_ids[target_id] = target_id;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004867 if (megasas_dbg_lvl & LD_PD_DEBUG)
4868 dev_info(&instance->pdev->dev,
4869 "Device %d: LD targetID: 0x%03x\n",
4870 i, target_id);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004871 }
4872 }
4873
4874 memcpy(instance->pd_list, instance->local_pd_list,
4875 sizeof(instance->pd_list));
4876 break;
4877
4878 case DCMD_TIMEOUT:
4879 switch (dcmd_timeout_ocr_possible(instance)) {
4880 case INITIATE_OCR:
4881 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304882 mutex_unlock(&instance->reset_mutex);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004883 megasas_reset_fusion(instance->host,
4884 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304885 mutex_lock(&instance->reset_mutex);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004886 break;
4887 case KILL_ADAPTER:
4888 megaraid_sas_kill_hba(instance);
4889 break;
4890 case IGNORE_TIMEOUT:
4891 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4892 __func__, __LINE__);
4893 break;
4894 }
4895 break;
4896 case DCMD_FAILED:
4897 dev_err(&instance->pdev->dev,
4898 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4899 __func__);
4900 break;
4901 }
4902
4903 if (ret != DCMD_TIMEOUT)
4904 megasas_return_cmd(instance, cmd);
4905
4906 return ret;
4907}
4908
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304909/*
4910 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4911 * instance : Controller's instance
4912*/
4913static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4914{
4915 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004916 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304917
4918 fusion = instance->ctrl_context;
4919 /* For MFI based controllers return dummy success */
4920 if (!fusion)
4921 return;
4922
4923 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004924 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304925 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004926 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304927 instance->supportmax256vd = 1;
4928
4929 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4930 * MEGASAS_MAX_DEV_PER_CHANNEL;
4931 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4932 * MEGASAS_MAX_DEV_PER_CHANNEL;
4933 if (instance->supportmax256vd) {
4934 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4935 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4936 } else {
4937 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4938 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4939 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304940
4941 dev_info(&instance->pdev->dev,
Shivasharan Scba67d92018-10-16 23:37:56 -07004942 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4943 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4944 instance->ctrl_info_buf->max_lds);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304945
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004946 if (instance->max_raid_mapsize) {
4947 ventura_map_sz = instance->max_raid_mapsize *
4948 MR_MIN_MAP_SIZE; /* 64k */
4949 fusion->current_map_sz = ventura_map_sz;
4950 fusion->max_map_sz = ventura_map_sz;
4951 } else {
4952 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4953 (sizeof(struct MR_LD_SPAN_MAP) *
4954 (instance->fw_supported_vd_count - 1));
4955 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304956
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004957 fusion->max_map_sz =
4958 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304959
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004960 if (instance->supportmax256vd)
4961 fusion->current_map_sz = fusion->new_map_sz;
4962 else
4963 fusion->current_map_sz = fusion->old_map_sz;
4964 }
4965 /* irrespective of FW raid maps, driver raid map is constant */
4966 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304967}
4968
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004969/*
4970 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4971 * dcmd.hdr.length - number of bytes to read
4972 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4973 * Desc: Fill in snapdump properties
4974 * Status: MFI_STAT_OK- Command successful
4975 */
4976void megasas_get_snapdump_properties(struct megasas_instance *instance)
4977{
4978 int ret = 0;
4979 struct megasas_cmd *cmd;
4980 struct megasas_dcmd_frame *dcmd;
4981 struct MR_SNAPDUMP_PROPERTIES *ci;
4982 dma_addr_t ci_h = 0;
4983
4984 ci = instance->snapdump_prop;
4985 ci_h = instance->snapdump_prop_h;
4986
4987 if (!ci)
4988 return;
4989
4990 cmd = megasas_get_cmd(instance);
4991
4992 if (!cmd) {
4993 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4994 return;
4995 }
4996
4997 dcmd = &cmd->frame->dcmd;
4998
4999 memset(ci, 0, sizeof(*ci));
5000 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5001
5002 dcmd->cmd = MFI_CMD_DCMD;
5003 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5004 dcmd->sge_count = 1;
5005 dcmd->flags = MFI_FRAME_DIR_READ;
5006 dcmd->timeout = 0;
5007 dcmd->pad_0 = 0;
5008 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5009 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5010
5011 megasas_set_dma_settings(instance, dcmd, ci_h,
5012 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5013
5014 if (!instance->mask_interrupts) {
5015 ret = megasas_issue_blocked_cmd(instance, cmd,
5016 MFI_IO_TIMEOUT_SECS);
5017 } else {
5018 ret = megasas_issue_polled(instance, cmd);
5019 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5020 }
5021
5022 switch (ret) {
5023 case DCMD_SUCCESS:
5024 instance->snapdump_wait_time =
5025 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5026 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5027 break;
5028
5029 case DCMD_TIMEOUT:
5030 switch (dcmd_timeout_ocr_possible(instance)) {
5031 case INITIATE_OCR:
5032 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305033 mutex_unlock(&instance->reset_mutex);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005034 megasas_reset_fusion(instance->host,
5035 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305036 mutex_lock(&instance->reset_mutex);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005037 break;
5038 case KILL_ADAPTER:
5039 megaraid_sas_kill_hba(instance);
5040 break;
5041 case IGNORE_TIMEOUT:
5042 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5043 __func__, __LINE__);
5044 break;
5045 }
5046 }
5047
5048 if (ret != DCMD_TIMEOUT)
5049 megasas_return_cmd(instance, cmd);
5050}
5051
adam radford21c9e162013-09-06 15:27:14 -07005052/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005053 * megasas_get_controller_info - Returns FW's controller structure
5054 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005055 *
5056 * Issues an internal command (DCMD) to get the FW's controller structure.
5057 * This information is mainly used to find out the maximum IO transfer per
5058 * command supported by the FW.
5059 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305060int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305061megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005062{
5063 int ret = 0;
5064 struct megasas_cmd *cmd;
5065 struct megasas_dcmd_frame *dcmd;
5066 struct megasas_ctrl_info *ci;
5067 dma_addr_t ci_h = 0;
5068
Shivasharan S9b3d0282017-10-19 02:48:56 -07005069 ci = instance->ctrl_info_buf;
5070 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305071
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005072 cmd = megasas_get_cmd(instance);
5073
5074 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005075 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005076 return -ENOMEM;
5077 }
5078
5079 dcmd = &cmd->frame->dcmd;
5080
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005081 memset(ci, 0, sizeof(*ci));
5082 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5083
5084 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305085 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005086 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005087 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005088 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005089 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305090 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5091 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305092 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005093
Shivasharan S107a60d2017-10-19 02:49:05 -07005094 megasas_set_dma_settings(instance, dcmd, ci_h,
5095 sizeof(struct megasas_ctrl_info));
5096
Shivasharan Se7d36b82017-10-19 02:48:50 -07005097 if ((instance->adapter_type != MFI_SERIES) &&
Shivasharan S54b28042018-01-05 05:27:47 -08005098 !instance->mask_interrupts) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305099 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Shivasharan S54b28042018-01-05 05:27:47 -08005100 } else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305101 ret = megasas_issue_polled(instance, cmd);
Shivasharan S54b28042018-01-05 05:27:47 -08005102 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5103 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305104
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305105 switch (ret) {
5106 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305107 /* Save required controller information in
5108 * CPU endianness format.
5109 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005110 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005111 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
Shivasharan S9ad18a92017-10-19 02:48:57 -07005112 le32_to_cpus((u32 *)&ci->adapterOperations2);
5113 le32_to_cpus((u32 *)&ci->adapterOperations3);
5114 le16_to_cpus((u16 *)&ci->adapter_operations4);
Chandrakanth Patil58136852019-06-25 16:34:30 +05305115 le32_to_cpus((u32 *)&ci->adapter_operations5);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305116
5117 /* Update the latest Ext VD info.
5118 * From Init path, store current firmware details.
5119 * From OCR path, detect any firmware properties changes.
5120 * in case of Firmware upgrade without system reboot.
5121 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305122 megasas_update_ext_vd_details(instance);
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305123 instance->support_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005124 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05005125 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005126 ci->adapter_operations4.support_pd_map_target_id;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005127 instance->support_nvme_passthru =
5128 ci->adapter_operations4.support_nvme_passthru;
Chandrakanth Patil58136852019-06-25 16:34:30 +05305129 instance->support_pci_lane_margining =
5130 ci->adapter_operations5.support_pci_lane_margining;
Shivasharan Se9495e22018-06-04 03:45:12 -07005131 instance->task_abort_tmo = ci->TaskAbortTO;
5132 instance->max_reset_tmo = ci->MaxResetTO;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305133
5134 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005135 instance->is_imr = (ci->memory_size ? 0 : 1);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005136
5137 instance->snapdump_wait_time =
5138 (ci->properties.on_off_properties2.enable_snap_dump ?
5139 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5140
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005141 instance->enable_fw_dev_list =
5142 ci->properties.on_off_properties2.enable_fw_dev_list;
5143
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305144 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305145 "controller type\t: %s(%dMB)\n",
5146 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07005147 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305148
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05305149 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005150 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305151 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005152 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305153 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5154 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305155 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5156 instance->secure_jbod_support ? "Yes" : "No");
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005157 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5158 instance->support_nvme_passthru ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07005159 dev_info(&instance->pdev->dev,
5160 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5161 instance->task_abort_tmo, instance->max_reset_tmo);
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305162 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5163 instance->support_seqnum_jbod_fp ? "Yes" : "No");
Chandrakanth Patil58136852019-06-25 16:34:30 +05305164 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5165 instance->support_pci_lane_margining ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07005166
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305167 break;
5168
5169 case DCMD_TIMEOUT:
5170 switch (dcmd_timeout_ocr_possible(instance)) {
5171 case INITIATE_OCR:
5172 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305173 mutex_unlock(&instance->reset_mutex);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305174 megasas_reset_fusion(instance->host,
5175 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305176 mutex_lock(&instance->reset_mutex);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305177 break;
5178 case KILL_ADAPTER:
5179 megaraid_sas_kill_hba(instance);
5180 break;
5181 case IGNORE_TIMEOUT:
5182 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5183 __func__, __LINE__);
5184 break;
5185 }
Shivasharan S2747e6b2018-06-04 03:45:09 -07005186 break;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305187 case DCMD_FAILED:
5188 megaraid_sas_kill_hba(instance);
5189 break;
5190
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305191 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005192
Shivasharan S2747e6b2018-06-04 03:45:09 -07005193 if (ret != DCMD_TIMEOUT)
5194 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305195
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005196 return ret;
5197}
5198
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305199/*
5200 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5201 * to firmware
5202 *
5203 * @instance: Adapter soft state
5204 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5205 MR_CRASH_BUF_TURN_OFF = 0
5206 MR_CRASH_BUF_TURN_ON = 1
5207 * @return 0 on success non-zero on failure.
5208 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5209 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5210 * that driver supports crash dump feature. This DCMD will be sent only if
5211 * crash dump feature is supported by the FW.
5212 *
5213 */
5214int megasas_set_crash_dump_params(struct megasas_instance *instance,
5215 u8 crash_buf_state)
5216{
5217 int ret = 0;
5218 struct megasas_cmd *cmd;
5219 struct megasas_dcmd_frame *dcmd;
5220
5221 cmd = megasas_get_cmd(instance);
5222
5223 if (!cmd) {
5224 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5225 return -ENOMEM;
5226 }
5227
5228
5229 dcmd = &cmd->frame->dcmd;
5230
5231 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5232 dcmd->mbox.b[0] = crash_buf_state;
5233 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305234 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305235 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005236 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305237 dcmd->timeout = 0;
5238 dcmd->pad_0 = 0;
5239 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5240 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305241
Shivasharan S107a60d2017-10-19 02:49:05 -07005242 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5243 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305244
Shivasharan Se7d36b82017-10-19 02:48:50 -07005245 if ((instance->adapter_type != MFI_SERIES) &&
5246 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305247 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305248 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305249 ret = megasas_issue_polled(instance, cmd);
5250
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305251 if (ret == DCMD_TIMEOUT) {
5252 switch (dcmd_timeout_ocr_possible(instance)) {
5253 case INITIATE_OCR:
5254 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5255 megasas_reset_fusion(instance->host,
5256 MFI_IO_TIMEOUT_OCR);
5257 break;
5258 case KILL_ADAPTER:
5259 megaraid_sas_kill_hba(instance);
5260 break;
5261 case IGNORE_TIMEOUT:
5262 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5263 __func__, __LINE__);
5264 break;
5265 }
5266 } else
5267 megasas_return_cmd(instance, cmd);
5268
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305269 return ret;
5270}
5271
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005272/**
bo yang31ea7082007-11-07 12:09:50 -05005273 * megasas_issue_init_mfi - Initializes the FW
5274 * @instance: Adapter soft state
5275 *
5276 * Issues the INIT MFI cmd
5277 */
5278static int
5279megasas_issue_init_mfi(struct megasas_instance *instance)
5280{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05305281 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05005282 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05005283 struct megasas_init_frame *init_frame;
5284 struct megasas_init_queue_info *initq_info;
5285 dma_addr_t init_frame_h;
5286 dma_addr_t initq_info_h;
5287
5288 /*
5289 * Prepare a init frame. Note the init frame points to queue info
5290 * structure. Each frame has SGL allocated after first 64 bytes. For
5291 * this frame - since we don't need any SGL - we use SGL's space as
5292 * queue info structure
5293 *
5294 * We will not get a NULL command below. We just created the pool.
5295 */
5296 cmd = megasas_get_cmd(instance);
5297
5298 init_frame = (struct megasas_init_frame *)cmd->frame;
5299 initq_info = (struct megasas_init_queue_info *)
5300 ((unsigned long)init_frame + 64);
5301
5302 init_frame_h = cmd->frame_phys_addr;
5303 initq_info_h = init_frame_h + 64;
5304
5305 context = init_frame->context;
5306 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5307 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5308 init_frame->context = context;
5309
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305310 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5311 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05005312
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305313 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5314 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05005315
5316 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305317 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305318 init_frame->queue_info_new_phys_addr_lo =
5319 cpu_to_le32(lower_32_bits(initq_info_h));
5320 init_frame->queue_info_new_phys_addr_hi =
5321 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05005322
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305323 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05005324
5325 /*
5326 * disable the intr before firing the init frame to FW
5327 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305328 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05005329
5330 /*
5331 * Issue the init frame in polled mode
5332 */
5333
5334 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005335 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05005336 megasas_return_cmd(instance, cmd);
5337 goto fail_fw_init;
5338 }
5339
5340 megasas_return_cmd(instance, cmd);
5341
5342 return 0;
5343
5344fail_fw_init:
5345 return -EINVAL;
5346}
5347
adam radfordcd50ba82010-12-21 10:23:23 -08005348static u32
5349megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005350{
5351 u32 context_sz;
5352 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005354 /*
5355 * Get various operational parameters from status register
5356 */
Shivasharan Sde516372018-12-17 00:47:39 -08005357 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07005358 /*
5359 * Reduce the max supported cmds by 1. This is to ensure that the
5360 * reply_q_sz (1 more than the max cmd that driver may send)
5361 * does not exceed max cmds that the FW can support
5362 */
5363 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08005364 instance->max_mfi_cmds = instance->max_fw_cmds;
Shivasharan Sde516372018-12-17 00:47:39 -08005365 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08005366 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005367 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305368 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5369 * are reserved for IOCTL + driver's internal DCMDs.
5370 */
5371 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5372 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5373 instance->max_scsi_cmds = (instance->max_fw_cmds -
5374 MEGASAS_SKINNY_INT_CMDS);
5375 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5376 } else {
5377 instance->max_scsi_cmds = (instance->max_fw_cmds -
5378 MEGASAS_INT_CMDS);
5379 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5380 }
5381
Sumit Saxena308ec452016-01-28 21:04:30 +05305382 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305383 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005384 * Create a pool of commands
5385 */
5386 if (megasas_alloc_cmds(instance))
5387 goto fail_alloc_cmds;
5388
5389 /*
5390 * Allocate memory for reply queue. Length of reply queue should
5391 * be _one_ more than the maximum commands handled by the firmware.
5392 *
5393 * Note: When FW completes commands, it places corresponding contex
5394 * values in this circular reply queue. This circular queue is a fairly
5395 * typical producer-consumer queue. FW is the producer (of completed
5396 * commands) and the driver is the consumer.
5397 */
5398 context_sz = sizeof(u32);
5399 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5400
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005401 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5402 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005403
5404 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005405 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005406 goto fail_reply_queue;
5407 }
5408
bo yang31ea7082007-11-07 12:09:50 -05005409 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005410 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005411
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305412 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305413 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5414 "Fail from %s %d\n", instance->unique_id,
5415 __func__, __LINE__);
5416 goto fail_fw_init;
5417 }
5418
bo yang39a98552010-09-22 22:36:29 -04005419 instance->fw_support_ieee = 0;
5420 instance->fw_support_ieee =
Shivasharan Sde516372018-12-17 00:47:39 -08005421 (instance->instancet->read_fw_status_reg(instance) &
bo yang39a98552010-09-22 22:36:29 -04005422 0x04000000);
5423
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005424 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04005425 instance->fw_support_ieee);
5426
5427 if (instance->fw_support_ieee)
5428 instance->flag_ieee = 1;
5429
adam radfordcd50ba82010-12-21 10:23:23 -08005430 return 0;
5431
5432fail_fw_init:
5433
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005434 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
adam radfordcd50ba82010-12-21 10:23:23 -08005435 instance->reply_queue, instance->reply_queue_h);
5436fail_reply_queue:
5437 megasas_free_cmds(instance);
5438
5439fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08005440 return 1;
5441}
5442
Shivasharan S62a04f82019-05-07 10:05:35 -07005443static
5444void megasas_setup_irq_poll(struct megasas_instance *instance)
5445{
5446 struct megasas_irq_context *irq_ctx;
5447 u32 count, i;
5448
5449 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5450
5451 /* Initialize IRQ poll */
5452 for (i = 0; i < count; i++) {
5453 irq_ctx = &instance->irq_context[i];
5454 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5455 irq_ctx->irq_poll_scheduled = false;
5456 irq_poll_init(&irq_ctx->irqpoll,
5457 instance->threshold_reply_count,
5458 megasas_irqpoll);
5459 }
5460}
5461
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305462/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01005463 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305464 * @instance: Adapter soft state
5465 *
5466 * Do not enable interrupt, only setup ISRs.
5467 *
5468 * Return 0 on success.
5469 */
5470static int
5471megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5472{
5473 struct pci_dev *pdev;
5474
5475 pdev = instance->pdev;
5476 instance->irq_context[0].instance = instance;
5477 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005478 if (request_irq(pci_irq_vector(pdev, 0),
5479 instance->instancet->service_isr, IRQF_SHARED,
5480 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305481 dev_err(&instance->pdev->dev,
5482 "Failed to register IRQ from %s %d\n",
5483 __func__, __LINE__);
5484 return -1;
5485 }
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305486 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305487 instance->low_latency_index_start = 0;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305488 return 0;
5489}
5490
5491/**
5492 * megasas_setup_irqs_msix - register MSI-x interrupts.
5493 * @instance: Adapter soft state
5494 * @is_probe: Driver probe check
5495 *
5496 * Do not enable interrupt, only setup ISRs.
5497 *
5498 * Return 0 on success.
5499 */
5500static int
5501megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5502{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005503 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305504 struct pci_dev *pdev;
5505
5506 pdev = instance->pdev;
5507
5508 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305509 for (i = 0; i < instance->msix_vectors; i++) {
5510 instance->irq_context[i].instance = instance;
5511 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005512 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305513 instance->instancet->service_isr, 0, "megasas",
5514 &instance->irq_context[i])) {
5515 dev_err(&instance->pdev->dev,
5516 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005517 for (j = 0; j < i; j++)
5518 free_irq(pci_irq_vector(pdev, j),
5519 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305520 /* Retry irq register for IO_APIC*/
5521 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005522 instance->msix_load_balance = false;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005523 if (is_probe) {
5524 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305525 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005526 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305527 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005528 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305529 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305530 }
Shivasharan S1d15d902019-05-07 10:05:36 -07005531
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305532 return 0;
5533}
5534
5535/*
5536 * megasas_destroy_irqs- unregister interrupts.
5537 * @instance: Adapter soft state
5538 * return: void
5539 */
5540static void
5541megasas_destroy_irqs(struct megasas_instance *instance) {
5542
5543 int i;
Shivasharan S62a04f82019-05-07 10:05:35 -07005544 int count;
5545 struct megasas_irq_context *irq_ctx;
5546
5547 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5548 if (instance->adapter_type != MFI_SERIES) {
5549 for (i = 0; i < count; i++) {
5550 irq_ctx = &instance->irq_context[i];
5551 irq_poll_disable(&irq_ctx->irqpoll);
5552 }
5553 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305554
5555 if (instance->msix_vectors)
5556 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005557 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305558 &instance->irq_context[i]);
5559 }
5560 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005561 free_irq(pci_irq_vector(instance->pdev, 0),
5562 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305563}
5564
adam radfordcd50ba82010-12-21 10:23:23 -08005565/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305566 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5567 * @instance: Adapter soft state
5568 * @is_probe: Driver probe check
5569 *
5570 * Return 0 on success.
5571 */
5572void
5573megasas_setup_jbod_map(struct megasas_instance *instance)
5574{
5575 int i;
5576 struct fusion_context *fusion = instance->ctrl_context;
5577 u32 pd_seq_map_sz;
5578
5579 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5580 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5581
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305582 instance->use_seqnum_jbod_fp =
5583 instance->support_seqnum_jbod_fp;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305584 if (reset_devices || !fusion ||
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305585 !instance->support_seqnum_jbod_fp) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305586 dev_info(&instance->pdev->dev,
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305587 "JBOD sequence map is disabled %s %d\n",
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305588 __func__, __LINE__);
5589 instance->use_seqnum_jbod_fp = false;
5590 return;
5591 }
5592
5593 if (fusion->pd_seq_sync[0])
5594 goto skip_alloc;
5595
5596 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5597 fusion->pd_seq_sync[i] = dma_alloc_coherent
5598 (&instance->pdev->dev, pd_seq_map_sz,
5599 &fusion->pd_seq_phys[i], GFP_KERNEL);
5600 if (!fusion->pd_seq_sync[i]) {
5601 dev_err(&instance->pdev->dev,
5602 "Failed to allocate memory from %s %d\n",
5603 __func__, __LINE__);
5604 if (i == 1) {
5605 dma_free_coherent(&instance->pdev->dev,
5606 pd_seq_map_sz, fusion->pd_seq_sync[0],
5607 fusion->pd_seq_phys[0]);
5608 fusion->pd_seq_sync[0] = NULL;
5609 }
5610 instance->use_seqnum_jbod_fp = false;
5611 return;
5612 }
5613 }
5614
5615skip_alloc:
5616 if (!megasas_sync_pd_seq_num(instance, false) &&
5617 !megasas_sync_pd_seq_num(instance, true))
5618 instance->use_seqnum_jbod_fp = true;
5619 else
5620 instance->use_seqnum_jbod_fp = false;
5621}
5622
Ming Leiadbe5522018-03-13 17:42:40 +08005623static void megasas_setup_reply_map(struct megasas_instance *instance)
5624{
5625 const struct cpumask *mask;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305626 unsigned int queue, cpu, low_latency_index_start;
Ming Leiadbe5522018-03-13 17:42:40 +08005627
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305628 low_latency_index_start = instance->low_latency_index_start;
5629
5630 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
Ming Leiadbe5522018-03-13 17:42:40 +08005631 mask = pci_irq_get_affinity(instance->pdev, queue);
5632 if (!mask)
5633 goto fallback;
5634
5635 for_each_cpu(cpu, mask)
5636 instance->reply_map[cpu] = queue;
5637 }
5638 return;
5639
5640fallback:
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305641 queue = low_latency_index_start;
5642 for_each_possible_cpu(cpu) {
5643 instance->reply_map[cpu] = queue;
5644 if (queue == (instance->msix_vectors - 1))
5645 queue = low_latency_index_start;
5646 else
5647 queue++;
5648 }
Ming Leiadbe5522018-03-13 17:42:40 +08005649}
5650
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305651/**
Shivasharan Sdaa06812019-01-29 01:38:12 -08005652 * megasas_get_device_list - Get the PD and LD device list from FW.
5653 * @instance: Adapter soft state
5654 * @return: Success or failure
5655 *
5656 * Issue DCMDs to Firmware to get the PD and LD list.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005657 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5658 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan Sdaa06812019-01-29 01:38:12 -08005659 */
5660static
5661int megasas_get_device_list(struct megasas_instance *instance)
5662{
5663 memset(instance->pd_list, 0,
5664 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5665 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5666
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005667 if (instance->enable_fw_dev_list) {
5668 if (megasas_host_device_list_query(instance, true))
5669 return FAILED;
5670 } else {
5671 if (megasas_get_pd_list(instance) < 0) {
5672 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5673 return FAILED;
5674 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005675
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005676 if (megasas_ld_list_query(instance,
5677 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5678 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5679 return FAILED;
5680 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005681 }
5682
5683 return SUCCESS;
5684}
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305685
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305686/**
5687 * megasas_set_high_iops_queue_affinity_hint - Set affinity hint for high IOPS queues
5688 * @instance: Adapter soft state
5689 * return: void
5690 */
5691static inline void
5692megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5693{
5694 int i;
5695 int local_numa_node;
5696
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305697 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305698 local_numa_node = dev_to_node(&instance->pdev->dev);
5699
5700 for (i = 0; i < instance->low_latency_index_start; i++)
5701 irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5702 cpumask_of_node(local_numa_node));
5703 }
5704}
5705
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305706static int
5707__megasas_alloc_irq_vectors(struct megasas_instance *instance)
5708{
5709 int i, irq_flags;
5710 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5711 struct irq_affinity *descp = &desc;
5712
5713 irq_flags = PCI_IRQ_MSIX;
5714
5715 if (instance->smp_affinity_enable)
5716 irq_flags |= PCI_IRQ_AFFINITY;
5717 else
5718 descp = NULL;
5719
5720 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5721 instance->low_latency_index_start,
5722 instance->msix_vectors, irq_flags, descp);
5723
5724 return i;
5725}
5726
5727/**
5728 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5729 * @instance: Adapter soft state
5730 * return: void
5731 */
5732static void
5733megasas_alloc_irq_vectors(struct megasas_instance *instance)
5734{
5735 int i;
5736 unsigned int num_msix_req;
5737
5738 i = __megasas_alloc_irq_vectors(instance);
5739
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305740 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
5741 (i != instance->msix_vectors)) {
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305742 if (instance->msix_vectors)
5743 pci_free_irq_vectors(instance->pdev);
5744 /* Disable Balanced IOPS mode and try realloc vectors */
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305745 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305746 instance->low_latency_index_start = 1;
5747 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5748
5749 instance->msix_vectors = min(num_msix_req,
5750 instance->msix_vectors);
5751
5752 i = __megasas_alloc_irq_vectors(instance);
5753
5754 }
5755
5756 dev_info(&instance->pdev->dev,
5757 "requested/available msix %d/%d\n", instance->msix_vectors, i);
5758
5759 if (i > 0)
5760 instance->msix_vectors = i;
5761 else
5762 instance->msix_vectors = 0;
5763
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305764 if (instance->smp_affinity_enable)
5765 megasas_set_high_iops_queue_affinity_hint(instance);
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305766}
5767
Shivasharan Sdaa06812019-01-29 01:38:12 -08005768/**
adam radfordcd50ba82010-12-21 10:23:23 -08005769 * megasas_init_fw - Initializes the FW
5770 * @instance: Adapter soft state
5771 *
5772 * This is the main function for initializing firmware
5773 */
5774
5775static int megasas_init_fw(struct megasas_instance *instance)
5776{
5777 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005778 u32 max_sectors_2, tmp_sectors, msix_enable;
Shivasharan S81b76452018-10-16 23:37:51 -07005779 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005780 resource_size_t base_addr;
Shivasharan S9a598712019-05-07 10:05:42 -07005781 void *base_addr_phys;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305782 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005783 unsigned long bar_list;
Shivasharan Sce884182019-05-07 10:05:48 -07005784 int i, j, loop;
adam radford229fe472014-03-10 02:51:56 -07005785 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305786 struct fusion_context *fusion;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305787 bool intr_coalescing;
5788 unsigned int num_msix_req;
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305789 u16 lnksta, speed;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305790
5791 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005792
5793 /* Find first memory bar */
5794 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005795 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005796 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005797 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005798 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005799 return -EBUSY;
5800 }
5801
Ben Collins11f8a7b2013-09-13 12:46:44 -04005802 base_addr = pci_resource_start(instance->pdev, instance->bar);
5803 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005804
5805 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005806 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005807 goto fail_ioremap;
5808 }
5809
Shivasharan S9a598712019-05-07 10:05:42 -07005810 base_addr_phys = &base_addr;
5811 dev_printk(KERN_DEBUG, &instance->pdev->dev,
5812 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
5813 instance->bar, base_addr_phys, instance->reg_set);
5814
Shivasharan Se7d36b82017-10-19 02:48:50 -07005815 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005816 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005817 else {
5818 switch (instance->pdev->device) {
5819 case PCI_DEVICE_ID_LSI_SAS1078R:
5820 case PCI_DEVICE_ID_LSI_SAS1078DE:
5821 instance->instancet = &megasas_instance_template_ppc;
5822 break;
5823 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5824 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5825 instance->instancet = &megasas_instance_template_gen2;
5826 break;
5827 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5828 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5829 instance->instancet = &megasas_instance_template_skinny;
5830 break;
5831 case PCI_DEVICE_ID_LSI_SAS1064R:
5832 case PCI_DEVICE_ID_DELL_PERC5:
5833 default:
5834 instance->instancet = &megasas_instance_template_xscale;
5835 instance->pd_list_not_supported = 1;
5836 break;
5837 }
adam radfordcd50ba82010-12-21 10:23:23 -08005838 }
5839
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305840 if (megasas_transition_to_ready(instance, 0)) {
Shivasharan S78409d42019-05-07 10:05:34 -07005841 dev_info(&instance->pdev->dev,
5842 "Failed to transition controller to ready from %s!\n",
5843 __func__);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005844 if (instance->adapter_type != MFI_SERIES) {
Shivasharan Sde93b402018-10-16 23:37:42 -07005845 status_reg = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08005846 instance);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005847 if (status_reg & MFI_RESET_ADAPTER) {
Shivasharan S78409d42019-05-07 10:05:34 -07005848 if (megasas_adp_reset_wait_for_ready
5849 (instance, true, 0) == FAILED)
Shivasharan Sf10fb852019-05-07 10:05:32 -07005850 goto fail_ready_state;
5851 } else {
5852 goto fail_ready_state;
5853 }
5854 } else {
Shivasharan Sde93b402018-10-16 23:37:42 -07005855 atomic_set(&instance->fw_reset_no_pci_access, 1);
5856 instance->instancet->adp_reset
5857 (instance, instance->reg_set);
5858 atomic_set(&instance->fw_reset_no_pci_access, 0);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305859
Shivasharan Sde93b402018-10-16 23:37:42 -07005860 /*waiting for about 30 second before retry*/
5861 ssleep(30);
5862
5863 if (megasas_transition_to_ready(instance, 0))
5864 goto fail_ready_state;
Shivasharan Sde93b402018-10-16 23:37:42 -07005865 }
Shivasharan S78409d42019-05-07 10:05:34 -07005866
5867 dev_info(&instance->pdev->dev,
5868 "FW restarted successfully from %s!\n",
5869 __func__);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305870 }
adam radfordcd50ba82010-12-21 10:23:23 -08005871
Shivasharan Se5d65b42017-10-19 02:48:59 -07005872 megasas_init_ctrl_params(instance);
5873
Shivasharan S107a60d2017-10-19 02:49:05 -07005874 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005875 goto fail_ready_state;
5876
5877 if (megasas_alloc_ctrl_mem(instance))
5878 goto fail_alloc_dma_buf;
5879
5880 if (megasas_alloc_ctrl_dma_buffers(instance))
5881 goto fail_alloc_dma_buf;
5882
5883 fusion = instance->ctrl_context;
5884
Shivasharan S630d42b2018-12-17 00:47:37 -08005885 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005886 scratch_pad_2 =
Shivasharan S272652f2018-12-17 00:47:40 -08005887 megasas_readl(instance,
5888 &instance->reg_set->outbound_scratch_pad_2);
Shivasharan S81b76452018-10-16 23:37:51 -07005889 instance->max_raid_mapsize = ((scratch_pad_2 >>
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005890 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5891 MR_MAX_RAID_MAP_SIZE_MASK);
5892 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305893
Chandrakanth Patil7fc55702019-06-25 16:34:29 +05305894 switch (instance->adapter_type) {
5895 case VENTURA_SERIES:
Chandrakanth Patil49f2bf12019-06-25 16:34:28 +05305896 fusion->pcie_bw_limitation = true;
Chandrakanth Patil7fc55702019-06-25 16:34:29 +05305897 break;
5898 case AERO_SERIES:
5899 fusion->r56_div_offload = true;
5900 break;
5901 default:
5902 break;
5903 }
Chandrakanth Patil49f2bf12019-06-25 16:34:28 +05305904
adam radford3f1abce2011-05-11 18:33:47 -07005905 /* Check if MSI-X is supported while in ready state */
Shivasharan Sde516372018-12-17 00:47:39 -08005906 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
adam radford3f1abce2011-05-11 18:33:47 -07005907 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005908 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005909
Shivasharan S272652f2018-12-17 00:47:40 -08005910 scratch_pad_1 = megasas_readl
5911 (instance, &instance->reg_set->outbound_scratch_pad_1);
adam radfordc8e858f2011-10-08 18:15:13 -07005912 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305913 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005914 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5915 /* Thunderbolt Series*/
Shivasharan S81b76452018-10-16 23:37:51 -07005916 instance->msix_vectors = (scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305917 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005918 } else {
Shivasharan S81b76452018-10-16 23:37:51 -07005919 instance->msix_vectors = ((scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305920 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5921 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005922
5923 /*
5924 * For Invader series, > 8 MSI-x vectors
5925 * supported by FW/HW implies combined
5926 * reply queue mode is enabled.
5927 * For Ventura series, > 16 MSI-x vectors
5928 * supported by FW/HW implies combined
5929 * reply queue mode is enabled.
5930 */
5931 switch (instance->adapter_type) {
5932 case INVADER_SERIES:
5933 if (instance->msix_vectors > 8)
5934 instance->msix_combined = true;
5935 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08005936 case AERO_SERIES:
Shivasharan Se29c3222018-10-16 23:37:46 -07005937 case VENTURA_SERIES:
5938 if (instance->msix_vectors > 16)
5939 instance->msix_combined = true;
5940 break;
5941 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005942
Sumit Saxena179ac142016-01-28 21:04:28 +05305943 if (rdpq_enable)
Shivasharan S81b76452018-10-16 23:37:51 -07005944 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
Sumit Saxena179ac142016-01-28 21:04:28 +05305945 1 : 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005946
Shivasharan S1175b882019-06-28 02:50:39 -07005947 if (instance->adapter_type >= INVADER_SERIES &&
5948 !instance->msix_combined) {
Shivasharan S1d15d902019-05-07 10:05:36 -07005949 instance->msix_load_balance = true;
5950 instance->smp_affinity_enable = false;
5951 }
5952
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305953 /* Save 1-15 reply post index address to local memory
5954 * Index 0 is already saved from reg offset
5955 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5956 */
5957 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5958 instance->reply_post_host_index_addr[loop] =
5959 (u32 __iomem *)
5960 ((u8 __iomem *)instance->reg_set +
5961 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5962 + (loop * 0x10));
5963 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305964 }
Shivasharan Sce884182019-05-07 10:05:48 -07005965
5966 dev_info(&instance->pdev->dev,
5967 "firmware supports msix\t: (%d)",
5968 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305969 if (msix_vectors)
5970 instance->msix_vectors = min(msix_vectors,
5971 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305972 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005973 instance->msix_vectors = 1;
Shivasharan S1d15d902019-05-07 10:05:36 -07005974
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305975
5976 /*
5977 * For Aero (if some conditions are met), driver will configure a
5978 * few additional reply queues with interrupt coalescing enabled.
5979 * These queues with interrupt coalescing enabled are called
5980 * High IOPS queues and rest of reply queues (based on number of
5981 * logical CPUs) are termed as Low latency queues.
5982 *
5983 * Total Number of reply queues = High IOPS queues + low latency queues
5984 *
5985 * For rest of fusion adapters, 1 additional reply queue will be
5986 * reserved for management commands, rest of reply queues
5987 * (based on number of logical CPUs) will be used for IOs and
5988 * referenced as IO queues.
5989 * Total Number of reply queues = 1 + IO queues
5990 *
5991 * MFI adapters supports single MSI-x so single reply queue
5992 * will be used for IO and management commands.
5993 */
5994
5995 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
5996 true : false;
5997 if (intr_coalescing &&
5998 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
5999 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306000 instance->perf_mode = MR_BALANCED_PERF_MODE;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006001 else
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306002 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05306003
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306004
6005 if (instance->adapter_type == AERO_SERIES) {
6006 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6007 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6008
6009 /*
6010 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6011 * in latency perf mode and enable R1 PCI bandwidth algorithm
6012 */
6013 if (speed < 0x4) {
6014 instance->perf_mode = MR_LATENCY_PERF_MODE;
6015 fusion->pcie_bw_limitation = true;
6016 }
6017
6018 /*
6019 * Performance mode settings provided through module parameter-perf_mode will
6020 * take affect only for:
6021 * 1. Aero family of adapters.
6022 * 2. When user sets module parameter- perf_mode in range of 0-2.
6023 */
6024 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6025 (perf_mode <= MR_LATENCY_PERF_MODE))
6026 instance->perf_mode = perf_mode;
6027 /*
6028 * If intr coalescing is not supported by controller FW, then IOPS
6029 * and Balanced modes are not feasible.
6030 */
6031 if (!intr_coalescing)
6032 instance->perf_mode = MR_LATENCY_PERF_MODE;
6033
6034 }
6035
6036 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
Chandrakanth Patil132147d2019-06-25 16:34:31 +05306037 instance->low_latency_index_start =
6038 MR_HIGH_IOPS_QUEUE_COUNT;
6039 else
6040 instance->low_latency_index_start = 1;
6041
6042 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6043
6044 instance->msix_vectors = min(num_msix_req,
6045 instance->msix_vectors);
6046
6047 megasas_alloc_irq_vectors(instance);
6048 if (!instance->msix_vectors)
Shivasharan S1d15d902019-05-07 10:05:36 -07006049 instance->msix_load_balance = false;
adam radfordc8e858f2011-10-08 18:15:13 -07006050 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05006051 /*
6052 * MSI-X host index 0 is common for all adapter.
6053 * It is used for all MPT based Adapters.
6054 */
6055 if (instance->msix_combined) {
6056 instance->reply_post_host_index_addr[0] =
6057 (u32 *)((u8 *)instance->reg_set +
6058 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6059 } else {
6060 instance->reply_post_host_index_addr[0] =
6061 (u32 *)((u8 *)instance->reg_set +
6062 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6063 }
6064
Shivasharan S64ff64b2017-03-10 03:22:12 -08006065 if (!instance->msix_vectors) {
6066 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6067 if (i < 0)
Shivasharan S8a25fa12018-10-16 23:37:44 -07006068 goto fail_init_adapter;
Shivasharan S64ff64b2017-03-10 03:22:12 -08006069 }
adam radford3f1abce2011-05-11 18:33:47 -07006070
Ming Leiadbe5522018-03-13 17:42:40 +08006071 megasas_setup_reply_map(instance);
6072
Tomas Henzl258c3af2015-06-02 16:09:46 +05306073 dev_info(&instance->pdev->dev,
Tomas Henzl258c3af2015-06-02 16:09:46 +05306074 "current msix/online cpus\t: (%d/%d)\n",
6075 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05306076 dev_info(&instance->pdev->dev,
6077 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05306078
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05306079 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6080 (unsigned long)instance);
6081
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306082 /*
6083 * Below are default value for legacy Firmware.
6084 * non-fusion based controllers
6085 */
6086 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6087 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08006088 /* Get operational params, sge flags, send init cmd to controller */
6089 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08006090 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08006091
Shivasharan S630d42b2018-12-17 00:47:37 -08006092 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07006093 scratch_pad_3 =
Shivasharan S272652f2018-12-17 00:47:40 -08006094 megasas_readl(instance,
6095 &instance->reg_set->outbound_scratch_pad_3);
Shivasharan S81b76452018-10-16 23:37:51 -07006096 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
Shivasharan S15dd0382017-02-10 00:59:10 -08006097 MR_DEFAULT_NVME_PAGE_SHIFT)
6098 instance->nvme_page_size =
Shivasharan S81b76452018-10-16 23:37:51 -07006099 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
Shivasharan S15dd0382017-02-10 00:59:10 -08006100
6101 dev_info(&instance->pdev->dev,
6102 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6103 }
6104
Tomas Henzl18103ef2016-11-01 17:32:02 +01006105 if (instance->msix_vectors ?
6106 megasas_setup_irqs_msix(instance, 1) :
6107 megasas_setup_irqs_ioapic(instance))
6108 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05306109
Shivasharan S62a04f82019-05-07 10:05:35 -07006110 if (instance->adapter_type != MFI_SERIES)
6111 megasas_setup_irq_poll(instance);
6112
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306113 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08006114
Andy Lutomirski13f30772016-05-03 10:24:31 -07006115 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08006116
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306117 megasas_setup_jbod_map(instance);
6118
Shivasharan Sdaa06812019-01-29 01:38:12 -08006119 if (megasas_get_device_list(instance) != SUCCESS) {
6120 dev_err(&instance->pdev->dev,
6121 "%s: megasas_get_device_list failed\n",
6122 __func__);
Shivasharan S72bff2d2017-02-10 00:59:33 -08006123 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006124 }
Yang, Bo81e403c2009-10-06 14:27:54 -06006125
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006126 /* stream detection initialization */
Shivasharan S630d42b2018-12-17 00:47:37 -08006127 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006128 fusion->stream_detect_by_ld =
Kees Cook6396bb22018-06-12 14:03:40 -07006129 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6130 sizeof(struct LD_STREAM_DETECT *),
6131 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006132 if (!fusion->stream_detect_by_ld) {
6133 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08006134 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006135 goto fail_get_ld_pd_list;
6136 }
6137 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6138 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08006139 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006140 GFP_KERNEL);
6141 if (!fusion->stream_detect_by_ld[i]) {
6142 dev_err(&instance->pdev->dev,
6143 "unable to allocate stream detect by LD\n ");
6144 for (j = 0; j < i; ++j)
6145 kfree(fusion->stream_detect_by_ld[j]);
6146 kfree(fusion->stream_detect_by_ld);
6147 fusion->stream_detect_by_ld = NULL;
6148 goto fail_get_ld_pd_list;
6149 }
6150 fusion->stream_detect_by_ld[i]->mru_bit_map
6151 = MR_STREAM_BITMAP;
6152 }
6153 }
6154
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006155 /*
6156 * Compute the max allowed sectors per IO: The controller info has two
6157 * limits on max sectors. Driver should use the minimum of these two.
6158 *
6159 * 1 << stripe_sz_ops.min = max sectors per strip
6160 *
6161 * Note that older firmwares ( < FW ver 30) didn't report information
6162 * to calculate max_sectors_1. So the number ended up as zero always.
6163 */
bo yang14faea92007-11-09 04:14:00 -05006164 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07006165 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006166
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306167 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6168 le16_to_cpu(ctrl_info->max_strips_per_io);
6169 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006170
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006171 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306172
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05306173 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6174 instance->passive = ctrl_info->cluster.passive;
6175 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306176 instance->UnevenSpanSupport =
6177 ctrl_info->adapterOperations2.supportUnevenSpans;
6178 if (instance->UnevenSpanSupport) {
6179 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08006180 if (MR_ValidateMapInfo(instance, instance->map_id))
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306181 fusion->fast_path_io = 1;
6182 else
6183 fusion->fast_path_io = 0;
6184
6185 }
6186 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306187 instance->requestorId = ctrl_info->iov.requestorId;
6188 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6189 if (!ctrl_info->adapterOperations2.activePassive)
6190 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306191
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306192 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6193 instance->PlasmaFW111 ? "1.11" : "new");
6194
6195 if (instance->PlasmaFW111) {
6196 iovPtr = (struct IOV_111 *)
6197 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6198 instance->requestorId = iovPtr->requestorId;
6199 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306200 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306201 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6202 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306203 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306204
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306205 instance->crash_dump_fw_support =
6206 ctrl_info->adapterOperations3.supportCrashDump;
6207 instance->crash_dump_drv_support =
6208 (instance->crash_dump_fw_support &&
6209 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306210 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306211 megasas_set_crash_dump_params(instance,
6212 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07006213
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306214 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306215 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006216 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306217 CRASH_DMA_BUF_SIZE,
6218 instance->crash_dump_buf,
6219 instance->crash_dump_h);
6220 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05006221 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05306222
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006223 if (instance->snapdump_wait_time) {
6224 megasas_get_snapdump_properties(instance);
6225 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6226 instance->snapdump_wait_time);
6227 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306228
6229 dev_info(&instance->pdev->dev,
6230 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6231 le16_to_cpu(ctrl_info->pci.vendor_id),
6232 le16_to_cpu(ctrl_info->pci.device_id),
6233 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6234 le16_to_cpu(ctrl_info->pci.sub_device_id));
6235 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6236 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306237 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6238 instance->crash_dump_drv_support ? "yes" : "no");
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05306239 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6240 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306241
bo yang14faea92007-11-09 04:14:00 -05006242 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05306243 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05006244 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6245 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006246
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306247 /* Check for valid throttlequeuedepth module parameter */
6248 if (throttlequeuedepth &&
6249 throttlequeuedepth <= instance->max_scsi_cmds)
6250 instance->throttlequeuedepth = throttlequeuedepth;
6251 else
6252 instance->throttlequeuedepth =
6253 MEGASAS_THROTTLE_QUEUE_DEPTH;
6254
Shivasharan Se636a7a2017-08-23 04:46:56 -07006255 if ((resetwaittime < 1) ||
6256 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05306257 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6258
6259 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6260 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05006261
adam radford229fe472014-03-10 02:51:56 -07006262 /* Launch SR-IOV heartbeat timer */
6263 if (instance->requestorId) {
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006264 if (!megasas_sriov_start_heartbeat(instance, 1)) {
Kees Cookc251a7b2017-10-22 15:30:04 -07006265 megasas_start_timer(instance);
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006266 } else {
adam radford229fe472014-03-10 02:51:56 -07006267 instance->skip_heartbeat_timer_del = 1;
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006268 goto fail_get_ld_pd_list;
6269 }
adam radford229fe472014-03-10 02:51:56 -07006270 }
6271
Shivasharan S3f6194a2018-10-16 23:37:39 -07006272 /*
6273 * Create and start watchdog thread which will monitor
6274 * controller state every 1 sec and trigger OCR when
6275 * it enters fault state
6276 */
6277 if (instance->adapter_type != MFI_SERIES)
6278 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6279 goto fail_start_watchdog;
6280
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006281 return 0;
6282
Shivasharan S3f6194a2018-10-16 23:37:39 -07006283fail_start_watchdog:
6284 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6285 del_timer_sync(&instance->sriov_heartbeat_timer);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006286fail_get_ld_pd_list:
6287 instance->instancet->disable_intr(instance);
Hannes Reineckefad119b2016-12-02 12:52:23 +01006288 megasas_destroy_irqs(instance);
Shivasharan S8a25fa12018-10-16 23:37:44 -07006289fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306290 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006291 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306292 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07006293fail_alloc_dma_buf:
6294 megasas_free_ctrl_dma_buffers(instance);
6295 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08006296fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006297 iounmap(instance->reg_set);
6298
Shivasharan S72bff2d2017-02-10 00:59:33 -08006299fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07006300 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006301
Shivasharan S72bff2d2017-02-10 00:59:33 -08006302 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6303 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006304 return -EINVAL;
6305}
6306
6307/**
6308 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01006309 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006310 */
6311static void megasas_release_mfi(struct megasas_instance *instance)
6312{
adam radford9c915a82010-12-21 13:34:31 -08006313 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006314
adam radford9c915a82010-12-21 13:34:31 -08006315 if (instance->reply_queue)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006316 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006317 instance->reply_queue, instance->reply_queue_h);
6318
6319 megasas_free_cmds(instance);
6320
6321 iounmap(instance->reg_set);
6322
Yinghai Lue7f85162016-08-05 23:37:34 -07006323 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006324}
6325
6326/**
6327 * megasas_get_seq_num - Gets latest event sequence numbers
6328 * @instance: Adapter soft state
6329 * @eli: FW event log sequence numbers information
6330 *
6331 * FW maintains a log of all events in a non-volatile area. Upper layers would
6332 * usually find out the latest sequence number of the events, the seq number at
6333 * the boot etc. They would "read" all the events below the latest seq number
6334 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6335 * number), they would subsribe to AEN (asynchronous event notification) and
6336 * wait for the events to happen.
6337 */
6338static int
6339megasas_get_seq_num(struct megasas_instance *instance,
6340 struct megasas_evt_log_info *eli)
6341{
6342 struct megasas_cmd *cmd;
6343 struct megasas_dcmd_frame *dcmd;
6344 struct megasas_evt_log_info *el_info;
6345 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08006346 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006347
6348 cmd = megasas_get_cmd(instance);
6349
6350 if (!cmd) {
6351 return -ENOMEM;
6352 }
6353
6354 dcmd = &cmd->frame->dcmd;
Luis Chamberlain750afb02019-01-04 09:23:09 +01006355 el_info = dma_alloc_coherent(&instance->pdev->dev,
6356 sizeof(struct megasas_evt_log_info),
6357 &el_info_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006358 if (!el_info) {
6359 megasas_return_cmd(instance, cmd);
6360 return -ENOMEM;
6361 }
6362
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006363 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6364
6365 dcmd->cmd = MFI_CMD_DCMD;
6366 dcmd->cmd_status = 0x0;
6367 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006368 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006369 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006370 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306371 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6372 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07006373
6374 megasas_set_dma_settings(instance, dcmd, el_info_h,
6375 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006376
Shivasharan Sb051cc62018-01-05 05:27:38 -08006377 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6378 if (ret != DCMD_SUCCESS) {
6379 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6380 __func__, __LINE__);
6381 goto dcmd_failed;
6382 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006383
Shivasharan Sb051cc62018-01-05 05:27:38 -08006384 /*
6385 * Copy the data back into callers buffer
6386 */
6387 eli->newest_seq_num = el_info->newest_seq_num;
6388 eli->oldest_seq_num = el_info->oldest_seq_num;
6389 eli->clear_seq_num = el_info->clear_seq_num;
6390 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6391 eli->boot_seq_num = el_info->boot_seq_num;
6392
6393dcmd_failed:
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006394 dma_free_coherent(&instance->pdev->dev,
6395 sizeof(struct megasas_evt_log_info),
6396 el_info, el_info_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006397
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306398 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006399
Shivasharan Sb051cc62018-01-05 05:27:38 -08006400 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006401}
6402
6403/**
6404 * megasas_register_aen - Registers for asynchronous event notification
6405 * @instance: Adapter soft state
6406 * @seq_num: The starting sequence number
6407 * @class_locale: Class of the event
6408 *
6409 * This function subscribes for AEN for events beyond the @seq_num. It requests
6410 * to be notified if and only if the event is of type @class_locale
6411 */
6412static int
6413megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6414 u32 class_locale_word)
6415{
6416 int ret_val;
6417 struct megasas_cmd *cmd;
6418 struct megasas_dcmd_frame *dcmd;
6419 union megasas_evt_class_locale curr_aen;
6420 union megasas_evt_class_locale prev_aen;
6421
6422 /*
6423 * If there an AEN pending already (aen_cmd), check if the
6424 * class_locale of that pending AEN is inclusive of the new
6425 * AEN request we currently have. If it is, then we don't have
6426 * to do anything. In other words, whichever events the current
6427 * AEN request is subscribing to, have already been subscribed
6428 * to.
6429 *
6430 * If the old_cmd is _not_ inclusive, then we have to abort
6431 * that command, form a class_locale that is superset of both
6432 * old and current and re-issue to the FW
6433 */
6434
6435 curr_aen.word = class_locale_word;
6436
6437 if (instance->aen_cmd) {
6438
Christoph Hellwiga9555532015-04-23 16:34:24 +05306439 prev_aen.word =
6440 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006441
Shivasharan S91b3d9f2017-08-23 04:47:01 -07006442 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6443 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6444 dev_info(&instance->pdev->dev,
6445 "%s %d out of range class %d send by application\n",
6446 __func__, __LINE__, curr_aen.members.class);
6447 return 0;
6448 }
6449
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006450 /*
6451 * A class whose enum value is smaller is inclusive of all
6452 * higher values. If a PROGRESS (= -1) was previously
6453 * registered, then a new registration requests for higher
6454 * classes need not be sent to FW. They are automatically
6455 * included.
6456 *
6457 * Locale numbers don't have such hierarchy. They are bitmap
6458 * values
6459 */
6460 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306461 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006462 curr_aen.members.locale)) {
6463 /*
6464 * Previously issued event registration includes
6465 * current request. Nothing to do.
6466 */
6467 return 0;
6468 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306469 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006470
6471 if (prev_aen.members.class < curr_aen.members.class)
6472 curr_aen.members.class = prev_aen.members.class;
6473
6474 instance->aen_cmd->abort_aen = 1;
6475 ret_val = megasas_issue_blocked_abort_cmd(instance,
6476 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306477 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006478
6479 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006480 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006481 "previous AEN command\n");
6482 return ret_val;
6483 }
6484 }
6485 }
6486
6487 cmd = megasas_get_cmd(instance);
6488
6489 if (!cmd)
6490 return -ENOMEM;
6491
6492 dcmd = &cmd->frame->dcmd;
6493
6494 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6495
6496 /*
6497 * Prepare DCMD for aen registration
6498 */
6499 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6500
6501 dcmd->cmd = MFI_CMD_DCMD;
6502 dcmd->cmd_status = 0x0;
6503 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006504 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006505 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006506 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306507 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6508 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6509 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04006510 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306511 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07006512
6513 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6514 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006515
Yang, Bof4c9a132009-10-06 14:43:28 -06006516 if (instance->aen_cmd != NULL) {
6517 megasas_return_cmd(instance, cmd);
6518 return 0;
6519 }
6520
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006521 /*
6522 * Store reference to the cmd used to register for AEN. When an
6523 * application wants us to register for AEN, we have to abort this
6524 * cmd and re-register with a new EVENT LOCALE supplied by that app
6525 */
6526 instance->aen_cmd = cmd;
6527
6528 /*
6529 * Issue the aen registration frame
6530 */
adam radford9c915a82010-12-21 13:34:31 -08006531 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006532
6533 return 0;
6534}
6535
Shivasharan S96188a82017-02-10 00:59:11 -08006536/* megasas_get_target_prop - Send DCMD with below details to firmware.
6537 *
6538 * This DCMD will fetch few properties of LD/system PD defined
6539 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6540 *
6541 * DCMD send by drivers whenever new target is added to the OS.
6542 *
6543 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6544 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6545 * 0 = system PD, 1 = LD.
6546 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6547 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6548 *
6549 * @instance: Adapter soft state
6550 * @sdev: OS provided scsi device
6551 *
6552 * Returns 0 on success non-zero on failure.
6553 */
Shivasharan Se9495e22018-06-04 03:45:12 -07006554int
Shivasharan S96188a82017-02-10 00:59:11 -08006555megasas_get_target_prop(struct megasas_instance *instance,
6556 struct scsi_device *sdev)
6557{
6558 int ret;
6559 struct megasas_cmd *cmd;
6560 struct megasas_dcmd_frame *dcmd;
Shivasharan Sc8f96df2019-06-28 18:02:12 -07006561 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6562 sdev->id;
Shivasharan S96188a82017-02-10 00:59:11 -08006563
6564 cmd = megasas_get_cmd(instance);
6565
6566 if (!cmd) {
6567 dev_err(&instance->pdev->dev,
6568 "Failed to get cmd %s\n", __func__);
6569 return -ENOMEM;
6570 }
6571
6572 dcmd = &cmd->frame->dcmd;
6573
6574 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6575 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6576 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6577
6578 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6579 dcmd->cmd = MFI_CMD_DCMD;
6580 dcmd->cmd_status = 0xFF;
6581 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006582 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08006583 dcmd->timeout = 0;
6584 dcmd->pad_0 = 0;
6585 dcmd->data_xfer_len =
6586 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6587 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08006588
Shivasharan S107a60d2017-10-19 02:49:05 -07006589 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6590 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08006591
Shivasharan Se7d36b82017-10-19 02:48:50 -07006592 if ((instance->adapter_type != MFI_SERIES) &&
6593 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08006594 ret = megasas_issue_blocked_cmd(instance,
6595 cmd, MFI_IO_TIMEOUT_SECS);
6596 else
6597 ret = megasas_issue_polled(instance, cmd);
6598
6599 switch (ret) {
6600 case DCMD_TIMEOUT:
6601 switch (dcmd_timeout_ocr_possible(instance)) {
6602 case INITIATE_OCR:
6603 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05306604 mutex_unlock(&instance->reset_mutex);
Shivasharan S96188a82017-02-10 00:59:11 -08006605 megasas_reset_fusion(instance->host,
6606 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05306607 mutex_lock(&instance->reset_mutex);
Shivasharan S96188a82017-02-10 00:59:11 -08006608 break;
6609 case KILL_ADAPTER:
6610 megaraid_sas_kill_hba(instance);
6611 break;
6612 case IGNORE_TIMEOUT:
6613 dev_info(&instance->pdev->dev,
6614 "Ignore DCMD timeout: %s %d\n",
6615 __func__, __LINE__);
6616 break;
6617 }
6618 break;
6619
6620 default:
6621 megasas_return_cmd(instance, cmd);
6622 }
6623 if (ret != DCMD_SUCCESS)
6624 dev_err(&instance->pdev->dev,
6625 "return from %s %d return value %d\n",
6626 __func__, __LINE__, ret);
6627
6628 return ret;
6629}
6630
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006631/**
6632 * megasas_start_aen - Subscribes to AEN during driver load time
6633 * @instance: Adapter soft state
6634 */
6635static int megasas_start_aen(struct megasas_instance *instance)
6636{
6637 struct megasas_evt_log_info eli;
6638 union megasas_evt_class_locale class_locale;
6639
6640 /*
6641 * Get the latest sequence number from FW
6642 */
6643 memset(&eli, 0, sizeof(eli));
6644
6645 if (megasas_get_seq_num(instance, &eli))
6646 return -1;
6647
6648 /*
6649 * Register AEN with FW for latest sequence number plus 1
6650 */
6651 class_locale.members.reserved = 0;
6652 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6653 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6654
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306655 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05306656 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306657 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006658}
6659
6660/**
6661 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6662 * @instance: Adapter soft state
6663 */
6664static int megasas_io_attach(struct megasas_instance *instance)
6665{
6666 struct Scsi_Host *host = instance->host;
6667
6668 /*
6669 * Export parameters required by SCSI mid-layer
6670 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006671 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306672 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006673 host->this_id = instance->init_id;
6674 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08006675
6676 if (instance->fw_support_ieee)
6677 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6678
Yang, Bo1fd10682010-10-12 07:18:50 -06006679 /*
6680 * Check if the module parameter value for max_sectors can be used
6681 */
6682 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6683 instance->max_sectors_per_req = max_sectors;
6684 else {
6685 if (max_sectors) {
6686 if (((instance->pdev->device ==
6687 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6688 (instance->pdev->device ==
6689 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6690 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6691 instance->max_sectors_per_req = max_sectors;
6692 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006693 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06006694 "and <= %d (or < 1MB for GEN2 controller)\n",
6695 instance->max_sectors_per_req);
6696 }
6697 }
6698 }
6699
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006700 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08006701 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006702 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6703 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6704 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08006705 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006706
6707 /*
6708 * Notify the mid-layer about the new controller
6709 */
6710 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306711 dev_err(&instance->pdev->dev,
6712 "Failed to add host from %s %d\n",
6713 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006714 return -ENODEV;
6715 }
6716
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006717 return 0;
6718}
6719
Shivasharan S107a60d2017-10-19 02:49:05 -07006720/**
6721 * megasas_set_dma_mask - Set DMA mask for supported controllers
6722 *
6723 * @instance: Adapter soft state
6724 * Description:
6725 *
Shivasharan S894169d2018-12-18 05:59:54 -08006726 * For Ventura, driver/FW will operate in 63bit DMA addresses.
Shivasharan S107a60d2017-10-19 02:49:05 -07006727 *
6728 * For invader-
6729 * By default, driver/FW will operate in 32bit DMA addresses
6730 * for consistent DMA mapping but if 32 bit consistent
Shivasharan S894169d2018-12-18 05:59:54 -08006731 * DMA mask fails, driver will try with 63 bit consistent
6732 * mask provided FW is true 63bit DMA capable
Shivasharan S107a60d2017-10-19 02:49:05 -07006733 *
6734 * For older controllers(Thunderbolt and MFI based adapters)-
6735 * driver/FW will operate in 32 bit consistent DMA addresses.
6736 */
bo yang31ea7082007-11-07 12:09:50 -05006737static int
Shivasharan S107a60d2017-10-19 02:49:05 -07006738megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05006739{
Shivasharan S107a60d2017-10-19 02:49:05 -07006740 u64 consistent_mask;
6741 struct pci_dev *pdev;
Shivasharan S81b76452018-10-16 23:37:51 -07006742 u32 scratch_pad_1;
bo yang31ea7082007-11-07 12:09:50 -05006743
Shivasharan S107a60d2017-10-19 02:49:05 -07006744 pdev = instance->pdev;
Shivasharan S630d42b2018-12-17 00:47:37 -08006745 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
Shivasharan S894169d2018-12-18 05:59:54 -08006746 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
Shivasharan S107a60d2017-10-19 02:49:05 -07006747
6748 if (IS_DMA64) {
Shivasharan S894169d2018-12-18 05:59:54 -08006749 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006750 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6751 goto fail_set_dma_mask;
6752
Shivasharan S894169d2018-12-18 05:59:54 -08006753 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006754 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6755 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6756 /*
6757 * If 32 bit DMA mask fails, then try for 64 bit mask
6758 * for FW capable of handling 64 bit DMA.
6759 */
Shivasharan S272652f2018-12-17 00:47:40 -08006760 scratch_pad_1 = megasas_readl
6761 (instance, &instance->reg_set->outbound_scratch_pad_1);
Shivasharan S107a60d2017-10-19 02:49:05 -07006762
Shivasharan S81b76452018-10-16 23:37:51 -07006763 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
Shivasharan S107a60d2017-10-19 02:49:05 -07006764 goto fail_set_dma_mask;
6765 else if (dma_set_mask_and_coherent(&pdev->dev,
Shivasharan S894169d2018-12-18 05:59:54 -08006766 DMA_BIT_MASK(63)))
bo yang31ea7082007-11-07 12:09:50 -05006767 goto fail_set_dma_mask;
6768 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006769 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6770 goto fail_set_dma_mask;
6771
6772 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6773 instance->consistent_mask_64bit = false;
6774 else
6775 instance->consistent_mask_64bit = true;
6776
6777 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
Tomas Henzld1f38d92019-01-02 16:07:25 +01006778 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
Shivasharan S894169d2018-12-18 05:59:54 -08006779 (instance->consistent_mask_64bit ? "63" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306780
bo yang31ea7082007-11-07 12:09:50 -05006781 return 0;
6782
6783fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006784 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6785 return -1;
6786
bo yang31ea7082007-11-07 12:09:50 -05006787}
6788
Shivasharan Sc3651782017-10-19 02:48:48 -07006789/*
6790 * megasas_set_adapter_type - Set adapter type.
6791 * Supported controllers can be divided in
Shivasharan S154a7cd2018-12-17 00:47:38 -08006792 * different categories-
6793 * enum MR_ADAPTER_TYPE {
6794 * MFI_SERIES = 1,
6795 * THUNDERBOLT_SERIES = 2,
6796 * INVADER_SERIES = 3,
6797 * VENTURA_SERIES = 4,
6798 * AERO_SERIES = 5,
6799 * };
Shivasharan Sc3651782017-10-19 02:48:48 -07006800 * @instance: Adapter soft state
6801 * return: void
6802 */
6803static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6804{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006805 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6806 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006807 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006808 } else {
6809 switch (instance->pdev->device) {
Shivasharan S469f72d2018-11-09 09:47:20 -08006810 case PCI_DEVICE_ID_LSI_AERO_10E1:
6811 case PCI_DEVICE_ID_LSI_AERO_10E2:
6812 case PCI_DEVICE_ID_LSI_AERO_10E5:
6813 case PCI_DEVICE_ID_LSI_AERO_10E6:
Shivasharan S154a7cd2018-12-17 00:47:38 -08006814 instance->adapter_type = AERO_SERIES;
6815 break;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006816 case PCI_DEVICE_ID_LSI_VENTURA:
6817 case PCI_DEVICE_ID_LSI_CRUSADER:
6818 case PCI_DEVICE_ID_LSI_HARPOON:
6819 case PCI_DEVICE_ID_LSI_TOMCAT:
6820 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6821 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6822 instance->adapter_type = VENTURA_SERIES;
6823 break;
6824 case PCI_DEVICE_ID_LSI_FUSION:
6825 case PCI_DEVICE_ID_LSI_PLASMA:
6826 instance->adapter_type = THUNDERBOLT_SERIES;
6827 break;
6828 case PCI_DEVICE_ID_LSI_INVADER:
6829 case PCI_DEVICE_ID_LSI_INTRUDER:
6830 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6831 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6832 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6833 case PCI_DEVICE_ID_LSI_FURY:
6834 instance->adapter_type = INVADER_SERIES;
6835 break;
6836 default: /* For all other supported controllers */
6837 instance->adapter_type = MFI_SERIES;
6838 break;
6839 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006840 }
6841}
6842
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006843static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6844{
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006845 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6846 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6847 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6848 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006849
6850 if (!instance->producer || !instance->consumer) {
6851 dev_err(&instance->pdev->dev,
6852 "Failed to allocate memory for producer, consumer\n");
6853 return -1;
6854 }
6855
6856 *instance->producer = 0;
6857 *instance->consumer = 0;
6858 return 0;
6859}
6860
6861/**
6862 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6863 * structures which are not common across MFI
6864 * adapters and fusion adapters.
6865 * For MFI based adapters, allocate producer and
6866 * consumer buffers. For fusion adapters, allocate
6867 * memory for fusion context.
6868 * @instance: Adapter soft state
6869 * return: 0 for SUCCESS
6870 */
6871static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6872{
Kees Cook6396bb22018-06-12 14:03:40 -07006873 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
Ming Leiadbe5522018-03-13 17:42:40 +08006874 GFP_KERNEL);
6875 if (!instance->reply_map)
6876 return -ENOMEM;
6877
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006878 switch (instance->adapter_type) {
6879 case MFI_SERIES:
6880 if (megasas_alloc_mfi_ctrl_mem(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006881 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006882 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08006883 case AERO_SERIES:
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006884 case VENTURA_SERIES:
6885 case THUNDERBOLT_SERIES:
6886 case INVADER_SERIES:
6887 if (megasas_alloc_fusion_context(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006888 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006889 break;
6890 }
6891
6892 return 0;
Ming Leiadbe5522018-03-13 17:42:40 +08006893 fail:
6894 kfree(instance->reply_map);
6895 instance->reply_map = NULL;
6896 return -ENOMEM;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006897}
6898
6899/*
6900 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6901 * producer, consumer buffers for MFI adapters
6902 *
6903 * @instance - Adapter soft instance
6904 *
6905 */
6906static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6907{
Ming Leiadbe5522018-03-13 17:42:40 +08006908 kfree(instance->reply_map);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006909 if (instance->adapter_type == MFI_SERIES) {
6910 if (instance->producer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006911 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006912 instance->producer,
6913 instance->producer_h);
6914 if (instance->consumer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006915 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006916 instance->consumer,
6917 instance->consumer_h);
6918 } else {
6919 megasas_free_fusion_context(instance);
6920 }
6921}
6922
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006923/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006924 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6925 * driver load time
6926 *
6927 * @instance- Adapter soft instance
6928 * @return- O for SUCCESS
6929 */
6930static inline
6931int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6932{
6933 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006934 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006935
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006936 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6937 sizeof(struct megasas_evt_detail),
6938 &instance->evt_detail_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006939
6940 if (!instance->evt_detail) {
6941 dev_err(&instance->pdev->dev,
6942 "Failed to allocate event detail buffer\n");
6943 return -ENOMEM;
6944 }
6945
Shivasharan S9b3d0282017-10-19 02:48:56 -07006946 if (fusion) {
6947 fusion->ioc_init_request =
6948 dma_alloc_coherent(&pdev->dev,
6949 sizeof(struct MPI2_IOC_INIT_REQUEST),
6950 &fusion->ioc_init_request_phys,
6951 GFP_KERNEL);
6952
6953 if (!fusion->ioc_init_request) {
6954 dev_err(&pdev->dev,
6955 "Failed to allocate PD list buffer\n");
6956 return -ENOMEM;
6957 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006958
6959 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6960 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6961 &instance->snapdump_prop_h, GFP_KERNEL);
6962
6963 if (!instance->snapdump_prop)
6964 dev_err(&pdev->dev,
6965 "Failed to allocate snapdump properties buffer\n");
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006966
6967 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6968 HOST_DEVICE_LIST_SZ,
6969 &instance->host_device_list_buf_h,
6970 GFP_KERNEL);
6971
6972 if (!instance->host_device_list_buf) {
6973 dev_err(&pdev->dev,
6974 "Failed to allocate targetid list buffer\n");
6975 return -ENOMEM;
6976 }
6977
Shivasharan S9b3d0282017-10-19 02:48:56 -07006978 }
6979
6980 instance->pd_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006981 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006982 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006983 &instance->pd_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006984
6985 if (!instance->pd_list_buf) {
6986 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6987 return -ENOMEM;
6988 }
6989
6990 instance->ctrl_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006991 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006992 sizeof(struct megasas_ctrl_info),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006993 &instance->ctrl_info_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006994
6995 if (!instance->ctrl_info_buf) {
6996 dev_err(&pdev->dev,
6997 "Failed to allocate controller info buffer\n");
6998 return -ENOMEM;
6999 }
7000
7001 instance->ld_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007002 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07007003 sizeof(struct MR_LD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007004 &instance->ld_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07007005
7006 if (!instance->ld_list_buf) {
7007 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7008 return -ENOMEM;
7009 }
7010
7011 instance->ld_targetid_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007012 dma_alloc_coherent(&pdev->dev,
7013 sizeof(struct MR_LD_TARGETID_LIST),
7014 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07007015
7016 if (!instance->ld_targetid_list_buf) {
7017 dev_err(&pdev->dev,
7018 "Failed to allocate LD targetid list buffer\n");
7019 return -ENOMEM;
7020 }
7021
Shivasharan S1b4bed22017-10-19 02:48:55 -07007022 if (!reset_devices) {
7023 instance->system_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007024 dma_alloc_coherent(&pdev->dev,
7025 sizeof(struct MR_DRV_SYSTEM_INFO),
7026 &instance->system_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007027 instance->pd_info =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007028 dma_alloc_coherent(&pdev->dev,
7029 sizeof(struct MR_PD_INFO),
7030 &instance->pd_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007031 instance->tgt_prop =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007032 dma_alloc_coherent(&pdev->dev,
7033 sizeof(struct MR_TARGET_PROPERTIES),
7034 &instance->tgt_prop_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007035 instance->crash_dump_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007036 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7037 &instance->crash_dump_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007038
7039 if (!instance->system_info_buf)
7040 dev_err(&instance->pdev->dev,
7041 "Failed to allocate system info buffer\n");
7042
7043 if (!instance->pd_info)
7044 dev_err(&instance->pdev->dev,
7045 "Failed to allocate pd_info buffer\n");
7046
7047 if (!instance->tgt_prop)
7048 dev_err(&instance->pdev->dev,
7049 "Failed to allocate tgt_prop buffer\n");
7050
7051 if (!instance->crash_dump_buf)
7052 dev_err(&instance->pdev->dev,
7053 "Failed to allocate crash dump buffer\n");
7054 }
7055
7056 return 0;
7057}
7058
7059/*
7060 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
7061 * during driver load time
7062 *
7063 * @instance- Adapter soft instance
7064 *
7065 */
7066static inline
7067void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7068{
7069 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07007070 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07007071
7072 if (instance->evt_detail)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007073 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007074 instance->evt_detail,
7075 instance->evt_detail_h);
7076
Shivasharan S9b3d0282017-10-19 02:48:56 -07007077 if (fusion && fusion->ioc_init_request)
7078 dma_free_coherent(&pdev->dev,
7079 sizeof(struct MPI2_IOC_INIT_REQUEST),
7080 fusion->ioc_init_request,
7081 fusion->ioc_init_request_phys);
7082
7083 if (instance->pd_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007084 dma_free_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07007085 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7086 instance->pd_list_buf,
7087 instance->pd_list_buf_h);
7088
7089 if (instance->ld_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007090 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007091 instance->ld_list_buf,
7092 instance->ld_list_buf_h);
7093
7094 if (instance->ld_targetid_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007095 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007096 instance->ld_targetid_list_buf,
7097 instance->ld_targetid_list_buf_h);
7098
7099 if (instance->ctrl_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007100 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007101 instance->ctrl_info_buf,
7102 instance->ctrl_info_buf_h);
7103
Shivasharan S1b4bed22017-10-19 02:48:55 -07007104 if (instance->system_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007105 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007106 instance->system_info_buf,
7107 instance->system_info_h);
7108
7109 if (instance->pd_info)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007110 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007111 instance->pd_info, instance->pd_info_h);
7112
7113 if (instance->tgt_prop)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007114 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007115 instance->tgt_prop, instance->tgt_prop_h);
7116
7117 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007118 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
Shivasharan S1b4bed22017-10-19 02:48:55 -07007119 instance->crash_dump_buf,
7120 instance->crash_dump_h);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07007121
7122 if (instance->snapdump_prop)
7123 dma_free_coherent(&pdev->dev,
7124 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7125 instance->snapdump_prop,
7126 instance->snapdump_prop_h);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08007127
7128 if (instance->host_device_list_buf)
7129 dma_free_coherent(&pdev->dev,
7130 HOST_DEVICE_LIST_SZ,
7131 instance->host_device_list_buf,
7132 instance->host_device_list_buf_h);
7133
Shivasharan S1b4bed22017-10-19 02:48:55 -07007134}
7135
Shivasharan S7535f272017-10-19 02:48:58 -07007136/*
7137 * megasas_init_ctrl_params - Initialize controller's instance
7138 * parameters before FW init
7139 * @instance - Adapter soft instance
7140 * @return - void
7141 */
7142static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7143{
7144 instance->fw_crash_state = UNAVAILABLE;
7145
7146 megasas_poll_wait_aen = 0;
7147 instance->issuepend_done = 1;
7148 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7149
7150 /*
7151 * Initialize locks and queues
7152 */
7153 INIT_LIST_HEAD(&instance->cmd_pool);
7154 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7155
7156 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S1d15d902019-05-07 10:05:36 -07007157 atomic64_set(&instance->total_io_count, 0);
Shivasharan S7535f272017-10-19 02:48:58 -07007158
7159 init_waitqueue_head(&instance->int_cmd_wait_q);
7160 init_waitqueue_head(&instance->abort_cmd_wait_q);
7161
7162 spin_lock_init(&instance->crashdump_lock);
7163 spin_lock_init(&instance->mfi_pool_lock);
7164 spin_lock_init(&instance->hba_lock);
7165 spin_lock_init(&instance->stream_lock);
7166 spin_lock_init(&instance->completion_lock);
7167
Shivasharan S7535f272017-10-19 02:48:58 -07007168 mutex_init(&instance->reset_mutex);
7169
7170 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7171 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7172 instance->flag_ieee = 1;
7173
7174 megasas_dbg_lvl = 0;
7175 instance->flag = 0;
7176 instance->unload = 1;
7177 instance->last_time = 0;
7178 instance->disableOnlineCtrlReset = 1;
7179 instance->UnevenSpanSupport = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07007180 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7181 instance->msix_load_balance = false;
Shivasharan S7535f272017-10-19 02:48:58 -07007182
Shivasharan S3f6194a2018-10-16 23:37:39 -07007183 if (instance->adapter_type != MFI_SERIES)
Shivasharan S7535f272017-10-19 02:48:58 -07007184 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Shivasharan S3f6194a2018-10-16 23:37:39 -07007185 else
Shivasharan S7535f272017-10-19 02:48:58 -07007186 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007187}
7188
7189/**
7190 * megasas_probe_one - PCI hotplug entry point
7191 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08007192 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007193 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007194static int megasas_probe_one(struct pci_dev *pdev,
7195 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007196{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307197 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007198 struct Scsi_Host *host;
7199 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08007200 u16 control = 0;
7201
Shivasharan S469f72d2018-11-09 09:47:20 -08007202 switch (pdev->device) {
Chandrakanth Patildd807692019-06-25 16:34:20 +05307203 case PCI_DEVICE_ID_LSI_AERO_10E0:
7204 case PCI_DEVICE_ID_LSI_AERO_10E3:
7205 case PCI_DEVICE_ID_LSI_AERO_10E4:
7206 case PCI_DEVICE_ID_LSI_AERO_10E7:
7207 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7208 return 1;
Shivasharan S469f72d2018-11-09 09:47:20 -08007209 case PCI_DEVICE_ID_LSI_AERO_10E1:
7210 case PCI_DEVICE_ID_LSI_AERO_10E5:
7211 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7212 break;
7213 }
7214
adam radford66192dfe2011-02-24 20:56:28 -08007215 /* Reset MSI-X in the kdump kernel */
7216 if (reset_devices) {
7217 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7218 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06007219 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08007220 &control);
7221 if (control & PCI_MSIX_FLAGS_ENABLE) {
7222 dev_info(&pdev->dev, "resetting MSI-X\n");
7223 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06007224 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08007225 control &
7226 ~PCI_MSIX_FLAGS_ENABLE);
7227 }
7228 }
7229 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007230
7231 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007232 * PCI prepping: enable device set bus mastering and dma mask
7233 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007234 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007235
7236 if (rval) {
7237 return rval;
7238 }
7239
7240 pci_set_master(pdev);
7241
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007242 host = scsi_host_alloc(&megasas_template,
7243 sizeof(struct megasas_instance));
7244
7245 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007246 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007247 goto fail_alloc_instance;
7248 }
7249
7250 instance = (struct megasas_instance *)host->hostdata;
7251 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007252 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007253
7254 /*
7255 * Initialize PCI related and misc parameters
7256 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007257 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007258 instance->host = host;
7259 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7260 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7261
Shivasharan Sc3651782017-10-19 02:48:48 -07007262 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07007263
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007264 /*
adam radford0a770662011-02-24 20:56:12 -08007265 * Initialize MFI Firmware
7266 */
7267 if (megasas_init_fw(instance))
7268 goto fail_init_mfi;
7269
adam radford229fe472014-03-10 02:51:56 -07007270 if (instance->requestorId) {
7271 if (instance->PlasmaFW111) {
7272 instance->vf_affiliation_111 =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007273 dma_alloc_coherent(&pdev->dev,
7274 sizeof(struct MR_LD_VF_AFFILIATION_111),
7275 &instance->vf_affiliation_111_h,
7276 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007277 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007278 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007279 "memory for VF affiliation buffer\n");
7280 } else {
7281 instance->vf_affiliation =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007282 dma_alloc_coherent(&pdev->dev,
7283 (MAX_LOGICAL_DRIVES + 1) *
7284 sizeof(struct MR_LD_VF_AFFILIATION),
7285 &instance->vf_affiliation_h,
7286 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007287 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007288 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007289 "memory for VF affiliation buffer\n");
7290 }
7291 }
7292
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007293 /*
7294 * Store instance in PCI softstate
7295 */
7296 pci_set_drvdata(pdev, instance);
7297
7298 /*
7299 * Add this controller to megasas_mgmt_info structure so that it
7300 * can be exported to management applications
7301 */
7302 megasas_mgmt_info.count++;
7303 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7304 megasas_mgmt_info.max_index++;
7305
7306 /*
adam radford541f90b2011-05-11 18:34:29 -07007307 * Register with SCSI mid-layer
7308 */
7309 if (megasas_io_attach(instance))
7310 goto fail_io_attach;
7311
7312 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307313 /*
7314 * Trigger SCSI to scan our drives
7315 */
Shivasharan Sf6fe5732019-01-29 01:38:14 -08007316 if (!instance->enable_fw_dev_list ||
7317 (instance->host_device_list_buf->count > 0))
7318 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07007319
7320 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007321 * Initiate AEN (Asynchronous Event Notification)
7322 */
7323 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007324 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007325 goto fail_start_aen;
7326 }
7327
Shivasharan Sba535722019-05-07 10:05:49 -07007328 megasas_setup_debugfs(instance);
7329
Adam Radford9ea81f82014-07-09 15:17:57 -07007330 /* Get current SR-IOV LD/VF affiliation */
7331 if (instance->requestorId)
7332 megasas_get_ld_vf_affiliation(instance, 1);
7333
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007334 return 0;
7335
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007336fail_start_aen:
7337fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007338 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007339 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08007340 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007341
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307342 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307343 megasas_destroy_irqs(instance);
7344
Shivasharan Se7d36b82017-10-19 02:48:50 -07007345 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08007346 megasas_release_fusion(instance);
7347 else
7348 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007349 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007350 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307351fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007352 scsi_host_put(host);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007353fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007354 pci_disable_device(pdev);
7355
7356 return -ENODEV;
7357}
7358
7359/**
7360 * megasas_flush_cache - Requests FW to flush all its caches
7361 * @instance: Adapter soft state
7362 */
7363static void megasas_flush_cache(struct megasas_instance *instance)
7364{
7365 struct megasas_cmd *cmd;
7366 struct megasas_dcmd_frame *dcmd;
7367
Sumit Saxena8a01a412016-01-28 21:04:32 +05307368 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007369 return;
7370
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007371 cmd = megasas_get_cmd(instance);
7372
7373 if (!cmd)
7374 return;
7375
7376 dcmd = &cmd->frame->dcmd;
7377
7378 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7379
7380 dcmd->cmd = MFI_CMD_DCMD;
7381 dcmd->cmd_status = 0x0;
7382 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307383 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007384 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007385 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007386 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307387 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007388 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7389
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307390 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7391 != DCMD_SUCCESS) {
7392 dev_err(&instance->pdev->dev,
7393 "return from %s %d\n", __func__, __LINE__);
7394 return;
7395 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007396
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307397 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007398}
7399
7400/**
7401 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7402 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05007403 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007404 */
bo yang31ea7082007-11-07 12:09:50 -05007405static void megasas_shutdown_controller(struct megasas_instance *instance,
7406 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007407{
7408 struct megasas_cmd *cmd;
7409 struct megasas_dcmd_frame *dcmd;
7410
Sumit Saxena8a01a412016-01-28 21:04:32 +05307411 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007412 return;
7413
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007414 cmd = megasas_get_cmd(instance);
7415
7416 if (!cmd)
7417 return;
7418
7419 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307420 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307421 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08007422 if (instance->map_update_cmd)
7423 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307424 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307425 if (instance->jbod_seq_cmd)
7426 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307427 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307428
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007429 dcmd = &cmd->frame->dcmd;
7430
7431 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7432
7433 dcmd->cmd = MFI_CMD_DCMD;
7434 dcmd->cmd_status = 0x0;
7435 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307436 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007437 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007438 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007439 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307440 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007441
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307442 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7443 != DCMD_SUCCESS) {
7444 dev_err(&instance->pdev->dev,
7445 "return from %s %d\n", __func__, __LINE__);
7446 return;
7447 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007448
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307449 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007450}
7451
Jiri Slaby33139b22008-05-01 17:56:02 +02007452#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007453/**
bo yangad84db22007-11-09 04:40:16 -05007454 * megasas_suspend - driver suspend entry point
7455 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05007456 * @state: PCI power state to suspend routine
7457 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007458static int
bo yang31ea7082007-11-07 12:09:50 -05007459megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7460{
bo yang31ea7082007-11-07 12:09:50 -05007461 struct megasas_instance *instance;
7462
7463 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307464
7465 if (!instance)
7466 return 0;
7467
Yang, Bo0c79e682009-10-06 14:47:35 -06007468 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05007469
Shivasharan Sf7331f12019-05-07 10:05:46 -07007470 dev_info(&pdev->dev, "%s is called\n", __func__);
7471
adam radford229fe472014-03-10 02:51:56 -07007472 /* Shutdown SR-IOV heartbeat timer */
7473 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7474 del_timer_sync(&instance->sriov_heartbeat_timer);
7475
Shivasharan S3f6194a2018-10-16 23:37:39 -07007476 /* Stop the FW fault detection watchdog */
7477 if (instance->adapter_type != MFI_SERIES)
7478 megasas_fusion_stop_watchdog(instance);
7479
bo yang31ea7082007-11-07 12:09:50 -05007480 megasas_flush_cache(instance);
7481 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007482
7483 /* cancel the delayed work if this work still in queue */
7484 if (instance->ev != NULL) {
7485 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007486 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007487 instance->ev = NULL;
7488 }
7489
bo yang31ea7082007-11-07 12:09:50 -05007490 tasklet_kill(&instance->isr_tasklet);
7491
7492 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307493 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007494
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307495 megasas_destroy_irqs(instance);
7496
adam radfordc8e858f2011-10-08 18:15:13 -07007497 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007498 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05007499
7500 pci_save_state(pdev);
7501 pci_disable_device(pdev);
7502
7503 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7504
7505 return 0;
7506}
7507
7508/**
7509 * megasas_resume- driver resume entry point
7510 * @pdev: PCI device structure
7511 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007512static int
bo yang31ea7082007-11-07 12:09:50 -05007513megasas_resume(struct pci_dev *pdev)
7514{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307515 int rval;
bo yang31ea7082007-11-07 12:09:50 -05007516 struct Scsi_Host *host;
7517 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01007518 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05007519
7520 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307521
7522 if (!instance)
7523 return 0;
7524
bo yang31ea7082007-11-07 12:09:50 -05007525 host = instance->host;
7526 pci_set_power_state(pdev, PCI_D0);
7527 pci_enable_wake(pdev, PCI_D0, 0);
7528 pci_restore_state(pdev);
7529
Shivasharan Sf7331f12019-05-07 10:05:46 -07007530 dev_info(&pdev->dev, "%s is called\n", __func__);
bo yang31ea7082007-11-07 12:09:50 -05007531 /*
7532 * PCI prepping: enable device set bus mastering and dma mask
7533 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007534 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05007535
7536 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007537 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05007538 return rval;
7539 }
7540
7541 pci_set_master(pdev);
7542
Shivasharan S107a60d2017-10-19 02:49:05 -07007543 /*
7544 * We expect the FW state to be READY
7545 */
7546 if (megasas_transition_to_ready(instance, 0))
7547 goto fail_ready_state;
7548
7549 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05007550 goto fail_set_dma_mask;
7551
7552 /*
7553 * Initialize MFI Firmware
7554 */
7555
bo yang31ea7082007-11-07 12:09:50 -05007556 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08007557 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05007558
adam radford3f1abce2011-05-11 18:33:47 -07007559 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01007560 if (instance->msix_vectors) {
7561 irq_flags = PCI_IRQ_MSIX;
Shivasharan S1d15d902019-05-07 10:05:36 -07007562 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007563 irq_flags |= PCI_IRQ_AFFINITY;
7564 }
7565 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7566 instance->msix_vectors ?
7567 instance->msix_vectors : 1, irq_flags);
7568 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02007569 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07007570
Ming Leiadbe5522018-03-13 17:42:40 +08007571 megasas_setup_reply_map(instance);
7572
Shivasharan Se7d36b82017-10-19 02:48:50 -07007573 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007574 megasas_reset_reply_desc(instance);
7575 if (megasas_ioc_init_fusion(instance)) {
7576 megasas_free_cmds(instance);
7577 megasas_free_cmds_fusion(instance);
7578 goto fail_init_mfi;
7579 }
7580 if (!megasas_get_map_info(instance))
7581 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307582 } else {
adam radford9c915a82010-12-21 13:34:31 -08007583 *instance->producer = 0;
7584 *instance->consumer = 0;
7585 if (megasas_issue_init_mfi(instance))
7586 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08007587 }
bo yang31ea7082007-11-07 12:09:50 -05007588
Shivasharan Sc3b10a52018-06-04 03:45:10 -07007589 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7590 goto fail_init_mfi;
7591
adam radford9c915a82010-12-21 13:34:31 -08007592 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7593 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05007594
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307595 if (instance->msix_vectors ?
7596 megasas_setup_irqs_msix(instance, 0) :
7597 megasas_setup_irqs_ioapic(instance))
7598 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05007599
Shivasharan S62a04f82019-05-07 10:05:35 -07007600 if (instance->adapter_type != MFI_SERIES)
7601 megasas_setup_irq_poll(instance);
7602
adam radford229fe472014-03-10 02:51:56 -07007603 /* Re-launch SR-IOV heartbeat timer */
7604 if (instance->requestorId) {
7605 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07007606 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307607 else {
adam radford229fe472014-03-10 02:51:56 -07007608 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307609 goto fail_init_mfi;
7610 }
adam radford229fe472014-03-10 02:51:56 -07007611 }
7612
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307613 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307614 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06007615 instance->unload = 0;
7616
adam radford541f90b2011-05-11 18:34:29 -07007617 /*
7618 * Initiate AEN (Asynchronous Event Notification)
7619 */
7620 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007621 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07007622
Shivasharan S3f6194a2018-10-16 23:37:39 -07007623 /* Re-launch FW fault watchdog */
7624 if (instance->adapter_type != MFI_SERIES)
7625 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7626 goto fail_start_watchdog;
7627
bo yang31ea7082007-11-07 12:09:50 -05007628 return 0;
7629
Shivasharan S3f6194a2018-10-16 23:37:39 -07007630fail_start_watchdog:
7631 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7632 del_timer_sync(&instance->sriov_heartbeat_timer);
bo yang31ea7082007-11-07 12:09:50 -05007633fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07007634 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007635 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05007636 scsi_host_put(host);
7637
Shivasharan S107a60d2017-10-19 02:49:05 -07007638fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05007639fail_set_dma_mask:
7640fail_ready_state:
7641
7642 pci_disable_device(pdev);
7643
7644 return -ENODEV;
7645}
Jiri Slaby33139b22008-05-01 17:56:02 +02007646#else
7647#define megasas_suspend NULL
7648#define megasas_resume NULL
7649#endif
bo yang31ea7082007-11-07 12:09:50 -05007650
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007651static inline int
7652megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7653{
7654 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7655 int i;
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007656 u8 adp_state;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007657
7658 for (i = 0; i < wait_time; i++) {
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007659 adp_state = atomic_read(&instance->adprecovery);
7660 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7661 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007662 break;
7663
7664 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7665 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7666
7667 msleep(1000);
7668 }
7669
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007670 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7671 dev_info(&instance->pdev->dev,
7672 "%s HBA failed to become operational, adp_state %d\n",
7673 __func__, adp_state);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007674 return 1;
7675 }
7676
7677 return 0;
7678}
7679
bo yang31ea7082007-11-07 12:09:50 -05007680/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007681 * megasas_detach_one - PCI hot"un"plug entry point
7682 * @pdev: PCI device structure
7683 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007684static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007685{
7686 int i;
7687 struct Scsi_Host *host;
7688 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08007689 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307690 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007691
7692 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307693
7694 if (!instance)
7695 return;
7696
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007697 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08007698 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007699
adam radford229fe472014-03-10 02:51:56 -07007700 /* Shutdown SR-IOV heartbeat timer */
7701 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7702 del_timer_sync(&instance->sriov_heartbeat_timer);
7703
Shivasharan S3f6194a2018-10-16 23:37:39 -07007704 /* Stop the FW fault detection watchdog */
7705 if (instance->adapter_type != MFI_SERIES)
7706 megasas_fusion_stop_watchdog(instance);
7707
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307708 if (instance->fw_crash_state != UNAVAILABLE)
7709 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007710 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08007711 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007712
7713 if (megasas_wait_for_adapter_operational(instance))
7714 goto skip_firing_dcmds;
7715
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007716 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05007717 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007718
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007719skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007720 /* cancel the delayed work if this work still in queue*/
7721 if (instance->ev != NULL) {
7722 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007723 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007724 instance->ev = NULL;
7725 }
7726
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307727 /* cancel all wait events */
7728 wake_up_all(&instance->int_cmd_wait_q);
7729
Sumant Patro5d018ad2006-10-03 13:13:18 -07007730 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007731
7732 /*
7733 * Take the instance off the instance array. Note that we will not
7734 * decrement the max_index. We let this array be sparse array
7735 */
7736 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7737 if (megasas_mgmt_info.instance[i] == instance) {
7738 megasas_mgmt_info.count--;
7739 megasas_mgmt_info.instance[i] = NULL;
7740
7741 break;
7742 }
7743 }
7744
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307745 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007746
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307747 megasas_destroy_irqs(instance);
7748
adam radfordc8e858f2011-10-08 18:15:13 -07007749 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007750 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007751
Shivasharan S630d42b2018-12-17 00:47:37 -08007752 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05007753 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7754 kfree(fusion->stream_detect_by_ld[i]);
7755 kfree(fusion->stream_detect_by_ld);
7756 fusion->stream_detect_by_ld = NULL;
7757 }
7758
7759
Shivasharan Se7d36b82017-10-19 02:48:50 -07007760 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007761 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307762 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7763 (sizeof(struct MR_PD_CFG_SEQ) *
7764 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307765 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08007766 if (fusion->ld_map[i])
7767 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307768 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08007769 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307770 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07007771 if (fusion->ld_drv_map[i]) {
7772 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7773 vfree(fusion->ld_drv_map[i]);
7774 else
7775 free_pages((ulong)fusion->ld_drv_map[i],
7776 fusion->drv_map_pages);
7777 }
7778
Maurizio Lombardi546e5592016-01-22 13:41:42 +01007779 if (fusion->pd_seq_sync[i])
7780 dma_free_coherent(&instance->pdev->dev,
7781 pd_seq_map_sz,
7782 fusion->pd_seq_sync[i],
7783 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307784 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307785 } else {
adam radford9c915a82010-12-21 13:34:31 -08007786 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08007787 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007788
adam radford229fe472014-03-10 02:51:56 -07007789 if (instance->vf_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007790 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
adam radford229fe472014-03-10 02:51:56 -07007791 sizeof(struct MR_LD_VF_AFFILIATION),
7792 instance->vf_affiliation,
7793 instance->vf_affiliation_h);
7794
7795 if (instance->vf_affiliation_111)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007796 dma_free_coherent(&pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07007797 sizeof(struct MR_LD_VF_AFFILIATION_111),
7798 instance->vf_affiliation_111,
7799 instance->vf_affiliation_111_h);
7800
7801 if (instance->hb_host_mem)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007802 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
adam radford229fe472014-03-10 02:51:56 -07007803 instance->hb_host_mem,
7804 instance->hb_host_mem_h);
7805
Shivasharan S1b4bed22017-10-19 02:48:55 -07007806 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307807
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007808 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307809
Shivasharan Sba535722019-05-07 10:05:49 -07007810 megasas_destroy_debugfs(instance);
7811
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007812 scsi_host_put(host);
7813
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007814 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007815}
7816
7817/**
7818 * megasas_shutdown - Shutdown entry point
7819 * @device: Generic device structure
7820 */
7821static void megasas_shutdown(struct pci_dev *pdev)
7822{
7823 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07007824
Chandrakanth Patildd807692019-06-25 16:34:20 +05307825 if (!instance)
7826 return;
7827
Yang, Bo0c79e682009-10-06 14:47:35 -06007828 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007829
7830 if (megasas_wait_for_adapter_operational(instance))
7831 goto skip_firing_dcmds;
7832
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007833 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07007834 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007835
7836skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307837 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307838 megasas_destroy_irqs(instance);
7839
adam radfordc8e858f2011-10-08 18:15:13 -07007840 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007841 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007842}
7843
7844/**
7845 * megasas_mgmt_open - char node "open" entry point
7846 */
7847static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7848{
7849 /*
7850 * Allow only those users with admin rights
7851 */
7852 if (!capable(CAP_SYS_ADMIN))
7853 return -EACCES;
7854
7855 return 0;
7856}
7857
7858/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007859 * megasas_mgmt_fasync - Async notifier registration from applications
7860 *
7861 * This function adds the calling process to a driver global queue. When an
7862 * event occurs, SIGIO will be sent to all processes in this queue.
7863 */
7864static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7865{
7866 int rc;
7867
Arjan van de Ven0b950672006-01-11 13:16:10 +01007868 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007869
7870 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7871
Arjan van de Ven0b950672006-01-11 13:16:10 +01007872 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007873
7874 if (rc >= 0) {
7875 /* For sanity check when we get ioctl */
7876 filep->private_data = filep;
7877 return 0;
7878 }
7879
7880 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7881
7882 return rc;
7883}
7884
7885/**
Yang, Boc3518832009-10-06 14:18:02 -06007886 * megasas_mgmt_poll - char node "poll" entry point
7887 * */
Al Viroafc9a422017-07-03 06:39:46 -04007888static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
Yang, Boc3518832009-10-06 14:18:02 -06007889{
Al Viroafc9a422017-07-03 06:39:46 -04007890 __poll_t mask;
Yang, Boc3518832009-10-06 14:18:02 -06007891 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007892
Yang, Boc3518832009-10-06 14:18:02 -06007893 poll_wait(file, &megasas_poll_wait, wait);
7894 spin_lock_irqsave(&poll_aen_lock, flags);
7895 if (megasas_poll_wait_aen)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007896 mask = (EPOLLIN | EPOLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007897 else
7898 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307899 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007900 spin_unlock_irqrestore(&poll_aen_lock, flags);
7901 return mask;
7902}
7903
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307904/*
7905 * megasas_set_crash_dump_params_ioctl:
7906 * Send CRASH_DUMP_MODE DCMD to all controllers
7907 * @cmd: MFI command frame
7908 */
7909
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007910static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307911{
7912 struct megasas_instance *local_instance;
7913 int i, error = 0;
7914 int crash_support;
7915
7916 crash_support = cmd->frame->dcmd.mbox.w[0];
7917
7918 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7919 local_instance = megasas_mgmt_info.instance[i];
7920 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307921 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307922 MEGASAS_HBA_OPERATIONAL) &&
7923 !megasas_set_crash_dump_params(local_instance,
7924 crash_support)) {
7925 local_instance->crash_dump_app_support =
7926 crash_support;
7927 dev_info(&local_instance->pdev->dev,
7928 "Application firmware crash "
7929 "dump mode set success\n");
7930 error = 0;
7931 } else {
7932 dev_info(&local_instance->pdev->dev,
7933 "Application firmware crash "
7934 "dump mode set failed\n");
7935 error = -1;
7936 }
7937 }
7938 }
7939 return error;
7940}
7941
Yang, Boc3518832009-10-06 14:18:02 -06007942/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007943 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7944 * @instance: Adapter soft state
7945 * @argp: User's ioctl packet
7946 */
7947static int
7948megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7949 struct megasas_iocpacket __user * user_ioc,
7950 struct megasas_iocpacket *ioc)
7951{
Shivasharan S107a60d2017-10-19 02:49:05 -07007952 struct megasas_sge64 *kern_sge64 = NULL;
7953 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007954 struct megasas_cmd *cmd;
7955 void *kbuff_arr[MAX_IOCTL_SGE];
7956 dma_addr_t buf_handle = 0;
7957 int error = 0, i;
7958 void *sense = NULL;
7959 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007960 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007961 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007962
7963 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7964
7965 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007966 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007967 ioc->sge_count, MAX_IOCTL_SGE);
7968 return -EINVAL;
7969 }
7970
Shivasharan Sf870bcb2018-01-05 05:33:04 -08007971 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7972 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
Chandrakanth Patil58136852019-06-25 16:34:30 +05307973 !instance->support_nvme_passthru) ||
7974 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
7975 !instance->support_pci_lane_margining)) {
Shivasharan S82add4e2017-10-19 02:49:02 -07007976 dev_err(&instance->pdev->dev,
7977 "Received invalid ioctl command 0x%x\n",
7978 ioc->frame.hdr.cmd);
7979 return -ENOTSUPP;
7980 }
7981
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007982 cmd = megasas_get_cmd(instance);
7983 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007984 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007985 return -ENOMEM;
7986 }
7987
7988 /*
7989 * User's IOCTL packet has 2 frames (maximum). Copy those two
7990 * frames into our cmd's frames. cmd->frame's context will get
7991 * overwritten when we copy from user's frames. So set that value
7992 * alone separately
7993 */
7994 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307995 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007996 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07007997
7998 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7999
8000 if (instance->consistent_mask_64bit)
8001 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8002 MFI_FRAME_SENSE64));
8003 else
8004 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308005 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07008006
8007 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8008 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008009
Shivasharan S8823abe2017-08-23 04:47:00 -07008010 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308011 mutex_lock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008012 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8013 megasas_return_cmd(instance, cmd);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308014 mutex_unlock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008015 return -1;
8016 }
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308017 mutex_unlock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008018 }
8019
Shivasharan S8823abe2017-08-23 04:47:00 -07008020 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05308021 error = megasas_set_crash_dump_params_ioctl(cmd);
8022 megasas_return_cmd(instance, cmd);
8023 return error;
8024 }
8025
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008026 /*
8027 * The management interface between applications and the fw uses
8028 * MFI frames. E.g, RAID configuration changes, LD property changes
8029 * etc are accomplishes through different kinds of MFI frames. The
8030 * driver needs to care only about substituting user buffers with
8031 * kernel buffers in SGLs. The location of SGL is embedded in the
8032 * struct iocpacket itself.
8033 */
Shivasharan S107a60d2017-10-19 02:49:05 -07008034 if (instance->consistent_mask_64bit)
8035 kern_sge64 = (struct megasas_sge64 *)
8036 ((unsigned long)cmd->frame + ioc->sgl_off);
8037 else
8038 kern_sge32 = (struct megasas_sge32 *)
8039 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008040
8041 /*
8042 * For each user buffer, create a mirror buffer and copy in
8043 */
8044 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01008045 if (!ioc->sgl[i].iov_len)
8046 continue;
8047
Sumant Patro9f35fa82007-02-14 12:55:45 -08008048 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008049 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08008050 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008051 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008052 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8053 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008054 error = -ENOMEM;
8055 goto out;
8056 }
8057
8058 /*
8059 * We don't change the dma_coherent_mask, so
Christoph Hellwig60ee6522018-10-10 19:31:25 +02008060 * dma_alloc_coherent only returns 32bit addresses
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008061 */
Shivasharan S107a60d2017-10-19 02:49:05 -07008062 if (instance->consistent_mask_64bit) {
8063 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8064 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8065 } else {
8066 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8067 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8068 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008069
8070 /*
8071 * We created a kernel buffer corresponding to the
8072 * user buffer. Now copy in from the user buffer
8073 */
8074 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8075 (u32) (ioc->sgl[i].iov_len))) {
8076 error = -EFAULT;
8077 goto out;
8078 }
8079 }
8080
8081 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08008082 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8083 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008084 if (!sense) {
8085 error = -ENOMEM;
8086 goto out;
8087 }
8088
8089 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06008090 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07008091 if (instance->consistent_mask_64bit)
8092 *sense_ptr = cpu_to_le64(sense_handle);
8093 else
8094 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008095 }
8096
8097 /*
8098 * Set the sync_cmd flag so that the ISR knows not to complete this
8099 * cmd to the SCSI mid-layer
8100 */
8101 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308102 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
8103 cmd->sync_cmd = 0;
8104 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07008105 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8106 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8107 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308108 return -EBUSY;
8109 }
8110
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008111 cmd->sync_cmd = 0;
8112
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05308113 if (instance->unload == 1) {
8114 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8115 "don't submit data to application\n");
8116 goto out;
8117 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008118 /*
8119 * copy out the kernel buffers to user buffers
8120 */
8121 for (i = 0; i < ioc->sge_count; i++) {
8122 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8123 ioc->sgl[i].iov_len)) {
8124 error = -EFAULT;
8125 goto out;
8126 }
8127 }
8128
8129 /*
8130 * copy out the sense
8131 */
8132 if (ioc->sense_len) {
8133 /*
bo yangb70a41e2008-03-18 03:13:06 -04008134 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008135 * sense buffer address
8136 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06008137 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8138 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008139
Shivasharan S318aaef2017-02-10 00:59:22 -08008140 if (copy_to_user((void __user *)((unsigned long)
8141 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04008142 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008143 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05008144 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008145 error = -EFAULT;
8146 goto out;
8147 }
8148 }
8149
8150 /*
8151 * copy the status codes returned by the fw
8152 */
8153 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8154 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008155 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008156 error = -EFAULT;
8157 }
8158
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008159out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008160 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08008161 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008162 sense, sense_handle);
8163 }
8164
Bjørn Mork7a6a7312012-11-21 09:54:48 +01008165 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01008166 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07008167 if (instance->consistent_mask_64bit)
8168 dma_free_coherent(&instance->pdev->dev,
8169 le32_to_cpu(kern_sge64[i].length),
8170 kbuff_arr[i],
8171 le64_to_cpu(kern_sge64[i].phys_addr));
8172 else
8173 dma_free_coherent(&instance->pdev->dev,
8174 le32_to_cpu(kern_sge32[i].length),
8175 kbuff_arr[i],
8176 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05308177 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01008178 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008179 }
8180
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05308181 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008182 return error;
8183}
8184
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008185static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8186{
8187 struct megasas_iocpacket __user *user_ioc =
8188 (struct megasas_iocpacket __user *)arg;
8189 struct megasas_iocpacket *ioc;
8190 struct megasas_instance *instance;
8191 int error;
8192
Markus Elfring709ab232016-08-21 10:39:04 +02008193 ioc = memdup_user(user_ioc, sizeof(*ioc));
8194 if (IS_ERR(ioc))
8195 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008196
8197 instance = megasas_lookup_instance(ioc->host_no);
8198 if (!instance) {
8199 error = -ENODEV;
8200 goto out_kfree_ioc;
8201 }
8202
adam radford229fe472014-03-10 02:51:56 -07008203 /* Block ioctls in VF mode */
8204 if (instance->requestorId && !allow_vf_ioctls) {
8205 error = -ENODEV;
8206 goto out_kfree_ioc;
8207 }
8208
Sumit Saxena8a01a412016-01-28 21:04:32 +05308209 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008210 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06008211 error = -ENODEV;
8212 goto out_kfree_ioc;
8213 }
8214
8215 if (instance->unload == 1) {
8216 error = -ENODEV;
8217 goto out_kfree_ioc;
8218 }
8219
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008220 if (down_interruptible(&instance->ioctl_sem)) {
8221 error = -ERESTARTSYS;
8222 goto out_kfree_ioc;
8223 }
bo yang39a98552010-09-22 22:36:29 -04008224
Shivasharan S619831f2018-01-05 05:27:43 -08008225 if (megasas_wait_for_adapter_operational(instance)) {
bo yang39a98552010-09-22 22:36:29 -04008226 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03008227 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04008228 }
bo yang39a98552010-09-22 22:36:29 -04008229
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008230 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008231out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008232 up(&instance->ioctl_sem);
8233
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008234out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008235 kfree(ioc);
8236 return error;
8237}
8238
8239static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8240{
8241 struct megasas_instance *instance;
8242 struct megasas_aen aen;
8243 int error;
8244
8245 if (file->private_data != file) {
8246 printk(KERN_DEBUG "megasas: fasync_helper was not "
8247 "called first\n");
8248 return -EINVAL;
8249 }
8250
8251 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8252 return -EFAULT;
8253
8254 instance = megasas_lookup_instance(aen.host_no);
8255
8256 if (!instance)
8257 return -ENODEV;
8258
Sumit Saxena8a01a412016-01-28 21:04:32 +05308259 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04008260 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06008261 }
8262
8263 if (instance->unload == 1) {
8264 return -ENODEV;
8265 }
8266
Shivasharan S619831f2018-01-05 05:27:43 -08008267 if (megasas_wait_for_adapter_operational(instance))
bo yang39a98552010-09-22 22:36:29 -04008268 return -ENODEV;
bo yang39a98552010-09-22 22:36:29 -04008269
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308270 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008271 error = megasas_register_aen(instance, aen.seq_num,
8272 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308273 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008274 return error;
8275}
8276
8277/**
8278 * megasas_mgmt_ioctl - char node ioctl entry point
8279 */
8280static long
8281megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8282{
8283 switch (cmd) {
8284 case MEGASAS_IOC_FIRMWARE:
8285 return megasas_mgmt_ioctl_fw(file, arg);
8286
8287 case MEGASAS_IOC_GET_AEN:
8288 return megasas_mgmt_ioctl_aen(file, arg);
8289 }
8290
8291 return -ENOTTY;
8292}
8293
8294#ifdef CONFIG_COMPAT
8295static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8296{
8297 struct compat_megasas_iocpacket __user *cioc =
8298 (struct compat_megasas_iocpacket __user *)arg;
8299 struct megasas_iocpacket __user *ioc =
8300 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8301 int i;
8302 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008303 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308304 u32 local_sense_off;
8305 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308306 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008307
Jeff Garzik83aabc12006-10-04 06:34:03 -04008308 if (clear_user(ioc, sizeof(*ioc)))
8309 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008310
8311 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8312 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8313 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8314 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8315 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8316 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8317 return -EFAULT;
8318
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008319 /*
8320 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8321 * sense_len is not null, so prepare the 64bit value under
8322 * the same condition.
8323 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308324 if (get_user(local_sense_off, &ioc->sense_off) ||
8325 get_user(local_sense_len, &ioc->sense_len) ||
8326 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308327 return -EFAULT;
8328
Wenwen Wang47db7872018-10-06 13:34:21 -05008329 if (local_sense_off != user_sense_off)
8330 return -EINVAL;
8331
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308332 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008333 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308334 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008335 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308336 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008337 if (get_user(ptr, sense_cioc_ptr) ||
8338 put_user(compat_ptr(ptr), sense_ioc_ptr))
8339 return -EFAULT;
8340 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008341
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008342 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008343 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8344 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8345 copy_in_user(&ioc->sgl[i].iov_len,
8346 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8347 return -EFAULT;
8348 }
8349
8350 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8351
8352 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8353 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8354 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8355 return -EFAULT;
8356 }
8357 return error;
8358}
8359
8360static long
8361megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8362 unsigned long arg)
8363{
8364 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08008365 case MEGASAS_IOC_FIRMWARE32:
8366 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008367 case MEGASAS_IOC_GET_AEN:
8368 return megasas_mgmt_ioctl_aen(file, arg);
8369 }
8370
8371 return -ENOTTY;
8372}
8373#endif
8374
8375/*
8376 * File operations structure for management interface
8377 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08008378static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008379 .owner = THIS_MODULE,
8380 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008381 .fasync = megasas_mgmt_fasync,
8382 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06008383 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008384#ifdef CONFIG_COMPAT
8385 .compat_ioctl = megasas_mgmt_compat_ioctl,
8386#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02008387 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008388};
8389
8390/*
8391 * PCI hotplug support registration structure
8392 */
8393static struct pci_driver megasas_pci_driver = {
8394
8395 .name = "megaraid_sas",
8396 .id_table = megasas_pci_table,
8397 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008398 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05008399 .suspend = megasas_suspend,
8400 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008401 .shutdown = megasas_shutdown,
8402};
8403
8404/*
8405 * Sysfs driver attributes
8406 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008407static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008408{
8409 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8410 MEGASAS_VERSION);
8411}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008412static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008413
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008414static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308415{
8416 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8417 MEGASAS_RELDATE);
8418}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008419static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308420
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008421static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06008422{
8423 return sprintf(buf, "%u\n", support_poll_for_event);
8424}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008425static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008426
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008427static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06008428{
8429 return sprintf(buf, "%u\n", support_device_change);
8430}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008431static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06008432
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008433static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07008434{
bo yangad84db22007-11-09 04:40:16 -05008435 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07008436}
8437
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008438static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8439 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07008440{
8441 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008442
8443 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07008444 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8445 retval = -EINVAL;
8446 }
8447 return retval;
8448}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008449static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05008450
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008451static ssize_t
8452support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8453{
8454 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8455}
8456
8457static DRIVER_ATTR_RO(support_nvme_encapsulation);
8458
Chandrakanth Patil58136852019-06-25 16:34:30 +05308459static ssize_t
8460support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8461{
8462 return sprintf(buf, "%u\n", support_pci_lane_margining);
8463}
8464
8465static DRIVER_ATTR_RO(support_pci_lane_margining);
8466
Shivasharan Sb4a42212017-02-10 00:59:16 -08008467static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8468{
8469 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8470 scsi_remove_device(sdev);
8471 scsi_device_put(sdev);
8472}
8473
Shivasharan S44abbaf2019-01-29 01:38:13 -08008474/**
8475 * megasas_update_device_list - Update the PD and LD device list from FW
8476 * after an AEN event notification
8477 * @instance: Adapter soft state
8478 * @event_type: Indicates type of event (PD or LD event)
8479 *
8480 * @return: Success or failure
8481 *
8482 * Issue DCMDs to Firmware to update the internal device list in driver.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008483 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8484 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan S44abbaf2019-01-29 01:38:13 -08008485 */
8486static
8487int megasas_update_device_list(struct megasas_instance *instance,
8488 int event_type)
8489{
8490 int dcmd_ret = DCMD_SUCCESS;
8491
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008492 if (instance->enable_fw_dev_list) {
8493 dcmd_ret = megasas_host_device_list_query(instance, false);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008494 if (dcmd_ret != DCMD_SUCCESS)
8495 goto out;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008496 } else {
8497 if (event_type & SCAN_PD_CHANNEL) {
8498 dcmd_ret = megasas_get_pd_list(instance);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008499
Shivasharan S44abbaf2019-01-29 01:38:13 -08008500 if (dcmd_ret != DCMD_SUCCESS)
8501 goto out;
8502 }
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008503
8504 if (event_type & SCAN_VD_CHANNEL) {
8505 if (!instance->requestorId ||
8506 (instance->requestorId &&
8507 megasas_get_ld_vf_affiliation(instance, 0))) {
8508 dcmd_ret = megasas_ld_list_query(instance,
8509 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8510 if (dcmd_ret != DCMD_SUCCESS)
8511 goto out;
8512 }
8513 }
Shivasharan S44abbaf2019-01-29 01:38:13 -08008514 }
8515
8516out:
8517 return dcmd_ret;
8518}
8519
8520/**
8521 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8522 * after an AEN event notification
8523 * @instance: Adapter soft state
8524 * @scan_type: Indicates type of devices (PD/LD) to add
8525 * @return void
8526 */
8527static
8528void megasas_add_remove_devices(struct megasas_instance *instance,
8529 int scan_type)
8530{
8531 int i, j;
8532 u16 pd_index = 0;
8533 u16 ld_index = 0;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008534 u16 channel = 0, id = 0;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008535 struct Scsi_Host *host;
8536 struct scsi_device *sdev1;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008537 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8538 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008539
8540 host = instance->host;
8541
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008542 if (instance->enable_fw_dev_list) {
8543 targetid_list = instance->host_device_list_buf;
8544 for (i = 0; i < targetid_list->count; i++) {
8545 targetid_entry = &targetid_list->host_device_list[i];
8546 if (targetid_entry->flags.u.bits.is_sys_pd) {
8547 channel = le16_to_cpu(targetid_entry->target_id) /
8548 MEGASAS_MAX_DEV_PER_CHANNEL;
8549 id = le16_to_cpu(targetid_entry->target_id) %
8550 MEGASAS_MAX_DEV_PER_CHANNEL;
8551 } else {
8552 channel = MEGASAS_MAX_PD_CHANNELS +
8553 (le16_to_cpu(targetid_entry->target_id) /
8554 MEGASAS_MAX_DEV_PER_CHANNEL);
8555 id = le16_to_cpu(targetid_entry->target_id) %
8556 MEGASAS_MAX_DEV_PER_CHANNEL;
8557 }
8558 sdev1 = scsi_device_lookup(host, channel, id, 0);
8559 if (!sdev1) {
8560 scsi_add_device(host, channel, id, 0);
8561 } else {
8562 scsi_device_put(sdev1);
8563 }
8564 }
8565 }
8566
Shivasharan S44abbaf2019-01-29 01:38:13 -08008567 if (scan_type & SCAN_PD_CHANNEL) {
8568 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8569 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8570 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8571 sdev1 = scsi_device_lookup(host, i, j, 0);
8572 if (instance->pd_list[pd_index].driveState ==
8573 MR_PD_STATE_SYSTEM) {
8574 if (!sdev1)
8575 scsi_add_device(host, i, j, 0);
8576 else
8577 scsi_device_put(sdev1);
8578 } else {
8579 if (sdev1)
8580 megasas_remove_scsi_device(sdev1);
8581 }
8582 }
8583 }
8584 }
8585
8586 if (scan_type & SCAN_VD_CHANNEL) {
8587 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8588 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8589 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8590 sdev1 = scsi_device_lookup(host,
8591 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8592 if (instance->ld_ids[ld_index] != 0xff) {
8593 if (!sdev1)
8594 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8595 else
8596 scsi_device_put(sdev1);
8597 } else {
8598 if (sdev1)
8599 megasas_remove_scsi_device(sdev1);
8600 }
8601 }
8602 }
8603 }
8604
8605}
8606
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008607static void
8608megasas_aen_polling(struct work_struct *work)
8609{
8610 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08008611 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008612 struct megasas_instance *instance = ev->instance;
8613 union megasas_evt_class_locale class_locale;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008614 int event_type = 0;
YueHaibingde192122019-05-25 20:42:02 +08008615 u32 seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008616 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308617 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008618
8619 if (!instance) {
8620 printk(KERN_ERR "invalid instance!\n");
8621 kfree(ev);
8622 return;
8623 }
adam radford229fe472014-03-10 02:51:56 -07008624
adam radford229fe472014-03-10 02:51:56 -07008625 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308626 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07008627
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008628 instance->ev = NULL;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008629 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05308630 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008631
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308632 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308633
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008634 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308635 case MR_EVT_PD_REMOVED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008636 event_type = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07008637 break;
8638
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308639 case MR_EVT_LD_OFFLINE:
8640 case MR_EVT_CFG_CLEARED:
8641 case MR_EVT_LD_DELETED:
8642 case MR_EVT_LD_CREATED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008643 event_type = SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308644 break;
8645
8646 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8647 case MR_EVT_FOREIGN_CFG_IMPORTED:
8648 case MR_EVT_LD_STATE_CHANGE:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008649 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308650 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8651 instance->host->host_no);
8652 break;
8653
8654 case MR_EVT_CTRL_PROP_CHANGED:
Shivasharan Sf0c21df2018-10-16 23:37:40 -07008655 dcmd_ret = megasas_get_ctrl_info(instance);
8656 if (dcmd_ret == DCMD_SUCCESS &&
8657 instance->snapdump_wait_time) {
8658 megasas_get_snapdump_properties(instance);
8659 dev_info(&instance->pdev->dev,
8660 "Snap dump wait time\t: %d\n",
8661 instance->snapdump_wait_time);
8662 }
8663 break;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308664 default:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008665 event_type = 0;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308666 break;
8667 }
8668 } else {
8669 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8670 mutex_unlock(&instance->reset_mutex);
8671 kfree(ev);
8672 return;
8673 }
8674
Shivasharan S44abbaf2019-01-29 01:38:13 -08008675 if (event_type)
8676 dcmd_ret = megasas_update_device_list(instance, event_type);
8677
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308678 mutex_unlock(&instance->reset_mutex);
8679
Shivasharan S44abbaf2019-01-29 01:38:13 -08008680 if (event_type && dcmd_ret == DCMD_SUCCESS)
8681 megasas_add_remove_devices(instance, event_type);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008682
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308683 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308684 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8685 else
8686 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008687
8688 /* Register AEN with FW for latest sequence number plus 1 */
8689 class_locale.members.reserved = 0;
8690 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8691 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308692
8693 if (instance->aen_cmd != NULL) {
8694 kfree(ev);
8695 return;
8696 }
8697
8698 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008699 error = megasas_register_aen(instance, seq_num,
8700 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008701 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308702 dev_err(&instance->pdev->dev,
8703 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008704
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308705 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008706 kfree(ev);
8707}
8708
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008709/**
8710 * megasas_init - Driver load entry point
8711 */
8712static int __init megasas_init(void)
8713{
8714 int rval;
8715
8716 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07008717 * Booted in kdump kernel, minimize memory footprints by
8718 * disabling few features
8719 */
8720 if (reset_devices) {
8721 msix_vectors = 1;
8722 rdpq_enable = 0;
8723 dual_qdepth_disable = 1;
8724 }
8725
8726 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008727 * Announce driver version and other information
8728 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05308729 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008730
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07008731 spin_lock_init(&poll_aen_lock);
8732
Yang, Bo72c4fd32009-10-06 14:20:59 -06008733 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06008734 support_device_change = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008735 support_nvme_encapsulation = true;
Chandrakanth Patil58136852019-06-25 16:34:30 +05308736 support_pci_lane_margining = true;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008737
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008738 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8739
8740 /*
8741 * Register character device node
8742 */
8743 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8744
8745 if (rval < 0) {
8746 printk(KERN_DEBUG "megasas: failed to open device node\n");
8747 return rval;
8748 }
8749
8750 megasas_mgmt_majorno = rval;
8751
Shivasharan Sba535722019-05-07 10:05:49 -07008752 megasas_init_debugfs();
8753
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008754 /*
8755 * Register ourselves as PCI hotplug module
8756 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00008757 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008758
8759 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09008760 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04008761 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008762 }
8763
Shivasharan Sd956a112019-06-28 02:50:40 -07008764 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
8765 (event_log_level > MFI_EVT_CLASS_DEAD)) {
8766 printk(KERN_WARNING "megarid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
8767 event_log_level = MFI_EVT_CLASS_CRITICAL;
8768 }
8769
Jeff Garzik83aabc12006-10-04 06:34:03 -04008770 rval = driver_create_file(&megasas_pci_driver.driver,
8771 &driver_attr_version);
8772 if (rval)
8773 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008774
8775 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308776 &driver_attr_release_date);
8777 if (rval)
8778 goto err_dcf_rel_date;
8779
8780 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008781 &driver_attr_support_poll_for_event);
8782 if (rval)
8783 goto err_dcf_support_poll_for_event;
8784
Jeff Garzik83aabc12006-10-04 06:34:03 -04008785 rval = driver_create_file(&megasas_pci_driver.driver,
8786 &driver_attr_dbg_lvl);
8787 if (rval)
8788 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05008789 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008790 &driver_attr_support_device_change);
8791 if (rval)
8792 goto err_dcf_support_device_change;
8793
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008794 rval = driver_create_file(&megasas_pci_driver.driver,
8795 &driver_attr_support_nvme_encapsulation);
8796 if (rval)
8797 goto err_dcf_support_nvme_encapsulation;
8798
Chandrakanth Patil58136852019-06-25 16:34:30 +05308799 rval = driver_create_file(&megasas_pci_driver.driver,
8800 &driver_attr_support_pci_lane_margining);
8801 if (rval)
8802 goto err_dcf_support_pci_lane_margining;
8803
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008804 return rval;
bo yangad84db22007-11-09 04:40:16 -05008805
Chandrakanth Patil58136852019-06-25 16:34:30 +05308806err_dcf_support_pci_lane_margining:
8807 driver_remove_file(&megasas_pci_driver.driver,
8808 &driver_attr_support_nvme_encapsulation);
8809
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008810err_dcf_support_nvme_encapsulation:
8811 driver_remove_file(&megasas_pci_driver.driver,
8812 &driver_attr_support_device_change);
8813
Yang, Bo837f5fe2010-10-11 06:59:20 -06008814err_dcf_support_device_change:
8815 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05008816 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008817err_dcf_dbg_lvl:
8818 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008819 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008820err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308821 driver_remove_file(&megasas_pci_driver.driver,
8822 &driver_attr_release_date);
8823err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04008824 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8825err_dcf_attr_ver:
8826 pci_unregister_driver(&megasas_pci_driver);
8827err_pcidrv:
Shivasharan Sba535722019-05-07 10:05:49 -07008828 megasas_exit_debugfs();
Jeff Garzik83aabc12006-10-04 06:34:03 -04008829 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08008830 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008831}
8832
8833/**
8834 * megasas_exit - Driver unload entry point
8835 */
8836static void __exit megasas_exit(void)
8837{
Sumant Patro658dced2006-10-03 13:09:14 -07008838 driver_remove_file(&megasas_pci_driver.driver,
8839 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008840 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008841 &driver_attr_support_poll_for_event);
8842 driver_remove_file(&megasas_pci_driver.driver,
8843 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308844 driver_remove_file(&megasas_pci_driver.driver,
8845 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008846 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008847 driver_remove_file(&megasas_pci_driver.driver,
8848 &driver_attr_support_nvme_encapsulation);
Chandrakanth Patil58136852019-06-25 16:34:30 +05308849 driver_remove_file(&megasas_pci_driver.driver,
8850 &driver_attr_support_pci_lane_margining);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008851
8852 pci_unregister_driver(&megasas_pci_driver);
Shivasharan Sba535722019-05-07 10:05:49 -07008853 megasas_exit_debugfs();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008854 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8855}
8856
8857module_init(megasas_init);
8858module_exit(megasas_exit);