blob: f9f07935556ed2f7862052d613bd953e210b2454 [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;
Junxiao Bi3b5f3072019-07-22 09:15:24 -07003166 unsigned long chunk_left_bytes;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303167 unsigned long src_addr;
3168 unsigned long flags;
3169 u32 buff_offset;
3170
3171 spin_lock_irqsave(&instance->crashdump_lock, flags);
3172 buff_offset = instance->fw_crash_buffer_offset;
3173 if (!instance->crash_dump_buf &&
3174 !((instance->fw_crash_state == AVAILABLE) ||
3175 (instance->fw_crash_state == COPYING))) {
3176 dev_err(&instance->pdev->dev,
3177 "Firmware crash dump is not available\n");
3178 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3179 return -EINVAL;
3180 }
3181
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003182 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303183 dev_err(&instance->pdev->dev,
3184 "Firmware crash dump offset is out of range\n");
3185 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3186 return 0;
3187 }
3188
3189 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
Junxiao Bi3b5f3072019-07-22 09:15:24 -07003190 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3191 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303192 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3193
3194 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3195 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003196 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303197 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3198
3199 return size;
3200}
3201
3202static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003203fw_crash_buffer_size_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303204 struct device_attribute *attr, char *buf)
3205{
3206 struct Scsi_Host *shost = class_to_shost(cdev);
3207 struct megasas_instance *instance =
3208 (struct megasas_instance *) shost->hostdata;
3209
3210 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3211 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3212}
3213
3214static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003215fw_crash_state_store(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303216 struct device_attribute *attr, const char *buf, size_t count)
3217{
3218 struct Scsi_Host *shost = class_to_shost(cdev);
3219 struct megasas_instance *instance =
3220 (struct megasas_instance *) shost->hostdata;
3221 int val = 0;
3222 unsigned long flags;
3223
3224 if (kstrtoint(buf, 0, &val) != 0)
3225 return -EINVAL;
3226
3227 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3228 dev_err(&instance->pdev->dev, "application updates invalid "
3229 "firmware crash state\n");
3230 return -EINVAL;
3231 }
3232
3233 instance->fw_crash_state = val;
3234
3235 if ((val == COPIED) || (val == COPY_ERROR)) {
3236 spin_lock_irqsave(&instance->crashdump_lock, flags);
3237 megasas_free_host_crash_buffer(instance);
3238 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3239 if (val == COPY_ERROR)
3240 dev_info(&instance->pdev->dev, "application failed to "
3241 "copy Firmware crash dump\n");
3242 else
3243 dev_info(&instance->pdev->dev, "Firmware crash dump "
3244 "copied successfully\n");
3245 }
3246 return strlen(buf);
3247}
3248
3249static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003250fw_crash_state_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303251 struct device_attribute *attr, char *buf)
3252{
3253 struct Scsi_Host *shost = class_to_shost(cdev);
3254 struct megasas_instance *instance =
3255 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003256
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303257 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3258}
3259
3260static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003261page_size_show(struct device *cdev,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303262 struct device_attribute *attr, char *buf)
3263{
3264 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3265}
3266
Sumit Saxena308ec452016-01-28 21:04:30 +05303267static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003268ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
Sumit Saxena308ec452016-01-28 21:04:30 +05303269 char *buf)
3270{
3271 struct Scsi_Host *shost = class_to_shost(cdev);
3272 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3273
3274 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3275}
3276
Shivasharan S88d155c2018-01-05 05:27:46 -08003277static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003278fw_cmds_outstanding_show(struct device *cdev,
Shivasharan S88d155c2018-01-05 05:27:46 -08003279 struct device_attribute *attr, char *buf)
3280{
3281 struct Scsi_Host *shost = class_to_shost(cdev);
3282 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3283
3284 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3285}
3286
Shivasharan Scfb9a302019-05-07 10:05:40 -07003287static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003288dump_system_regs_show(struct device *cdev,
Shivasharan Scfb9a302019-05-07 10:05:40 -07003289 struct device_attribute *attr, char *buf)
3290{
3291 struct Scsi_Host *shost = class_to_shost(cdev);
3292 struct megasas_instance *instance =
3293 (struct megasas_instance *)shost->hostdata;
3294
3295 return megasas_dump_sys_regs(instance->reg_set, buf);
3296}
3297
Shivasharan Sa6024a92019-05-07 10:05:43 -07003298static ssize_t
Tomas Henzld6354682019-05-29 18:00:41 +02003299raid_map_id_show(struct device *cdev, struct device_attribute *attr,
Shivasharan Sa6024a92019-05-07 10:05:43 -07003300 char *buf)
3301{
3302 struct Scsi_Host *shost = class_to_shost(cdev);
3303 struct megasas_instance *instance =
3304 (struct megasas_instance *)shost->hostdata;
3305
3306 return snprintf(buf, PAGE_SIZE, "%ld\n",
3307 (unsigned long)instance->map_id);
3308}
3309
Tomas Henzld6354682019-05-29 18:00:41 +02003310static DEVICE_ATTR_RW(fw_crash_buffer);
3311static DEVICE_ATTR_RO(fw_crash_buffer_size);
3312static DEVICE_ATTR_RW(fw_crash_state);
3313static DEVICE_ATTR_RO(page_size);
3314static DEVICE_ATTR_RO(ldio_outstanding);
3315static DEVICE_ATTR_RO(fw_cmds_outstanding);
3316static DEVICE_ATTR_RO(dump_system_regs);
3317static DEVICE_ATTR_RO(raid_map_id);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303318
YueHaibing6764f512019-07-02 21:01:14 +08003319static struct device_attribute *megaraid_host_attrs[] = {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303320 &dev_attr_fw_crash_buffer_size,
3321 &dev_attr_fw_crash_buffer,
3322 &dev_attr_fw_crash_state,
3323 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303324 &dev_attr_ldio_outstanding,
Shivasharan S88d155c2018-01-05 05:27:46 -08003325 &dev_attr_fw_cmds_outstanding,
Shivasharan Scfb9a302019-05-07 10:05:40 -07003326 &dev_attr_dump_system_regs,
Shivasharan Sa6024a92019-05-07 10:05:43 -07003327 &dev_attr_raid_map_id,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303328 NULL,
3329};
3330
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003331/*
3332 * Scsi host template for megaraid_sas driver
3333 */
3334static struct scsi_host_template megasas_template = {
3335
3336 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303337 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003338 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003339 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003340 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303341 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003342 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003343 .eh_target_reset_handler = megasas_reset_target,
3344 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003345 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003346 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303347 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003348 .bios_param = megasas_bios_param,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003349 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwig07d9aa12019-06-17 14:20:00 +02003350 .max_segment_size = 0xffffffff,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003351 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003352};
3353
3354/**
3355 * megasas_complete_int_cmd - Completes an internal command
3356 * @instance: Adapter soft state
3357 * @cmd: Command to be completed
3358 *
3359 * The megasas_issue_blocked_cmd() function waits for a command to complete
3360 * after it issues a command. This function wakes up that waiting routine by
3361 * calling wake_up() on the wait queue.
3362 */
3363static void
3364megasas_complete_int_cmd(struct megasas_instance *instance,
3365 struct megasas_cmd *cmd)
3366{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303367 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003368 wake_up(&instance->int_cmd_wait_q);
3369}
3370
3371/**
3372 * megasas_complete_abort - Completes aborting a command
3373 * @instance: Adapter soft state
3374 * @cmd: Cmd that was issued to abort another cmd
3375 *
adam radford0d490162010-12-14 19:17:17 -08003376 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3377 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003378 * wakes up all functions waiting on the same wait queue.
3379 */
3380static void
3381megasas_complete_abort(struct megasas_instance *instance,
3382 struct megasas_cmd *cmd)
3383{
3384 if (cmd->sync_cmd) {
3385 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303386 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003387 wake_up(&instance->abort_cmd_wait_q);
3388 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003389}
3390
3391/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003392 * megasas_complete_cmd - Completes a command
3393 * @instance: Adapter soft state
3394 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003395 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003396 * SCSI mid-layer instead of the value returned
3397 * by the FW. This should be used if caller wants
3398 * an alternate status (as in the case of aborted
3399 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003400 */
adam radford9c915a82010-12-21 13:34:31 -08003401void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003402megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3403 u8 alt_status)
3404{
3405 int exception = 0;
3406 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003407 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003408 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303409 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003410
bo yang39a98552010-09-22 22:36:29 -04003411 /* flag for the retry reset */
3412 cmd->retry_for_fw_reset = 0;
3413
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003414 if (cmd->scmd)
3415 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003416
3417 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003418 case MFI_CMD_INVALID:
3419 /* Some older 1068 controller FW may keep a pended
3420 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3421 when booting the kdump kernel. Ignore this command to
3422 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003423 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3424 "completed\n");
3425 dev_warn(&instance->pdev->dev, "If you have a controller "
3426 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003427 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003428 case MFI_CMD_PD_SCSI_IO:
3429 case MFI_CMD_LD_SCSI_IO:
3430
3431 /*
3432 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3433 * issued either through an IO path or an IOCTL path. If it
3434 * was via IOCTL, we will send it to internal completion.
3435 */
3436 if (cmd->sync_cmd) {
3437 cmd->sync_cmd = 0;
3438 megasas_complete_int_cmd(instance, cmd);
3439 break;
3440 }
Gustavo A. R. Silva8d07f7d2018-11-27 22:32:27 -06003441 /* fall through */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003442
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003443 case MFI_CMD_LD_READ:
3444 case MFI_CMD_LD_WRITE:
3445
3446 if (alt_status) {
3447 cmd->scmd->result = alt_status << 16;
3448 exception = 1;
3449 }
3450
3451 if (exception) {
3452
Sumant Patroe4a082c2006-05-30 12:03:37 -07003453 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003454
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003455 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003456 cmd->scmd->scsi_done(cmd->scmd);
3457 megasas_return_cmd(instance, cmd);
3458
3459 break;
3460 }
3461
3462 switch (hdr->cmd_status) {
3463
3464 case MFI_STAT_OK:
3465 cmd->scmd->result = DID_OK << 16;
3466 break;
3467
3468 case MFI_STAT_SCSI_IO_FAILED:
3469 case MFI_STAT_LD_INIT_IN_PROGRESS:
3470 cmd->scmd->result =
3471 (DID_ERROR << 16) | hdr->scsi_status;
3472 break;
3473
3474 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3475
3476 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3477
3478 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3479 memset(cmd->scmd->sense_buffer, 0,
3480 SCSI_SENSE_BUFFERSIZE);
3481 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3482 hdr->sense_len);
3483
3484 cmd->scmd->result |= DRIVER_SENSE << 24;
3485 }
3486
3487 break;
3488
3489 case MFI_STAT_LD_OFFLINE:
3490 case MFI_STAT_DEVICE_NOT_FOUND:
3491 cmd->scmd->result = DID_BAD_TARGET << 16;
3492 break;
3493
3494 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003495 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003496 hdr->cmd_status);
3497 cmd->scmd->result = DID_ERROR << 16;
3498 break;
3499 }
3500
Sumant Patroe4a082c2006-05-30 12:03:37 -07003501 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003502
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003503 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003504 cmd->scmd->scsi_done(cmd->scmd);
3505 megasas_return_cmd(instance, cmd);
3506
3507 break;
3508
3509 case MFI_CMD_SMP:
3510 case MFI_CMD_STP:
Shivasharan Sf870bcb2018-01-05 05:33:04 -08003511 case MFI_CMD_NVME:
Chandrakanth Patil58136852019-06-25 16:34:30 +05303512 case MFI_CMD_TOOLBOX:
Shivasharan S82add4e2017-10-19 02:49:02 -07003513 megasas_complete_int_cmd(instance, cmd);
3514 break;
3515
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003516 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303517 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003518 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303519 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3520 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303521 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003522 spin_lock_irqsave(instance->host->host_lock, flags);
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003523 status = cmd->frame->hdr.cmd_status;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303524 instance->map_update_cmd = NULL;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003525 if (status != MFI_STAT_OK) {
3526 if (status != MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003527 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003528 cmd->frame->hdr.cmd_status);
3529 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303530 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003531 spin_unlock_irqrestore(
3532 instance->host->host_lock,
3533 flags);
3534 break;
3535 }
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003536 }
3537
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303538 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303539
3540 /*
3541 * Set fast path IO to ZERO.
3542 * Validate Map will set proper value.
3543 * Meanwhile all IOs will go as LD IO.
3544 */
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003545 if (status == MFI_STAT_OK &&
3546 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3547 instance->map_id++;
adam radford9c915a82010-12-21 13:34:31 -08003548 fusion->fast_path_io = 1;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003549 } else {
adam radford9c915a82010-12-21 13:34:31 -08003550 fusion->fast_path_io = 0;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003551 }
3552
adam radford9c915a82010-12-21 13:34:31 -08003553 megasas_sync_map_info(instance);
3554 spin_unlock_irqrestore(instance->host->host_lock,
3555 flags);
3556 break;
3557 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303558 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3559 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003560 spin_lock_irqsave(&poll_aen_lock, flags);
3561 megasas_poll_wait_aen = 0;
3562 spin_unlock_irqrestore(&poll_aen_lock, flags);
3563 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003564
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303565 /* FW has an updated PD sequence */
3566 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3567 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3568
3569 spin_lock_irqsave(instance->host->host_lock, flags);
3570 status = cmd->frame->hdr.cmd_status;
3571 instance->jbod_seq_cmd = NULL;
3572 megasas_return_cmd(instance, cmd);
3573
3574 if (status == MFI_STAT_OK) {
3575 instance->pd_seq_map_id++;
3576 /* Re-register a pd sync seq num cmd */
3577 if (megasas_sync_pd_seq_num(instance, true))
3578 instance->use_seqnum_jbod_fp = false;
3579 } else
3580 instance->use_seqnum_jbod_fp = false;
3581
3582 spin_unlock_irqrestore(instance->host->host_lock, flags);
3583 break;
3584 }
3585
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003586 /*
3587 * See if got an event notification
3588 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303589 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003590 megasas_service_aen(instance, cmd);
3591 else
3592 megasas_complete_int_cmd(instance, cmd);
3593
3594 break;
3595
3596 case MFI_CMD_ABORT:
3597 /*
3598 * Cmd issued to abort another cmd returned
3599 */
3600 megasas_complete_abort(instance, cmd);
3601 break;
3602
3603 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003604 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003605 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003606 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003607 break;
3608 }
3609}
3610
3611/**
bo yang39a98552010-09-22 22:36:29 -04003612 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003613 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003614 * @instance: Adapter soft state
3615 */
3616static inline void
3617megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3618{
3619 struct megasas_cmd *cmd;
3620 struct list_head clist_local;
3621 union megasas_evt_class_locale class_locale;
3622 unsigned long flags;
3623 u32 seq_num;
3624
3625 INIT_LIST_HEAD(&clist_local);
3626 spin_lock_irqsave(&instance->hba_lock, flags);
3627 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3628 spin_unlock_irqrestore(&instance->hba_lock, flags);
3629
3630 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003631 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003632 struct megasas_cmd, list);
3633 list_del_init(&cmd->list);
3634
3635 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003636 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3637 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003638 cmd, cmd->scmd, cmd->sync_cmd);
3639
3640 cmd->retry_for_fw_reset++;
3641
3642 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003643 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003644 "was tried multiple times during reset."
3645 "Shutting down the HBA\n",
3646 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303647 instance->instancet->disable_intr(instance);
3648 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003649 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003650 return;
3651 }
3652 }
3653
3654 if (cmd->sync_cmd == 1) {
3655 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003656 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003657 "cmd attached to internal command!\n");
3658 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003659 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003660 "on the internal reset queue,"
3661 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303662 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003663 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003664 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003665 0, instance->reg_set);
3666 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003667 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003668 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003669 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003670
3671 atomic_inc(&instance->fw_outstanding);
3672 instance->instancet->fire_cmd(instance,
3673 cmd->frame_phys_addr,
3674 cmd->frame_count-1, instance->reg_set);
3675 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003676 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003677 "internal reset defer list while re-issue!!\n",
3678 cmd);
3679 }
3680 }
3681
3682 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003683 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003684 megasas_return_cmd(instance, instance->aen_cmd);
3685
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003686 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003687 }
3688
3689 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003690 * Initiate AEN (Asynchronous Event Notification)
3691 */
bo yang39a98552010-09-22 22:36:29 -04003692 seq_num = instance->last_seq_num;
3693 class_locale.members.reserved = 0;
3694 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3695 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3696
3697 megasas_register_aen(instance, seq_num, class_locale.word);
3698}
3699
3700/**
3701 * Move the internal reset pending commands to a deferred queue.
3702 *
3703 * We move the commands pending at internal reset time to a
3704 * pending queue. This queue would be flushed after successful
3705 * completion of the internal reset sequence. if the internal reset
3706 * did not complete in time, the kernel reset handler would flush
3707 * these commands.
3708 **/
3709static void
3710megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3711{
3712 struct megasas_cmd *cmd;
3713 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003714 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003715 u32 defer_index;
3716 unsigned long flags;
3717
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003718 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303719 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003720 for (i = 0; i < max_cmd; i++) {
3721 cmd = instance->cmd_list[i];
3722 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003723 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003724 "on the defer queue as internal\n",
3725 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3726
3727 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003728 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003729 " moving this cmd:%p, %d %p, it was"
3730 "discovered on some list?\n",
3731 cmd, cmd->sync_cmd, cmd->scmd);
3732
3733 list_del_init(&cmd->list);
3734 }
3735 defer_index++;
3736 list_add_tail(&cmd->list,
3737 &instance->internal_reset_pending_q);
3738 }
3739 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303740 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003741}
3742
3743
3744static void
3745process_fw_state_change_wq(struct work_struct *work)
3746{
3747 struct megasas_instance *instance =
3748 container_of(work, struct megasas_instance, work_init);
3749 u32 wait;
3750 unsigned long flags;
3751
Sumit Saxena8a01a412016-01-28 21:04:32 +05303752 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003753 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303754 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003755 return ;
3756 }
3757
Sumit Saxena8a01a412016-01-28 21:04:32 +05303758 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003759 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003760 "state, restarting it...\n");
3761
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303762 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003763 atomic_set(&instance->fw_outstanding, 0);
3764
3765 atomic_set(&instance->fw_reset_no_pci_access, 1);
3766 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003767 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003768
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003769 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003770 "initiating next stage...\n");
3771
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003772 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003773 "state 2 starting...\n");
3774
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003775 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003776 for (wait = 0; wait < 30; wait++) {
3777 msleep(1000);
3778 }
3779
adam radford058a8fa2011-10-08 18:14:27 -07003780 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003781 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003782
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303783 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003784 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003785 return ;
3786 }
3787
3788 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3789 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3790 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3791 ) {
3792 *instance->consumer = *instance->producer;
3793 } else {
3794 *instance->consumer = 0;
3795 *instance->producer = 0;
3796 }
3797
3798 megasas_issue_init_mfi(instance);
3799
3800 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303801 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003802 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303803 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003804
3805 megasas_issue_pending_cmds_again(instance);
3806 instance->issuepend_done = 1;
3807 }
bo yang39a98552010-09-22 22:36:29 -04003808}
3809
3810/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003811 * megasas_deplete_reply_queue - Processes all completed commands
3812 * @instance: Adapter soft state
3813 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003814 * SCSI mid-layer instead of the status
3815 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003816 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003817 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003818static int
bo yang39a98552010-09-22 22:36:29 -04003819megasas_deplete_reply_queue(struct megasas_instance *instance,
3820 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003821{
bo yang39a98552010-09-22 22:36:29 -04003822 u32 mfiStatus;
3823 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003824
bo yang39a98552010-09-22 22:36:29 -04003825 if ((mfiStatus = instance->instancet->check_reset(instance,
3826 instance->reg_set)) == 1) {
3827 return IRQ_HANDLED;
3828 }
3829
Shivasharan Sde516372018-12-17 00:47:39 -08003830 mfiStatus = instance->instancet->clear_intr(instance);
3831 if (mfiStatus == 0) {
adam radforde1419192011-02-24 20:56:21 -08003832 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003833 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003834 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003835 }
3836
3837 instance->mfiStatus = mfiStatus;
3838
3839 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3840 fw_state = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08003841 instance) & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003842
3843 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003844 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003845 fw_state);
3846 }
3847
3848 if ((fw_state == MFI_STATE_FAULT) &&
3849 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003850 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003851
3852 if ((instance->pdev->device ==
3853 PCI_DEVICE_ID_LSI_SAS1064R) ||
3854 (instance->pdev->device ==
3855 PCI_DEVICE_ID_DELL_PERC5) ||
3856 (instance->pdev->device ==
3857 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3858
3859 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303860 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003861 }
3862
3863
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303864 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303865 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003866 instance->issuepend_done = 0;
3867
3868 atomic_set(&instance->fw_outstanding, 0);
3869 megasas_internal_reset_defer_cmds(instance);
3870
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003871 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303872 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003873
3874 schedule_work(&instance->work_init);
3875 return IRQ_HANDLED;
3876
3877 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003878 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003879 fw_state, instance->disableOnlineCtrlReset);
3880 }
3881 }
3882
Sumant Patro5d018ad2006-10-03 13:13:18 -07003883 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003884 return IRQ_HANDLED;
3885}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003886/**
3887 * megasas_isr - isr entry point
3888 */
David Howells7d12e782006-10-05 14:55:46 +01003889static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003890{
adam radfordc8e858f2011-10-08 18:15:13 -07003891 struct megasas_irq_context *irq_context = devp;
3892 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003893 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003894 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003895
adam radfordc8e858f2011-10-08 18:15:13 -07003896 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003897 return IRQ_HANDLED;
3898
bo yang39a98552010-09-22 22:36:29 -04003899 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003900 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003901 spin_unlock_irqrestore(&instance->hba_lock, flags);
3902
3903 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003904}
3905
3906/**
3907 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003908 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003909 *
3910 * During the initialization, FW passes can potentially be in any one of
3911 * several possible states. If the FW in operational, waiting-for-handshake
3912 * states, driver must take steps to bring it to ready state. Otherwise, it
3913 * has to wait for the ready state.
3914 */
adam radford9c915a82010-12-21 13:34:31 -08003915int
adam radford058a8fa2011-10-08 18:14:27 -07003916megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003917{
3918 int i;
3919 u8 max_wait;
3920 u32 fw_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003921 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922
Shivasharan Sde516372018-12-17 00:47:39 -08003923 abs_state = instance->instancet->read_fw_status_reg(instance);
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003924 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925
Sumant Patroe3bbff92006-10-03 12:28:49 -07003926 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003927 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003928 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003929
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003930 while (fw_state != MFI_STATE_READY) {
3931
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003932 switch (fw_state) {
3933
3934 case MFI_STATE_FAULT:
Shivasharan Sb6661342019-05-07 10:05:44 -07003935 dev_printk(KERN_ERR, &instance->pdev->dev,
3936 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
3937 abs_state & MFI_STATE_FAULT_CODE,
3938 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
adam radford058a8fa2011-10-08 18:14:27 -07003939 if (ocr) {
3940 max_wait = MEGASAS_RESET_WAIT_TIME;
adam radford058a8fa2011-10-08 18:14:27 -07003941 break;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003942 } else {
3943 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
3944 megasas_dump_reg_set(instance->reg_set);
adam radford058a8fa2011-10-08 18:14:27 -07003945 return -ENODEV;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003946 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003947
3948 case MFI_STATE_WAIT_HANDSHAKE:
3949 /*
3950 * Set the CLR bit in inbound doorbell
3951 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003952 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003953 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3954 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003955 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003956 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003957 writel(
3958 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003959 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303960 else
Yang, Bo87911122009-10-06 14:31:54 -06003961 writel(
3962 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3963 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003964
Yang, Bo7218df62009-10-06 14:52:20 -06003965 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003966 break;
3967
Sumant Patroe3bbff92006-10-03 12:28:49 -07003968 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003969 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003970 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3971 (instance->pdev->device ==
3972 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003973 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003974 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003975 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303976 else
Yang, Bo87911122009-10-06 14:31:54 -06003977 writel(MFI_INIT_HOTPLUG,
3978 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003979
Yang, Bo7218df62009-10-06 14:52:20 -06003980 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003981 break;
3982
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003983 case MFI_STATE_OPERATIONAL:
3984 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003985 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003986 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303987 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003988 if ((instance->pdev->device ==
3989 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3990 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003991 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003992 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003993 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003994 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303995
Shivasharan Se7d36b82017-10-19 02:48:50 -07003996 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003997 for (i = 0; i < (10 * 1000); i += 20) {
Shivasharan S272652f2018-12-17 00:47:40 -08003998 if (megasas_readl(
3999 instance,
adam radford9c915a82010-12-21 13:34:31 -08004000 &instance->
4001 reg_set->
4002 doorbell) & 1)
4003 msleep(20);
4004 else
4005 break;
4006 }
4007 }
Yang, Bo87911122009-10-06 14:31:54 -06004008 } else
4009 writel(MFI_RESET_FLAGS,
4010 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004011
Yang, Bo7218df62009-10-06 14:52:20 -06004012 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004013 break;
4014
4015 case MFI_STATE_UNDEFINED:
4016 /*
4017 * This state should not last for more than 2 seconds
4018 */
Yang, Bo7218df62009-10-06 14:52:20 -06004019 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004020 break;
4021
4022 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06004023 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004024 break;
4025
4026 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06004027 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004028 break;
4029
4030 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06004031 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004032 break;
4033
4034 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06004035 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004036 break;
4037
4038 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06004039 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004040 break;
4041
4042 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004043 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004044 fw_state);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004045 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4046 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004047 return -ENODEV;
4048 }
4049
4050 /*
4051 * The cur_state should not last for more than max_wait secs
4052 */
Steve Sistare31b6a052019-03-01 06:46:28 -08004053 for (i = 0; i < max_wait * 50; i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004054 curr_abs_state = instance->instancet->
Shivasharan Sde516372018-12-17 00:47:39 -08004055 read_fw_status_reg(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004056
Yang, Bo7218df62009-10-06 14:52:20 -06004057 if (abs_state == curr_abs_state) {
Steve Sistare31b6a052019-03-01 06:46:28 -08004058 msleep(20);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004059 } else
4060 break;
4061 }
4062
4063 /*
4064 * Return error if fw_state hasn't changed after max_wait
4065 */
Yang, Bo7218df62009-10-06 14:52:20 -06004066 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004067 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004068 "in %d secs\n", fw_state, max_wait);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004069 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4070 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004071 return -ENODEV;
4072 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004073
4074 abs_state = curr_abs_state;
4075 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04004076 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004077 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004078
4079 return 0;
4080}
4081
4082/**
4083 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4084 * @instance: Adapter soft state
4085 */
4086static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4087{
4088 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004089 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004090 struct megasas_cmd *cmd;
4091
4092 if (!instance->frame_dma_pool)
4093 return;
4094
4095 /*
4096 * Return all frames to pool
4097 */
4098 for (i = 0; i < max_cmd; i++) {
4099
4100 cmd = instance->cmd_list[i];
4101
4102 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02004103 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004104 cmd->frame_phys_addr);
4105
4106 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02004107 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004108 cmd->sense_phys_addr);
4109 }
4110
4111 /*
4112 * Now destroy the pool itself
4113 */
Romain Perierfc69d862017-07-06 10:13:06 +02004114 dma_pool_destroy(instance->frame_dma_pool);
4115 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004116
4117 instance->frame_dma_pool = NULL;
4118 instance->sense_dma_pool = NULL;
4119}
4120
4121/**
4122 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4123 * @instance: Adapter soft state
4124 *
4125 * Each command packet has an embedded DMA memory buffer that is used for
4126 * filling MFI frame and the SG list that immediately follows the frame. This
4127 * function creates those DMA memory buffers for each command packet by using
4128 * PCI pool facility.
4129 */
4130static int megasas_create_frame_pool(struct megasas_instance *instance)
4131{
4132 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004133 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004134 u32 frame_count;
4135 struct megasas_cmd *cmd;
4136
adam radford9c915a82010-12-21 13:34:31 -08004137 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004138
4139 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05304140 * For MFI controllers.
4141 * max_num_sge = 60
4142 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4143 * Total 960 byte (15 MFI frame of 64 byte)
4144 *
4145 * Fusion adapter require only 3 extra frame.
4146 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4147 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4148 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004149 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07004150 frame_count = (instance->adapter_type == MFI_SERIES) ?
4151 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08004152 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004153 /*
4154 * Use DMA pool facility provided by PCI layer
4155 */
Romain Perierfc69d862017-07-06 10:13:06 +02004156 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4157 &instance->pdev->dev,
4158 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004159
4160 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004161 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004162 return -ENOMEM;
4163 }
4164
Romain Perierfc69d862017-07-06 10:13:06 +02004165 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4166 &instance->pdev->dev, 128,
4167 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004168
4169 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004170 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004171
Romain Perierfc69d862017-07-06 10:13:06 +02004172 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004173 instance->frame_dma_pool = NULL;
4174
4175 return -ENOMEM;
4176 }
4177
4178 /*
4179 * Allocate and attach a frame to each of the commands in cmd_list.
4180 * By making cmd->index as the context instead of the &cmd, we can
4181 * always use 32bit context regardless of the architecture
4182 */
4183 for (i = 0; i < max_cmd; i++) {
4184
4185 cmd = instance->cmd_list[i];
4186
Souptick Joarder61b142a2018-02-15 21:55:06 +05304187 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004188 GFP_KERNEL, &cmd->frame_phys_addr);
4189
Romain Perierfc69d862017-07-06 10:13:06 +02004190 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004191 GFP_KERNEL, &cmd->sense_phys_addr);
4192
4193 /*
4194 * megasas_teardown_frame_pool() takes care of freeing
4195 * whatever has been allocated
4196 */
4197 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004198 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004199 megasas_teardown_frame_pool(instance);
4200 return -ENOMEM;
4201 }
4202
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304203 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004204 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004205 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004206 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004207 }
4208
4209 return 0;
4210}
4211
4212/**
4213 * megasas_free_cmds - Free all the cmds in the free cmd pool
4214 * @instance: Adapter soft state
4215 */
adam radford9c915a82010-12-21 13:34:31 -08004216void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004217{
4218 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004219
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004220 /* First free the MFI frame pool */
4221 megasas_teardown_frame_pool(instance);
4222
4223 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004224 for (i = 0; i < instance->max_mfi_cmds; i++)
4225
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004226 kfree(instance->cmd_list[i]);
4227
4228 /* Free the cmd_list buffer itself */
4229 kfree(instance->cmd_list);
4230 instance->cmd_list = NULL;
4231
4232 INIT_LIST_HEAD(&instance->cmd_pool);
4233}
4234
4235/**
4236 * megasas_alloc_cmds - Allocates the command packets
4237 * @instance: Adapter soft state
4238 *
4239 * Each command that is issued to the FW, whether IO commands from the OS or
4240 * internal commands like IOCTLs, are wrapped in local data structure called
4241 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4242 * the FW.
4243 *
4244 * Each frame has a 32-bit field called context (tag). This context is used
4245 * to get back the megasas_cmd from the frame when a frame gets completed in
4246 * the ISR. Typically the address of the megasas_cmd itself would be used as
4247 * the context. But we wanted to keep the differences between 32 and 64 bit
4248 * systems to the mininum. We always use 32 bit integers for the context. In
4249 * this driver, the 32 bit values are the indices into an array cmd_list.
4250 * This array is used only to look up the megasas_cmd given the context. The
4251 * free commands themselves are maintained in a linked list called cmd_pool.
4252 */
adam radford9c915a82010-12-21 13:34:31 -08004253int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004254{
4255 int i;
4256 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004257 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004258 struct megasas_cmd *cmd;
4259
adam radford9c915a82010-12-21 13:34:31 -08004260 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004261
4262 /*
4263 * instance->cmd_list is an array of struct megasas_cmd pointers.
4264 * Allocate the dynamic array first and then allocate individual
4265 * commands.
4266 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004267 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004268
4269 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004270 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004271 return -ENOMEM;
4272 }
4273
adam radford9c915a82010-12-21 13:34:31 -08004274 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004275
4276 for (i = 0; i < max_cmd; i++) {
4277 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4278 GFP_KERNEL);
4279
4280 if (!instance->cmd_list[i]) {
4281
4282 for (j = 0; j < i; j++)
4283 kfree(instance->cmd_list[j]);
4284
4285 kfree(instance->cmd_list);
4286 instance->cmd_list = NULL;
4287
4288 return -ENOMEM;
4289 }
4290 }
4291
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004292 for (i = 0; i < max_cmd; i++) {
4293 cmd = instance->cmd_list[i];
4294 memset(cmd, 0, sizeof(struct megasas_cmd));
4295 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004296 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004297 cmd->instance = instance;
4298
4299 list_add_tail(&cmd->list, &instance->cmd_pool);
4300 }
4301
4302 /*
4303 * Create a frame pool and assign one frame to each cmd
4304 */
4305 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004306 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004307 megasas_free_cmds(instance);
Jason Yanbcf3b672019-02-15 19:50:27 +08004308 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004309 }
4310
4311 return 0;
4312}
4313
Yang, Bo81e403c2009-10-06 14:27:54 -06004314/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304315 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4316 * @instance: Adapter soft state
4317 *
4318 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4319 * or FW is not under OCR.
4320 */
4321inline int
4322dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4323
Shivasharan Se7d36b82017-10-19 02:48:50 -07004324 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304325 return KILL_ADAPTER;
4326 else if (instance->unload ||
4327 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4328 return IGNORE_TIMEOUT;
4329 else
4330 return INITIATE_OCR;
4331}
4332
Shivasharan S15dd0382017-02-10 00:59:10 -08004333static void
4334megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304335{
4336 int ret;
4337 struct megasas_cmd *cmd;
4338 struct megasas_dcmd_frame *dcmd;
4339
Shivasharan S15dd0382017-02-10 00:59:10 -08004340 struct MR_PRIV_DEVICE *mr_device_priv_data;
4341 u16 device_id = 0;
4342
4343 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304344 cmd = megasas_get_cmd(instance);
4345
4346 if (!cmd) {
4347 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004348 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304349 }
4350
4351 dcmd = &cmd->frame->dcmd;
4352
4353 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4354 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4355
4356 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4357 dcmd->cmd = MFI_CMD_DCMD;
4358 dcmd->cmd_status = 0xFF;
4359 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004360 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304361 dcmd->timeout = 0;
4362 dcmd->pad_0 = 0;
4363 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4364 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304365
Shivasharan S107a60d2017-10-19 02:49:05 -07004366 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4367 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304368
Shivasharan Se7d36b82017-10-19 02:48:50 -07004369 if ((instance->adapter_type != MFI_SERIES) &&
4370 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304371 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4372 else
4373 ret = megasas_issue_polled(instance, cmd);
4374
4375 switch (ret) {
4376 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004377 mr_device_priv_data = sdev->hostdata;
4378 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4379 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304380 instance->pd_info->state.ddf.pdType.intf;
4381 break;
4382
4383 case DCMD_TIMEOUT:
4384
4385 switch (dcmd_timeout_ocr_possible(instance)) {
4386 case INITIATE_OCR:
4387 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304388 mutex_unlock(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05304389 megasas_reset_fusion(instance->host,
4390 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304391 mutex_lock(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05304392 break;
4393 case KILL_ADAPTER:
4394 megaraid_sas_kill_hba(instance);
4395 break;
4396 case IGNORE_TIMEOUT:
4397 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4398 __func__, __LINE__);
4399 break;
4400 }
4401
4402 break;
4403 }
4404
4405 if (ret != DCMD_TIMEOUT)
4406 megasas_return_cmd(instance, cmd);
4407
Shivasharan S15dd0382017-02-10 00:59:10 -08004408 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304409}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304410/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004411 * megasas_get_pd_list_info - Returns FW's pd_list structure
4412 * @instance: Adapter soft state
4413 * @pd_list: pd_list structure
4414 *
4415 * Issues an internal command (DCMD) to get the FW's controller PD
4416 * list structure. This information is mainly used to find out SYSTEM
4417 * supported by the FW.
4418 */
4419static int
4420megasas_get_pd_list(struct megasas_instance *instance)
4421{
4422 int ret = 0, pd_index = 0;
4423 struct megasas_cmd *cmd;
4424 struct megasas_dcmd_frame *dcmd;
4425 struct MR_PD_LIST *ci;
4426 struct MR_PD_ADDRESS *pd_addr;
Yang, Bo81e403c2009-10-06 14:27:54 -06004427
Sumit Saxenad9083162016-07-08 03:30:16 -07004428 if (instance->pd_list_not_supported) {
4429 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4430 "not supported by firmware\n");
4431 return ret;
4432 }
4433
Shivasharan S9b3d0282017-10-19 02:48:56 -07004434 ci = instance->pd_list_buf;
Shivasharan S9b3d0282017-10-19 02:48:56 -07004435
Yang, Bo81e403c2009-10-06 14:27:54 -06004436 cmd = megasas_get_cmd(instance);
4437
4438 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004439 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004440 return -ENOMEM;
4441 }
4442
4443 dcmd = &cmd->frame->dcmd;
4444
Yang, Bo81e403c2009-10-06 14:27:54 -06004445 memset(ci, 0, sizeof(*ci));
4446 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4447
4448 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4449 dcmd->mbox.b[1] = 0;
4450 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304451 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004452 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004453 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004454 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004455 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304456 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4457 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004458
Shivasharan S107a60d2017-10-19 02:49:05 -07004459 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4460 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004461
Shivasharan Se7d36b82017-10-19 02:48:50 -07004462 if ((instance->adapter_type != MFI_SERIES) &&
4463 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304464 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304465 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304466 else
4467 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004468
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304469 switch (ret) {
4470 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004471 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4472 "failed/not supported by firmware\n");
4473
Shivasharan Se7d36b82017-10-19 02:48:50 -07004474 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004475 megaraid_sas_kill_hba(instance);
4476 else
4477 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304478 break;
4479 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004480
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304481 switch (dcmd_timeout_ocr_possible(instance)) {
4482 case INITIATE_OCR:
4483 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4484 /*
4485 * DCMD failed from AEN path.
4486 * AEN path already hold reset_mutex to avoid PCI access
4487 * while OCR is in progress.
4488 */
4489 mutex_unlock(&instance->reset_mutex);
4490 megasas_reset_fusion(instance->host,
4491 MFI_IO_TIMEOUT_OCR);
4492 mutex_lock(&instance->reset_mutex);
4493 break;
4494 case KILL_ADAPTER:
4495 megaraid_sas_kill_hba(instance);
4496 break;
4497 case IGNORE_TIMEOUT:
4498 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4499 __func__, __LINE__);
4500 break;
4501 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004502
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304503 break;
4504
4505 case DCMD_SUCCESS:
4506 pd_addr = ci->addr;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004507 if (megasas_dbg_lvl & LD_PD_DEBUG)
4508 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4509 __func__, le32_to_cpu(ci->count));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304510
4511 if ((le32_to_cpu(ci->count) >
4512 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4513 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004514
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304515 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304516 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004517
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304518 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304519 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304520 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304521 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304522 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304523 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304524 MR_PD_STATE_SYSTEM;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004525 if (megasas_dbg_lvl & LD_PD_DEBUG)
4526 dev_info(&instance->pdev->dev,
4527 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4528 pd_index, le16_to_cpu(pd_addr->deviceId),
4529 pd_addr->scsiDevType);
Yang, Bo81e403c2009-10-06 14:27:54 -06004530 pd_addr++;
4531 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304532
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304533 memcpy(instance->pd_list, instance->local_pd_list,
4534 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304535 break;
4536
Yang, Bo81e403c2009-10-06 14:27:54 -06004537 }
4538
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304539 if (ret != DCMD_TIMEOUT)
4540 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004541
4542 return ret;
4543}
4544
Yang, Bobdc6fb82009-12-06 08:30:19 -07004545/*
4546 * megasas_get_ld_list_info - Returns FW's ld_list structure
4547 * @instance: Adapter soft state
4548 * @ld_list: ld_list structure
4549 *
4550 * Issues an internal command (DCMD) to get the FW's controller PD
4551 * list structure. This information is mainly used to find out SYSTEM
4552 * supported by the FW.
4553 */
4554static int
4555megasas_get_ld_list(struct megasas_instance *instance)
4556{
4557 int ret = 0, ld_index = 0, ids = 0;
4558 struct megasas_cmd *cmd;
4559 struct megasas_dcmd_frame *dcmd;
4560 struct MR_LD_LIST *ci;
4561 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304562 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004563
Shivasharan S9b3d0282017-10-19 02:48:56 -07004564 ci = instance->ld_list_buf;
4565 ci_h = instance->ld_list_buf_h;
4566
Yang, Bobdc6fb82009-12-06 08:30:19 -07004567 cmd = megasas_get_cmd(instance);
4568
4569 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004570 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004571 return -ENOMEM;
4572 }
4573
4574 dcmd = &cmd->frame->dcmd;
4575
Yang, Bobdc6fb82009-12-06 08:30:19 -07004576 memset(ci, 0, sizeof(*ci));
4577 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4578
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304579 if (instance->supportmax256vd)
4580 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004581 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304582 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004583 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004584 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004585 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304586 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4587 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004588 dcmd->pad_0 = 0;
4589
Shivasharan S107a60d2017-10-19 02:49:05 -07004590 megasas_set_dma_settings(instance, dcmd, ci_h,
4591 sizeof(struct MR_LD_LIST));
4592
Shivasharan Se7d36b82017-10-19 02:48:50 -07004593 if ((instance->adapter_type != MFI_SERIES) &&
4594 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304595 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304596 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304597 else
4598 ret = megasas_issue_polled(instance, cmd);
4599
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304600 ld_count = le32_to_cpu(ci->ldCount);
4601
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304602 switch (ret) {
4603 case DCMD_FAILED:
4604 megaraid_sas_kill_hba(instance);
4605 break;
4606 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004607
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304608 switch (dcmd_timeout_ocr_possible(instance)) {
4609 case INITIATE_OCR:
4610 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4611 /*
4612 * DCMD failed from AEN path.
4613 * AEN path already hold reset_mutex to avoid PCI access
4614 * while OCR is in progress.
4615 */
4616 mutex_unlock(&instance->reset_mutex);
4617 megasas_reset_fusion(instance->host,
4618 MFI_IO_TIMEOUT_OCR);
4619 mutex_lock(&instance->reset_mutex);
4620 break;
4621 case KILL_ADAPTER:
4622 megaraid_sas_kill_hba(instance);
4623 break;
4624 case IGNORE_TIMEOUT:
4625 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4626 __func__, __LINE__);
4627 break;
4628 }
4629
4630 break;
4631
4632 case DCMD_SUCCESS:
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004633 if (megasas_dbg_lvl & LD_PD_DEBUG)
4634 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4635 __func__, ld_count);
4636
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304637 if (ld_count > instance->fw_supported_vd_count)
4638 break;
4639
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304640 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004641
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304642 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004643 if (ci->ldList[ld_index].state != 0) {
4644 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304645 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004646 if (megasas_dbg_lvl & LD_PD_DEBUG)
4647 dev_info(&instance->pdev->dev,
4648 "LD%d: targetID: 0x%03x\n",
4649 ld_index, ids);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004650 }
4651 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304652
4653 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004654 }
4655
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304656 if (ret != DCMD_TIMEOUT)
4657 megasas_return_cmd(instance, cmd);
4658
Yang, Bobdc6fb82009-12-06 08:30:19 -07004659 return ret;
4660}
4661
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004662/**
adam radford21c9e162013-09-06 15:27:14 -07004663 * megasas_ld_list_query - Returns FW's ld_list structure
4664 * @instance: Adapter soft state
4665 * @ld_list: ld_list structure
4666 *
4667 * Issues an internal command (DCMD) to get the FW's controller PD
4668 * list structure. This information is mainly used to find out SYSTEM
4669 * supported by the FW.
4670 */
4671static int
4672megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4673{
4674 int ret = 0, ld_index = 0, ids = 0;
4675 struct megasas_cmd *cmd;
4676 struct megasas_dcmd_frame *dcmd;
4677 struct MR_LD_TARGETID_LIST *ci;
4678 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304679 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004680
Shivasharan S9b3d0282017-10-19 02:48:56 -07004681 ci = instance->ld_targetid_list_buf;
4682 ci_h = instance->ld_targetid_list_buf_h;
4683
adam radford21c9e162013-09-06 15:27:14 -07004684 cmd = megasas_get_cmd(instance);
4685
4686 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004687 dev_warn(&instance->pdev->dev,
4688 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004689 return -ENOMEM;
4690 }
4691
4692 dcmd = &cmd->frame->dcmd;
4693
adam radford21c9e162013-09-06 15:27:14 -07004694 memset(ci, 0, sizeof(*ci));
4695 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4696
4697 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304698 if (instance->supportmax256vd)
4699 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004700
4701 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304702 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004703 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004704 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004705 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304706 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4707 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004708 dcmd->pad_0 = 0;
4709
Shivasharan S107a60d2017-10-19 02:49:05 -07004710 megasas_set_dma_settings(instance, dcmd, ci_h,
4711 sizeof(struct MR_LD_TARGETID_LIST));
4712
Shivasharan Se7d36b82017-10-19 02:48:50 -07004713 if ((instance->adapter_type != MFI_SERIES) &&
4714 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304715 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304716 else
4717 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004718
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304719 switch (ret) {
4720 case DCMD_FAILED:
4721 dev_info(&instance->pdev->dev,
4722 "DCMD not supported by firmware - %s %d\n",
4723 __func__, __LINE__);
4724 ret = megasas_get_ld_list(instance);
4725 break;
4726 case DCMD_TIMEOUT:
4727 switch (dcmd_timeout_ocr_possible(instance)) {
4728 case INITIATE_OCR:
4729 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4730 /*
4731 * DCMD failed from AEN path.
4732 * AEN path already hold reset_mutex to avoid PCI access
4733 * while OCR is in progress.
4734 */
4735 mutex_unlock(&instance->reset_mutex);
4736 megasas_reset_fusion(instance->host,
4737 MFI_IO_TIMEOUT_OCR);
4738 mutex_lock(&instance->reset_mutex);
4739 break;
4740 case KILL_ADAPTER:
4741 megaraid_sas_kill_hba(instance);
4742 break;
4743 case IGNORE_TIMEOUT:
4744 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4745 __func__, __LINE__);
4746 break;
4747 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304748
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304749 break;
4750 case DCMD_SUCCESS:
4751 tgtid_count = le32_to_cpu(ci->count);
4752
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004753 if (megasas_dbg_lvl & LD_PD_DEBUG)
4754 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4755 __func__, tgtid_count);
4756
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304757 if ((tgtid_count > (instance->fw_supported_vd_count)))
4758 break;
4759
adam radford21c9e162013-09-06 15:27:14 -07004760 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304761 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004762 ids = ci->targetId[ld_index];
4763 instance->ld_ids[ids] = ci->targetId[ld_index];
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004764 if (megasas_dbg_lvl & LD_PD_DEBUG)
4765 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4766 ld_index, ci->targetId[ld_index]);
adam radford21c9e162013-09-06 15:27:14 -07004767 }
4768
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304769 break;
adam radford21c9e162013-09-06 15:27:14 -07004770 }
4771
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304772 if (ret != DCMD_TIMEOUT)
4773 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004774
4775 return ret;
4776}
4777
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004778/**
4779 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4780 * dcmd.mbox - reserved
4781 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4782 * Desc: This DCMD will return the combined device list
4783 * Status: MFI_STAT_OK - List returned successfully
4784 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4785 * disabled
4786 * @instance: Adapter soft state
4787 * @is_probe: Driver probe check
4788 * Return: 0 if DCMD succeeded
4789 * non-zero if failed
4790 */
YueHaibing7c3f8ca2019-04-13 22:21:38 +08004791static int
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004792megasas_host_device_list_query(struct megasas_instance *instance,
4793 bool is_probe)
4794{
4795 int ret, i, target_id;
4796 struct megasas_cmd *cmd;
4797 struct megasas_dcmd_frame *dcmd;
4798 struct MR_HOST_DEVICE_LIST *ci;
4799 u32 count;
4800 dma_addr_t ci_h;
4801
4802 ci = instance->host_device_list_buf;
4803 ci_h = instance->host_device_list_buf_h;
4804
4805 cmd = megasas_get_cmd(instance);
4806
4807 if (!cmd) {
4808 dev_warn(&instance->pdev->dev,
4809 "%s: failed to get cmd\n",
4810 __func__);
4811 return -ENOMEM;
4812 }
4813
4814 dcmd = &cmd->frame->dcmd;
4815
4816 memset(ci, 0, sizeof(*ci));
4817 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4818
4819 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4820 dcmd->cmd = MFI_CMD_DCMD;
4821 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4822 dcmd->sge_count = 1;
4823 dcmd->flags = MFI_FRAME_DIR_READ;
4824 dcmd->timeout = 0;
4825 dcmd->pad_0 = 0;
4826 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4827 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4828
4829 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4830
4831 if (!instance->mask_interrupts) {
4832 ret = megasas_issue_blocked_cmd(instance, cmd,
4833 MFI_IO_TIMEOUT_SECS);
4834 } else {
4835 ret = megasas_issue_polled(instance, cmd);
4836 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4837 }
4838
4839 switch (ret) {
4840 case DCMD_SUCCESS:
4841 /* Fill the internal pd_list and ld_ids array based on
4842 * targetIds returned by FW
4843 */
4844 count = le32_to_cpu(ci->count);
4845
Chandrakanth Patila4413a52019-06-25 16:34:27 +05304846 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
4847 break;
4848
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004849 if (megasas_dbg_lvl & LD_PD_DEBUG)
4850 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
4851 __func__, count);
4852
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004853 memset(instance->local_pd_list, 0,
4854 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4855 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4856 for (i = 0; i < count; i++) {
4857 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4858 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4859 instance->local_pd_list[target_id].tid = target_id;
4860 instance->local_pd_list[target_id].driveType =
4861 ci->host_device_list[i].scsi_type;
4862 instance->local_pd_list[target_id].driveState =
4863 MR_PD_STATE_SYSTEM;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004864 if (megasas_dbg_lvl & LD_PD_DEBUG)
4865 dev_info(&instance->pdev->dev,
4866 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
4867 i, target_id, ci->host_device_list[i].scsi_type);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004868 } else {
4869 instance->ld_ids[target_id] = target_id;
Shivasharan S0a11c0b2019-05-07 10:05:47 -07004870 if (megasas_dbg_lvl & LD_PD_DEBUG)
4871 dev_info(&instance->pdev->dev,
4872 "Device %d: LD targetID: 0x%03x\n",
4873 i, target_id);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004874 }
4875 }
4876
4877 memcpy(instance->pd_list, instance->local_pd_list,
4878 sizeof(instance->pd_list));
4879 break;
4880
4881 case DCMD_TIMEOUT:
4882 switch (dcmd_timeout_ocr_possible(instance)) {
4883 case INITIATE_OCR:
4884 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304885 mutex_unlock(&instance->reset_mutex);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004886 megasas_reset_fusion(instance->host,
4887 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05304888 mutex_lock(&instance->reset_mutex);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004889 break;
4890 case KILL_ADAPTER:
4891 megaraid_sas_kill_hba(instance);
4892 break;
4893 case IGNORE_TIMEOUT:
4894 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4895 __func__, __LINE__);
4896 break;
4897 }
4898 break;
4899 case DCMD_FAILED:
4900 dev_err(&instance->pdev->dev,
4901 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4902 __func__);
4903 break;
4904 }
4905
4906 if (ret != DCMD_TIMEOUT)
4907 megasas_return_cmd(instance, cmd);
4908
4909 return ret;
4910}
4911
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304912/*
4913 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4914 * instance : Controller's instance
4915*/
4916static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4917{
4918 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004919 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304920
4921 fusion = instance->ctrl_context;
4922 /* For MFI based controllers return dummy success */
4923 if (!fusion)
4924 return;
4925
4926 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004927 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304928 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004929 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304930 instance->supportmax256vd = 1;
4931
4932 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4933 * MEGASAS_MAX_DEV_PER_CHANNEL;
4934 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4935 * MEGASAS_MAX_DEV_PER_CHANNEL;
4936 if (instance->supportmax256vd) {
4937 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4938 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4939 } else {
4940 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4941 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4942 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304943
4944 dev_info(&instance->pdev->dev,
Shivasharan Scba67d92018-10-16 23:37:56 -07004945 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4946 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4947 instance->ctrl_info_buf->max_lds);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304948
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004949 if (instance->max_raid_mapsize) {
4950 ventura_map_sz = instance->max_raid_mapsize *
4951 MR_MIN_MAP_SIZE; /* 64k */
4952 fusion->current_map_sz = ventura_map_sz;
4953 fusion->max_map_sz = ventura_map_sz;
4954 } else {
4955 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4956 (sizeof(struct MR_LD_SPAN_MAP) *
4957 (instance->fw_supported_vd_count - 1));
4958 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304959
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004960 fusion->max_map_sz =
4961 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304962
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004963 if (instance->supportmax256vd)
4964 fusion->current_map_sz = fusion->new_map_sz;
4965 else
4966 fusion->current_map_sz = fusion->old_map_sz;
4967 }
4968 /* irrespective of FW raid maps, driver raid map is constant */
4969 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304970}
4971
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004972/*
4973 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4974 * dcmd.hdr.length - number of bytes to read
4975 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4976 * Desc: Fill in snapdump properties
4977 * Status: MFI_STAT_OK- Command successful
4978 */
4979void megasas_get_snapdump_properties(struct megasas_instance *instance)
4980{
4981 int ret = 0;
4982 struct megasas_cmd *cmd;
4983 struct megasas_dcmd_frame *dcmd;
4984 struct MR_SNAPDUMP_PROPERTIES *ci;
4985 dma_addr_t ci_h = 0;
4986
4987 ci = instance->snapdump_prop;
4988 ci_h = instance->snapdump_prop_h;
4989
4990 if (!ci)
4991 return;
4992
4993 cmd = megasas_get_cmd(instance);
4994
4995 if (!cmd) {
4996 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4997 return;
4998 }
4999
5000 dcmd = &cmd->frame->dcmd;
5001
5002 memset(ci, 0, sizeof(*ci));
5003 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5004
5005 dcmd->cmd = MFI_CMD_DCMD;
5006 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5007 dcmd->sge_count = 1;
5008 dcmd->flags = MFI_FRAME_DIR_READ;
5009 dcmd->timeout = 0;
5010 dcmd->pad_0 = 0;
5011 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5012 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5013
5014 megasas_set_dma_settings(instance, dcmd, ci_h,
5015 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5016
5017 if (!instance->mask_interrupts) {
5018 ret = megasas_issue_blocked_cmd(instance, cmd,
5019 MFI_IO_TIMEOUT_SECS);
5020 } else {
5021 ret = megasas_issue_polled(instance, cmd);
5022 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5023 }
5024
5025 switch (ret) {
5026 case DCMD_SUCCESS:
5027 instance->snapdump_wait_time =
5028 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5029 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5030 break;
5031
5032 case DCMD_TIMEOUT:
5033 switch (dcmd_timeout_ocr_possible(instance)) {
5034 case INITIATE_OCR:
5035 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305036 mutex_unlock(&instance->reset_mutex);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005037 megasas_reset_fusion(instance->host,
5038 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305039 mutex_lock(&instance->reset_mutex);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005040 break;
5041 case KILL_ADAPTER:
5042 megaraid_sas_kill_hba(instance);
5043 break;
5044 case IGNORE_TIMEOUT:
5045 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5046 __func__, __LINE__);
5047 break;
5048 }
5049 }
5050
5051 if (ret != DCMD_TIMEOUT)
5052 megasas_return_cmd(instance, cmd);
5053}
5054
adam radford21c9e162013-09-06 15:27:14 -07005055/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005056 * megasas_get_controller_info - Returns FW's controller structure
5057 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005058 *
5059 * Issues an internal command (DCMD) to get the FW's controller structure.
5060 * This information is mainly used to find out the maximum IO transfer per
5061 * command supported by the FW.
5062 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305063int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305064megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005065{
5066 int ret = 0;
5067 struct megasas_cmd *cmd;
5068 struct megasas_dcmd_frame *dcmd;
5069 struct megasas_ctrl_info *ci;
5070 dma_addr_t ci_h = 0;
5071
Shivasharan S9b3d0282017-10-19 02:48:56 -07005072 ci = instance->ctrl_info_buf;
5073 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305074
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005075 cmd = megasas_get_cmd(instance);
5076
5077 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005078 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005079 return -ENOMEM;
5080 }
5081
5082 dcmd = &cmd->frame->dcmd;
5083
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005084 memset(ci, 0, sizeof(*ci));
5085 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5086
5087 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305088 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005089 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005090 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005091 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005092 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305093 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5094 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305095 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005096
Shivasharan S107a60d2017-10-19 02:49:05 -07005097 megasas_set_dma_settings(instance, dcmd, ci_h,
5098 sizeof(struct megasas_ctrl_info));
5099
Shivasharan Se7d36b82017-10-19 02:48:50 -07005100 if ((instance->adapter_type != MFI_SERIES) &&
Shivasharan S54b28042018-01-05 05:27:47 -08005101 !instance->mask_interrupts) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305102 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Shivasharan S54b28042018-01-05 05:27:47 -08005103 } else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305104 ret = megasas_issue_polled(instance, cmd);
Shivasharan S54b28042018-01-05 05:27:47 -08005105 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5106 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305107
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305108 switch (ret) {
5109 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305110 /* Save required controller information in
5111 * CPU endianness format.
5112 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005113 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005114 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
Shivasharan S9ad18a92017-10-19 02:48:57 -07005115 le32_to_cpus((u32 *)&ci->adapterOperations2);
5116 le32_to_cpus((u32 *)&ci->adapterOperations3);
5117 le16_to_cpus((u16 *)&ci->adapter_operations4);
Chandrakanth Patil58136852019-06-25 16:34:30 +05305118 le32_to_cpus((u32 *)&ci->adapter_operations5);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305119
5120 /* Update the latest Ext VD info.
5121 * From Init path, store current firmware details.
5122 * From OCR path, detect any firmware properties changes.
5123 * in case of Firmware upgrade without system reboot.
5124 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305125 megasas_update_ext_vd_details(instance);
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305126 instance->support_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005127 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05005128 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005129 ci->adapter_operations4.support_pd_map_target_id;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005130 instance->support_nvme_passthru =
5131 ci->adapter_operations4.support_nvme_passthru;
Chandrakanth Patil58136852019-06-25 16:34:30 +05305132 instance->support_pci_lane_margining =
5133 ci->adapter_operations5.support_pci_lane_margining;
Shivasharan Se9495e22018-06-04 03:45:12 -07005134 instance->task_abort_tmo = ci->TaskAbortTO;
5135 instance->max_reset_tmo = ci->MaxResetTO;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305136
5137 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005138 instance->is_imr = (ci->memory_size ? 0 : 1);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005139
5140 instance->snapdump_wait_time =
5141 (ci->properties.on_off_properties2.enable_snap_dump ?
5142 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5143
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005144 instance->enable_fw_dev_list =
5145 ci->properties.on_off_properties2.enable_fw_dev_list;
5146
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305147 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305148 "controller type\t: %s(%dMB)\n",
5149 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07005150 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305151
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05305152 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005153 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305154 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005155 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305156 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5157 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305158 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5159 instance->secure_jbod_support ? "Yes" : "No");
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005160 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5161 instance->support_nvme_passthru ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07005162 dev_info(&instance->pdev->dev,
5163 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5164 instance->task_abort_tmo, instance->max_reset_tmo);
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305165 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5166 instance->support_seqnum_jbod_fp ? "Yes" : "No");
Chandrakanth Patil58136852019-06-25 16:34:30 +05305167 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5168 instance->support_pci_lane_margining ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07005169
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305170 break;
5171
5172 case DCMD_TIMEOUT:
5173 switch (dcmd_timeout_ocr_possible(instance)) {
5174 case INITIATE_OCR:
5175 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305176 mutex_unlock(&instance->reset_mutex);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305177 megasas_reset_fusion(instance->host,
5178 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05305179 mutex_lock(&instance->reset_mutex);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305180 break;
5181 case KILL_ADAPTER:
5182 megaraid_sas_kill_hba(instance);
5183 break;
5184 case IGNORE_TIMEOUT:
5185 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5186 __func__, __LINE__);
5187 break;
5188 }
Shivasharan S2747e6b2018-06-04 03:45:09 -07005189 break;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305190 case DCMD_FAILED:
5191 megaraid_sas_kill_hba(instance);
5192 break;
5193
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305194 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005195
Shivasharan S2747e6b2018-06-04 03:45:09 -07005196 if (ret != DCMD_TIMEOUT)
5197 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305198
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005199 return ret;
5200}
5201
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305202/*
5203 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5204 * to firmware
5205 *
5206 * @instance: Adapter soft state
5207 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5208 MR_CRASH_BUF_TURN_OFF = 0
5209 MR_CRASH_BUF_TURN_ON = 1
5210 * @return 0 on success non-zero on failure.
5211 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5212 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5213 * that driver supports crash dump feature. This DCMD will be sent only if
5214 * crash dump feature is supported by the FW.
5215 *
5216 */
5217int megasas_set_crash_dump_params(struct megasas_instance *instance,
5218 u8 crash_buf_state)
5219{
5220 int ret = 0;
5221 struct megasas_cmd *cmd;
5222 struct megasas_dcmd_frame *dcmd;
5223
5224 cmd = megasas_get_cmd(instance);
5225
5226 if (!cmd) {
5227 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5228 return -ENOMEM;
5229 }
5230
5231
5232 dcmd = &cmd->frame->dcmd;
5233
5234 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5235 dcmd->mbox.b[0] = crash_buf_state;
5236 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305237 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305238 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005239 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305240 dcmd->timeout = 0;
5241 dcmd->pad_0 = 0;
5242 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5243 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305244
Shivasharan S107a60d2017-10-19 02:49:05 -07005245 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5246 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305247
Shivasharan Se7d36b82017-10-19 02:48:50 -07005248 if ((instance->adapter_type != MFI_SERIES) &&
5249 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305250 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305251 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305252 ret = megasas_issue_polled(instance, cmd);
5253
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305254 if (ret == DCMD_TIMEOUT) {
5255 switch (dcmd_timeout_ocr_possible(instance)) {
5256 case INITIATE_OCR:
5257 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5258 megasas_reset_fusion(instance->host,
5259 MFI_IO_TIMEOUT_OCR);
5260 break;
5261 case KILL_ADAPTER:
5262 megaraid_sas_kill_hba(instance);
5263 break;
5264 case IGNORE_TIMEOUT:
5265 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5266 __func__, __LINE__);
5267 break;
5268 }
5269 } else
5270 megasas_return_cmd(instance, cmd);
5271
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305272 return ret;
5273}
5274
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005275/**
bo yang31ea7082007-11-07 12:09:50 -05005276 * megasas_issue_init_mfi - Initializes the FW
5277 * @instance: Adapter soft state
5278 *
5279 * Issues the INIT MFI cmd
5280 */
5281static int
5282megasas_issue_init_mfi(struct megasas_instance *instance)
5283{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05305284 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05005285 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05005286 struct megasas_init_frame *init_frame;
5287 struct megasas_init_queue_info *initq_info;
5288 dma_addr_t init_frame_h;
5289 dma_addr_t initq_info_h;
5290
5291 /*
5292 * Prepare a init frame. Note the init frame points to queue info
5293 * structure. Each frame has SGL allocated after first 64 bytes. For
5294 * this frame - since we don't need any SGL - we use SGL's space as
5295 * queue info structure
5296 *
5297 * We will not get a NULL command below. We just created the pool.
5298 */
5299 cmd = megasas_get_cmd(instance);
5300
5301 init_frame = (struct megasas_init_frame *)cmd->frame;
5302 initq_info = (struct megasas_init_queue_info *)
5303 ((unsigned long)init_frame + 64);
5304
5305 init_frame_h = cmd->frame_phys_addr;
5306 initq_info_h = init_frame_h + 64;
5307
5308 context = init_frame->context;
5309 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5310 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5311 init_frame->context = context;
5312
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305313 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5314 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05005315
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305316 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5317 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05005318
5319 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305320 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305321 init_frame->queue_info_new_phys_addr_lo =
5322 cpu_to_le32(lower_32_bits(initq_info_h));
5323 init_frame->queue_info_new_phys_addr_hi =
5324 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05005325
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305326 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05005327
5328 /*
5329 * disable the intr before firing the init frame to FW
5330 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305331 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05005332
5333 /*
5334 * Issue the init frame in polled mode
5335 */
5336
5337 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005338 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05005339 megasas_return_cmd(instance, cmd);
5340 goto fail_fw_init;
5341 }
5342
5343 megasas_return_cmd(instance, cmd);
5344
5345 return 0;
5346
5347fail_fw_init:
5348 return -EINVAL;
5349}
5350
adam radfordcd50ba82010-12-21 10:23:23 -08005351static u32
5352megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005353{
5354 u32 context_sz;
5355 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005356
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005357 /*
5358 * Get various operational parameters from status register
5359 */
Shivasharan Sde516372018-12-17 00:47:39 -08005360 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07005361 /*
5362 * Reduce the max supported cmds by 1. This is to ensure that the
5363 * reply_q_sz (1 more than the max cmd that driver may send)
5364 * does not exceed max cmds that the FW can support
5365 */
5366 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08005367 instance->max_mfi_cmds = instance->max_fw_cmds;
Shivasharan Sde516372018-12-17 00:47:39 -08005368 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08005369 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005370 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305371 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5372 * are reserved for IOCTL + driver's internal DCMDs.
5373 */
5374 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5375 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5376 instance->max_scsi_cmds = (instance->max_fw_cmds -
5377 MEGASAS_SKINNY_INT_CMDS);
5378 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5379 } else {
5380 instance->max_scsi_cmds = (instance->max_fw_cmds -
5381 MEGASAS_INT_CMDS);
5382 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5383 }
5384
Sumit Saxena308ec452016-01-28 21:04:30 +05305385 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305386 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005387 * Create a pool of commands
5388 */
5389 if (megasas_alloc_cmds(instance))
5390 goto fail_alloc_cmds;
5391
5392 /*
5393 * Allocate memory for reply queue. Length of reply queue should
5394 * be _one_ more than the maximum commands handled by the firmware.
5395 *
5396 * Note: When FW completes commands, it places corresponding contex
5397 * values in this circular reply queue. This circular queue is a fairly
5398 * typical producer-consumer queue. FW is the producer (of completed
5399 * commands) and the driver is the consumer.
5400 */
5401 context_sz = sizeof(u32);
5402 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5403
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005404 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5405 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005406
5407 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005408 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005409 goto fail_reply_queue;
5410 }
5411
bo yang31ea7082007-11-07 12:09:50 -05005412 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005413 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005414
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305415 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305416 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5417 "Fail from %s %d\n", instance->unique_id,
5418 __func__, __LINE__);
5419 goto fail_fw_init;
5420 }
5421
bo yang39a98552010-09-22 22:36:29 -04005422 instance->fw_support_ieee = 0;
5423 instance->fw_support_ieee =
Shivasharan Sde516372018-12-17 00:47:39 -08005424 (instance->instancet->read_fw_status_reg(instance) &
bo yang39a98552010-09-22 22:36:29 -04005425 0x04000000);
5426
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005427 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04005428 instance->fw_support_ieee);
5429
5430 if (instance->fw_support_ieee)
5431 instance->flag_ieee = 1;
5432
adam radfordcd50ba82010-12-21 10:23:23 -08005433 return 0;
5434
5435fail_fw_init:
5436
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005437 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
adam radfordcd50ba82010-12-21 10:23:23 -08005438 instance->reply_queue, instance->reply_queue_h);
5439fail_reply_queue:
5440 megasas_free_cmds(instance);
5441
5442fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08005443 return 1;
5444}
5445
Shivasharan S62a04f82019-05-07 10:05:35 -07005446static
5447void megasas_setup_irq_poll(struct megasas_instance *instance)
5448{
5449 struct megasas_irq_context *irq_ctx;
5450 u32 count, i;
5451
5452 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5453
5454 /* Initialize IRQ poll */
5455 for (i = 0; i < count; i++) {
5456 irq_ctx = &instance->irq_context[i];
5457 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5458 irq_ctx->irq_poll_scheduled = false;
5459 irq_poll_init(&irq_ctx->irqpoll,
5460 instance->threshold_reply_count,
5461 megasas_irqpoll);
5462 }
5463}
5464
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305465/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01005466 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305467 * @instance: Adapter soft state
5468 *
5469 * Do not enable interrupt, only setup ISRs.
5470 *
5471 * Return 0 on success.
5472 */
5473static int
5474megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5475{
5476 struct pci_dev *pdev;
5477
5478 pdev = instance->pdev;
5479 instance->irq_context[0].instance = instance;
5480 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005481 if (request_irq(pci_irq_vector(pdev, 0),
5482 instance->instancet->service_isr, IRQF_SHARED,
5483 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305484 dev_err(&instance->pdev->dev,
5485 "Failed to register IRQ from %s %d\n",
5486 __func__, __LINE__);
5487 return -1;
5488 }
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305489 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305490 instance->low_latency_index_start = 0;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305491 return 0;
5492}
5493
5494/**
5495 * megasas_setup_irqs_msix - register MSI-x interrupts.
5496 * @instance: Adapter soft state
5497 * @is_probe: Driver probe check
5498 *
5499 * Do not enable interrupt, only setup ISRs.
5500 *
5501 * Return 0 on success.
5502 */
5503static int
5504megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5505{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005506 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305507 struct pci_dev *pdev;
5508
5509 pdev = instance->pdev;
5510
5511 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305512 for (i = 0; i < instance->msix_vectors; i++) {
5513 instance->irq_context[i].instance = instance;
5514 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005515 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305516 instance->instancet->service_isr, 0, "megasas",
5517 &instance->irq_context[i])) {
5518 dev_err(&instance->pdev->dev,
5519 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005520 for (j = 0; j < i; j++)
5521 free_irq(pci_irq_vector(pdev, j),
5522 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305523 /* Retry irq register for IO_APIC*/
5524 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005525 instance->msix_load_balance = false;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005526 if (is_probe) {
5527 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305528 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005529 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305530 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005531 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305532 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305533 }
Shivasharan S1d15d902019-05-07 10:05:36 -07005534
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305535 return 0;
5536}
5537
5538/*
5539 * megasas_destroy_irqs- unregister interrupts.
5540 * @instance: Adapter soft state
5541 * return: void
5542 */
5543static void
5544megasas_destroy_irqs(struct megasas_instance *instance) {
5545
5546 int i;
Shivasharan S62a04f82019-05-07 10:05:35 -07005547 int count;
5548 struct megasas_irq_context *irq_ctx;
5549
5550 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5551 if (instance->adapter_type != MFI_SERIES) {
5552 for (i = 0; i < count; i++) {
5553 irq_ctx = &instance->irq_context[i];
5554 irq_poll_disable(&irq_ctx->irqpoll);
5555 }
5556 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305557
5558 if (instance->msix_vectors)
5559 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005560 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305561 &instance->irq_context[i]);
5562 }
5563 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005564 free_irq(pci_irq_vector(instance->pdev, 0),
5565 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305566}
5567
adam radfordcd50ba82010-12-21 10:23:23 -08005568/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305569 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5570 * @instance: Adapter soft state
5571 * @is_probe: Driver probe check
5572 *
5573 * Return 0 on success.
5574 */
5575void
5576megasas_setup_jbod_map(struct megasas_instance *instance)
5577{
5578 int i;
5579 struct fusion_context *fusion = instance->ctrl_context;
5580 u32 pd_seq_map_sz;
5581
5582 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5583 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5584
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305585 instance->use_seqnum_jbod_fp =
5586 instance->support_seqnum_jbod_fp;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305587 if (reset_devices || !fusion ||
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305588 !instance->support_seqnum_jbod_fp) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305589 dev_info(&instance->pdev->dev,
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05305590 "JBOD sequence map is disabled %s %d\n",
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305591 __func__, __LINE__);
5592 instance->use_seqnum_jbod_fp = false;
5593 return;
5594 }
5595
5596 if (fusion->pd_seq_sync[0])
5597 goto skip_alloc;
5598
5599 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5600 fusion->pd_seq_sync[i] = dma_alloc_coherent
5601 (&instance->pdev->dev, pd_seq_map_sz,
5602 &fusion->pd_seq_phys[i], GFP_KERNEL);
5603 if (!fusion->pd_seq_sync[i]) {
5604 dev_err(&instance->pdev->dev,
5605 "Failed to allocate memory from %s %d\n",
5606 __func__, __LINE__);
5607 if (i == 1) {
5608 dma_free_coherent(&instance->pdev->dev,
5609 pd_seq_map_sz, fusion->pd_seq_sync[0],
5610 fusion->pd_seq_phys[0]);
5611 fusion->pd_seq_sync[0] = NULL;
5612 }
5613 instance->use_seqnum_jbod_fp = false;
5614 return;
5615 }
5616 }
5617
5618skip_alloc:
5619 if (!megasas_sync_pd_seq_num(instance, false) &&
5620 !megasas_sync_pd_seq_num(instance, true))
5621 instance->use_seqnum_jbod_fp = true;
5622 else
5623 instance->use_seqnum_jbod_fp = false;
5624}
5625
Ming Leiadbe5522018-03-13 17:42:40 +08005626static void megasas_setup_reply_map(struct megasas_instance *instance)
5627{
5628 const struct cpumask *mask;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305629 unsigned int queue, cpu, low_latency_index_start;
Ming Leiadbe5522018-03-13 17:42:40 +08005630
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305631 low_latency_index_start = instance->low_latency_index_start;
5632
5633 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
Ming Leiadbe5522018-03-13 17:42:40 +08005634 mask = pci_irq_get_affinity(instance->pdev, queue);
5635 if (!mask)
5636 goto fallback;
5637
5638 for_each_cpu(cpu, mask)
5639 instance->reply_map[cpu] = queue;
5640 }
5641 return;
5642
5643fallback:
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305644 queue = low_latency_index_start;
5645 for_each_possible_cpu(cpu) {
5646 instance->reply_map[cpu] = queue;
5647 if (queue == (instance->msix_vectors - 1))
5648 queue = low_latency_index_start;
5649 else
5650 queue++;
5651 }
Ming Leiadbe5522018-03-13 17:42:40 +08005652}
5653
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305654/**
Shivasharan Sdaa06812019-01-29 01:38:12 -08005655 * megasas_get_device_list - Get the PD and LD device list from FW.
5656 * @instance: Adapter soft state
5657 * @return: Success or failure
5658 *
5659 * Issue DCMDs to Firmware to get the PD and LD list.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005660 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5661 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan Sdaa06812019-01-29 01:38:12 -08005662 */
5663static
5664int megasas_get_device_list(struct megasas_instance *instance)
5665{
5666 memset(instance->pd_list, 0,
5667 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5668 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5669
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005670 if (instance->enable_fw_dev_list) {
5671 if (megasas_host_device_list_query(instance, true))
5672 return FAILED;
5673 } else {
5674 if (megasas_get_pd_list(instance) < 0) {
5675 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5676 return FAILED;
5677 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005678
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005679 if (megasas_ld_list_query(instance,
5680 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5681 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5682 return FAILED;
5683 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005684 }
5685
5686 return SUCCESS;
5687}
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305688
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305689/**
5690 * megasas_set_high_iops_queue_affinity_hint - Set affinity hint for high IOPS queues
5691 * @instance: Adapter soft state
5692 * return: void
5693 */
5694static inline void
5695megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5696{
5697 int i;
5698 int local_numa_node;
5699
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305700 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305701 local_numa_node = dev_to_node(&instance->pdev->dev);
5702
5703 for (i = 0; i < instance->low_latency_index_start; i++)
5704 irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5705 cpumask_of_node(local_numa_node));
5706 }
5707}
5708
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305709static int
5710__megasas_alloc_irq_vectors(struct megasas_instance *instance)
5711{
5712 int i, irq_flags;
5713 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5714 struct irq_affinity *descp = &desc;
5715
5716 irq_flags = PCI_IRQ_MSIX;
5717
5718 if (instance->smp_affinity_enable)
5719 irq_flags |= PCI_IRQ_AFFINITY;
5720 else
5721 descp = NULL;
5722
5723 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5724 instance->low_latency_index_start,
5725 instance->msix_vectors, irq_flags, descp);
5726
5727 return i;
5728}
5729
5730/**
5731 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5732 * @instance: Adapter soft state
5733 * return: void
5734 */
5735static void
5736megasas_alloc_irq_vectors(struct megasas_instance *instance)
5737{
5738 int i;
5739 unsigned int num_msix_req;
5740
5741 i = __megasas_alloc_irq_vectors(instance);
5742
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305743 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
5744 (i != instance->msix_vectors)) {
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305745 if (instance->msix_vectors)
5746 pci_free_irq_vectors(instance->pdev);
5747 /* Disable Balanced IOPS mode and try realloc vectors */
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305748 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305749 instance->low_latency_index_start = 1;
5750 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5751
5752 instance->msix_vectors = min(num_msix_req,
5753 instance->msix_vectors);
5754
5755 i = __megasas_alloc_irq_vectors(instance);
5756
5757 }
5758
5759 dev_info(&instance->pdev->dev,
5760 "requested/available msix %d/%d\n", instance->msix_vectors, i);
5761
5762 if (i > 0)
5763 instance->msix_vectors = i;
5764 else
5765 instance->msix_vectors = 0;
5766
Chandrakanth Patilf0b9e7b2019-06-25 16:34:33 +05305767 if (instance->smp_affinity_enable)
5768 megasas_set_high_iops_queue_affinity_hint(instance);
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305769}
5770
Shivasharan Sdaa06812019-01-29 01:38:12 -08005771/**
adam radfordcd50ba82010-12-21 10:23:23 -08005772 * megasas_init_fw - Initializes the FW
5773 * @instance: Adapter soft state
5774 *
5775 * This is the main function for initializing firmware
5776 */
5777
5778static int megasas_init_fw(struct megasas_instance *instance)
5779{
5780 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005781 u32 max_sectors_2, tmp_sectors, msix_enable;
Shivasharan S81b76452018-10-16 23:37:51 -07005782 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005783 resource_size_t base_addr;
Shivasharan S9a598712019-05-07 10:05:42 -07005784 void *base_addr_phys;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305785 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005786 unsigned long bar_list;
Shivasharan Sce884182019-05-07 10:05:48 -07005787 int i, j, loop;
adam radford229fe472014-03-10 02:51:56 -07005788 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305789 struct fusion_context *fusion;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305790 bool intr_coalescing;
5791 unsigned int num_msix_req;
Chandrakanth Patil299ee422019-06-25 16:34:35 +05305792 u16 lnksta, speed;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305793
5794 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005795
5796 /* Find first memory bar */
5797 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005798 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005799 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005800 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005801 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005802 return -EBUSY;
5803 }
5804
Ben Collins11f8a7b2013-09-13 12:46:44 -04005805 base_addr = pci_resource_start(instance->pdev, instance->bar);
5806 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005807
5808 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005809 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005810 goto fail_ioremap;
5811 }
5812
Shivasharan S9a598712019-05-07 10:05:42 -07005813 base_addr_phys = &base_addr;
5814 dev_printk(KERN_DEBUG, &instance->pdev->dev,
5815 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
5816 instance->bar, base_addr_phys, instance->reg_set);
5817
Shivasharan Se7d36b82017-10-19 02:48:50 -07005818 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005819 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005820 else {
5821 switch (instance->pdev->device) {
5822 case PCI_DEVICE_ID_LSI_SAS1078R:
5823 case PCI_DEVICE_ID_LSI_SAS1078DE:
5824 instance->instancet = &megasas_instance_template_ppc;
5825 break;
5826 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5827 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5828 instance->instancet = &megasas_instance_template_gen2;
5829 break;
5830 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5831 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5832 instance->instancet = &megasas_instance_template_skinny;
5833 break;
5834 case PCI_DEVICE_ID_LSI_SAS1064R:
5835 case PCI_DEVICE_ID_DELL_PERC5:
5836 default:
5837 instance->instancet = &megasas_instance_template_xscale;
5838 instance->pd_list_not_supported = 1;
5839 break;
5840 }
adam radfordcd50ba82010-12-21 10:23:23 -08005841 }
5842
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305843 if (megasas_transition_to_ready(instance, 0)) {
Shivasharan S78409d42019-05-07 10:05:34 -07005844 dev_info(&instance->pdev->dev,
5845 "Failed to transition controller to ready from %s!\n",
5846 __func__);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005847 if (instance->adapter_type != MFI_SERIES) {
Shivasharan Sde93b402018-10-16 23:37:42 -07005848 status_reg = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08005849 instance);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005850 if (status_reg & MFI_RESET_ADAPTER) {
Shivasharan S78409d42019-05-07 10:05:34 -07005851 if (megasas_adp_reset_wait_for_ready
5852 (instance, true, 0) == FAILED)
Shivasharan Sf10fb852019-05-07 10:05:32 -07005853 goto fail_ready_state;
5854 } else {
5855 goto fail_ready_state;
5856 }
5857 } else {
Shivasharan Sde93b402018-10-16 23:37:42 -07005858 atomic_set(&instance->fw_reset_no_pci_access, 1);
5859 instance->instancet->adp_reset
5860 (instance, instance->reg_set);
5861 atomic_set(&instance->fw_reset_no_pci_access, 0);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305862
Shivasharan Sde93b402018-10-16 23:37:42 -07005863 /*waiting for about 30 second before retry*/
5864 ssleep(30);
5865
5866 if (megasas_transition_to_ready(instance, 0))
5867 goto fail_ready_state;
Shivasharan Sde93b402018-10-16 23:37:42 -07005868 }
Shivasharan S78409d42019-05-07 10:05:34 -07005869
5870 dev_info(&instance->pdev->dev,
5871 "FW restarted successfully from %s!\n",
5872 __func__);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305873 }
adam radfordcd50ba82010-12-21 10:23:23 -08005874
Shivasharan Se5d65b42017-10-19 02:48:59 -07005875 megasas_init_ctrl_params(instance);
5876
Shivasharan S107a60d2017-10-19 02:49:05 -07005877 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005878 goto fail_ready_state;
5879
5880 if (megasas_alloc_ctrl_mem(instance))
5881 goto fail_alloc_dma_buf;
5882
5883 if (megasas_alloc_ctrl_dma_buffers(instance))
5884 goto fail_alloc_dma_buf;
5885
5886 fusion = instance->ctrl_context;
5887
Shivasharan S630d42b2018-12-17 00:47:37 -08005888 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005889 scratch_pad_2 =
Shivasharan S272652f2018-12-17 00:47:40 -08005890 megasas_readl(instance,
5891 &instance->reg_set->outbound_scratch_pad_2);
Shivasharan S81b76452018-10-16 23:37:51 -07005892 instance->max_raid_mapsize = ((scratch_pad_2 >>
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005893 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5894 MR_MAX_RAID_MAP_SIZE_MASK);
5895 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305896
Chandrakanth Patil7fc55702019-06-25 16:34:29 +05305897 switch (instance->adapter_type) {
5898 case VENTURA_SERIES:
Chandrakanth Patil49f2bf12019-06-25 16:34:28 +05305899 fusion->pcie_bw_limitation = true;
Chandrakanth Patil7fc55702019-06-25 16:34:29 +05305900 break;
5901 case AERO_SERIES:
5902 fusion->r56_div_offload = true;
5903 break;
5904 default:
5905 break;
5906 }
Chandrakanth Patil49f2bf12019-06-25 16:34:28 +05305907
adam radford3f1abce2011-05-11 18:33:47 -07005908 /* Check if MSI-X is supported while in ready state */
Shivasharan Sde516372018-12-17 00:47:39 -08005909 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
adam radford3f1abce2011-05-11 18:33:47 -07005910 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005911 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005912
Shivasharan S272652f2018-12-17 00:47:40 -08005913 scratch_pad_1 = megasas_readl
5914 (instance, &instance->reg_set->outbound_scratch_pad_1);
adam radfordc8e858f2011-10-08 18:15:13 -07005915 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305916 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005917 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5918 /* Thunderbolt Series*/
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_OFFSET) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005921 } else {
Shivasharan S81b76452018-10-16 23:37:51 -07005922 instance->msix_vectors = ((scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305923 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5924 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005925
5926 /*
5927 * For Invader series, > 8 MSI-x vectors
5928 * supported by FW/HW implies combined
5929 * reply queue mode is enabled.
5930 * For Ventura series, > 16 MSI-x vectors
5931 * supported by FW/HW implies combined
5932 * reply queue mode is enabled.
5933 */
5934 switch (instance->adapter_type) {
5935 case INVADER_SERIES:
5936 if (instance->msix_vectors > 8)
5937 instance->msix_combined = true;
5938 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08005939 case AERO_SERIES:
Shivasharan Se29c3222018-10-16 23:37:46 -07005940 case VENTURA_SERIES:
5941 if (instance->msix_vectors > 16)
5942 instance->msix_combined = true;
5943 break;
5944 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005945
Sumit Saxena179ac142016-01-28 21:04:28 +05305946 if (rdpq_enable)
Shivasharan S81b76452018-10-16 23:37:51 -07005947 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
Sumit Saxena179ac142016-01-28 21:04:28 +05305948 1 : 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005949
Shivasharan S1175b882019-06-28 02:50:39 -07005950 if (instance->adapter_type >= INVADER_SERIES &&
5951 !instance->msix_combined) {
Shivasharan S1d15d902019-05-07 10:05:36 -07005952 instance->msix_load_balance = true;
5953 instance->smp_affinity_enable = false;
5954 }
5955
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305956 /* Save 1-15 reply post index address to local memory
5957 * Index 0 is already saved from reg offset
5958 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5959 */
5960 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5961 instance->reply_post_host_index_addr[loop] =
5962 (u32 __iomem *)
5963 ((u8 __iomem *)instance->reg_set +
5964 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5965 + (loop * 0x10));
5966 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305967 }
Shivasharan Sce884182019-05-07 10:05:48 -07005968
5969 dev_info(&instance->pdev->dev,
5970 "firmware supports msix\t: (%d)",
5971 instance->msix_vectors);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305972 if (msix_vectors)
5973 instance->msix_vectors = min(msix_vectors,
5974 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305975 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005976 instance->msix_vectors = 1;
Shivasharan S1d15d902019-05-07 10:05:36 -07005977
Chandrakanth Patil132147d2019-06-25 16:34:31 +05305978
5979 /*
5980 * For Aero (if some conditions are met), driver will configure a
5981 * few additional reply queues with interrupt coalescing enabled.
5982 * These queues with interrupt coalescing enabled are called
5983 * High IOPS queues and rest of reply queues (based on number of
5984 * logical CPUs) are termed as Low latency queues.
5985 *
5986 * Total Number of reply queues = High IOPS queues + low latency queues
5987 *
5988 * For rest of fusion adapters, 1 additional reply queue will be
5989 * reserved for management commands, rest of reply queues
5990 * (based on number of logical CPUs) will be used for IOs and
5991 * referenced as IO queues.
5992 * Total Number of reply queues = 1 + IO queues
5993 *
5994 * MFI adapters supports single MSI-x so single reply queue
5995 * will be used for IO and management commands.
5996 */
5997
5998 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
5999 true : false;
6000 if (intr_coalescing &&
6001 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6002 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306003 instance->perf_mode = MR_BALANCED_PERF_MODE;
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02006004 else
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306005 instance->perf_mode = MR_LATENCY_PERF_MODE;
Chandrakanth Patil132147d2019-06-25 16:34:31 +05306006
Chandrakanth Patil299ee422019-06-25 16:34:35 +05306007
6008 if (instance->adapter_type == AERO_SERIES) {
6009 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6010 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6011
6012 /*
6013 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6014 * in latency perf mode and enable R1 PCI bandwidth algorithm
6015 */
6016 if (speed < 0x4) {
6017 instance->perf_mode = MR_LATENCY_PERF_MODE;
6018 fusion->pcie_bw_limitation = true;
6019 }
6020
6021 /*
6022 * Performance mode settings provided through module parameter-perf_mode will
6023 * take affect only for:
6024 * 1. Aero family of adapters.
6025 * 2. When user sets module parameter- perf_mode in range of 0-2.
6026 */
6027 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6028 (perf_mode <= MR_LATENCY_PERF_MODE))
6029 instance->perf_mode = perf_mode;
6030 /*
6031 * If intr coalescing is not supported by controller FW, then IOPS
6032 * and Balanced modes are not feasible.
6033 */
6034 if (!intr_coalescing)
6035 instance->perf_mode = MR_LATENCY_PERF_MODE;
6036
6037 }
6038
6039 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
Chandrakanth Patil132147d2019-06-25 16:34:31 +05306040 instance->low_latency_index_start =
6041 MR_HIGH_IOPS_QUEUE_COUNT;
6042 else
6043 instance->low_latency_index_start = 1;
6044
6045 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6046
6047 instance->msix_vectors = min(num_msix_req,
6048 instance->msix_vectors);
6049
6050 megasas_alloc_irq_vectors(instance);
6051 if (!instance->msix_vectors)
Shivasharan S1d15d902019-05-07 10:05:36 -07006052 instance->msix_load_balance = false;
adam radfordc8e858f2011-10-08 18:15:13 -07006053 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05006054 /*
6055 * MSI-X host index 0 is common for all adapter.
6056 * It is used for all MPT based Adapters.
6057 */
6058 if (instance->msix_combined) {
6059 instance->reply_post_host_index_addr[0] =
6060 (u32 *)((u8 *)instance->reg_set +
6061 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6062 } else {
6063 instance->reply_post_host_index_addr[0] =
6064 (u32 *)((u8 *)instance->reg_set +
6065 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6066 }
6067
Shivasharan S64ff64b2017-03-10 03:22:12 -08006068 if (!instance->msix_vectors) {
6069 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6070 if (i < 0)
Shivasharan S8a25fa12018-10-16 23:37:44 -07006071 goto fail_init_adapter;
Shivasharan S64ff64b2017-03-10 03:22:12 -08006072 }
adam radford3f1abce2011-05-11 18:33:47 -07006073
Ming Leiadbe5522018-03-13 17:42:40 +08006074 megasas_setup_reply_map(instance);
6075
Tomas Henzl258c3af2015-06-02 16:09:46 +05306076 dev_info(&instance->pdev->dev,
Tomas Henzl258c3af2015-06-02 16:09:46 +05306077 "current msix/online cpus\t: (%d/%d)\n",
6078 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05306079 dev_info(&instance->pdev->dev,
6080 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05306081
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05306082 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6083 (unsigned long)instance);
6084
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306085 /*
6086 * Below are default value for legacy Firmware.
6087 * non-fusion based controllers
6088 */
6089 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6090 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08006091 /* Get operational params, sge flags, send init cmd to controller */
6092 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08006093 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08006094
Shivasharan S630d42b2018-12-17 00:47:37 -08006095 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07006096 scratch_pad_3 =
Shivasharan S272652f2018-12-17 00:47:40 -08006097 megasas_readl(instance,
6098 &instance->reg_set->outbound_scratch_pad_3);
Shivasharan S81b76452018-10-16 23:37:51 -07006099 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
Shivasharan S15dd0382017-02-10 00:59:10 -08006100 MR_DEFAULT_NVME_PAGE_SHIFT)
6101 instance->nvme_page_size =
Shivasharan S81b76452018-10-16 23:37:51 -07006102 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
Shivasharan S15dd0382017-02-10 00:59:10 -08006103
6104 dev_info(&instance->pdev->dev,
6105 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6106 }
6107
Tomas Henzl18103ef2016-11-01 17:32:02 +01006108 if (instance->msix_vectors ?
6109 megasas_setup_irqs_msix(instance, 1) :
6110 megasas_setup_irqs_ioapic(instance))
6111 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05306112
Shivasharan S62a04f82019-05-07 10:05:35 -07006113 if (instance->adapter_type != MFI_SERIES)
6114 megasas_setup_irq_poll(instance);
6115
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306116 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08006117
Andy Lutomirski13f30772016-05-03 10:24:31 -07006118 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08006119
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306120 megasas_setup_jbod_map(instance);
6121
Shivasharan Sdaa06812019-01-29 01:38:12 -08006122 if (megasas_get_device_list(instance) != SUCCESS) {
6123 dev_err(&instance->pdev->dev,
6124 "%s: megasas_get_device_list failed\n",
6125 __func__);
Shivasharan S72bff2d2017-02-10 00:59:33 -08006126 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01006127 }
Yang, Bo81e403c2009-10-06 14:27:54 -06006128
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006129 /* stream detection initialization */
Shivasharan S630d42b2018-12-17 00:47:37 -08006130 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006131 fusion->stream_detect_by_ld =
Kees Cook6396bb22018-06-12 14:03:40 -07006132 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6133 sizeof(struct LD_STREAM_DETECT *),
6134 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006135 if (!fusion->stream_detect_by_ld) {
6136 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08006137 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006138 goto fail_get_ld_pd_list;
6139 }
6140 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6141 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08006142 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006143 GFP_KERNEL);
6144 if (!fusion->stream_detect_by_ld[i]) {
6145 dev_err(&instance->pdev->dev,
6146 "unable to allocate stream detect by LD\n ");
6147 for (j = 0; j < i; ++j)
6148 kfree(fusion->stream_detect_by_ld[j]);
6149 kfree(fusion->stream_detect_by_ld);
6150 fusion->stream_detect_by_ld = NULL;
6151 goto fail_get_ld_pd_list;
6152 }
6153 fusion->stream_detect_by_ld[i]->mru_bit_map
6154 = MR_STREAM_BITMAP;
6155 }
6156 }
6157
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006158 /*
6159 * Compute the max allowed sectors per IO: The controller info has two
6160 * limits on max sectors. Driver should use the minimum of these two.
6161 *
6162 * 1 << stripe_sz_ops.min = max sectors per strip
6163 *
6164 * Note that older firmwares ( < FW ver 30) didn't report information
6165 * to calculate max_sectors_1. So the number ended up as zero always.
6166 */
bo yang14faea92007-11-09 04:14:00 -05006167 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07006168 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006169
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306170 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6171 le16_to_cpu(ctrl_info->max_strips_per_io);
6172 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006173
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006174 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306175
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05306176 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6177 instance->passive = ctrl_info->cluster.passive;
6178 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306179 instance->UnevenSpanSupport =
6180 ctrl_info->adapterOperations2.supportUnevenSpans;
6181 if (instance->UnevenSpanSupport) {
6182 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08006183 if (MR_ValidateMapInfo(instance, instance->map_id))
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306184 fusion->fast_path_io = 1;
6185 else
6186 fusion->fast_path_io = 0;
6187
6188 }
6189 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306190 instance->requestorId = ctrl_info->iov.requestorId;
6191 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6192 if (!ctrl_info->adapterOperations2.activePassive)
6193 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306194
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306195 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6196 instance->PlasmaFW111 ? "1.11" : "new");
6197
6198 if (instance->PlasmaFW111) {
6199 iovPtr = (struct IOV_111 *)
6200 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6201 instance->requestorId = iovPtr->requestorId;
6202 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05306203 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05306204 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6205 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306206 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05306207
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306208 instance->crash_dump_fw_support =
6209 ctrl_info->adapterOperations3.supportCrashDump;
6210 instance->crash_dump_drv_support =
6211 (instance->crash_dump_fw_support &&
6212 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306213 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306214 megasas_set_crash_dump_params(instance,
6215 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07006216
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306217 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306218 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006219 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306220 CRASH_DMA_BUF_SIZE,
6221 instance->crash_dump_buf,
6222 instance->crash_dump_h);
6223 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05006224 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05306225
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006226 if (instance->snapdump_wait_time) {
6227 megasas_get_snapdump_properties(instance);
6228 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6229 instance->snapdump_wait_time);
6230 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306231
6232 dev_info(&instance->pdev->dev,
6233 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6234 le16_to_cpu(ctrl_info->pci.vendor_id),
6235 le16_to_cpu(ctrl_info->pci.device_id),
6236 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6237 le16_to_cpu(ctrl_info->pci.sub_device_id));
6238 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6239 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306240 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6241 instance->crash_dump_drv_support ? "yes" : "no");
Chandrakanth Patil59db5a92019-06-25 16:34:26 +05306242 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6243 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306244
bo yang14faea92007-11-09 04:14:00 -05006245 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05306246 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05006247 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6248 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006249
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306250 /* Check for valid throttlequeuedepth module parameter */
6251 if (throttlequeuedepth &&
6252 throttlequeuedepth <= instance->max_scsi_cmds)
6253 instance->throttlequeuedepth = throttlequeuedepth;
6254 else
6255 instance->throttlequeuedepth =
6256 MEGASAS_THROTTLE_QUEUE_DEPTH;
6257
Shivasharan Se636a7a2017-08-23 04:46:56 -07006258 if ((resetwaittime < 1) ||
6259 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05306260 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6261
6262 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6263 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05006264
adam radford229fe472014-03-10 02:51:56 -07006265 /* Launch SR-IOV heartbeat timer */
6266 if (instance->requestorId) {
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006267 if (!megasas_sriov_start_heartbeat(instance, 1)) {
Kees Cookc251a7b2017-10-22 15:30:04 -07006268 megasas_start_timer(instance);
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006269 } else {
adam radford229fe472014-03-10 02:51:56 -07006270 instance->skip_heartbeat_timer_del = 1;
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006271 goto fail_get_ld_pd_list;
6272 }
adam radford229fe472014-03-10 02:51:56 -07006273 }
6274
Shivasharan S3f6194a2018-10-16 23:37:39 -07006275 /*
6276 * Create and start watchdog thread which will monitor
6277 * controller state every 1 sec and trigger OCR when
6278 * it enters fault state
6279 */
6280 if (instance->adapter_type != MFI_SERIES)
6281 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6282 goto fail_start_watchdog;
6283
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006284 return 0;
6285
Shivasharan S3f6194a2018-10-16 23:37:39 -07006286fail_start_watchdog:
6287 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6288 del_timer_sync(&instance->sriov_heartbeat_timer);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006289fail_get_ld_pd_list:
6290 instance->instancet->disable_intr(instance);
Hannes Reineckefad119b2016-12-02 12:52:23 +01006291 megasas_destroy_irqs(instance);
Shivasharan S8a25fa12018-10-16 23:37:44 -07006292fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306293 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006294 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306295 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07006296fail_alloc_dma_buf:
6297 megasas_free_ctrl_dma_buffers(instance);
6298 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08006299fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006300 iounmap(instance->reg_set);
6301
Shivasharan S72bff2d2017-02-10 00:59:33 -08006302fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07006303 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006304
Shivasharan S72bff2d2017-02-10 00:59:33 -08006305 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6306 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006307 return -EINVAL;
6308}
6309
6310/**
6311 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01006312 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006313 */
6314static void megasas_release_mfi(struct megasas_instance *instance)
6315{
adam radford9c915a82010-12-21 13:34:31 -08006316 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006317
adam radford9c915a82010-12-21 13:34:31 -08006318 if (instance->reply_queue)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006319 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006320 instance->reply_queue, instance->reply_queue_h);
6321
6322 megasas_free_cmds(instance);
6323
6324 iounmap(instance->reg_set);
6325
Yinghai Lue7f85162016-08-05 23:37:34 -07006326 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006327}
6328
6329/**
6330 * megasas_get_seq_num - Gets latest event sequence numbers
6331 * @instance: Adapter soft state
6332 * @eli: FW event log sequence numbers information
6333 *
6334 * FW maintains a log of all events in a non-volatile area. Upper layers would
6335 * usually find out the latest sequence number of the events, the seq number at
6336 * the boot etc. They would "read" all the events below the latest seq number
6337 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6338 * number), they would subsribe to AEN (asynchronous event notification) and
6339 * wait for the events to happen.
6340 */
6341static int
6342megasas_get_seq_num(struct megasas_instance *instance,
6343 struct megasas_evt_log_info *eli)
6344{
6345 struct megasas_cmd *cmd;
6346 struct megasas_dcmd_frame *dcmd;
6347 struct megasas_evt_log_info *el_info;
6348 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08006349 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006350
6351 cmd = megasas_get_cmd(instance);
6352
6353 if (!cmd) {
6354 return -ENOMEM;
6355 }
6356
6357 dcmd = &cmd->frame->dcmd;
Luis Chamberlain750afb02019-01-04 09:23:09 +01006358 el_info = dma_alloc_coherent(&instance->pdev->dev,
6359 sizeof(struct megasas_evt_log_info),
6360 &el_info_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006361 if (!el_info) {
6362 megasas_return_cmd(instance, cmd);
6363 return -ENOMEM;
6364 }
6365
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6367
6368 dcmd->cmd = MFI_CMD_DCMD;
6369 dcmd->cmd_status = 0x0;
6370 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006371 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006372 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006373 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306374 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6375 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07006376
6377 megasas_set_dma_settings(instance, dcmd, el_info_h,
6378 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006379
Shivasharan Sb051cc62018-01-05 05:27:38 -08006380 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6381 if (ret != DCMD_SUCCESS) {
6382 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6383 __func__, __LINE__);
6384 goto dcmd_failed;
6385 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006386
Shivasharan Sb051cc62018-01-05 05:27:38 -08006387 /*
6388 * Copy the data back into callers buffer
6389 */
6390 eli->newest_seq_num = el_info->newest_seq_num;
6391 eli->oldest_seq_num = el_info->oldest_seq_num;
6392 eli->clear_seq_num = el_info->clear_seq_num;
6393 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6394 eli->boot_seq_num = el_info->boot_seq_num;
6395
6396dcmd_failed:
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006397 dma_free_coherent(&instance->pdev->dev,
6398 sizeof(struct megasas_evt_log_info),
6399 el_info, el_info_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006400
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306401 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006402
Shivasharan Sb051cc62018-01-05 05:27:38 -08006403 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006404}
6405
6406/**
6407 * megasas_register_aen - Registers for asynchronous event notification
6408 * @instance: Adapter soft state
6409 * @seq_num: The starting sequence number
6410 * @class_locale: Class of the event
6411 *
6412 * This function subscribes for AEN for events beyond the @seq_num. It requests
6413 * to be notified if and only if the event is of type @class_locale
6414 */
6415static int
6416megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6417 u32 class_locale_word)
6418{
6419 int ret_val;
6420 struct megasas_cmd *cmd;
6421 struct megasas_dcmd_frame *dcmd;
6422 union megasas_evt_class_locale curr_aen;
6423 union megasas_evt_class_locale prev_aen;
6424
6425 /*
6426 * If there an AEN pending already (aen_cmd), check if the
6427 * class_locale of that pending AEN is inclusive of the new
6428 * AEN request we currently have. If it is, then we don't have
6429 * to do anything. In other words, whichever events the current
6430 * AEN request is subscribing to, have already been subscribed
6431 * to.
6432 *
6433 * If the old_cmd is _not_ inclusive, then we have to abort
6434 * that command, form a class_locale that is superset of both
6435 * old and current and re-issue to the FW
6436 */
6437
6438 curr_aen.word = class_locale_word;
6439
6440 if (instance->aen_cmd) {
6441
Christoph Hellwiga9555532015-04-23 16:34:24 +05306442 prev_aen.word =
6443 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006444
Shivasharan S91b3d9f2017-08-23 04:47:01 -07006445 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6446 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6447 dev_info(&instance->pdev->dev,
6448 "%s %d out of range class %d send by application\n",
6449 __func__, __LINE__, curr_aen.members.class);
6450 return 0;
6451 }
6452
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006453 /*
6454 * A class whose enum value is smaller is inclusive of all
6455 * higher values. If a PROGRESS (= -1) was previously
6456 * registered, then a new registration requests for higher
6457 * classes need not be sent to FW. They are automatically
6458 * included.
6459 *
6460 * Locale numbers don't have such hierarchy. They are bitmap
6461 * values
6462 */
6463 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306464 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006465 curr_aen.members.locale)) {
6466 /*
6467 * Previously issued event registration includes
6468 * current request. Nothing to do.
6469 */
6470 return 0;
6471 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306472 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006473
6474 if (prev_aen.members.class < curr_aen.members.class)
6475 curr_aen.members.class = prev_aen.members.class;
6476
6477 instance->aen_cmd->abort_aen = 1;
6478 ret_val = megasas_issue_blocked_abort_cmd(instance,
6479 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306480 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006481
6482 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006483 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006484 "previous AEN command\n");
6485 return ret_val;
6486 }
6487 }
6488 }
6489
6490 cmd = megasas_get_cmd(instance);
6491
6492 if (!cmd)
6493 return -ENOMEM;
6494
6495 dcmd = &cmd->frame->dcmd;
6496
6497 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6498
6499 /*
6500 * Prepare DCMD for aen registration
6501 */
6502 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6503
6504 dcmd->cmd = MFI_CMD_DCMD;
6505 dcmd->cmd_status = 0x0;
6506 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006507 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006508 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006509 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306510 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6511 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6512 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04006513 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306514 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07006515
6516 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6517 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006518
Yang, Bof4c9a132009-10-06 14:43:28 -06006519 if (instance->aen_cmd != NULL) {
6520 megasas_return_cmd(instance, cmd);
6521 return 0;
6522 }
6523
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006524 /*
6525 * Store reference to the cmd used to register for AEN. When an
6526 * application wants us to register for AEN, we have to abort this
6527 * cmd and re-register with a new EVENT LOCALE supplied by that app
6528 */
6529 instance->aen_cmd = cmd;
6530
6531 /*
6532 * Issue the aen registration frame
6533 */
adam radford9c915a82010-12-21 13:34:31 -08006534 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006535
6536 return 0;
6537}
6538
Shivasharan S96188a82017-02-10 00:59:11 -08006539/* megasas_get_target_prop - Send DCMD with below details to firmware.
6540 *
6541 * This DCMD will fetch few properties of LD/system PD defined
6542 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6543 *
6544 * DCMD send by drivers whenever new target is added to the OS.
6545 *
6546 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6547 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6548 * 0 = system PD, 1 = LD.
6549 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6550 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6551 *
6552 * @instance: Adapter soft state
6553 * @sdev: OS provided scsi device
6554 *
6555 * Returns 0 on success non-zero on failure.
6556 */
Shivasharan Se9495e22018-06-04 03:45:12 -07006557int
Shivasharan S96188a82017-02-10 00:59:11 -08006558megasas_get_target_prop(struct megasas_instance *instance,
6559 struct scsi_device *sdev)
6560{
6561 int ret;
6562 struct megasas_cmd *cmd;
6563 struct megasas_dcmd_frame *dcmd;
Shivasharan Sc8f96df2019-06-28 18:02:12 -07006564 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6565 sdev->id;
Shivasharan S96188a82017-02-10 00:59:11 -08006566
6567 cmd = megasas_get_cmd(instance);
6568
6569 if (!cmd) {
6570 dev_err(&instance->pdev->dev,
6571 "Failed to get cmd %s\n", __func__);
6572 return -ENOMEM;
6573 }
6574
6575 dcmd = &cmd->frame->dcmd;
6576
6577 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6578 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6579 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6580
6581 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6582 dcmd->cmd = MFI_CMD_DCMD;
6583 dcmd->cmd_status = 0xFF;
6584 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006585 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08006586 dcmd->timeout = 0;
6587 dcmd->pad_0 = 0;
6588 dcmd->data_xfer_len =
6589 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6590 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08006591
Shivasharan S107a60d2017-10-19 02:49:05 -07006592 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6593 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08006594
Shivasharan Se7d36b82017-10-19 02:48:50 -07006595 if ((instance->adapter_type != MFI_SERIES) &&
6596 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08006597 ret = megasas_issue_blocked_cmd(instance,
6598 cmd, MFI_IO_TIMEOUT_SECS);
6599 else
6600 ret = megasas_issue_polled(instance, cmd);
6601
6602 switch (ret) {
6603 case DCMD_TIMEOUT:
6604 switch (dcmd_timeout_ocr_possible(instance)) {
6605 case INITIATE_OCR:
6606 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05306607 mutex_unlock(&instance->reset_mutex);
Shivasharan S96188a82017-02-10 00:59:11 -08006608 megasas_reset_fusion(instance->host,
6609 MFI_IO_TIMEOUT_OCR);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05306610 mutex_lock(&instance->reset_mutex);
Shivasharan S96188a82017-02-10 00:59:11 -08006611 break;
6612 case KILL_ADAPTER:
6613 megaraid_sas_kill_hba(instance);
6614 break;
6615 case IGNORE_TIMEOUT:
6616 dev_info(&instance->pdev->dev,
6617 "Ignore DCMD timeout: %s %d\n",
6618 __func__, __LINE__);
6619 break;
6620 }
6621 break;
6622
6623 default:
6624 megasas_return_cmd(instance, cmd);
6625 }
6626 if (ret != DCMD_SUCCESS)
6627 dev_err(&instance->pdev->dev,
6628 "return from %s %d return value %d\n",
6629 __func__, __LINE__, ret);
6630
6631 return ret;
6632}
6633
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006634/**
6635 * megasas_start_aen - Subscribes to AEN during driver load time
6636 * @instance: Adapter soft state
6637 */
6638static int megasas_start_aen(struct megasas_instance *instance)
6639{
6640 struct megasas_evt_log_info eli;
6641 union megasas_evt_class_locale class_locale;
6642
6643 /*
6644 * Get the latest sequence number from FW
6645 */
6646 memset(&eli, 0, sizeof(eli));
6647
6648 if (megasas_get_seq_num(instance, &eli))
6649 return -1;
6650
6651 /*
6652 * Register AEN with FW for latest sequence number plus 1
6653 */
6654 class_locale.members.reserved = 0;
6655 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6656 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6657
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306658 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05306659 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306660 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006661}
6662
6663/**
6664 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6665 * @instance: Adapter soft state
6666 */
6667static int megasas_io_attach(struct megasas_instance *instance)
6668{
6669 struct Scsi_Host *host = instance->host;
6670
6671 /*
6672 * Export parameters required by SCSI mid-layer
6673 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006674 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306675 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006676 host->this_id = instance->init_id;
6677 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08006678
6679 if (instance->fw_support_ieee)
6680 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6681
Yang, Bo1fd10682010-10-12 07:18:50 -06006682 /*
6683 * Check if the module parameter value for max_sectors can be used
6684 */
6685 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6686 instance->max_sectors_per_req = max_sectors;
6687 else {
6688 if (max_sectors) {
6689 if (((instance->pdev->device ==
6690 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6691 (instance->pdev->device ==
6692 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6693 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6694 instance->max_sectors_per_req = max_sectors;
6695 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006696 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06006697 "and <= %d (or < 1MB for GEN2 controller)\n",
6698 instance->max_sectors_per_req);
6699 }
6700 }
6701 }
6702
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006703 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08006704 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006705 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6706 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6707 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08006708 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006709
6710 /*
6711 * Notify the mid-layer about the new controller
6712 */
6713 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306714 dev_err(&instance->pdev->dev,
6715 "Failed to add host from %s %d\n",
6716 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006717 return -ENODEV;
6718 }
6719
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006720 return 0;
6721}
6722
Shivasharan S107a60d2017-10-19 02:49:05 -07006723/**
6724 * megasas_set_dma_mask - Set DMA mask for supported controllers
6725 *
6726 * @instance: Adapter soft state
6727 * Description:
6728 *
Shivasharan S894169d2018-12-18 05:59:54 -08006729 * For Ventura, driver/FW will operate in 63bit DMA addresses.
Shivasharan S107a60d2017-10-19 02:49:05 -07006730 *
6731 * For invader-
6732 * By default, driver/FW will operate in 32bit DMA addresses
6733 * for consistent DMA mapping but if 32 bit consistent
Shivasharan S894169d2018-12-18 05:59:54 -08006734 * DMA mask fails, driver will try with 63 bit consistent
6735 * mask provided FW is true 63bit DMA capable
Shivasharan S107a60d2017-10-19 02:49:05 -07006736 *
6737 * For older controllers(Thunderbolt and MFI based adapters)-
6738 * driver/FW will operate in 32 bit consistent DMA addresses.
6739 */
bo yang31ea7082007-11-07 12:09:50 -05006740static int
Shivasharan S107a60d2017-10-19 02:49:05 -07006741megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05006742{
Shivasharan S107a60d2017-10-19 02:49:05 -07006743 u64 consistent_mask;
6744 struct pci_dev *pdev;
Shivasharan S81b76452018-10-16 23:37:51 -07006745 u32 scratch_pad_1;
bo yang31ea7082007-11-07 12:09:50 -05006746
Shivasharan S107a60d2017-10-19 02:49:05 -07006747 pdev = instance->pdev;
Shivasharan S630d42b2018-12-17 00:47:37 -08006748 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
Shivasharan S894169d2018-12-18 05:59:54 -08006749 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
Shivasharan S107a60d2017-10-19 02:49:05 -07006750
6751 if (IS_DMA64) {
Shivasharan S894169d2018-12-18 05:59:54 -08006752 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006753 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6754 goto fail_set_dma_mask;
6755
Shivasharan S894169d2018-12-18 05:59:54 -08006756 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006757 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6758 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6759 /*
6760 * If 32 bit DMA mask fails, then try for 64 bit mask
6761 * for FW capable of handling 64 bit DMA.
6762 */
Shivasharan S272652f2018-12-17 00:47:40 -08006763 scratch_pad_1 = megasas_readl
6764 (instance, &instance->reg_set->outbound_scratch_pad_1);
Shivasharan S107a60d2017-10-19 02:49:05 -07006765
Shivasharan S81b76452018-10-16 23:37:51 -07006766 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
Shivasharan S107a60d2017-10-19 02:49:05 -07006767 goto fail_set_dma_mask;
6768 else if (dma_set_mask_and_coherent(&pdev->dev,
Shivasharan S894169d2018-12-18 05:59:54 -08006769 DMA_BIT_MASK(63)))
bo yang31ea7082007-11-07 12:09:50 -05006770 goto fail_set_dma_mask;
6771 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006772 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6773 goto fail_set_dma_mask;
6774
6775 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6776 instance->consistent_mask_64bit = false;
6777 else
6778 instance->consistent_mask_64bit = true;
6779
6780 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
Tomas Henzld1f38d92019-01-02 16:07:25 +01006781 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
Shivasharan S894169d2018-12-18 05:59:54 -08006782 (instance->consistent_mask_64bit ? "63" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306783
bo yang31ea7082007-11-07 12:09:50 -05006784 return 0;
6785
6786fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006787 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6788 return -1;
6789
bo yang31ea7082007-11-07 12:09:50 -05006790}
6791
Shivasharan Sc3651782017-10-19 02:48:48 -07006792/*
6793 * megasas_set_adapter_type - Set adapter type.
6794 * Supported controllers can be divided in
Shivasharan S154a7cd2018-12-17 00:47:38 -08006795 * different categories-
6796 * enum MR_ADAPTER_TYPE {
6797 * MFI_SERIES = 1,
6798 * THUNDERBOLT_SERIES = 2,
6799 * INVADER_SERIES = 3,
6800 * VENTURA_SERIES = 4,
6801 * AERO_SERIES = 5,
6802 * };
Shivasharan Sc3651782017-10-19 02:48:48 -07006803 * @instance: Adapter soft state
6804 * return: void
6805 */
6806static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6807{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006808 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6809 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006810 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006811 } else {
6812 switch (instance->pdev->device) {
Shivasharan S469f72d2018-11-09 09:47:20 -08006813 case PCI_DEVICE_ID_LSI_AERO_10E1:
6814 case PCI_DEVICE_ID_LSI_AERO_10E2:
6815 case PCI_DEVICE_ID_LSI_AERO_10E5:
6816 case PCI_DEVICE_ID_LSI_AERO_10E6:
Shivasharan S154a7cd2018-12-17 00:47:38 -08006817 instance->adapter_type = AERO_SERIES;
6818 break;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006819 case PCI_DEVICE_ID_LSI_VENTURA:
6820 case PCI_DEVICE_ID_LSI_CRUSADER:
6821 case PCI_DEVICE_ID_LSI_HARPOON:
6822 case PCI_DEVICE_ID_LSI_TOMCAT:
6823 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6824 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6825 instance->adapter_type = VENTURA_SERIES;
6826 break;
6827 case PCI_DEVICE_ID_LSI_FUSION:
6828 case PCI_DEVICE_ID_LSI_PLASMA:
6829 instance->adapter_type = THUNDERBOLT_SERIES;
6830 break;
6831 case PCI_DEVICE_ID_LSI_INVADER:
6832 case PCI_DEVICE_ID_LSI_INTRUDER:
6833 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6834 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6835 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6836 case PCI_DEVICE_ID_LSI_FURY:
6837 instance->adapter_type = INVADER_SERIES;
6838 break;
6839 default: /* For all other supported controllers */
6840 instance->adapter_type = MFI_SERIES;
6841 break;
6842 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006843 }
6844}
6845
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006846static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6847{
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006848 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6849 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6850 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6851 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006852
6853 if (!instance->producer || !instance->consumer) {
6854 dev_err(&instance->pdev->dev,
6855 "Failed to allocate memory for producer, consumer\n");
6856 return -1;
6857 }
6858
6859 *instance->producer = 0;
6860 *instance->consumer = 0;
6861 return 0;
6862}
6863
6864/**
6865 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6866 * structures which are not common across MFI
6867 * adapters and fusion adapters.
6868 * For MFI based adapters, allocate producer and
6869 * consumer buffers. For fusion adapters, allocate
6870 * memory for fusion context.
6871 * @instance: Adapter soft state
6872 * return: 0 for SUCCESS
6873 */
6874static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6875{
Kees Cook6396bb22018-06-12 14:03:40 -07006876 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
Ming Leiadbe5522018-03-13 17:42:40 +08006877 GFP_KERNEL);
6878 if (!instance->reply_map)
6879 return -ENOMEM;
6880
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006881 switch (instance->adapter_type) {
6882 case MFI_SERIES:
6883 if (megasas_alloc_mfi_ctrl_mem(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006884 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006885 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08006886 case AERO_SERIES:
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006887 case VENTURA_SERIES:
6888 case THUNDERBOLT_SERIES:
6889 case INVADER_SERIES:
6890 if (megasas_alloc_fusion_context(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006891 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006892 break;
6893 }
6894
6895 return 0;
Ming Leiadbe5522018-03-13 17:42:40 +08006896 fail:
6897 kfree(instance->reply_map);
6898 instance->reply_map = NULL;
6899 return -ENOMEM;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006900}
6901
6902/*
6903 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6904 * producer, consumer buffers for MFI adapters
6905 *
6906 * @instance - Adapter soft instance
6907 *
6908 */
6909static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6910{
Ming Leiadbe5522018-03-13 17:42:40 +08006911 kfree(instance->reply_map);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006912 if (instance->adapter_type == MFI_SERIES) {
6913 if (instance->producer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006914 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006915 instance->producer,
6916 instance->producer_h);
6917 if (instance->consumer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006918 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006919 instance->consumer,
6920 instance->consumer_h);
6921 } else {
6922 megasas_free_fusion_context(instance);
6923 }
6924}
6925
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006926/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006927 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6928 * driver load time
6929 *
6930 * @instance- Adapter soft instance
6931 * @return- O for SUCCESS
6932 */
6933static inline
6934int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6935{
6936 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006937 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006938
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006939 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6940 sizeof(struct megasas_evt_detail),
6941 &instance->evt_detail_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006942
6943 if (!instance->evt_detail) {
6944 dev_err(&instance->pdev->dev,
6945 "Failed to allocate event detail buffer\n");
6946 return -ENOMEM;
6947 }
6948
Shivasharan S9b3d0282017-10-19 02:48:56 -07006949 if (fusion) {
6950 fusion->ioc_init_request =
6951 dma_alloc_coherent(&pdev->dev,
6952 sizeof(struct MPI2_IOC_INIT_REQUEST),
6953 &fusion->ioc_init_request_phys,
6954 GFP_KERNEL);
6955
6956 if (!fusion->ioc_init_request) {
6957 dev_err(&pdev->dev,
6958 "Failed to allocate PD list buffer\n");
6959 return -ENOMEM;
6960 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006961
6962 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6963 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6964 &instance->snapdump_prop_h, GFP_KERNEL);
6965
6966 if (!instance->snapdump_prop)
6967 dev_err(&pdev->dev,
6968 "Failed to allocate snapdump properties buffer\n");
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006969
6970 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6971 HOST_DEVICE_LIST_SZ,
6972 &instance->host_device_list_buf_h,
6973 GFP_KERNEL);
6974
6975 if (!instance->host_device_list_buf) {
6976 dev_err(&pdev->dev,
6977 "Failed to allocate targetid list buffer\n");
6978 return -ENOMEM;
6979 }
6980
Shivasharan S9b3d0282017-10-19 02:48:56 -07006981 }
6982
6983 instance->pd_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006984 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006985 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006986 &instance->pd_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006987
6988 if (!instance->pd_list_buf) {
6989 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6990 return -ENOMEM;
6991 }
6992
6993 instance->ctrl_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006994 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006995 sizeof(struct megasas_ctrl_info),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006996 &instance->ctrl_info_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006997
6998 if (!instance->ctrl_info_buf) {
6999 dev_err(&pdev->dev,
7000 "Failed to allocate controller info buffer\n");
7001 return -ENOMEM;
7002 }
7003
7004 instance->ld_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007005 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07007006 sizeof(struct MR_LD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007007 &instance->ld_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07007008
7009 if (!instance->ld_list_buf) {
7010 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7011 return -ENOMEM;
7012 }
7013
7014 instance->ld_targetid_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007015 dma_alloc_coherent(&pdev->dev,
7016 sizeof(struct MR_LD_TARGETID_LIST),
7017 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07007018
7019 if (!instance->ld_targetid_list_buf) {
7020 dev_err(&pdev->dev,
7021 "Failed to allocate LD targetid list buffer\n");
7022 return -ENOMEM;
7023 }
7024
Shivasharan S1b4bed22017-10-19 02:48:55 -07007025 if (!reset_devices) {
7026 instance->system_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007027 dma_alloc_coherent(&pdev->dev,
7028 sizeof(struct MR_DRV_SYSTEM_INFO),
7029 &instance->system_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007030 instance->pd_info =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007031 dma_alloc_coherent(&pdev->dev,
7032 sizeof(struct MR_PD_INFO),
7033 &instance->pd_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007034 instance->tgt_prop =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007035 dma_alloc_coherent(&pdev->dev,
7036 sizeof(struct MR_TARGET_PROPERTIES),
7037 &instance->tgt_prop_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007038 instance->crash_dump_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007039 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7040 &instance->crash_dump_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07007041
7042 if (!instance->system_info_buf)
7043 dev_err(&instance->pdev->dev,
7044 "Failed to allocate system info buffer\n");
7045
7046 if (!instance->pd_info)
7047 dev_err(&instance->pdev->dev,
7048 "Failed to allocate pd_info buffer\n");
7049
7050 if (!instance->tgt_prop)
7051 dev_err(&instance->pdev->dev,
7052 "Failed to allocate tgt_prop buffer\n");
7053
7054 if (!instance->crash_dump_buf)
7055 dev_err(&instance->pdev->dev,
7056 "Failed to allocate crash dump buffer\n");
7057 }
7058
7059 return 0;
7060}
7061
7062/*
7063 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
7064 * during driver load time
7065 *
7066 * @instance- Adapter soft instance
7067 *
7068 */
7069static inline
7070void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7071{
7072 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07007073 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07007074
7075 if (instance->evt_detail)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007076 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007077 instance->evt_detail,
7078 instance->evt_detail_h);
7079
Shivasharan S9b3d0282017-10-19 02:48:56 -07007080 if (fusion && fusion->ioc_init_request)
7081 dma_free_coherent(&pdev->dev,
7082 sizeof(struct MPI2_IOC_INIT_REQUEST),
7083 fusion->ioc_init_request,
7084 fusion->ioc_init_request_phys);
7085
7086 if (instance->pd_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007087 dma_free_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07007088 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7089 instance->pd_list_buf,
7090 instance->pd_list_buf_h);
7091
7092 if (instance->ld_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007093 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007094 instance->ld_list_buf,
7095 instance->ld_list_buf_h);
7096
7097 if (instance->ld_targetid_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007098 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007099 instance->ld_targetid_list_buf,
7100 instance->ld_targetid_list_buf_h);
7101
7102 if (instance->ctrl_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007103 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
Shivasharan S9b3d0282017-10-19 02:48:56 -07007104 instance->ctrl_info_buf,
7105 instance->ctrl_info_buf_h);
7106
Shivasharan S1b4bed22017-10-19 02:48:55 -07007107 if (instance->system_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007108 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007109 instance->system_info_buf,
7110 instance->system_info_h);
7111
7112 if (instance->pd_info)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007113 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007114 instance->pd_info, instance->pd_info_h);
7115
7116 if (instance->tgt_prop)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007117 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
Shivasharan S1b4bed22017-10-19 02:48:55 -07007118 instance->tgt_prop, instance->tgt_prop_h);
7119
7120 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007121 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
Shivasharan S1b4bed22017-10-19 02:48:55 -07007122 instance->crash_dump_buf,
7123 instance->crash_dump_h);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07007124
7125 if (instance->snapdump_prop)
7126 dma_free_coherent(&pdev->dev,
7127 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7128 instance->snapdump_prop,
7129 instance->snapdump_prop_h);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08007130
7131 if (instance->host_device_list_buf)
7132 dma_free_coherent(&pdev->dev,
7133 HOST_DEVICE_LIST_SZ,
7134 instance->host_device_list_buf,
7135 instance->host_device_list_buf_h);
7136
Shivasharan S1b4bed22017-10-19 02:48:55 -07007137}
7138
Shivasharan S7535f272017-10-19 02:48:58 -07007139/*
7140 * megasas_init_ctrl_params - Initialize controller's instance
7141 * parameters before FW init
7142 * @instance - Adapter soft instance
7143 * @return - void
7144 */
7145static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7146{
7147 instance->fw_crash_state = UNAVAILABLE;
7148
7149 megasas_poll_wait_aen = 0;
7150 instance->issuepend_done = 1;
7151 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7152
7153 /*
7154 * Initialize locks and queues
7155 */
7156 INIT_LIST_HEAD(&instance->cmd_pool);
7157 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7158
7159 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S1d15d902019-05-07 10:05:36 -07007160 atomic64_set(&instance->total_io_count, 0);
Shivasharan S7535f272017-10-19 02:48:58 -07007161
7162 init_waitqueue_head(&instance->int_cmd_wait_q);
7163 init_waitqueue_head(&instance->abort_cmd_wait_q);
7164
7165 spin_lock_init(&instance->crashdump_lock);
7166 spin_lock_init(&instance->mfi_pool_lock);
7167 spin_lock_init(&instance->hba_lock);
7168 spin_lock_init(&instance->stream_lock);
7169 spin_lock_init(&instance->completion_lock);
7170
Shivasharan S7535f272017-10-19 02:48:58 -07007171 mutex_init(&instance->reset_mutex);
7172
7173 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7174 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7175 instance->flag_ieee = 1;
7176
7177 megasas_dbg_lvl = 0;
7178 instance->flag = 0;
7179 instance->unload = 1;
7180 instance->last_time = 0;
7181 instance->disableOnlineCtrlReset = 1;
7182 instance->UnevenSpanSupport = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07007183 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7184 instance->msix_load_balance = false;
Shivasharan S7535f272017-10-19 02:48:58 -07007185
Shivasharan S3f6194a2018-10-16 23:37:39 -07007186 if (instance->adapter_type != MFI_SERIES)
Shivasharan S7535f272017-10-19 02:48:58 -07007187 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Shivasharan S3f6194a2018-10-16 23:37:39 -07007188 else
Shivasharan S7535f272017-10-19 02:48:58 -07007189 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007190}
7191
7192/**
7193 * megasas_probe_one - PCI hotplug entry point
7194 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08007195 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007196 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007197static int megasas_probe_one(struct pci_dev *pdev,
7198 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007199{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307200 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007201 struct Scsi_Host *host;
7202 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08007203 u16 control = 0;
7204
Shivasharan S469f72d2018-11-09 09:47:20 -08007205 switch (pdev->device) {
Chandrakanth Patildd807692019-06-25 16:34:20 +05307206 case PCI_DEVICE_ID_LSI_AERO_10E0:
7207 case PCI_DEVICE_ID_LSI_AERO_10E3:
7208 case PCI_DEVICE_ID_LSI_AERO_10E4:
7209 case PCI_DEVICE_ID_LSI_AERO_10E7:
7210 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7211 return 1;
Shivasharan S469f72d2018-11-09 09:47:20 -08007212 case PCI_DEVICE_ID_LSI_AERO_10E1:
7213 case PCI_DEVICE_ID_LSI_AERO_10E5:
7214 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7215 break;
7216 }
7217
adam radford66192dfe2011-02-24 20:56:28 -08007218 /* Reset MSI-X in the kdump kernel */
7219 if (reset_devices) {
7220 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7221 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06007222 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08007223 &control);
7224 if (control & PCI_MSIX_FLAGS_ENABLE) {
7225 dev_info(&pdev->dev, "resetting MSI-X\n");
7226 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06007227 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08007228 control &
7229 ~PCI_MSIX_FLAGS_ENABLE);
7230 }
7231 }
7232 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007233
7234 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007235 * PCI prepping: enable device set bus mastering and dma mask
7236 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007237 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007238
7239 if (rval) {
7240 return rval;
7241 }
7242
7243 pci_set_master(pdev);
7244
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007245 host = scsi_host_alloc(&megasas_template,
7246 sizeof(struct megasas_instance));
7247
7248 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007249 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007250 goto fail_alloc_instance;
7251 }
7252
7253 instance = (struct megasas_instance *)host->hostdata;
7254 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007255 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007256
7257 /*
7258 * Initialize PCI related and misc parameters
7259 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007260 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007261 instance->host = host;
7262 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7263 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7264
Shivasharan Sc3651782017-10-19 02:48:48 -07007265 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07007266
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007267 /*
adam radford0a770662011-02-24 20:56:12 -08007268 * Initialize MFI Firmware
7269 */
7270 if (megasas_init_fw(instance))
7271 goto fail_init_mfi;
7272
adam radford229fe472014-03-10 02:51:56 -07007273 if (instance->requestorId) {
7274 if (instance->PlasmaFW111) {
7275 instance->vf_affiliation_111 =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007276 dma_alloc_coherent(&pdev->dev,
7277 sizeof(struct MR_LD_VF_AFFILIATION_111),
7278 &instance->vf_affiliation_111_h,
7279 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007280 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007281 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007282 "memory for VF affiliation buffer\n");
7283 } else {
7284 instance->vf_affiliation =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007285 dma_alloc_coherent(&pdev->dev,
7286 (MAX_LOGICAL_DRIVES + 1) *
7287 sizeof(struct MR_LD_VF_AFFILIATION),
7288 &instance->vf_affiliation_h,
7289 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007290 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007291 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007292 "memory for VF affiliation buffer\n");
7293 }
7294 }
7295
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007296 /*
7297 * Store instance in PCI softstate
7298 */
7299 pci_set_drvdata(pdev, instance);
7300
7301 /*
7302 * Add this controller to megasas_mgmt_info structure so that it
7303 * can be exported to management applications
7304 */
7305 megasas_mgmt_info.count++;
7306 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7307 megasas_mgmt_info.max_index++;
7308
7309 /*
adam radford541f90b2011-05-11 18:34:29 -07007310 * Register with SCSI mid-layer
7311 */
7312 if (megasas_io_attach(instance))
7313 goto fail_io_attach;
7314
7315 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307316 /*
7317 * Trigger SCSI to scan our drives
7318 */
Shivasharan Sf6fe5732019-01-29 01:38:14 -08007319 if (!instance->enable_fw_dev_list ||
7320 (instance->host_device_list_buf->count > 0))
7321 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07007322
7323 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007324 * Initiate AEN (Asynchronous Event Notification)
7325 */
7326 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007327 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007328 goto fail_start_aen;
7329 }
7330
Shivasharan Sba535722019-05-07 10:05:49 -07007331 megasas_setup_debugfs(instance);
7332
Adam Radford9ea81f82014-07-09 15:17:57 -07007333 /* Get current SR-IOV LD/VF affiliation */
7334 if (instance->requestorId)
7335 megasas_get_ld_vf_affiliation(instance, 1);
7336
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007337 return 0;
7338
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007339fail_start_aen:
7340fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007341 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007342 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08007343 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007344
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307345 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307346 megasas_destroy_irqs(instance);
7347
Shivasharan Se7d36b82017-10-19 02:48:50 -07007348 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08007349 megasas_release_fusion(instance);
7350 else
7351 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007352 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007353 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307354fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007355 scsi_host_put(host);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007356fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007357 pci_disable_device(pdev);
7358
7359 return -ENODEV;
7360}
7361
7362/**
7363 * megasas_flush_cache - Requests FW to flush all its caches
7364 * @instance: Adapter soft state
7365 */
7366static void megasas_flush_cache(struct megasas_instance *instance)
7367{
7368 struct megasas_cmd *cmd;
7369 struct megasas_dcmd_frame *dcmd;
7370
Sumit Saxena8a01a412016-01-28 21:04:32 +05307371 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007372 return;
7373
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007374 cmd = megasas_get_cmd(instance);
7375
7376 if (!cmd)
7377 return;
7378
7379 dcmd = &cmd->frame->dcmd;
7380
7381 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7382
7383 dcmd->cmd = MFI_CMD_DCMD;
7384 dcmd->cmd_status = 0x0;
7385 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307386 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007387 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007388 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007389 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307390 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007391 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7392
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307393 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7394 != DCMD_SUCCESS) {
7395 dev_err(&instance->pdev->dev,
7396 "return from %s %d\n", __func__, __LINE__);
7397 return;
7398 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007399
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307400 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007401}
7402
7403/**
7404 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7405 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05007406 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007407 */
bo yang31ea7082007-11-07 12:09:50 -05007408static void megasas_shutdown_controller(struct megasas_instance *instance,
7409 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007410{
7411 struct megasas_cmd *cmd;
7412 struct megasas_dcmd_frame *dcmd;
7413
Sumit Saxena8a01a412016-01-28 21:04:32 +05307414 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007415 return;
7416
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007417 cmd = megasas_get_cmd(instance);
7418
7419 if (!cmd)
7420 return;
7421
7422 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307423 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307424 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08007425 if (instance->map_update_cmd)
7426 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307427 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307428 if (instance->jbod_seq_cmd)
7429 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307430 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307431
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007432 dcmd = &cmd->frame->dcmd;
7433
7434 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7435
7436 dcmd->cmd = MFI_CMD_DCMD;
7437 dcmd->cmd_status = 0x0;
7438 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307439 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007440 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007441 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007442 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307443 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007444
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307445 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7446 != DCMD_SUCCESS) {
7447 dev_err(&instance->pdev->dev,
7448 "return from %s %d\n", __func__, __LINE__);
7449 return;
7450 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007451
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307452 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007453}
7454
Jiri Slaby33139b22008-05-01 17:56:02 +02007455#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007456/**
bo yangad84db22007-11-09 04:40:16 -05007457 * megasas_suspend - driver suspend entry point
7458 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05007459 * @state: PCI power state to suspend routine
7460 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007461static int
bo yang31ea7082007-11-07 12:09:50 -05007462megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7463{
bo yang31ea7082007-11-07 12:09:50 -05007464 struct megasas_instance *instance;
7465
7466 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307467
7468 if (!instance)
7469 return 0;
7470
Yang, Bo0c79e682009-10-06 14:47:35 -06007471 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05007472
Shivasharan Sf7331f12019-05-07 10:05:46 -07007473 dev_info(&pdev->dev, "%s is called\n", __func__);
7474
adam radford229fe472014-03-10 02:51:56 -07007475 /* Shutdown SR-IOV heartbeat timer */
7476 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7477 del_timer_sync(&instance->sriov_heartbeat_timer);
7478
Shivasharan S3f6194a2018-10-16 23:37:39 -07007479 /* Stop the FW fault detection watchdog */
7480 if (instance->adapter_type != MFI_SERIES)
7481 megasas_fusion_stop_watchdog(instance);
7482
bo yang31ea7082007-11-07 12:09:50 -05007483 megasas_flush_cache(instance);
7484 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007485
7486 /* cancel the delayed work if this work still in queue */
7487 if (instance->ev != NULL) {
7488 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007489 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007490 instance->ev = NULL;
7491 }
7492
bo yang31ea7082007-11-07 12:09:50 -05007493 tasklet_kill(&instance->isr_tasklet);
7494
7495 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307496 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007497
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307498 megasas_destroy_irqs(instance);
7499
adam radfordc8e858f2011-10-08 18:15:13 -07007500 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007501 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05007502
7503 pci_save_state(pdev);
7504 pci_disable_device(pdev);
7505
7506 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7507
7508 return 0;
7509}
7510
7511/**
7512 * megasas_resume- driver resume entry point
7513 * @pdev: PCI device structure
7514 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007515static int
bo yang31ea7082007-11-07 12:09:50 -05007516megasas_resume(struct pci_dev *pdev)
7517{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307518 int rval;
bo yang31ea7082007-11-07 12:09:50 -05007519 struct Scsi_Host *host;
7520 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01007521 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05007522
7523 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307524
7525 if (!instance)
7526 return 0;
7527
bo yang31ea7082007-11-07 12:09:50 -05007528 host = instance->host;
7529 pci_set_power_state(pdev, PCI_D0);
7530 pci_enable_wake(pdev, PCI_D0, 0);
7531 pci_restore_state(pdev);
7532
Shivasharan Sf7331f12019-05-07 10:05:46 -07007533 dev_info(&pdev->dev, "%s is called\n", __func__);
bo yang31ea7082007-11-07 12:09:50 -05007534 /*
7535 * PCI prepping: enable device set bus mastering and dma mask
7536 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007537 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05007538
7539 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007540 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05007541 return rval;
7542 }
7543
7544 pci_set_master(pdev);
7545
Shivasharan S107a60d2017-10-19 02:49:05 -07007546 /*
7547 * We expect the FW state to be READY
7548 */
7549 if (megasas_transition_to_ready(instance, 0))
7550 goto fail_ready_state;
7551
7552 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05007553 goto fail_set_dma_mask;
7554
7555 /*
7556 * Initialize MFI Firmware
7557 */
7558
bo yang31ea7082007-11-07 12:09:50 -05007559 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08007560 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05007561
adam radford3f1abce2011-05-11 18:33:47 -07007562 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01007563 if (instance->msix_vectors) {
7564 irq_flags = PCI_IRQ_MSIX;
Shivasharan S1d15d902019-05-07 10:05:36 -07007565 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007566 irq_flags |= PCI_IRQ_AFFINITY;
7567 }
7568 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7569 instance->msix_vectors ?
7570 instance->msix_vectors : 1, irq_flags);
7571 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02007572 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07007573
Ming Leiadbe5522018-03-13 17:42:40 +08007574 megasas_setup_reply_map(instance);
7575
Shivasharan Se7d36b82017-10-19 02:48:50 -07007576 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007577 megasas_reset_reply_desc(instance);
7578 if (megasas_ioc_init_fusion(instance)) {
7579 megasas_free_cmds(instance);
7580 megasas_free_cmds_fusion(instance);
7581 goto fail_init_mfi;
7582 }
7583 if (!megasas_get_map_info(instance))
7584 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307585 } else {
adam radford9c915a82010-12-21 13:34:31 -08007586 *instance->producer = 0;
7587 *instance->consumer = 0;
7588 if (megasas_issue_init_mfi(instance))
7589 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08007590 }
bo yang31ea7082007-11-07 12:09:50 -05007591
Shivasharan Sc3b10a52018-06-04 03:45:10 -07007592 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7593 goto fail_init_mfi;
7594
adam radford9c915a82010-12-21 13:34:31 -08007595 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7596 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05007597
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307598 if (instance->msix_vectors ?
7599 megasas_setup_irqs_msix(instance, 0) :
7600 megasas_setup_irqs_ioapic(instance))
7601 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05007602
Shivasharan S62a04f82019-05-07 10:05:35 -07007603 if (instance->adapter_type != MFI_SERIES)
7604 megasas_setup_irq_poll(instance);
7605
adam radford229fe472014-03-10 02:51:56 -07007606 /* Re-launch SR-IOV heartbeat timer */
7607 if (instance->requestorId) {
7608 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07007609 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307610 else {
adam radford229fe472014-03-10 02:51:56 -07007611 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307612 goto fail_init_mfi;
7613 }
adam radford229fe472014-03-10 02:51:56 -07007614 }
7615
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307616 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307617 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06007618 instance->unload = 0;
7619
adam radford541f90b2011-05-11 18:34:29 -07007620 /*
7621 * Initiate AEN (Asynchronous Event Notification)
7622 */
7623 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007624 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07007625
Shivasharan S3f6194a2018-10-16 23:37:39 -07007626 /* Re-launch FW fault watchdog */
7627 if (instance->adapter_type != MFI_SERIES)
7628 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7629 goto fail_start_watchdog;
7630
bo yang31ea7082007-11-07 12:09:50 -05007631 return 0;
7632
Shivasharan S3f6194a2018-10-16 23:37:39 -07007633fail_start_watchdog:
7634 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7635 del_timer_sync(&instance->sriov_heartbeat_timer);
bo yang31ea7082007-11-07 12:09:50 -05007636fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07007637 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007638 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05007639 scsi_host_put(host);
7640
Shivasharan S107a60d2017-10-19 02:49:05 -07007641fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05007642fail_set_dma_mask:
7643fail_ready_state:
7644
7645 pci_disable_device(pdev);
7646
7647 return -ENODEV;
7648}
Jiri Slaby33139b22008-05-01 17:56:02 +02007649#else
7650#define megasas_suspend NULL
7651#define megasas_resume NULL
7652#endif
bo yang31ea7082007-11-07 12:09:50 -05007653
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007654static inline int
7655megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7656{
7657 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7658 int i;
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007659 u8 adp_state;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007660
7661 for (i = 0; i < wait_time; i++) {
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007662 adp_state = atomic_read(&instance->adprecovery);
7663 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7664 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007665 break;
7666
7667 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7668 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7669
7670 msleep(1000);
7671 }
7672
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007673 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7674 dev_info(&instance->pdev->dev,
7675 "%s HBA failed to become operational, adp_state %d\n",
7676 __func__, adp_state);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007677 return 1;
7678 }
7679
7680 return 0;
7681}
7682
bo yang31ea7082007-11-07 12:09:50 -05007683/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007684 * megasas_detach_one - PCI hot"un"plug entry point
7685 * @pdev: PCI device structure
7686 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007687static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007688{
7689 int i;
7690 struct Scsi_Host *host;
7691 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08007692 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307693 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007694
7695 instance = pci_get_drvdata(pdev);
Chandrakanth Patildd807692019-06-25 16:34:20 +05307696
7697 if (!instance)
7698 return;
7699
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007700 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08007701 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007702
adam radford229fe472014-03-10 02:51:56 -07007703 /* Shutdown SR-IOV heartbeat timer */
7704 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7705 del_timer_sync(&instance->sriov_heartbeat_timer);
7706
Shivasharan S3f6194a2018-10-16 23:37:39 -07007707 /* Stop the FW fault detection watchdog */
7708 if (instance->adapter_type != MFI_SERIES)
7709 megasas_fusion_stop_watchdog(instance);
7710
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307711 if (instance->fw_crash_state != UNAVAILABLE)
7712 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007713 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08007714 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007715
7716 if (megasas_wait_for_adapter_operational(instance))
7717 goto skip_firing_dcmds;
7718
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007719 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05007720 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007721
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007722skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007723 /* cancel the delayed work if this work still in queue*/
7724 if (instance->ev != NULL) {
7725 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007726 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007727 instance->ev = NULL;
7728 }
7729
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307730 /* cancel all wait events */
7731 wake_up_all(&instance->int_cmd_wait_q);
7732
Sumant Patro5d018ad2006-10-03 13:13:18 -07007733 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007734
7735 /*
7736 * Take the instance off the instance array. Note that we will not
7737 * decrement the max_index. We let this array be sparse array
7738 */
7739 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7740 if (megasas_mgmt_info.instance[i] == instance) {
7741 megasas_mgmt_info.count--;
7742 megasas_mgmt_info.instance[i] = NULL;
7743
7744 break;
7745 }
7746 }
7747
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307748 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007749
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307750 megasas_destroy_irqs(instance);
7751
adam radfordc8e858f2011-10-08 18:15:13 -07007752 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007753 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007754
Shivasharan S630d42b2018-12-17 00:47:37 -08007755 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05007756 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7757 kfree(fusion->stream_detect_by_ld[i]);
7758 kfree(fusion->stream_detect_by_ld);
7759 fusion->stream_detect_by_ld = NULL;
7760 }
7761
7762
Shivasharan Se7d36b82017-10-19 02:48:50 -07007763 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007764 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307765 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7766 (sizeof(struct MR_PD_CFG_SEQ) *
7767 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307768 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08007769 if (fusion->ld_map[i])
7770 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307771 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08007772 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307773 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07007774 if (fusion->ld_drv_map[i]) {
7775 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7776 vfree(fusion->ld_drv_map[i]);
7777 else
7778 free_pages((ulong)fusion->ld_drv_map[i],
7779 fusion->drv_map_pages);
7780 }
7781
Maurizio Lombardi546e5592016-01-22 13:41:42 +01007782 if (fusion->pd_seq_sync[i])
7783 dma_free_coherent(&instance->pdev->dev,
7784 pd_seq_map_sz,
7785 fusion->pd_seq_sync[i],
7786 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307787 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307788 } else {
adam radford9c915a82010-12-21 13:34:31 -08007789 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08007790 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007791
adam radford229fe472014-03-10 02:51:56 -07007792 if (instance->vf_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007793 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
adam radford229fe472014-03-10 02:51:56 -07007794 sizeof(struct MR_LD_VF_AFFILIATION),
7795 instance->vf_affiliation,
7796 instance->vf_affiliation_h);
7797
7798 if (instance->vf_affiliation_111)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007799 dma_free_coherent(&pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07007800 sizeof(struct MR_LD_VF_AFFILIATION_111),
7801 instance->vf_affiliation_111,
7802 instance->vf_affiliation_111_h);
7803
7804 if (instance->hb_host_mem)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007805 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
adam radford229fe472014-03-10 02:51:56 -07007806 instance->hb_host_mem,
7807 instance->hb_host_mem_h);
7808
Shivasharan S1b4bed22017-10-19 02:48:55 -07007809 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307810
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007811 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307812
Shivasharan Sba535722019-05-07 10:05:49 -07007813 megasas_destroy_debugfs(instance);
7814
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007815 scsi_host_put(host);
7816
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007817 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007818}
7819
7820/**
7821 * megasas_shutdown - Shutdown entry point
7822 * @device: Generic device structure
7823 */
7824static void megasas_shutdown(struct pci_dev *pdev)
7825{
7826 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07007827
Chandrakanth Patildd807692019-06-25 16:34:20 +05307828 if (!instance)
7829 return;
7830
Yang, Bo0c79e682009-10-06 14:47:35 -06007831 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007832
7833 if (megasas_wait_for_adapter_operational(instance))
7834 goto skip_firing_dcmds;
7835
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007836 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07007837 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007838
7839skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307840 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307841 megasas_destroy_irqs(instance);
7842
adam radfordc8e858f2011-10-08 18:15:13 -07007843 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007844 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007845}
7846
7847/**
7848 * megasas_mgmt_open - char node "open" entry point
7849 */
7850static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7851{
7852 /*
7853 * Allow only those users with admin rights
7854 */
7855 if (!capable(CAP_SYS_ADMIN))
7856 return -EACCES;
7857
7858 return 0;
7859}
7860
7861/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007862 * megasas_mgmt_fasync - Async notifier registration from applications
7863 *
7864 * This function adds the calling process to a driver global queue. When an
7865 * event occurs, SIGIO will be sent to all processes in this queue.
7866 */
7867static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7868{
7869 int rc;
7870
Arjan van de Ven0b950672006-01-11 13:16:10 +01007871 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007872
7873 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7874
Arjan van de Ven0b950672006-01-11 13:16:10 +01007875 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007876
7877 if (rc >= 0) {
7878 /* For sanity check when we get ioctl */
7879 filep->private_data = filep;
7880 return 0;
7881 }
7882
7883 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7884
7885 return rc;
7886}
7887
7888/**
Yang, Boc3518832009-10-06 14:18:02 -06007889 * megasas_mgmt_poll - char node "poll" entry point
7890 * */
Al Viroafc9a422017-07-03 06:39:46 -04007891static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
Yang, Boc3518832009-10-06 14:18:02 -06007892{
Al Viroafc9a422017-07-03 06:39:46 -04007893 __poll_t mask;
Yang, Boc3518832009-10-06 14:18:02 -06007894 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007895
Yang, Boc3518832009-10-06 14:18:02 -06007896 poll_wait(file, &megasas_poll_wait, wait);
7897 spin_lock_irqsave(&poll_aen_lock, flags);
7898 if (megasas_poll_wait_aen)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007899 mask = (EPOLLIN | EPOLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007900 else
7901 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307902 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007903 spin_unlock_irqrestore(&poll_aen_lock, flags);
7904 return mask;
7905}
7906
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307907/*
7908 * megasas_set_crash_dump_params_ioctl:
7909 * Send CRASH_DUMP_MODE DCMD to all controllers
7910 * @cmd: MFI command frame
7911 */
7912
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007913static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307914{
7915 struct megasas_instance *local_instance;
7916 int i, error = 0;
7917 int crash_support;
7918
7919 crash_support = cmd->frame->dcmd.mbox.w[0];
7920
7921 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7922 local_instance = megasas_mgmt_info.instance[i];
7923 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307924 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307925 MEGASAS_HBA_OPERATIONAL) &&
7926 !megasas_set_crash_dump_params(local_instance,
7927 crash_support)) {
7928 local_instance->crash_dump_app_support =
7929 crash_support;
7930 dev_info(&local_instance->pdev->dev,
7931 "Application firmware crash "
7932 "dump mode set success\n");
7933 error = 0;
7934 } else {
7935 dev_info(&local_instance->pdev->dev,
7936 "Application firmware crash "
7937 "dump mode set failed\n");
7938 error = -1;
7939 }
7940 }
7941 }
7942 return error;
7943}
7944
Yang, Boc3518832009-10-06 14:18:02 -06007945/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007946 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7947 * @instance: Adapter soft state
7948 * @argp: User's ioctl packet
7949 */
7950static int
7951megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7952 struct megasas_iocpacket __user * user_ioc,
7953 struct megasas_iocpacket *ioc)
7954{
Shivasharan S107a60d2017-10-19 02:49:05 -07007955 struct megasas_sge64 *kern_sge64 = NULL;
7956 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007957 struct megasas_cmd *cmd;
7958 void *kbuff_arr[MAX_IOCTL_SGE];
7959 dma_addr_t buf_handle = 0;
7960 int error = 0, i;
7961 void *sense = NULL;
7962 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007963 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007964 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007965
7966 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7967
7968 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007969 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007970 ioc->sge_count, MAX_IOCTL_SGE);
7971 return -EINVAL;
7972 }
7973
Shivasharan Sf870bcb2018-01-05 05:33:04 -08007974 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7975 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
Chandrakanth Patil58136852019-06-25 16:34:30 +05307976 !instance->support_nvme_passthru) ||
7977 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
7978 !instance->support_pci_lane_margining)) {
Shivasharan S82add4e2017-10-19 02:49:02 -07007979 dev_err(&instance->pdev->dev,
7980 "Received invalid ioctl command 0x%x\n",
7981 ioc->frame.hdr.cmd);
7982 return -ENOTSUPP;
7983 }
7984
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007985 cmd = megasas_get_cmd(instance);
7986 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007987 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007988 return -ENOMEM;
7989 }
7990
7991 /*
7992 * User's IOCTL packet has 2 frames (maximum). Copy those two
7993 * frames into our cmd's frames. cmd->frame's context will get
7994 * overwritten when we copy from user's frames. So set that value
7995 * alone separately
7996 */
7997 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307998 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007999 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07008000
8001 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8002
8003 if (instance->consistent_mask_64bit)
8004 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8005 MFI_FRAME_SENSE64));
8006 else
8007 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308008 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07008009
8010 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8011 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008012
Shivasharan S8823abe2017-08-23 04:47:00 -07008013 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308014 mutex_lock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008015 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8016 megasas_return_cmd(instance, cmd);
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308017 mutex_unlock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008018 return -1;
8019 }
Chandrakanth Patil7fa31742019-06-25 16:34:23 +05308020 mutex_unlock(&instance->reset_mutex);
Shivasharan S95c06082017-02-10 00:59:23 -08008021 }
8022
Shivasharan S8823abe2017-08-23 04:47:00 -07008023 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05308024 error = megasas_set_crash_dump_params_ioctl(cmd);
8025 megasas_return_cmd(instance, cmd);
8026 return error;
8027 }
8028
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008029 /*
8030 * The management interface between applications and the fw uses
8031 * MFI frames. E.g, RAID configuration changes, LD property changes
8032 * etc are accomplishes through different kinds of MFI frames. The
8033 * driver needs to care only about substituting user buffers with
8034 * kernel buffers in SGLs. The location of SGL is embedded in the
8035 * struct iocpacket itself.
8036 */
Shivasharan S107a60d2017-10-19 02:49:05 -07008037 if (instance->consistent_mask_64bit)
8038 kern_sge64 = (struct megasas_sge64 *)
8039 ((unsigned long)cmd->frame + ioc->sgl_off);
8040 else
8041 kern_sge32 = (struct megasas_sge32 *)
8042 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008043
8044 /*
8045 * For each user buffer, create a mirror buffer and copy in
8046 */
8047 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01008048 if (!ioc->sgl[i].iov_len)
8049 continue;
8050
Sumant Patro9f35fa82007-02-14 12:55:45 -08008051 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008052 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08008053 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008054 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008055 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8056 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008057 error = -ENOMEM;
8058 goto out;
8059 }
8060
8061 /*
8062 * We don't change the dma_coherent_mask, so
Christoph Hellwig60ee6522018-10-10 19:31:25 +02008063 * dma_alloc_coherent only returns 32bit addresses
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008064 */
Shivasharan S107a60d2017-10-19 02:49:05 -07008065 if (instance->consistent_mask_64bit) {
8066 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8067 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8068 } else {
8069 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8070 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8071 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008072
8073 /*
8074 * We created a kernel buffer corresponding to the
8075 * user buffer. Now copy in from the user buffer
8076 */
8077 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8078 (u32) (ioc->sgl[i].iov_len))) {
8079 error = -EFAULT;
8080 goto out;
8081 }
8082 }
8083
8084 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08008085 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8086 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008087 if (!sense) {
8088 error = -ENOMEM;
8089 goto out;
8090 }
8091
8092 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06008093 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07008094 if (instance->consistent_mask_64bit)
8095 *sense_ptr = cpu_to_le64(sense_handle);
8096 else
8097 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008098 }
8099
8100 /*
8101 * Set the sync_cmd flag so that the ISR knows not to complete this
8102 * cmd to the SCSI mid-layer
8103 */
8104 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308105 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
8106 cmd->sync_cmd = 0;
8107 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07008108 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8109 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8110 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308111 return -EBUSY;
8112 }
8113
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008114 cmd->sync_cmd = 0;
8115
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05308116 if (instance->unload == 1) {
8117 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8118 "don't submit data to application\n");
8119 goto out;
8120 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008121 /*
8122 * copy out the kernel buffers to user buffers
8123 */
8124 for (i = 0; i < ioc->sge_count; i++) {
8125 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8126 ioc->sgl[i].iov_len)) {
8127 error = -EFAULT;
8128 goto out;
8129 }
8130 }
8131
8132 /*
8133 * copy out the sense
8134 */
8135 if (ioc->sense_len) {
8136 /*
bo yangb70a41e2008-03-18 03:13:06 -04008137 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008138 * sense buffer address
8139 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06008140 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8141 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008142
Shivasharan S318aaef2017-02-10 00:59:22 -08008143 if (copy_to_user((void __user *)((unsigned long)
8144 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04008145 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008146 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05008147 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008148 error = -EFAULT;
8149 goto out;
8150 }
8151 }
8152
8153 /*
8154 * copy the status codes returned by the fw
8155 */
8156 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8157 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008158 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008159 error = -EFAULT;
8160 }
8161
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008162out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008163 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08008164 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008165 sense, sense_handle);
8166 }
8167
Bjørn Mork7a6a7312012-11-21 09:54:48 +01008168 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01008169 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07008170 if (instance->consistent_mask_64bit)
8171 dma_free_coherent(&instance->pdev->dev,
8172 le32_to_cpu(kern_sge64[i].length),
8173 kbuff_arr[i],
8174 le64_to_cpu(kern_sge64[i].phys_addr));
8175 else
8176 dma_free_coherent(&instance->pdev->dev,
8177 le32_to_cpu(kern_sge32[i].length),
8178 kbuff_arr[i],
8179 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05308180 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01008181 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008182 }
8183
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05308184 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008185 return error;
8186}
8187
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008188static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8189{
8190 struct megasas_iocpacket __user *user_ioc =
8191 (struct megasas_iocpacket __user *)arg;
8192 struct megasas_iocpacket *ioc;
8193 struct megasas_instance *instance;
8194 int error;
8195
Markus Elfring709ab232016-08-21 10:39:04 +02008196 ioc = memdup_user(user_ioc, sizeof(*ioc));
8197 if (IS_ERR(ioc))
8198 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008199
8200 instance = megasas_lookup_instance(ioc->host_no);
8201 if (!instance) {
8202 error = -ENODEV;
8203 goto out_kfree_ioc;
8204 }
8205
adam radford229fe472014-03-10 02:51:56 -07008206 /* Block ioctls in VF mode */
8207 if (instance->requestorId && !allow_vf_ioctls) {
8208 error = -ENODEV;
8209 goto out_kfree_ioc;
8210 }
8211
Sumit Saxena8a01a412016-01-28 21:04:32 +05308212 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05008213 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06008214 error = -ENODEV;
8215 goto out_kfree_ioc;
8216 }
8217
8218 if (instance->unload == 1) {
8219 error = -ENODEV;
8220 goto out_kfree_ioc;
8221 }
8222
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008223 if (down_interruptible(&instance->ioctl_sem)) {
8224 error = -ERESTARTSYS;
8225 goto out_kfree_ioc;
8226 }
bo yang39a98552010-09-22 22:36:29 -04008227
Shivasharan S619831f2018-01-05 05:27:43 -08008228 if (megasas_wait_for_adapter_operational(instance)) {
bo yang39a98552010-09-22 22:36:29 -04008229 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03008230 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04008231 }
bo yang39a98552010-09-22 22:36:29 -04008232
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008233 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008234out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008235 up(&instance->ioctl_sem);
8236
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008237out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008238 kfree(ioc);
8239 return error;
8240}
8241
8242static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8243{
8244 struct megasas_instance *instance;
8245 struct megasas_aen aen;
8246 int error;
8247
8248 if (file->private_data != file) {
8249 printk(KERN_DEBUG "megasas: fasync_helper was not "
8250 "called first\n");
8251 return -EINVAL;
8252 }
8253
8254 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8255 return -EFAULT;
8256
8257 instance = megasas_lookup_instance(aen.host_no);
8258
8259 if (!instance)
8260 return -ENODEV;
8261
Sumit Saxena8a01a412016-01-28 21:04:32 +05308262 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04008263 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06008264 }
8265
8266 if (instance->unload == 1) {
8267 return -ENODEV;
8268 }
8269
Shivasharan S619831f2018-01-05 05:27:43 -08008270 if (megasas_wait_for_adapter_operational(instance))
bo yang39a98552010-09-22 22:36:29 -04008271 return -ENODEV;
bo yang39a98552010-09-22 22:36:29 -04008272
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308273 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008274 error = megasas_register_aen(instance, aen.seq_num,
8275 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308276 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008277 return error;
8278}
8279
8280/**
8281 * megasas_mgmt_ioctl - char node ioctl entry point
8282 */
8283static long
8284megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8285{
8286 switch (cmd) {
8287 case MEGASAS_IOC_FIRMWARE:
8288 return megasas_mgmt_ioctl_fw(file, arg);
8289
8290 case MEGASAS_IOC_GET_AEN:
8291 return megasas_mgmt_ioctl_aen(file, arg);
8292 }
8293
8294 return -ENOTTY;
8295}
8296
8297#ifdef CONFIG_COMPAT
8298static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8299{
8300 struct compat_megasas_iocpacket __user *cioc =
8301 (struct compat_megasas_iocpacket __user *)arg;
8302 struct megasas_iocpacket __user *ioc =
8303 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8304 int i;
8305 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008306 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308307 u32 local_sense_off;
8308 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308309 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008310
Jeff Garzik83aabc12006-10-04 06:34:03 -04008311 if (clear_user(ioc, sizeof(*ioc)))
8312 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008313
8314 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8315 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8316 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8317 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8318 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8319 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8320 return -EFAULT;
8321
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008322 /*
8323 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8324 * sense_len is not null, so prepare the 64bit value under
8325 * the same condition.
8326 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308327 if (get_user(local_sense_off, &ioc->sense_off) ||
8328 get_user(local_sense_len, &ioc->sense_len) ||
8329 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308330 return -EFAULT;
8331
Wenwen Wang47db7872018-10-06 13:34:21 -05008332 if (local_sense_off != user_sense_off)
8333 return -EINVAL;
8334
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308335 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008336 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308337 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008338 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308339 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008340 if (get_user(ptr, sense_cioc_ptr) ||
8341 put_user(compat_ptr(ptr), sense_ioc_ptr))
8342 return -EFAULT;
8343 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008344
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008345 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008346 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8347 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8348 copy_in_user(&ioc->sgl[i].iov_len,
8349 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8350 return -EFAULT;
8351 }
8352
8353 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8354
8355 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8356 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8357 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8358 return -EFAULT;
8359 }
8360 return error;
8361}
8362
8363static long
8364megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8365 unsigned long arg)
8366{
8367 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08008368 case MEGASAS_IOC_FIRMWARE32:
8369 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008370 case MEGASAS_IOC_GET_AEN:
8371 return megasas_mgmt_ioctl_aen(file, arg);
8372 }
8373
8374 return -ENOTTY;
8375}
8376#endif
8377
8378/*
8379 * File operations structure for management interface
8380 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08008381static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008382 .owner = THIS_MODULE,
8383 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008384 .fasync = megasas_mgmt_fasync,
8385 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06008386 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008387#ifdef CONFIG_COMPAT
8388 .compat_ioctl = megasas_mgmt_compat_ioctl,
8389#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02008390 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008391};
8392
8393/*
8394 * PCI hotplug support registration structure
8395 */
8396static struct pci_driver megasas_pci_driver = {
8397
8398 .name = "megaraid_sas",
8399 .id_table = megasas_pci_table,
8400 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008401 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05008402 .suspend = megasas_suspend,
8403 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008404 .shutdown = megasas_shutdown,
8405};
8406
8407/*
8408 * Sysfs driver attributes
8409 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008410static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008411{
8412 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8413 MEGASAS_VERSION);
8414}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008415static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008416
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008417static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308418{
8419 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8420 MEGASAS_RELDATE);
8421}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008422static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308423
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008424static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06008425{
8426 return sprintf(buf, "%u\n", support_poll_for_event);
8427}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008428static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008429
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008430static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06008431{
8432 return sprintf(buf, "%u\n", support_device_change);
8433}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008434static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06008435
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008436static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07008437{
bo yangad84db22007-11-09 04:40:16 -05008438 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07008439}
8440
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008441static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8442 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07008443{
8444 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008445
8446 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07008447 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8448 retval = -EINVAL;
8449 }
8450 return retval;
8451}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008452static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05008453
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008454static ssize_t
8455support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8456{
8457 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8458}
8459
8460static DRIVER_ATTR_RO(support_nvme_encapsulation);
8461
Chandrakanth Patil58136852019-06-25 16:34:30 +05308462static ssize_t
8463support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8464{
8465 return sprintf(buf, "%u\n", support_pci_lane_margining);
8466}
8467
8468static DRIVER_ATTR_RO(support_pci_lane_margining);
8469
Shivasharan Sb4a42212017-02-10 00:59:16 -08008470static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8471{
8472 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8473 scsi_remove_device(sdev);
8474 scsi_device_put(sdev);
8475}
8476
Shivasharan S44abbaf2019-01-29 01:38:13 -08008477/**
8478 * megasas_update_device_list - Update the PD and LD device list from FW
8479 * after an AEN event notification
8480 * @instance: Adapter soft state
8481 * @event_type: Indicates type of event (PD or LD event)
8482 *
8483 * @return: Success or failure
8484 *
8485 * Issue DCMDs to Firmware to update the internal device list in driver.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008486 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8487 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan S44abbaf2019-01-29 01:38:13 -08008488 */
8489static
8490int megasas_update_device_list(struct megasas_instance *instance,
8491 int event_type)
8492{
8493 int dcmd_ret = DCMD_SUCCESS;
8494
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008495 if (instance->enable_fw_dev_list) {
8496 dcmd_ret = megasas_host_device_list_query(instance, false);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008497 if (dcmd_ret != DCMD_SUCCESS)
8498 goto out;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008499 } else {
8500 if (event_type & SCAN_PD_CHANNEL) {
8501 dcmd_ret = megasas_get_pd_list(instance);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008502
Shivasharan S44abbaf2019-01-29 01:38:13 -08008503 if (dcmd_ret != DCMD_SUCCESS)
8504 goto out;
8505 }
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008506
8507 if (event_type & SCAN_VD_CHANNEL) {
8508 if (!instance->requestorId ||
8509 (instance->requestorId &&
8510 megasas_get_ld_vf_affiliation(instance, 0))) {
8511 dcmd_ret = megasas_ld_list_query(instance,
8512 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8513 if (dcmd_ret != DCMD_SUCCESS)
8514 goto out;
8515 }
8516 }
Shivasharan S44abbaf2019-01-29 01:38:13 -08008517 }
8518
8519out:
8520 return dcmd_ret;
8521}
8522
8523/**
8524 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8525 * after an AEN event notification
8526 * @instance: Adapter soft state
8527 * @scan_type: Indicates type of devices (PD/LD) to add
8528 * @return void
8529 */
8530static
8531void megasas_add_remove_devices(struct megasas_instance *instance,
8532 int scan_type)
8533{
8534 int i, j;
8535 u16 pd_index = 0;
8536 u16 ld_index = 0;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008537 u16 channel = 0, id = 0;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008538 struct Scsi_Host *host;
8539 struct scsi_device *sdev1;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008540 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8541 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008542
8543 host = instance->host;
8544
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008545 if (instance->enable_fw_dev_list) {
8546 targetid_list = instance->host_device_list_buf;
8547 for (i = 0; i < targetid_list->count; i++) {
8548 targetid_entry = &targetid_list->host_device_list[i];
8549 if (targetid_entry->flags.u.bits.is_sys_pd) {
8550 channel = le16_to_cpu(targetid_entry->target_id) /
8551 MEGASAS_MAX_DEV_PER_CHANNEL;
8552 id = le16_to_cpu(targetid_entry->target_id) %
8553 MEGASAS_MAX_DEV_PER_CHANNEL;
8554 } else {
8555 channel = MEGASAS_MAX_PD_CHANNELS +
8556 (le16_to_cpu(targetid_entry->target_id) /
8557 MEGASAS_MAX_DEV_PER_CHANNEL);
8558 id = le16_to_cpu(targetid_entry->target_id) %
8559 MEGASAS_MAX_DEV_PER_CHANNEL;
8560 }
8561 sdev1 = scsi_device_lookup(host, channel, id, 0);
8562 if (!sdev1) {
8563 scsi_add_device(host, channel, id, 0);
8564 } else {
8565 scsi_device_put(sdev1);
8566 }
8567 }
8568 }
8569
Shivasharan S44abbaf2019-01-29 01:38:13 -08008570 if (scan_type & SCAN_PD_CHANNEL) {
8571 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8572 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8573 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8574 sdev1 = scsi_device_lookup(host, i, j, 0);
8575 if (instance->pd_list[pd_index].driveState ==
8576 MR_PD_STATE_SYSTEM) {
8577 if (!sdev1)
8578 scsi_add_device(host, i, j, 0);
8579 else
8580 scsi_device_put(sdev1);
8581 } else {
8582 if (sdev1)
8583 megasas_remove_scsi_device(sdev1);
8584 }
8585 }
8586 }
8587 }
8588
8589 if (scan_type & SCAN_VD_CHANNEL) {
8590 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8591 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8592 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8593 sdev1 = scsi_device_lookup(host,
8594 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8595 if (instance->ld_ids[ld_index] != 0xff) {
8596 if (!sdev1)
8597 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8598 else
8599 scsi_device_put(sdev1);
8600 } else {
8601 if (sdev1)
8602 megasas_remove_scsi_device(sdev1);
8603 }
8604 }
8605 }
8606 }
8607
8608}
8609
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008610static void
8611megasas_aen_polling(struct work_struct *work)
8612{
8613 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08008614 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008615 struct megasas_instance *instance = ev->instance;
8616 union megasas_evt_class_locale class_locale;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008617 int event_type = 0;
YueHaibingde192122019-05-25 20:42:02 +08008618 u32 seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008619 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308620 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008621
8622 if (!instance) {
8623 printk(KERN_ERR "invalid instance!\n");
8624 kfree(ev);
8625 return;
8626 }
adam radford229fe472014-03-10 02:51:56 -07008627
adam radford229fe472014-03-10 02:51:56 -07008628 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308629 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07008630
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008631 instance->ev = NULL;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008632 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05308633 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008634
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308635 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308636
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008637 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308638 case MR_EVT_PD_REMOVED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008639 event_type = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07008640 break;
8641
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308642 case MR_EVT_LD_OFFLINE:
8643 case MR_EVT_CFG_CLEARED:
8644 case MR_EVT_LD_DELETED:
8645 case MR_EVT_LD_CREATED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008646 event_type = SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308647 break;
8648
8649 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8650 case MR_EVT_FOREIGN_CFG_IMPORTED:
8651 case MR_EVT_LD_STATE_CHANGE:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008652 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308653 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8654 instance->host->host_no);
8655 break;
8656
8657 case MR_EVT_CTRL_PROP_CHANGED:
Shivasharan Sf0c21df2018-10-16 23:37:40 -07008658 dcmd_ret = megasas_get_ctrl_info(instance);
8659 if (dcmd_ret == DCMD_SUCCESS &&
8660 instance->snapdump_wait_time) {
8661 megasas_get_snapdump_properties(instance);
8662 dev_info(&instance->pdev->dev,
8663 "Snap dump wait time\t: %d\n",
8664 instance->snapdump_wait_time);
8665 }
8666 break;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308667 default:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008668 event_type = 0;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308669 break;
8670 }
8671 } else {
8672 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8673 mutex_unlock(&instance->reset_mutex);
8674 kfree(ev);
8675 return;
8676 }
8677
Shivasharan S44abbaf2019-01-29 01:38:13 -08008678 if (event_type)
8679 dcmd_ret = megasas_update_device_list(instance, event_type);
8680
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308681 mutex_unlock(&instance->reset_mutex);
8682
Shivasharan S44abbaf2019-01-29 01:38:13 -08008683 if (event_type && dcmd_ret == DCMD_SUCCESS)
8684 megasas_add_remove_devices(instance, event_type);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008685
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308686 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308687 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8688 else
8689 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008690
8691 /* Register AEN with FW for latest sequence number plus 1 */
8692 class_locale.members.reserved = 0;
8693 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8694 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308695
8696 if (instance->aen_cmd != NULL) {
8697 kfree(ev);
8698 return;
8699 }
8700
8701 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008702 error = megasas_register_aen(instance, seq_num,
8703 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008704 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308705 dev_err(&instance->pdev->dev,
8706 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008707
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308708 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008709 kfree(ev);
8710}
8711
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008712/**
8713 * megasas_init - Driver load entry point
8714 */
8715static int __init megasas_init(void)
8716{
8717 int rval;
8718
8719 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07008720 * Booted in kdump kernel, minimize memory footprints by
8721 * disabling few features
8722 */
8723 if (reset_devices) {
8724 msix_vectors = 1;
8725 rdpq_enable = 0;
8726 dual_qdepth_disable = 1;
8727 }
8728
8729 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008730 * Announce driver version and other information
8731 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05308732 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008733
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07008734 spin_lock_init(&poll_aen_lock);
8735
Yang, Bo72c4fd32009-10-06 14:20:59 -06008736 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06008737 support_device_change = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008738 support_nvme_encapsulation = true;
Chandrakanth Patil58136852019-06-25 16:34:30 +05308739 support_pci_lane_margining = true;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008740
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008741 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8742
8743 /*
8744 * Register character device node
8745 */
8746 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8747
8748 if (rval < 0) {
8749 printk(KERN_DEBUG "megasas: failed to open device node\n");
8750 return rval;
8751 }
8752
8753 megasas_mgmt_majorno = rval;
8754
Shivasharan Sba535722019-05-07 10:05:49 -07008755 megasas_init_debugfs();
8756
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008757 /*
8758 * Register ourselves as PCI hotplug module
8759 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00008760 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008761
8762 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09008763 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04008764 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008765 }
8766
Shivasharan Sd956a112019-06-28 02:50:40 -07008767 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
8768 (event_log_level > MFI_EVT_CLASS_DEAD)) {
Colin Ian King359603a2019-07-20 22:58:40 +01008769 pr_warn("megaraid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
Shivasharan Sd956a112019-06-28 02:50:40 -07008770 event_log_level = MFI_EVT_CLASS_CRITICAL;
8771 }
8772
Jeff Garzik83aabc12006-10-04 06:34:03 -04008773 rval = driver_create_file(&megasas_pci_driver.driver,
8774 &driver_attr_version);
8775 if (rval)
8776 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008777
8778 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308779 &driver_attr_release_date);
8780 if (rval)
8781 goto err_dcf_rel_date;
8782
8783 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008784 &driver_attr_support_poll_for_event);
8785 if (rval)
8786 goto err_dcf_support_poll_for_event;
8787
Jeff Garzik83aabc12006-10-04 06:34:03 -04008788 rval = driver_create_file(&megasas_pci_driver.driver,
8789 &driver_attr_dbg_lvl);
8790 if (rval)
8791 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05008792 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008793 &driver_attr_support_device_change);
8794 if (rval)
8795 goto err_dcf_support_device_change;
8796
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008797 rval = driver_create_file(&megasas_pci_driver.driver,
8798 &driver_attr_support_nvme_encapsulation);
8799 if (rval)
8800 goto err_dcf_support_nvme_encapsulation;
8801
Chandrakanth Patil58136852019-06-25 16:34:30 +05308802 rval = driver_create_file(&megasas_pci_driver.driver,
8803 &driver_attr_support_pci_lane_margining);
8804 if (rval)
8805 goto err_dcf_support_pci_lane_margining;
8806
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008807 return rval;
bo yangad84db22007-11-09 04:40:16 -05008808
Chandrakanth Patil58136852019-06-25 16:34:30 +05308809err_dcf_support_pci_lane_margining:
8810 driver_remove_file(&megasas_pci_driver.driver,
8811 &driver_attr_support_nvme_encapsulation);
8812
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008813err_dcf_support_nvme_encapsulation:
8814 driver_remove_file(&megasas_pci_driver.driver,
8815 &driver_attr_support_device_change);
8816
Yang, Bo837f5fe2010-10-11 06:59:20 -06008817err_dcf_support_device_change:
8818 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05008819 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008820err_dcf_dbg_lvl:
8821 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008822 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008823err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308824 driver_remove_file(&megasas_pci_driver.driver,
8825 &driver_attr_release_date);
8826err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04008827 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8828err_dcf_attr_ver:
8829 pci_unregister_driver(&megasas_pci_driver);
8830err_pcidrv:
Shivasharan Sba535722019-05-07 10:05:49 -07008831 megasas_exit_debugfs();
Jeff Garzik83aabc12006-10-04 06:34:03 -04008832 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08008833 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008834}
8835
8836/**
8837 * megasas_exit - Driver unload entry point
8838 */
8839static void __exit megasas_exit(void)
8840{
Sumant Patro658dced2006-10-03 13:09:14 -07008841 driver_remove_file(&megasas_pci_driver.driver,
8842 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008843 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008844 &driver_attr_support_poll_for_event);
8845 driver_remove_file(&megasas_pci_driver.driver,
8846 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308847 driver_remove_file(&megasas_pci_driver.driver,
8848 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008849 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008850 driver_remove_file(&megasas_pci_driver.driver,
8851 &driver_attr_support_nvme_encapsulation);
Chandrakanth Patil58136852019-06-25 16:34:30 +05308852 driver_remove_file(&megasas_pci_driver.driver,
8853 &driver_attr_support_pci_lane_margining);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008854
8855 pci_unregister_driver(&megasas_pci_driver);
Shivasharan Sba535722019-05-07 10:05:49 -07008856 megasas_exit_debugfs();
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008857 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8858}
8859
8860module_init(megasas_init);
8861module_exit(megasas_exit);