blob: fb0ccba97b324fedaa1eb40322273a326a368dd5 [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2003-2013 LSI Corporation
Shivasharan S365597c2018-10-16 23:37:43 -07005 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007 *
adam radford3f1530c2010-12-14 18:51:48 -08008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040012 *
adam radford3f1530c2010-12-14 18:51:48 -080013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040017 *
adam radford3f1530c2010-12-14 18:51:48 -080018 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040020 *
Shivasharan S365597c2018-10-16 23:37:43 -070021 * Authors: Broadcom Inc.
adam radford3f1530c2010-12-14 18:51:48 -080022 * Sreenivas Bagalkote
23 * Sumant Patro
24 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053025 * Adam Radford
Shivasharan S365597c2018-10-16 23:37:43 -070026 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040028 *
Shivasharan S365597c2018-10-16 23:37:43 -070029 * Send feedback to: megaraidlinux.pdl@broadcom.com
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040030 */
31
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/pci.h>
35#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040036#include <linux/moduleparam.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
39#include <linux/interrupt.h>
40#include <linux/delay.h>
41#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Shivasharan S318aaef2017-02-10 00:59:22 -080044#include <asm/unaligned.h>
Al Viro43399232005-10-04 17:36:04 +010045#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040046#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080047#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010048#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060049#include <linux/poll.h>
Shivasharan Sdef3e8d2017-08-23 04:47:03 -070050#include <linux/vmalloc.h>
Shivasharan S62a04f82019-05-07 10:05:35 -070051#include <linux/irq_poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070057#include <scsi/scsi_tcq.h>
Shivasharan S96c96032019-05-07 10:05:37 -070058#include <scsi/scsi_dbg.h>
adam radford9c915a82010-12-21 13:34:31 -080059#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040060#include "megaraid_sas.h"
61
bo yangad84db22007-11-09 04:40:16 -050062/*
Yang, Bo1fd10682010-10-12 07:18:50 -060063 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
65 */
66static unsigned int max_sectors;
67module_param_named(max_sectors, max_sectors, int, 0);
68MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
70
adam radford80d9da92010-12-21 10:17:40 -080071static int msix_disable;
72module_param(msix_disable, int, S_IRUGO);
73MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74
adam radford079eadd2012-10-01 19:26:59 -070075static unsigned int msix_vectors;
76module_param(msix_vectors, int, S_IRUGO);
77MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
78
adam radford229fe472014-03-10 02:51:56 -070079static int allow_vf_ioctls;
80module_param(allow_vf_ioctls, int, S_IRUGO);
81MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
82
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053083static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070084module_param(throttlequeuedepth, int, S_IRUGO);
85MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87
Sumit Saxenae3d178c2016-01-28 21:04:34 +053088unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070089module_param(resetwaittime, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070090MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
adam radfordc007b8b2012-07-17 18:20:24 -070091
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053092int smp_affinity_enable = 1;
93module_param(smp_affinity_enable, int, S_IRUGO);
Colin Ian King55d9a1d2018-04-29 13:25:32 +010094MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053095
Sumit Saxena179ac142016-01-28 21:04:28 +053096int rdpq_enable = 1;
97module_param(rdpq_enable, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070098MODULE_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 +053099
Sumit Saxena308ec452016-01-28 21:04:30 +0530100unsigned int dual_qdepth_disable;
101module_param(dual_qdepth_disable, int, S_IRUGO);
102MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530104unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
105module_param(scmd_timeout, int, S_IRUGO);
106MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400108MODULE_LICENSE("GPL");
109MODULE_VERSION(MEGASAS_VERSION);
Shivasharan S365597c2018-10-16 23:37:43 -0700110MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
111MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400112
adam radford058a8fa2011-10-08 18:14:27 -0700113int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400114static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700115static int megasas_ld_list_query(struct megasas_instance *instance,
116 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400117static int megasas_issue_init_mfi(struct megasas_instance *instance);
118static int megasas_register_aen(struct megasas_instance *instance,
119 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800120static void megasas_get_pd_info(struct megasas_instance *instance,
121 struct scsi_device *sdev);
Shivasharan Se9495e22018-06-04 03:45:12 -0700122
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400123/*
124 * PCI ID table for all supported controllers
125 */
126static struct pci_device_id megasas_pci_table[] = {
127
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
129 /* xscale IOP */
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
131 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
133 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
135 /* gen2*/
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
137 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
139 /* skinny*/
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
141 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
143 /* xscale IOP, vega */
144 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
145 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
147 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
149 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
151 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
153 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
155 /* Intruder */
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
157 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500160 /* VENTURA */
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Shivasharan S469f72d2018-11-09 09:47:20 -0800167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200171 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400172};
173
174MODULE_DEVICE_TABLE(pci, megasas_pci_table);
175
176static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700177struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400178static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100179static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400180
Yang, Boc3518832009-10-06 14:18:02 -0600181static int megasas_poll_wait_aen;
182static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600183static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800184u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600185static u32 support_device_change;
Shivasharan Sf870bcb2018-01-05 05:33:04 -0800186static bool support_nvme_encapsulation;
Sumant Patro658dced2006-10-03 13:09:14 -0700187
Yang, Boc3518832009-10-06 14:18:02 -0600188/* define lock for aen poll */
189spinlock_t poll_aen_lock;
190
adam radford9c915a82010-12-21 13:34:31 -0800191void
bo yang7343eb62007-11-09 04:35:44 -0500192megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
193 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800194static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800195megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
adam radfordebf054b2011-02-24 20:57:15 -0800196static int
197megasas_adp_reset_gen2(struct megasas_instance *instance,
198 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800199static irqreturn_t megasas_isr(int irq, void *devp);
200static u32
201megasas_init_adapter_mfi(struct megasas_instance *instance);
202u32
203megasas_build_and_issue_cmd(struct megasas_instance *instance,
204 struct scsi_cmnd *scmd);
205static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800206int
adam radford229fe472014-03-10 02:51:56 -0700207wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
208 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800209void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700210static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
211 int initial);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700212static int
Shivasharan S107a60d2017-10-19 02:49:05 -0700213megasas_set_dma_mask(struct megasas_instance *instance);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700214static int
215megasas_alloc_ctrl_mem(struct megasas_instance *instance);
216static inline void
217megasas_free_ctrl_mem(struct megasas_instance *instance);
218static inline int
219megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
220static inline void
221megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
222static inline void
223megasas_init_ctrl_params(struct megasas_instance *instance);
adam radfordcd50ba82010-12-21 10:23:23 -0800224
Shivasharan S272652f2018-12-17 00:47:40 -0800225u32 megasas_readl(struct megasas_instance *instance,
226 const volatile void __iomem *addr)
227{
228 u32 i = 0, ret_val;
229 /*
230 * Due to a HW errata in Aero controllers, reads to certain
231 * Fusion registers could intermittently return all zeroes.
232 * This behavior is transient in nature and subsequent reads will
233 * return valid value. As a workaround in driver, retry readl for
234 * upto three times until a non-zero value is read.
235 */
236 if (instance->adapter_type == AERO_SERIES) {
237 do {
238 ret_val = readl(addr);
239 i++;
240 } while (ret_val == 0 && i < 3);
241 return ret_val;
242 } else {
243 return readl(addr);
244 }
245}
246
Shivasharan S107a60d2017-10-19 02:49:05 -0700247/**
248 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
249 * @instance: Adapter soft state
250 * @dcmd: DCMD frame inside MFI command
251 * @dma_addr: DMA address of buffer to be passed to FW
252 * @dma_len: Length of DMA buffer to be passed to FW
253 * @return: void
254 */
255void megasas_set_dma_settings(struct megasas_instance *instance,
256 struct megasas_dcmd_frame *dcmd,
257 dma_addr_t dma_addr, u32 dma_len)
258{
259 if (instance->consistent_mask_64bit) {
260 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
261 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
262 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
263
264 } else {
265 dcmd->sgl.sge32[0].phys_addr =
266 cpu_to_le32(lower_32_bits(dma_addr));
267 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
268 dcmd->flags = cpu_to_le16(dcmd->flags);
269 }
270}
adam radfordcd50ba82010-12-21 10:23:23 -0800271
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800272void
adam radfordcd50ba82010-12-21 10:23:23 -0800273megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
274{
275 instance->instancet->fire_cmd(instance,
276 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800277 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800278}
bo yang7343eb62007-11-09 04:35:44 -0500279
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400280/**
281 * megasas_get_cmd - Get a command from the free pool
282 * @instance: Adapter soft state
283 *
284 * Returns a free command from the pool
285 */
adam radford9c915a82010-12-21 13:34:31 -0800286struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400287 *instance)
288{
289 unsigned long flags;
290 struct megasas_cmd *cmd = NULL;
291
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530292 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400293
294 if (!list_empty(&instance->cmd_pool)) {
295 cmd = list_entry((&instance->cmd_pool)->next,
296 struct megasas_cmd, list);
297 list_del_init(&cmd->list);
298 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500299 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400300 }
301
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530302 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400303 return cmd;
304}
305
306/**
307 * megasas_return_cmd - Return a cmd to free command pool
308 * @instance: Adapter soft state
309 * @cmd: Command packet to be returned to free command pool
310 */
Andi Kleen68b43742017-05-08 15:58:53 -0700311void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400312megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
313{
314 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530315 u32 blk_tags;
316 struct megasas_cmd_fusion *cmd_fusion;
317 struct fusion_context *fusion = instance->ctrl_context;
318
319 /* This flag is used only for fusion adapter.
320 * Wait for Interrupt for Polled mode DCMD
321 */
322 if (cmd->flags & DRV_DCMD_POLLED_MODE)
323 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400324
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530325 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400326
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530327 if (fusion) {
328 blk_tags = instance->max_scsi_cmds + cmd->index;
329 cmd_fusion = fusion->cmd_list[blk_tags];
330 megasas_return_cmd_fusion(instance, cmd_fusion);
331 }
332 cmd->scmd = NULL;
333 cmd->frame_count = 0;
334 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800335 memset(cmd->frame, 0, instance->mfi_frame_size);
336 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530337 if (!fusion && reset_devices)
338 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
339 list_add(&cmd->list, (&instance->cmd_pool)->next);
340
341 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
342
343}
Sumant Patro1341c932006-01-25 12:02:40 -0800344
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530345static const char *
346format_timestamp(uint32_t timestamp)
347{
348 static char buffer[32];
349
350 if ((timestamp & 0xff000000) == 0xff000000)
351 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
352 0x00ffffff);
353 else
354 snprintf(buffer, sizeof(buffer), "%us", timestamp);
355 return buffer;
356}
357
358static const char *
359format_class(int8_t class)
360{
361 static char buffer[6];
362
363 switch (class) {
364 case MFI_EVT_CLASS_DEBUG:
365 return "debug";
366 case MFI_EVT_CLASS_PROGRESS:
367 return "progress";
368 case MFI_EVT_CLASS_INFO:
369 return "info";
370 case MFI_EVT_CLASS_WARNING:
371 return "WARN";
372 case MFI_EVT_CLASS_CRITICAL:
373 return "CRIT";
374 case MFI_EVT_CLASS_FATAL:
375 return "FATAL";
376 case MFI_EVT_CLASS_DEAD:
377 return "DEAD";
378 default:
379 snprintf(buffer, sizeof(buffer), "%d", class);
380 return buffer;
381 }
382}
383
384/**
385 * megasas_decode_evt: Decode FW AEN event and print critical event
386 * for information.
387 * @instance: Adapter soft state
388 */
389static void
390megasas_decode_evt(struct megasas_instance *instance)
391{
392 struct megasas_evt_detail *evt_detail = instance->evt_detail;
393 union megasas_evt_class_locale class_locale;
394 class_locale.word = le32_to_cpu(evt_detail->cl.word);
395
396 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
397 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
398 le32_to_cpu(evt_detail->seq_num),
399 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
400 (class_locale.members.locale),
401 format_class(class_locale.members.class),
402 evt_detail->description);
403}
404
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400405/**
adam radford0d490162010-12-14 19:17:17 -0800406* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800407* (deviceid : 1064R, PERC5) controllers
408*/
409
410/**
411 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400412 * @regs: MFI register set
413 */
414static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530415megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400416{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530417 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500418
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530419 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400420 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400421
422 /* Dummy readl to force pci flush */
423 readl(&regs->outbound_intr_mask);
424}
425
426/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700427 * megasas_disable_intr_xscale -Disables interrupt
428 * @regs: MFI register set
429 */
430static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530431megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700432{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530433 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700434 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500435
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530436 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700437 writel(mask, &regs->outbound_intr_mask);
438 /* Dummy readl to force pci flush */
439 readl(&regs->outbound_intr_mask);
440}
441
442/**
Sumant Patro1341c932006-01-25 12:02:40 -0800443 * megasas_read_fw_status_reg_xscale - returns the current FW status value
444 * @regs: MFI register set
445 */
446static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800447megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800448{
Shivasharan Sde516372018-12-17 00:47:39 -0800449 return readl(&instance->reg_set->outbound_msg_0);
Sumant Patro1341c932006-01-25 12:02:40 -0800450}
451/**
452 * megasas_clear_interrupt_xscale - Check & clear interrupt
453 * @regs: MFI register set
454 */
adam radford0d490162010-12-14 19:17:17 -0800455static int
Shivasharan Sde516372018-12-17 00:47:39 -0800456megasas_clear_intr_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800457{
458 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400459 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800460 struct megasas_register_set __iomem *regs;
461 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500462
Sumant Patro1341c932006-01-25 12:02:40 -0800463 /*
464 * Check if it is our interrupt
465 */
466 status = readl(&regs->outbound_intr_status);
467
bo yang39a98552010-09-22 22:36:29 -0400468 if (status & MFI_OB_INTR_STATUS_MASK)
469 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
470 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
471 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800472
473 /*
474 * Clear the interrupt by writing back the same value
475 */
bo yang39a98552010-09-22 22:36:29 -0400476 if (mfiStatus)
477 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800478
Yang, Bo06f579d2008-08-10 12:42:37 -0700479 /* Dummy readl to force pci flush */
480 readl(&regs->outbound_intr_status);
481
bo yang39a98552010-09-22 22:36:29 -0400482 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800483}
484
485/**
486 * megasas_fire_cmd_xscale - Sends command to the FW
487 * @frame_phys_addr : Physical address of cmd
488 * @frame_count : Number of frames for the command
489 * @regs : MFI register set
490 */
adam radford0d490162010-12-14 19:17:17 -0800491static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600492megasas_fire_cmd_xscale(struct megasas_instance *instance,
493 dma_addr_t frame_phys_addr,
494 u32 frame_count,
495 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800496{
bo yang39a98552010-09-22 22:36:29 -0400497 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500498
bo yang39a98552010-09-22 22:36:29 -0400499 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800500 writel((frame_phys_addr >> 3)|(frame_count),
501 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400502 spin_unlock_irqrestore(&instance->hba_lock, flags);
503}
504
505/**
506 * megasas_adp_reset_xscale - For controller reset
507 * @regs: MFI register set
508 */
509static int
510megasas_adp_reset_xscale(struct megasas_instance *instance,
511 struct megasas_register_set __iomem *regs)
512{
513 u32 i;
514 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500515
bo yang39a98552010-09-22 22:36:29 -0400516 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
517
518 for (i = 0; i < 3; i++)
519 msleep(1000); /* sleep for 3 secs */
520 pcidata = 0;
521 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500522 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400523 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500524 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400525 pcidata &= ~0x2;
526 pci_write_config_dword(instance->pdev,
527 MFI_1068_PCSR_OFFSET, pcidata);
528
529 for (i = 0; i < 2; i++)
530 msleep(1000); /* need to wait 2 secs again */
531
532 pcidata = 0;
533 pci_read_config_dword(instance->pdev,
534 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500535 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400536 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500537 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400538 pcidata = 0;
539 pci_write_config_dword(instance->pdev,
540 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
541 }
542 }
543 return 0;
544}
545
546/**
547 * megasas_check_reset_xscale - For controller reset check
548 * @regs: MFI register set
549 */
550static int
551megasas_check_reset_xscale(struct megasas_instance *instance,
552 struct megasas_register_set __iomem *regs)
553{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530554 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530555 (le32_to_cpu(*instance->consumer) ==
556 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400557 return 1;
bo yang39a98552010-09-22 22:36:29 -0400558 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800559}
560
561static struct megasas_instance_template megasas_instance_template_xscale = {
562
563 .fire_cmd = megasas_fire_cmd_xscale,
564 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700565 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800566 .clear_intr = megasas_clear_intr_xscale,
567 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400568 .adp_reset = megasas_adp_reset_xscale,
569 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800570 .service_isr = megasas_isr,
571 .tasklet = megasas_complete_cmd_dpc,
572 .init_adapter = megasas_init_adapter_mfi,
573 .build_and_issue_cmd = megasas_build_and_issue_cmd,
574 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800575};
576
577/**
adam radford0d490162010-12-14 19:17:17 -0800578* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800579* to xscale (deviceid : 1064R, PERC5) controllers
580*/
581
582/**
adam radford0d490162010-12-14 19:17:17 -0800583* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500584* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800585*/
586
587/**
588 * megasas_enable_intr_ppc - Enables interrupts
589 * @regs: MFI register set
590 */
591static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530592megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800593{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530594 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500595
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530596 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800597 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800598
bo yang39a98552010-09-22 22:36:29 -0400599 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800600
601 /* Dummy readl to force pci flush */
602 readl(&regs->outbound_intr_mask);
603}
604
605/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700606 * megasas_disable_intr_ppc - Disable interrupt
607 * @regs: MFI register set
608 */
609static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530610megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700611{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530612 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700613 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500614
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530615 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700616 writel(mask, &regs->outbound_intr_mask);
617 /* Dummy readl to force pci flush */
618 readl(&regs->outbound_intr_mask);
619}
620
621/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800622 * megasas_read_fw_status_reg_ppc - returns the current FW status value
623 * @regs: MFI register set
624 */
625static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800626megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800627{
Shivasharan Sde516372018-12-17 00:47:39 -0800628 return readl(&instance->reg_set->outbound_scratch_pad_0);
Sumant Patrof9876f02006-02-03 15:34:35 -0800629}
630
631/**
632 * megasas_clear_interrupt_ppc - Check & clear interrupt
633 * @regs: MFI register set
634 */
adam radford0d490162010-12-14 19:17:17 -0800635static int
Shivasharan Sde516372018-12-17 00:47:39 -0800636megasas_clear_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800637{
adam radford3cc6851f92011-05-11 18:34:52 -0700638 u32 status, mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800639 struct megasas_register_set __iomem *regs;
640 regs = instance->reg_set;
adam radford3cc6851f92011-05-11 18:34:52 -0700641
Sumant Patrof9876f02006-02-03 15:34:35 -0800642 /*
643 * Check if it is our interrupt
644 */
645 status = readl(&regs->outbound_intr_status);
646
adam radford3cc6851f92011-05-11 18:34:52 -0700647 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
648 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
649
650 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
651 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800652
653 /*
654 * Clear the interrupt by writing back the same value
655 */
656 writel(status, &regs->outbound_doorbell_clear);
657
Yang, Bo06f579d2008-08-10 12:42:37 -0700658 /* Dummy readl to force pci flush */
659 readl(&regs->outbound_doorbell_clear);
660
adam radford3cc6851f92011-05-11 18:34:52 -0700661 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800662}
adam radford3cc6851f92011-05-11 18:34:52 -0700663
Sumant Patrof9876f02006-02-03 15:34:35 -0800664/**
665 * megasas_fire_cmd_ppc - Sends command to the FW
666 * @frame_phys_addr : Physical address of cmd
667 * @frame_count : Number of frames for the command
668 * @regs : MFI register set
669 */
adam radford0d490162010-12-14 19:17:17 -0800670static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600671megasas_fire_cmd_ppc(struct megasas_instance *instance,
672 dma_addr_t frame_phys_addr,
673 u32 frame_count,
674 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800675{
bo yang39a98552010-09-22 22:36:29 -0400676 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500677
bo yang39a98552010-09-22 22:36:29 -0400678 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800679 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800680 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400681 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800682}
683
bo yang39a98552010-09-22 22:36:29 -0400684/**
bo yang39a98552010-09-22 22:36:29 -0400685 * megasas_check_reset_ppc - For controller reset check
686 * @regs: MFI register set
687 */
688static int
689megasas_check_reset_ppc(struct megasas_instance *instance,
690 struct megasas_register_set __iomem *regs)
691{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530692 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700693 return 1;
694
bo yang39a98552010-09-22 22:36:29 -0400695 return 0;
696}
adam radford3cc6851f92011-05-11 18:34:52 -0700697
Sumant Patrof9876f02006-02-03 15:34:35 -0800698static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800699
Sumant Patrof9876f02006-02-03 15:34:35 -0800700 .fire_cmd = megasas_fire_cmd_ppc,
701 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700702 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800703 .clear_intr = megasas_clear_intr_ppc,
704 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700705 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400706 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800707 .service_isr = megasas_isr,
708 .tasklet = megasas_complete_cmd_dpc,
709 .init_adapter = megasas_init_adapter_mfi,
710 .build_and_issue_cmd = megasas_build_and_issue_cmd,
711 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800712};
713
714/**
Yang, Bo87911122009-10-06 14:31:54 -0600715 * megasas_enable_intr_skinny - Enables interrupts
716 * @regs: MFI register set
717 */
718static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530719megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600720{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530721 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500722
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530723 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600724 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
725
726 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
727
728 /* Dummy readl to force pci flush */
729 readl(&regs->outbound_intr_mask);
730}
731
732/**
733 * megasas_disable_intr_skinny - Disables interrupt
734 * @regs: MFI register set
735 */
736static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530737megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600738{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530739 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600740 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500741
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530742 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600743 writel(mask, &regs->outbound_intr_mask);
744 /* Dummy readl to force pci flush */
745 readl(&regs->outbound_intr_mask);
746}
747
748/**
749 * megasas_read_fw_status_reg_skinny - returns the current FW status value
750 * @regs: MFI register set
751 */
752static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800753megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600754{
Shivasharan Sde516372018-12-17 00:47:39 -0800755 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo87911122009-10-06 14:31:54 -0600756}
757
758/**
759 * megasas_clear_interrupt_skinny - Check & clear interrupt
760 * @regs: MFI register set
761 */
762static int
Shivasharan Sde516372018-12-17 00:47:39 -0800763megasas_clear_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600764{
765 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800766 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800767 struct megasas_register_set __iomem *regs;
768 regs = instance->reg_set;
adam radfordebf054b2011-02-24 20:57:15 -0800769
Yang, Bo87911122009-10-06 14:31:54 -0600770 /*
771 * Check if it is our interrupt
772 */
773 status = readl(&regs->outbound_intr_status);
774
775 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400776 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600777 }
778
779 /*
adam radfordebf054b2011-02-24 20:57:15 -0800780 * Check if it is our interrupt
781 */
Shivasharan Sde516372018-12-17 00:47:39 -0800782 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800783 MFI_STATE_FAULT) {
784 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
785 } else
786 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
787
788 /*
Yang, Bo87911122009-10-06 14:31:54 -0600789 * Clear the interrupt by writing back the same value
790 */
791 writel(status, &regs->outbound_intr_status);
792
793 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500794 * dummy read to flush PCI
795 */
Yang, Bo87911122009-10-06 14:31:54 -0600796 readl(&regs->outbound_intr_status);
797
adam radfordebf054b2011-02-24 20:57:15 -0800798 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600799}
800
801/**
802 * megasas_fire_cmd_skinny - Sends command to the FW
803 * @frame_phys_addr : Physical address of cmd
804 * @frame_count : Number of frames for the command
805 * @regs : MFI register set
806 */
807static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600808megasas_fire_cmd_skinny(struct megasas_instance *instance,
809 dma_addr_t frame_phys_addr,
810 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600811 struct megasas_register_set __iomem *regs)
812{
Yang, Bo0c79e682009-10-06 14:47:35 -0600813 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500814
bo yang39a98552010-09-22 22:36:29 -0400815 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530816 writel(upper_32_bits(frame_phys_addr),
817 &(regs)->inbound_high_queue_port);
818 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
819 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400820 spin_unlock_irqrestore(&instance->hba_lock, flags);
821}
822
823/**
bo yang39a98552010-09-22 22:36:29 -0400824 * megasas_check_reset_skinny - For controller reset check
825 * @regs: MFI register set
826 */
827static int
828megasas_check_reset_skinny(struct megasas_instance *instance,
829 struct megasas_register_set __iomem *regs)
830{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530831 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700832 return 1;
833
bo yang39a98552010-09-22 22:36:29 -0400834 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600835}
836
837static struct megasas_instance_template megasas_instance_template_skinny = {
838
839 .fire_cmd = megasas_fire_cmd_skinny,
840 .enable_intr = megasas_enable_intr_skinny,
841 .disable_intr = megasas_disable_intr_skinny,
842 .clear_intr = megasas_clear_intr_skinny,
843 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800844 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400845 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800846 .service_isr = megasas_isr,
847 .tasklet = megasas_complete_cmd_dpc,
848 .init_adapter = megasas_init_adapter_mfi,
849 .build_and_issue_cmd = megasas_build_and_issue_cmd,
850 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600851};
852
853
854/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700855* The following functions are defined for gen2 (deviceid : 0x78 0x79)
856* controllers
857*/
858
859/**
860 * megasas_enable_intr_gen2 - Enables interrupts
861 * @regs: MFI register set
862 */
863static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530864megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700865{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530866 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500867
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530868 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700869 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
870
871 /* write ~0x00000005 (4 & 1) to the intr mask*/
872 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
873
874 /* Dummy readl to force pci flush */
875 readl(&regs->outbound_intr_mask);
876}
877
878/**
879 * megasas_disable_intr_gen2 - Disables interrupt
880 * @regs: MFI register set
881 */
882static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530883megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700884{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530885 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700886 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500887
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530888 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700889 writel(mask, &regs->outbound_intr_mask);
890 /* Dummy readl to force pci flush */
891 readl(&regs->outbound_intr_mask);
892}
893
894/**
895 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
896 * @regs: MFI register set
897 */
898static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800899megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700900{
Shivasharan Sde516372018-12-17 00:47:39 -0800901 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700902}
903
904/**
905 * megasas_clear_interrupt_gen2 - Check & clear interrupt
906 * @regs: MFI register set
907 */
908static int
Shivasharan Sde516372018-12-17 00:47:39 -0800909megasas_clear_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700910{
911 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400912 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800913 struct megasas_register_set __iomem *regs;
914 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500915
Yang, Bo6610a6b2008-08-10 12:42:38 -0700916 /*
917 * Check if it is our interrupt
918 */
919 status = readl(&regs->outbound_intr_status);
920
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530921 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400922 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
923 }
924 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
925 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
926 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700927
928 /*
929 * Clear the interrupt by writing back the same value
930 */
bo yang39a98552010-09-22 22:36:29 -0400931 if (mfiStatus)
932 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700933
934 /* Dummy readl to force pci flush */
935 readl(&regs->outbound_intr_status);
936
bo yang39a98552010-09-22 22:36:29 -0400937 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700938}
939/**
940 * megasas_fire_cmd_gen2 - Sends command to the FW
941 * @frame_phys_addr : Physical address of cmd
942 * @frame_count : Number of frames for the command
943 * @regs : MFI register set
944 */
945static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600946megasas_fire_cmd_gen2(struct megasas_instance *instance,
947 dma_addr_t frame_phys_addr,
948 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700949 struct megasas_register_set __iomem *regs)
950{
bo yang39a98552010-09-22 22:36:29 -0400951 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500952
bo yang39a98552010-09-22 22:36:29 -0400953 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700954 writel((frame_phys_addr | (frame_count<<1))|1,
955 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400956 spin_unlock_irqrestore(&instance->hba_lock, flags);
957}
958
959/**
960 * megasas_adp_reset_gen2 - For controller reset
961 * @regs: MFI register set
962 */
963static int
964megasas_adp_reset_gen2(struct megasas_instance *instance,
965 struct megasas_register_set __iomem *reg_set)
966{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500967 u32 retry = 0 ;
968 u32 HostDiag;
969 u32 __iomem *seq_offset = &reg_set->seq_offset;
970 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400971
adam radfordebf054b2011-02-24 20:57:15 -0800972 if (instance->instancet == &megasas_instance_template_skinny) {
973 seq_offset = &reg_set->fusion_seq_offset;
974 hostdiag_offset = &reg_set->fusion_host_diag;
975 }
976
977 writel(0, seq_offset);
978 writel(4, seq_offset);
979 writel(0xb, seq_offset);
980 writel(2, seq_offset);
981 writel(7, seq_offset);
982 writel(0xd, seq_offset);
983
bo yang39a98552010-09-22 22:36:29 -0400984 msleep(1000);
985
adam radfordebf054b2011-02-24 20:57:15 -0800986 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400987
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500988 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400989 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800990 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500991 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400992 retry, HostDiag);
993
994 if (retry++ >= 100)
995 return 1;
996
997 }
998
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500999 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -04001000
adam radfordebf054b2011-02-24 20:57:15 -08001001 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -04001002
1003 ssleep(10);
1004
adam radfordebf054b2011-02-24 20:57:15 -08001005 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001006 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -04001007 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -08001008 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001009 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -04001010 retry, HostDiag);
1011
1012 if (retry++ >= 1000)
1013 return 1;
1014
1015 }
1016 return 0;
1017}
1018
1019/**
1020 * megasas_check_reset_gen2 - For controller reset check
1021 * @regs: MFI register set
1022 */
1023static int
1024megasas_check_reset_gen2(struct megasas_instance *instance,
1025 struct megasas_register_set __iomem *regs)
1026{
Sumit Saxena8a01a412016-01-28 21:04:32 +05301027 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -06001028 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -06001029
bo yang39a98552010-09-22 22:36:29 -04001030 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -07001031}
1032
1033static struct megasas_instance_template megasas_instance_template_gen2 = {
1034
1035 .fire_cmd = megasas_fire_cmd_gen2,
1036 .enable_intr = megasas_enable_intr_gen2,
1037 .disable_intr = megasas_disable_intr_gen2,
1038 .clear_intr = megasas_clear_intr_gen2,
1039 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -04001040 .adp_reset = megasas_adp_reset_gen2,
1041 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -08001042 .service_isr = megasas_isr,
1043 .tasklet = megasas_complete_cmd_dpc,
1044 .init_adapter = megasas_init_adapter_mfi,
1045 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1046 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -07001047};
1048
1049/**
Sumant Patrof9876f02006-02-03 15:34:35 -08001050* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -04001051* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -08001052*/
1053
adam radford9c915a82010-12-21 13:34:31 -08001054/*
1055 * Template added for TB (Fusion)
1056 */
1057extern struct megasas_instance_template megasas_instance_template_fusion;
1058
Sumant Patrof9876f02006-02-03 15:34:35 -08001059/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001060 * megasas_issue_polled - Issues a polling command
1061 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -08001062 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301064 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001065 */
adam radford9c915a82010-12-21 13:34:31 -08001066int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001067megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1068{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001069 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1070
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301071 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301072 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001073
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001074 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301075 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1076 __func__, __LINE__);
1077 return DCMD_NOT_FIRED;
1078 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001079
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001080 instance->instancet->issue_dcmd(instance, cmd);
1081
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301082 return wait_and_poll(instance, cmd, instance->requestorId ?
1083 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001084}
1085
1086/**
1087 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1088 * @instance: Adapter soft state
1089 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301090 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001091 *
1092 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001093 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001094 * Used to issue ioctl commands.
1095 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301096int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301098 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001099{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301100 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301101 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001103 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301104 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1105 __func__, __LINE__);
1106 return DCMD_NOT_FIRED;
1107 }
1108
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001109 instance->instancet->issue_dcmd(instance, cmd);
1110
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301111 if (timeout) {
1112 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301113 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301114 if (!ret) {
Shivasharan S2ce43502019-05-07 10:05:38 -07001115 dev_err(&instance->pdev->dev,
1116 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1117 cmd->frame->dcmd.opcode, __func__);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301118 return DCMD_TIMEOUT;
1119 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301120 } else
1121 wait_event(instance->int_cmd_wait_q,
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
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301124 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301125 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001126}
1127
1128/**
1129 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1130 * @instance: Adapter soft state
1131 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301132 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001133 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301134 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001136 * cmd and waits for return status.
1137 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001138 */
1139static int
1140megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301141 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001142{
1143 struct megasas_cmd *cmd;
1144 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301145 int ret = 0;
Shivasharan S2ce43502019-05-07 10:05:38 -07001146 u32 opcode;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001147
1148 cmd = megasas_get_cmd(instance);
1149
1150 if (!cmd)
1151 return -1;
1152
1153 abort_fr = &cmd->frame->abort;
1154
1155 /*
1156 * Prepare and issue the abort frame
1157 */
1158 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301159 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301160 abort_fr->flags = cpu_to_le16(0);
1161 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1162 abort_fr->abort_mfi_phys_addr_lo =
1163 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1164 abort_fr->abort_mfi_phys_addr_hi =
1165 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166
1167 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301168 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001169
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001170 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301171 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1172 __func__, __LINE__);
1173 return DCMD_NOT_FIRED;
1174 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001175
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001176 instance->instancet->issue_dcmd(instance, cmd);
1177
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301178 if (timeout) {
1179 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301180 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301181 if (!ret) {
Shivasharan S2ce43502019-05-07 10:05:38 -07001182 opcode = cmd_to_abort->frame->dcmd.opcode;
1183 dev_err(&instance->pdev->dev,
1184 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1185 opcode, __func__);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301186 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301187 }
1188 } else
1189 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301190 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301191
bo yang39a98552010-09-22 22:36:29 -04001192 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001193
1194 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301195 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1196 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001197}
1198
1199/**
1200 * megasas_make_sgl32 - Prepares 32-bit SGL
1201 * @instance: Adapter soft state
1202 * @scp: SCSI command from the mid-layer
1203 * @mfi_sgl: SGL to be filled in
1204 *
1205 * If successful, this function returns the number of SG elements. Otherwise,
1206 * it returnes -1.
1207 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001208static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001209megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1210 union megasas_sgl *mfi_sgl)
1211{
1212 int i;
1213 int sge_count;
1214 struct scatterlist *os_sgl;
1215
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001216 sge_count = scsi_dma_map(scp);
1217 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001218
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001219 if (sge_count) {
1220 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301221 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1222 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001223 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001224 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001225 return sge_count;
1226}
1227
1228/**
1229 * megasas_make_sgl64 - Prepares 64-bit SGL
1230 * @instance: Adapter soft state
1231 * @scp: SCSI command from the mid-layer
1232 * @mfi_sgl: SGL to be filled in
1233 *
1234 * If successful, this function returns the number of SG elements. Otherwise,
1235 * it returnes -1.
1236 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001237static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001238megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1239 union megasas_sgl *mfi_sgl)
1240{
1241 int i;
1242 int sge_count;
1243 struct scatterlist *os_sgl;
1244
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001245 sge_count = scsi_dma_map(scp);
1246 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001247
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001248 if (sge_count) {
1249 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301250 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1251 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001252 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001253 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001254 return sge_count;
1255}
1256
Yang, Bof4c9a132009-10-06 14:43:28 -06001257/**
1258 * megasas_make_sgl_skinny - Prepares IEEE SGL
1259 * @instance: Adapter soft state
1260 * @scp: SCSI command from the mid-layer
1261 * @mfi_sgl: SGL to be filled in
1262 *
1263 * If successful, this function returns the number of SG elements. Otherwise,
1264 * it returnes -1.
1265 */
1266static int
1267megasas_make_sgl_skinny(struct megasas_instance *instance,
1268 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1269{
1270 int i;
1271 int sge_count;
1272 struct scatterlist *os_sgl;
1273
1274 sge_count = scsi_dma_map(scp);
1275
1276 if (sge_count) {
1277 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301278 mfi_sgl->sge_skinny[i].length =
1279 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001280 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301281 cpu_to_le64(sg_dma_address(os_sgl));
1282 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001283 }
1284 }
1285 return sge_count;
1286}
1287
Sumant Patrob1df99d2006-10-03 12:40:47 -07001288 /**
1289 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001290 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001291 * @sge_count : number of sg elements
1292 *
1293 * Returns the number of frames required for numnber of sge's (sge_count)
1294 */
1295
Yang, Bof4c9a132009-10-06 14:43:28 -06001296static u32 megasas_get_frame_count(struct megasas_instance *instance,
1297 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001298{
1299 int num_cnt;
1300 int sge_bytes;
1301 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001302 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001303
1304 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1305 sizeof(struct megasas_sge32);
1306
Yang, Bof4c9a132009-10-06 14:43:28 -06001307 if (instance->flag_ieee) {
1308 sge_sz = sizeof(struct megasas_sge_skinny);
1309 }
1310
Sumant Patrob1df99d2006-10-03 12:40:47 -07001311 /*
bo yangd532dbe2008-03-17 03:36:43 -04001312 * Main frame can contain 2 SGEs for 64-bit SGLs and
1313 * 3 SGEs for 32-bit SGLs for ldio &
1314 * 1 SGEs for 64-bit SGLs and
1315 * 2 SGEs for 32-bit SGLs for pthru frame
1316 */
1317 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001318 if (instance->flag_ieee == 1) {
1319 num_cnt = sge_count - 1;
1320 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001321 num_cnt = sge_count - 1;
1322 else
1323 num_cnt = sge_count - 2;
1324 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001325 if (instance->flag_ieee == 1) {
1326 num_cnt = sge_count - 1;
1327 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001328 num_cnt = sge_count - 2;
1329 else
1330 num_cnt = sge_count - 3;
1331 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001332
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001333 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001334 sge_bytes = sge_sz * num_cnt;
1335
1336 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1337 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1338 }
1339 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001340 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001341
1342 if (frame_count > 7)
1343 frame_count = 8;
1344 return frame_count;
1345}
1346
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001347/**
1348 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1349 * @instance: Adapter soft state
1350 * @scp: SCSI command
1351 * @cmd: Command to be prepared in
1352 *
1353 * This function prepares CDB commands. These are typcially pass-through
1354 * commands to the devices.
1355 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001356static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001357megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358 struct megasas_cmd *cmd)
1359{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360 u32 is_logical;
1361 u32 device_id;
1362 u16 flags = 0;
1363 struct megasas_pthru_frame *pthru;
1364
Shivasharan S3cabd162017-02-10 00:59:05 -08001365 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301366 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001367 pthru = (struct megasas_pthru_frame *)cmd->frame;
1368
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001369 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001370 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001371 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001372 flags = MFI_FRAME_DIR_READ;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001373 else if (scp->sc_data_direction == DMA_NONE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001374 flags = MFI_FRAME_DIR_NONE;
1375
Yang, Bof4c9a132009-10-06 14:43:28 -06001376 if (instance->flag_ieee == 1) {
1377 flags |= MFI_FRAME_IEEE;
1378 }
1379
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380 /*
1381 * Prepare the DCDB frame
1382 */
1383 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1384 pthru->cmd_status = 0x0;
1385 pthru->scsi_status = 0x0;
1386 pthru->target_id = device_id;
1387 pthru->lun = scp->device->lun;
1388 pthru->cdb_len = scp->cmd_len;
1389 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001390 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301391 pthru->flags = cpu_to_le16(flags);
1392 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393
1394 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1395
1396 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001397 * If the command is for the tape device, set the
1398 * pthru timeout to the os layer timeout value.
1399 */
Yang, Bo8d568252009-10-06 14:12:21 -06001400 if (scp->device->type == TYPE_TAPE) {
1401 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301402 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001403 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301404 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001405 }
1406
1407 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001408 * Construct SGL
1409 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001410 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301411 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001412 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1413 &pthru->sgl);
1414 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301415 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001416 pthru->sge_count = megasas_make_sgl64(instance, scp,
1417 &pthru->sgl);
1418 } else
1419 pthru->sge_count = megasas_make_sgl32(instance, scp,
1420 &pthru->sgl);
1421
Yang, Bobdc6fb82009-12-06 08:30:19 -07001422 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001423 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001424 pthru->sge_count);
1425 return 0;
1426 }
1427
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001428 /*
1429 * Sense info specific
1430 */
1431 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301432 pthru->sense_buf_phys_addr_hi =
1433 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1434 pthru->sense_buf_phys_addr_lo =
1435 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001436
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001437 /*
1438 * Compute the total number of frames this command consumes. FW uses
1439 * this number to pull sufficient number of frames from host memory.
1440 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001441 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001442 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001443
1444 return cmd->frame_count;
1445}
1446
1447/**
1448 * megasas_build_ldio - Prepares IOs to logical devices
1449 * @instance: Adapter soft state
1450 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001451 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001452 *
1453 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1454 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001455static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001456megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1457 struct megasas_cmd *cmd)
1458{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459 u32 device_id;
1460 u8 sc = scp->cmnd[0];
1461 u16 flags = 0;
1462 struct megasas_io_frame *ldio;
1463
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301464 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001465 ldio = (struct megasas_io_frame *)cmd->frame;
1466
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001467 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001468 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001469 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470 flags = MFI_FRAME_DIR_READ;
1471
Yang, Bof4c9a132009-10-06 14:43:28 -06001472 if (instance->flag_ieee == 1) {
1473 flags |= MFI_FRAME_IEEE;
1474 }
1475
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001476 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001477 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001478 */
1479 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1480 ldio->cmd_status = 0x0;
1481 ldio->scsi_status = 0x0;
1482 ldio->target_id = device_id;
1483 ldio->timeout = 0;
1484 ldio->reserved_0 = 0;
1485 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301486 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001487 ldio->start_lba_hi = 0;
1488 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1489
1490 /*
1491 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1492 */
1493 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301494 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1495 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1496 ((u32) scp->cmnd[2] << 8) |
1497 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001498
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301499 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001500 }
1501
1502 /*
1503 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1504 */
1505 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301506 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1507 ((u32) scp->cmnd[7] << 8));
1508 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1509 ((u32) scp->cmnd[3] << 16) |
1510 ((u32) scp->cmnd[4] << 8) |
1511 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001512 }
1513
1514 /*
1515 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1516 */
1517 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301518 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1519 ((u32) scp->cmnd[7] << 16) |
1520 ((u32) scp->cmnd[8] << 8) |
1521 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001522
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301523 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1524 ((u32) scp->cmnd[3] << 16) |
1525 ((u32) scp->cmnd[4] << 8) |
1526 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001527 }
1528
1529 /*
1530 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1531 */
1532 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301533 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1534 ((u32) scp->cmnd[11] << 16) |
1535 ((u32) scp->cmnd[12] << 8) |
1536 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001537
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301538 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1539 ((u32) scp->cmnd[7] << 16) |
1540 ((u32) scp->cmnd[8] << 8) |
1541 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001542
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301543 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1544 ((u32) scp->cmnd[3] << 16) |
1545 ((u32) scp->cmnd[4] << 8) |
1546 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001547
1548 }
1549
1550 /*
1551 * Construct SGL
1552 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001553 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301554 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001555 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1556 &ldio->sgl);
1557 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301558 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001559 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1560 } else
1561 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1562
Yang, Bobdc6fb82009-12-06 08:30:19 -07001563 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001564 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001565 ldio->sge_count);
1566 return 0;
1567 }
1568
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001569 /*
1570 * Sense info specific
1571 */
1572 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1573 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301574 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001575
Sumant Patrob1df99d2006-10-03 12:40:47 -07001576 /*
1577 * Compute the total number of frames this command consumes. FW uses
1578 * this number to pull sufficient number of frames from host memory.
1579 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001580 cmd->frame_count = megasas_get_frame_count(instance,
1581 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001582
1583 return cmd->frame_count;
1584}
1585
1586/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301587 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1588 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001589 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001590 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001591 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301592inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001593{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301594 int ret;
1595
Sumant Patrocb59aa62006-01-25 11:53:25 -08001596 switch (cmd->cmnd[0]) {
1597 case READ_10:
1598 case WRITE_10:
1599 case READ_12:
1600 case WRITE_12:
1601 case READ_6:
1602 case WRITE_6:
1603 case READ_16:
1604 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001605 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301606 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1607 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001608 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001609 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301610 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001611 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301612 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001613}
1614
Sumant Patro658dced2006-10-03 13:09:14 -07001615 /**
1616 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001617 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001618 * @instance: Adapter soft state
1619 */
1620static inline void
1621megasas_dump_pending_frames(struct megasas_instance *instance)
1622{
1623 struct megasas_cmd *cmd;
1624 int i,n;
1625 union megasas_sgl *mfi_sgl;
1626 struct megasas_io_frame *ldio;
1627 struct megasas_pthru_frame *pthru;
1628 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001629 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001630
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001631 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1632 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 -07001633 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001634 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 -07001635 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001636 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 -07001637
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001638 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001639 for (i = 0; i < max_cmd; i++) {
1640 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001641 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001642 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001643 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 +05301644 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001645 ldio = (struct megasas_io_frame *)cmd->frame;
1646 mfi_sgl = &ldio->sgl;
1647 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001648 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 +05301649 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1650 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1651 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1652 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001653 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001654 pthru = (struct megasas_pthru_frame *) cmd->frame;
1655 mfi_sgl = &pthru->sgl;
1656 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001657 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 +05301658 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1659 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1660 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1661 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001662 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001663 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1664 for (n = 0; n < sgcount; n++) {
1665 if (IS_DMA64)
1666 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1667 le32_to_cpu(mfi_sgl->sge64[n].length),
1668 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1669 else
1670 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1671 le32_to_cpu(mfi_sgl->sge32[n].length),
1672 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001673 }
1674 }
Sumant Patro658dced2006-10-03 13:09:14 -07001675 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001676 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001677 for (i = 0; i < max_cmd; i++) {
1678
1679 cmd = instance->cmd_list[i];
1680
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001681 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001682 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001683 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001684 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001685}
1686
adam radfordcd50ba82010-12-21 10:23:23 -08001687u32
1688megasas_build_and_issue_cmd(struct megasas_instance *instance,
1689 struct scsi_cmnd *scmd)
1690{
1691 struct megasas_cmd *cmd;
1692 u32 frame_count;
1693
1694 cmd = megasas_get_cmd(instance);
1695 if (!cmd)
1696 return SCSI_MLQUEUE_HOST_BUSY;
1697
1698 /*
1699 * Logical drive command
1700 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301701 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001702 frame_count = megasas_build_ldio(instance, scmd, cmd);
1703 else
1704 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1705
1706 if (!frame_count)
1707 goto out_return_cmd;
1708
1709 cmd->scmd = scmd;
1710 scmd->SCp.ptr = (char *)cmd;
1711
1712 /*
1713 * Issue the command to the FW
1714 */
1715 atomic_inc(&instance->fw_outstanding);
1716
1717 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1718 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001719
1720 return 0;
1721out_return_cmd:
1722 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301723 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001724}
1725
1726
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001727/**
1728 * megasas_queue_command - Queue entry point
1729 * @scmd: SCSI command to be queued
1730 * @done: Callback entry point
1731 */
1732static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301733megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001734{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001735 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301736 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001737
1738 instance = (struct megasas_instance *)
1739 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001740
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301741 if (instance->unload == 1) {
1742 scmd->result = DID_NO_CONNECT << 16;
1743 scmd->scsi_done(scmd);
1744 return 0;
1745 }
1746
bo yang39a98552010-09-22 22:36:29 -04001747 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001748 return SCSI_MLQUEUE_HOST_BUSY;
1749
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301750
adam radford229fe472014-03-10 02:51:56 -07001751 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301752 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001753 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001754 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001755 return SCSI_MLQUEUE_HOST_BUSY;
1756 } else {
adam radford229fe472014-03-10 02:51:56 -07001757 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301758 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001759 return 0;
1760 }
1761 }
1762
Sumit Saxena8a01a412016-01-28 21:04:32 +05301763 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001764 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301765 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301766 return 0;
1767 }
1768
Sumit Saxena18365b12016-01-28 21:04:25 +05301769 mr_device_priv_data = scmd->device->hostdata;
1770 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301771 scmd->result = DID_NO_CONNECT << 16;
1772 scmd->scsi_done(scmd);
1773 return 0;
1774 }
1775
Sumit Saxena8a01a412016-01-28 21:04:32 +05301776 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001777 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001778
Sumit Saxena8a01a412016-01-28 21:04:32 +05301779 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301780 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301781
bo yang39a98552010-09-22 22:36:29 -04001782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001783 scmd->result = 0;
1784
Shivasharan S3cabd162017-02-10 00:59:05 -08001785 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301786 (scmd->device->id >= instance->fw_supported_vd_count ||
1787 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001788 scmd->result = DID_BAD_TARGET << 16;
1789 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001790 }
1791
Shivasharan S3cabd162017-02-10 00:59:05 -08001792 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1793 MEGASAS_IS_LOGICAL(scmd->device) &&
1794 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001795 scmd->result = DID_OK << 16;
1796 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001797 }
1798
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301799 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001800
Sumant Patrocb59aa62006-01-25 11:53:25 -08001801 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301802 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001803 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001804}
1805
Yang, Bo044833b2009-10-06 14:33:06 -06001806static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1807{
1808 int i;
1809
1810 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1811
1812 if ((megasas_mgmt_info.instance[i]) &&
1813 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1814 return megasas_mgmt_info.instance[i];
1815 }
1816
1817 return NULL;
1818}
1819
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301820/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001821* megasas_set_dynamic_target_properties -
1822* Device property set by driver may not be static and it is required to be
1823* updated after OCR
1824*
1825* set tm_capable.
1826* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301827*
1828* @sdev: OS provided scsi device
1829*
1830* Returns void
1831*/
Shivasharan Se9495e22018-06-04 03:45:12 -07001832void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1833 bool is_target_prop)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301834{
Shivasharan S15dd0382017-02-10 00:59:10 -08001835 u16 pd_index = 0, ld;
1836 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301837 struct megasas_instance *instance;
1838 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301839 struct MR_PRIV_DEVICE *mr_device_priv_data;
1840 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301841 struct MR_LD_RAID *raid;
1842 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1843
1844 instance = megasas_lookup_instance(sdev->host->host_no);
1845 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301846 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301847
Shivasharan Sed981b82017-02-10 00:59:06 -08001848 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301849 return;
1850
Shivasharan Sed981b82017-02-10 00:59:06 -08001851 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301852 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1853 + sdev->id;
1854 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1855 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001856 if (ld >= instance->fw_supported_vd_count)
1857 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301858 raid = MR_LdRaidGet(ld, local_map_ptr);
1859
1860 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001861 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001862
Sumit Saxena18365b12016-01-28 21:04:25 +05301863 mr_device_priv_data->is_tm_capable =
1864 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001865 } else if (instance->use_seqnum_jbod_fp) {
1866 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001867 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001868 pd_sync = (void *)fusion->pd_seq_sync
1869 [(instance->pd_seq_map_id - 1) & 1];
1870 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001871 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301872 }
Shivasharan Se9495e22018-06-04 03:45:12 -07001873
1874 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1875 /*
1876 * If FW provides a target reset timeout value, driver will use
1877 * it. If not set, fallback to default values.
1878 */
1879 mr_device_priv_data->target_reset_tmo =
1880 min_t(u8, instance->max_reset_tmo,
1881 instance->tgt_prop->reset_tmo);
1882 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1883 } else {
1884 mr_device_priv_data->target_reset_tmo =
1885 MEGASAS_DEFAULT_TM_TIMEOUT;
1886 mr_device_priv_data->task_abort_tmo =
1887 MEGASAS_DEFAULT_TM_TIMEOUT;
1888 }
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301889}
1890
Shivasharan S15dd0382017-02-10 00:59:10 -08001891/*
1892 * megasas_set_nvme_device_properties -
1893 * set nomerges=2
1894 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1895 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1896 *
1897 * MR firmware provides value in KB. Caller of this function converts
1898 * kb into bytes.
1899 *
1900 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1901 * MR firmware provides value 128 as (32 * 4K) = 128K.
1902 *
1903 * @sdev: scsi device
1904 * @max_io_size: maximum io transfer size
1905 *
1906 */
1907static inline void
1908megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301909{
Sumit Saxena2216c302016-01-28 21:04:26 +05301910 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001911 u32 mr_nvme_pg_size;
1912
1913 instance = (struct megasas_instance *)sdev->host->hostdata;
1914 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1915 MR_DEFAULT_NVME_PAGE_SIZE);
1916
1917 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1918
Bart Van Assche8b904b52018-03-07 17:10:10 -08001919 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
Shivasharan S15dd0382017-02-10 00:59:10 -08001920 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1921}
1922
1923
1924/*
1925 * megasas_set_static_target_properties -
1926 * Device property set by driver are static and it is not required to be
1927 * updated after OCR.
1928 *
1929 * set io timeout
1930 * set device queue depth
1931 * set nvme device properties. see - megasas_set_nvme_device_properties
1932 *
1933 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001934 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001935 */
Shivasharan S96188a82017-02-10 00:59:11 -08001936static void megasas_set_static_target_properties(struct scsi_device *sdev,
1937 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001938{
Shivasharan S15dd0382017-02-10 00:59:10 -08001939 u8 interface_type;
1940 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1941 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001942 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001943 struct megasas_instance *instance;
1944 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301945
1946 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001947 mr_device_priv_data = sdev->hostdata;
1948 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301949
Shivasharan S15dd0382017-02-10 00:59:10 -08001950 /*
1951 * The RAID firmware may require extended timeouts.
1952 */
1953 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301954
Shivasharan S15dd0382017-02-10 00:59:10 -08001955 switch (interface_type) {
1956 case SAS_PD:
1957 device_qd = MEGASAS_SAS_QD;
1958 break;
1959 case SATA_PD:
1960 device_qd = MEGASAS_SATA_QD;
1961 break;
1962 case NVME_PD:
1963 device_qd = MEGASAS_NVME_QD;
1964 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301965 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001966
Shivasharan S96188a82017-02-10 00:59:11 -08001967 if (is_target_prop) {
1968 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1969 if (tgt_device_qd &&
1970 (tgt_device_qd <= instance->host->can_queue))
1971 device_qd = tgt_device_qd;
1972
1973 /* max_io_size_kb will be set to non zero for
1974 * nvme based vd and syspd.
1975 */
1976 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1977 }
1978
Shivasharan S15dd0382017-02-10 00:59:10 -08001979 if (instance->nvme_page_size && max_io_size_kb)
1980 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1981
1982 scsi_change_queue_depth(sdev, device_qd);
1983
Sumit Saxena2216c302016-01-28 21:04:26 +05301984}
1985
Sumit Saxena18365b12016-01-28 21:04:25 +05301986
Christoph Hellwig147aab62006-02-17 12:13:48 +01001987static int megasas_slave_configure(struct scsi_device *sdev)
1988{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301989 u16 pd_index = 0;
1990 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001991 int ret_target_prop = DCMD_FAILED;
1992 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301993
1994 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001995 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001996 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301997 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1998 sdev->id;
1999 if (instance->pd_list[pd_index].driveState !=
2000 MR_PD_STATE_SYSTEM)
2001 return -ENXIO;
2002 }
2003 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302004
Shivasharan S149c5752018-01-05 05:27:42 -08002005 mutex_lock(&instance->reset_mutex);
Shivasharan S15dd0382017-02-10 00:59:10 -08002006 /* Send DCMD to Firmware and cache the information */
2007 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2008 megasas_get_pd_info(instance, sdev);
2009
Shivasharan S96188a82017-02-10 00:59:11 -08002010 /* Some ventura firmware may not have instance->nvme_page_size set.
2011 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2012 */
2013 if ((instance->tgt_prop) && (instance->nvme_page_size))
2014 ret_target_prop = megasas_get_target_prop(instance, sdev);
2015
2016 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2017 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08002018
Shivasharan S15dd0382017-02-10 00:59:10 -08002019 /* This sdev property may change post OCR */
Shivasharan Se9495e22018-06-04 03:45:12 -07002020 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2021
2022 mutex_unlock(&instance->reset_mutex);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05302023
Yang, Bo044833b2009-10-06 14:33:06 -06002024 return 0;
2025}
2026
2027static int megasas_slave_alloc(struct scsi_device *sdev)
2028{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002029 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06002030 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05302031 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002032
Yang, Bo044833b2009-10-06 14:33:06 -06002033 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08002034 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06002035 /*
2036 * Open the OS scan to the SYSTEM PD
2037 */
2038 pd_index =
2039 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2040 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08002041 if ((instance->pd_list_not_supported ||
2042 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302043 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05302044 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06002045 }
2046 return -ENXIO;
2047 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302048
2049scan_target:
2050 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2051 GFP_KERNEL);
2052 if (!mr_device_priv_data)
2053 return -ENOMEM;
2054 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08002055
2056 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2057 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01002058 return 0;
2059}
2060
Sumit Saxena18365b12016-01-28 21:04:25 +05302061static void megasas_slave_destroy(struct scsi_device *sdev)
2062{
2063 kfree(sdev->hostdata);
2064 sdev->hostdata = NULL;
2065}
2066
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302067/*
2068* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2069* kill adapter
2070* @instance: Adapter soft state
2071*
2072*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05302073static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302074{
2075 int i;
2076 struct megasas_cmd *cmd_mfi;
2077 struct megasas_cmd_fusion *cmd_fusion;
2078 struct fusion_context *fusion = instance->ctrl_context;
2079
2080 /* Find all outstanding ioctls */
2081 if (fusion) {
2082 for (i = 0; i < instance->max_fw_cmds; i++) {
2083 cmd_fusion = fusion->cmd_list[i];
2084 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2085 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2086 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002087 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2088 cmd_mfi->frame->hdr.cmd_status =
2089 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302090 megasas_complete_cmd(instance,
2091 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002092 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302093 }
2094 }
2095 } else {
2096 for (i = 0; i < instance->max_fw_cmds; i++) {
2097 cmd_mfi = instance->cmd_list[i];
2098 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2099 MFI_CMD_ABORT)
2100 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2101 }
2102 }
2103}
2104
2105
adam radford9c915a82010-12-21 13:34:31 -08002106void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002107{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302108 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302109 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302110 /* Wait 1 second to ensure IO or ioctls in build have posted */
2111 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002112 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302113 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002114 (instance->adapter_type != MFI_SERIES)) {
Shivasharan S5acad9b2018-10-16 23:37:47 -07002115 if (!instance->requestorId) {
2116 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2117 /* Flush */
2118 readl(&instance->reg_set->doorbell);
2119 }
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302120 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002121 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002122 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302123 writel(MFI_STOP_ADP,
2124 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002125 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302126 /* Complete outstanding ioctls when adapter is killed */
2127 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002128}
2129
2130 /**
2131 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2132 * restored to max value
2133 * @instance: Adapter soft state
2134 *
2135 */
2136void
2137megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2138{
2139 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302140
adam radford9c915a82010-12-21 13:34:31 -08002141 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002142 && time_after(jiffies, instance->last_time + 5 * HZ)
2143 && atomic_read(&instance->fw_outstanding) <
2144 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002145
2146 spin_lock_irqsave(instance->host->host_lock, flags);
2147 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002148
Sumit Saxena308ec452016-01-28 21:04:30 +05302149 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002150 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002151 }
2152}
2153
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002154/**
bo yang7343eb62007-11-09 04:35:44 -05002155 * megasas_complete_cmd_dpc - Returns FW's controller structure
2156 * @instance_addr: Address of adapter soft state
2157 *
2158 * Tasklet to complete cmds
2159 */
2160static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2161{
2162 u32 producer;
2163 u32 consumer;
2164 u32 context;
2165 struct megasas_cmd *cmd;
2166 struct megasas_instance *instance =
2167 (struct megasas_instance *)instance_addr;
2168 unsigned long flags;
2169
2170 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302171 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002172 return;
2173
2174 spin_lock_irqsave(&instance->completion_lock, flags);
2175
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302176 producer = le32_to_cpu(*instance->producer);
2177 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002178
2179 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302180 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002181 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002182 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002183 context);
2184 BUG();
2185 }
bo yang7343eb62007-11-09 04:35:44 -05002186
2187 cmd = instance->cmd_list[context];
2188
2189 megasas_complete_cmd(instance, cmd, DID_OK);
2190
2191 consumer++;
2192 if (consumer == (instance->max_fw_cmds + 1)) {
2193 consumer = 0;
2194 }
2195 }
2196
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302197 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002198
2199 spin_unlock_irqrestore(&instance->completion_lock, flags);
2200
2201 /*
2202 * Check if we can restore can_queue
2203 */
adam radford9c915a82010-12-21 13:34:31 -08002204 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002205}
2206
Kees Cookc251a7b2017-10-22 15:30:04 -07002207static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2208
adam radford229fe472014-03-10 02:51:56 -07002209/**
Kees Cookc251a7b2017-10-22 15:30:04 -07002210 * megasas_start_timer - Initializes sriov heartbeat timer object
adam radford229fe472014-03-10 02:51:56 -07002211 * @instance: Adapter soft state
adam radford229fe472014-03-10 02:51:56 -07002212 *
2213 */
Kees Cookc251a7b2017-10-22 15:30:04 -07002214void megasas_start_timer(struct megasas_instance *instance)
adam radford229fe472014-03-10 02:51:56 -07002215{
Kees Cookc251a7b2017-10-22 15:30:04 -07002216 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2217
2218 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2219 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
adam radford229fe472014-03-10 02:51:56 -07002220 add_timer(timer);
2221}
2222
Yang, Bo707e09b2010-10-12 07:20:27 -06002223static void
2224megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2225
2226static void
2227process_fw_state_change_wq(struct work_struct *work);
2228
2229void megasas_do_ocr(struct megasas_instance *instance)
2230{
2231 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2232 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2233 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302234 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002235 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302236 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302237 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002238 instance->issuepend_done = 0;
2239
2240 atomic_set(&instance->fw_outstanding, 0);
2241 megasas_internal_reset_defer_cmds(instance);
2242 process_fw_state_change_wq(&instance->work_init);
2243}
2244
Adam Radford4cbfea82014-07-09 15:17:56 -07002245static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2246 int initial)
adam radford229fe472014-03-10 02:51:56 -07002247{
2248 struct megasas_cmd *cmd;
2249 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002250 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002251 dma_addr_t new_affiliation_111_h;
2252 int ld, retval = 0;
2253 u8 thisVf;
2254
2255 cmd = megasas_get_cmd(instance);
2256
2257 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002258 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2259 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002260 instance->host->host_no);
2261 return -ENOMEM;
2262 }
2263
2264 dcmd = &cmd->frame->dcmd;
2265
Adam Radford4cbfea82014-07-09 15:17:56 -07002266 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002267 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2268 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002269 megasas_return_cmd(instance, cmd);
2270 return -ENOMEM;
2271 }
2272
2273 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002274 memset(instance->vf_affiliation_111, 0,
2275 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002276 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002277 new_affiliation_111 =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002278 dma_alloc_coherent(&instance->pdev->dev,
2279 sizeof(struct MR_LD_VF_AFFILIATION_111),
2280 &new_affiliation_111_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002281 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002282 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2283 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002284 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002285 megasas_return_cmd(instance, cmd);
2286 return -ENOMEM;
2287 }
adam radford229fe472014-03-10 02:51:56 -07002288 }
2289
2290 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2291
2292 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302293 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002294 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302295 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002296 dcmd->timeout = 0;
2297 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302298 dcmd->data_xfer_len =
2299 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2300 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002301
Adam Radford4cbfea82014-07-09 15:17:56 -07002302 if (initial)
2303 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302304 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002305 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302306 dcmd->sgl.sge32[0].phys_addr =
2307 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002308
Christoph Hellwig2213a462015-04-23 16:33:54 +05302309 dcmd->sgl.sge32[0].length = cpu_to_le32(
2310 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002311
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002312 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002313 "scsi%d\n", instance->host->host_no);
2314
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302315 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002316 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2317 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002318 dcmd->cmd_status, instance->host->host_no);
2319 retval = 1; /* Do a scan if we couldn't get affiliation */
2320 goto out;
2321 }
2322
2323 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002324 thisVf = new_affiliation_111->thisVf;
2325 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2326 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2327 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002328 dev_warn(&instance->pdev->dev, "SR-IOV: "
2329 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002330 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002331 memcpy(instance->vf_affiliation_111,
2332 new_affiliation_111,
2333 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002334 retval = 1;
2335 goto out;
2336 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002337 }
2338out:
2339 if (new_affiliation_111) {
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002340 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002341 sizeof(struct MR_LD_VF_AFFILIATION_111),
2342 new_affiliation_111,
2343 new_affiliation_111_h);
2344 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302345
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302346 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002347
2348 return retval;
2349}
2350
2351static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2352 int initial)
2353{
2354 struct megasas_cmd *cmd;
2355 struct megasas_dcmd_frame *dcmd;
2356 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2357 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2358 dma_addr_t new_affiliation_h;
2359 int i, j, retval = 0, found = 0, doscan = 0;
2360 u8 thisVf;
2361
2362 cmd = megasas_get_cmd(instance);
2363
2364 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002365 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2366 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002367 instance->host->host_no);
2368 return -ENOMEM;
2369 }
2370
2371 dcmd = &cmd->frame->dcmd;
2372
2373 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002374 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2375 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002376 megasas_return_cmd(instance, cmd);
2377 return -ENOMEM;
2378 }
2379
2380 if (initial)
2381 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2382 sizeof(struct MR_LD_VF_AFFILIATION));
2383 else {
2384 new_affiliation =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002385 dma_alloc_coherent(&instance->pdev->dev,
2386 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2387 &new_affiliation_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002388 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002389 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2390 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002391 instance->host->host_no);
2392 megasas_return_cmd(instance, cmd);
2393 return -ENOMEM;
2394 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002395 }
2396
2397 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2398
2399 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302400 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002401 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302402 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002403 dcmd->timeout = 0;
2404 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302405 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2406 sizeof(struct MR_LD_VF_AFFILIATION));
2407 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002408
2409 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302410 dcmd->sgl.sge32[0].phys_addr =
2411 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002412 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302413 dcmd->sgl.sge32[0].phys_addr =
2414 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002415
Christoph Hellwig2213a462015-04-23 16:33:54 +05302416 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2417 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002418
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002419 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002420 "scsi%d\n", instance->host->host_no);
2421
Adam Radford4cbfea82014-07-09 15:17:56 -07002422
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302423 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002424 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2425 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002426 dcmd->cmd_status, instance->host->host_no);
2427 retval = 1; /* Do a scan if we couldn't get affiliation */
2428 goto out;
2429 }
2430
2431 if (!initial) {
2432 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002433 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2434 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002435 instance->host->host_no);
2436 retval = 1;
2437 goto out;
2438 }
2439 newmap = new_affiliation->map;
2440 savedmap = instance->vf_affiliation->map;
2441 thisVf = new_affiliation->thisVf;
2442 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2443 found = 0;
2444 for (j = 0; j < instance->vf_affiliation->ldCount;
2445 j++) {
2446 if (newmap->ref.targetId ==
2447 savedmap->ref.targetId) {
2448 found = 1;
2449 if (newmap->policy[thisVf] !=
2450 savedmap->policy[thisVf]) {
2451 doscan = 1;
2452 goto out;
2453 }
adam radford229fe472014-03-10 02:51:56 -07002454 }
2455 savedmap = (struct MR_LD_VF_MAP *)
2456 ((unsigned char *)savedmap +
2457 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002458 }
2459 if (!found && newmap->policy[thisVf] !=
2460 MR_LD_ACCESS_HIDDEN) {
2461 doscan = 1;
2462 goto out;
2463 }
2464 newmap = (struct MR_LD_VF_MAP *)
2465 ((unsigned char *)newmap + newmap->size);
2466 }
2467
2468 newmap = new_affiliation->map;
2469 savedmap = instance->vf_affiliation->map;
2470
2471 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2472 found = 0;
2473 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2474 if (savedmap->ref.targetId ==
2475 newmap->ref.targetId) {
2476 found = 1;
2477 if (savedmap->policy[thisVf] !=
2478 newmap->policy[thisVf]) {
2479 doscan = 1;
2480 goto out;
2481 }
2482 }
adam radford229fe472014-03-10 02:51:56 -07002483 newmap = (struct MR_LD_VF_MAP *)
2484 ((unsigned char *)newmap +
2485 newmap->size);
2486 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002487 if (!found && savedmap->policy[thisVf] !=
2488 MR_LD_ACCESS_HIDDEN) {
2489 doscan = 1;
2490 goto out;
2491 }
2492 savedmap = (struct MR_LD_VF_MAP *)
2493 ((unsigned char *)savedmap +
2494 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002495 }
2496 }
2497out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002498 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002499 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2500 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002501 memcpy(instance->vf_affiliation, new_affiliation,
2502 new_affiliation->size);
2503 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002504 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002505
2506 if (new_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002507 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002508 (MAX_LOGICAL_DRIVES + 1) *
2509 sizeof(struct MR_LD_VF_AFFILIATION),
2510 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302511 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002512
2513 return retval;
2514}
2515
Adam Radford4cbfea82014-07-09 15:17:56 -07002516/* This function will get the current SR-IOV LD/VF affiliation */
2517static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2518 int initial)
2519{
2520 int retval;
2521
2522 if (instance->PlasmaFW111)
2523 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2524 else
2525 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2526 return retval;
2527}
2528
adam radford229fe472014-03-10 02:51:56 -07002529/* This function will tell FW to start the SR-IOV heartbeat */
2530int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2531 int initial)
2532{
2533 struct megasas_cmd *cmd;
2534 struct megasas_dcmd_frame *dcmd;
2535 int retval = 0;
2536
2537 cmd = megasas_get_cmd(instance);
2538
2539 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002540 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2541 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002542 instance->host->host_no);
2543 return -ENOMEM;
2544 }
2545
2546 dcmd = &cmd->frame->dcmd;
2547
2548 if (initial) {
2549 instance->hb_host_mem =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002550 dma_alloc_coherent(&instance->pdev->dev,
2551 sizeof(struct MR_CTRL_HB_HOST_MEM),
2552 &instance->hb_host_mem_h,
2553 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07002554 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002555 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2556 " memory for heartbeat host memory for scsi%d\n",
2557 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002558 retval = -ENOMEM;
2559 goto out;
2560 }
adam radford229fe472014-03-10 02:51:56 -07002561 }
2562
2563 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2564
Christoph Hellwig2213a462015-04-23 16:33:54 +05302565 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002566 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302567 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002568 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302569 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002570 dcmd->timeout = 0;
2571 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302572 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2573 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
Shivasharan S107a60d2017-10-19 02:49:05 -07002574
2575 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2576 sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002577
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002578 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002579 instance->host->host_no);
2580
Shivasharan Se7d36b82017-10-19 02:48:50 -07002581 if ((instance->adapter_type != MFI_SERIES) &&
2582 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302583 retval = megasas_issue_blocked_cmd(instance, cmd,
2584 MEGASAS_ROUTINE_WAIT_TIME_VF);
2585 else
2586 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002587
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302588 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302589 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2590 "_MEM_ALLOC DCMD %s for scsi%d\n",
2591 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2592 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002593 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002594 }
2595
2596out:
2597 megasas_return_cmd(instance, cmd);
2598
2599 return retval;
2600}
2601
2602/* Handler for SR-IOV heartbeat */
Kees Cookc251a7b2017-10-22 15:30:04 -07002603static void megasas_sriov_heartbeat_handler(struct timer_list *t)
adam radford229fe472014-03-10 02:51:56 -07002604{
2605 struct megasas_instance *instance =
Kees Cookc251a7b2017-10-22 15:30:04 -07002606 from_timer(instance, t, sriov_heartbeat_timer);
adam radford229fe472014-03-10 02:51:56 -07002607
2608 if (instance->hb_host_mem->HB.fwCounter !=
2609 instance->hb_host_mem->HB.driverCounter) {
2610 instance->hb_host_mem->HB.driverCounter =
2611 instance->hb_host_mem->HB.fwCounter;
2612 mod_timer(&instance->sriov_heartbeat_timer,
2613 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2614 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002615 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002616 "completed for scsi%d\n", instance->host->host_no);
2617 schedule_work(&instance->work_init);
2618 }
2619}
2620
bo yang7343eb62007-11-09 04:35:44 -05002621/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002622 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2623 * @instance: Adapter soft state
2624 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002625 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002626 * complete all its outstanding commands. Returns error if one or more IOs
2627 * are pending after this time period. It also marks the controller dead.
2628 */
2629static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2630{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302631 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002632 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002633 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002634 unsigned long flags;
2635 struct list_head clist_local;
2636 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002637 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002638
Sumit Saxenaccc75072016-01-28 21:04:33 +05302639 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2640 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2641 __func__, __LINE__);
2642 return FAILED;
2643 }
bo yang39a98552010-09-22 22:36:29 -04002644
Sumit Saxena8a01a412016-01-28 21:04:32 +05302645 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002646
2647 INIT_LIST_HEAD(&clist_local);
2648 spin_lock_irqsave(&instance->hba_lock, flags);
2649 list_splice_init(&instance->internal_reset_pending_q,
2650 &clist_local);
2651 spin_unlock_irqrestore(&instance->hba_lock, flags);
2652
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002653 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002654 for (i = 0; i < wait_time; i++) {
2655 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302656 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002657 break;
2658 }
2659
Sumit Saxena8a01a412016-01-28 21:04:32 +05302660 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002661 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302662 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002663 return FAILED;
2664 }
2665
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002666 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002667 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002668 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002669 struct megasas_cmd, list);
2670 list_del_init(&reset_cmd->list);
2671 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002672 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002673 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002674 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002675 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002676
2677 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2678 megasas_return_cmd(instance, reset_cmd);
2679 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002680 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002681 "reset queue\n",
2682 reset_cmd);
2683
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302684 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002685 instance->instancet->fire_cmd(instance,
2686 reset_cmd->frame_phys_addr,
2687 0, instance->reg_set);
2688 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002689 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002690 "cmds lst\n",
2691 reset_cmd);
2692 }
2693 reset_index++;
2694 }
2695
2696 return SUCCESS;
2697 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002698
adam radfordc007b8b2012-07-17 18:20:24 -07002699 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302700 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002701
2702 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002703 break;
2704
2705 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002706 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002707 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002708 /*
2709 * Call cmd completion routine. Cmd to be
2710 * be completed directly without depending on isr.
2711 */
2712 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002713 }
2714
2715 msleep(1000);
2716 }
2717
Yang, Bo707e09b2010-10-12 07:20:27 -06002718 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302719 outstanding = atomic_read(&instance->fw_outstanding);
Shivasharan Sde516372018-12-17 00:47:39 -08002720 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002721
Sumit Saxenaccc75072016-01-28 21:04:33 +05302722 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2723 goto no_outstanding;
2724
2725 if (instance->disableOnlineCtrlReset)
2726 goto kill_hba_and_failed;
2727 do {
2728 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2729 dev_info(&instance->pdev->dev,
Colin Ian Kingefc372c2019-04-17 14:51:09 +01002730 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
Sumit Saxenaccc75072016-01-28 21:04:33 +05302731 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2732 if (i == 3)
2733 goto kill_hba_and_failed;
2734 megasas_do_ocr(instance);
2735
2736 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2737 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2738 __func__, __LINE__);
2739 return FAILED;
2740 }
2741 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2742 __func__, __LINE__);
2743
2744 for (sl = 0; sl < 10; sl++)
2745 msleep(500);
2746
2747 outstanding = atomic_read(&instance->fw_outstanding);
2748
Shivasharan Sde516372018-12-17 00:47:39 -08002749 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302750 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2751 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002752 }
2753 i++;
2754 } while (i <= 3);
2755
Sumit Saxenaccc75072016-01-28 21:04:33 +05302756no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002757
Sumit Saxenaccc75072016-01-28 21:04:33 +05302758 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2759 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002760 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302761
2762kill_hba_and_failed:
2763
2764 /* Reset not supported, kill adapter */
2765 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2766 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2767 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2768 atomic_read(&instance->fw_outstanding));
2769 megasas_dump_pending_frames(instance);
2770 megaraid_sas_kill_hba(instance);
2771
2772 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002773}
2774
2775/**
2776 * megasas_generic_reset - Generic reset routine
2777 * @scmd: Mid-layer SCSI command
2778 *
2779 * This routine implements a generic reset handler for device, bus and host
2780 * reset requests. Device, bus and host specific reset handlers can use this
2781 * function after they do their specific tasks.
2782 */
2783static int megasas_generic_reset(struct scsi_cmnd *scmd)
2784{
2785 int ret_val;
2786 struct megasas_instance *instance;
2787
2788 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2789
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002790 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2791 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792
Sumit Saxena8a01a412016-01-28 21:04:32 +05302793 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002794 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002795 return FAILED;
2796 }
2797
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002798 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002800 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002801 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002802 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002803
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002804 return ret_val;
2805}
2806
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002807/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002808 * megasas_reset_timer - quiesce the adapter if required
2809 * @scmd: scsi cmnd
2810 *
2811 * Sets the FW busy flag and reduces the host->can_queue if the
2812 * cmd has not been completed within the timeout period.
2813 */
2814static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002815blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002816{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002817 struct megasas_instance *instance;
2818 unsigned long flags;
2819
2820 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302821 (scmd_timeout * 2) * HZ)) {
Christoph Hellwig66005932018-05-29 15:52:29 +02002822 return BLK_EH_DONE;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002823 }
2824
adam radfordf575c5d2011-10-13 16:01:12 -07002825 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002826 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2827 /* FW is busy, throttle IO */
2828 spin_lock_irqsave(instance->host->host_lock, flags);
2829
adam radfordc5daa6a2012-07-17 18:20:03 -07002830 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002831 instance->last_time = jiffies;
2832 instance->flag |= MEGASAS_FW_BUSY;
2833
2834 spin_unlock_irqrestore(instance->host->host_lock, flags);
2835 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002836 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002837}
2838
2839/**
Shivasharan S4fe55032019-05-07 10:05:39 -07002840 * megasas_dump - This function will print hexdump of provided buffer.
2841 * @buf: Buffer to be dumped
2842 * @sz: Size in bytes
2843 * @format: Different formats of dumping e.g. format=n will
2844 * cause only 'n' 32 bit words to be dumped in a single
2845 * line.
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002846 */
Shivasharan S96c96032019-05-07 10:05:37 -07002847inline void
Shivasharan S4fe55032019-05-07 10:05:39 -07002848megasas_dump(void *buf, int sz, int format)
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002849{
2850 int i;
Shivasharan S4fe55032019-05-07 10:05:39 -07002851 __le32 *buf_loc = (__le32 *)buf;
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002852
Shivasharan S4fe55032019-05-07 10:05:39 -07002853 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2854 if ((i % format) == 0) {
2855 if (i != 0)
2856 printk(KERN_CONT "\n");
2857 printk(KERN_CONT "%08x: ", (i * 4));
2858 }
2859 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002860 }
Shivasharan S4fe55032019-05-07 10:05:39 -07002861 printk(KERN_CONT "\n");
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002862}
2863
2864/**
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07002865 * megasas_dump_reg_set - This function will print hexdump of register set
2866 * @buf: Buffer to be dumped
2867 * @sz: Size in bytes
2868 * @format: Different formats of dumping e.g. format=n will
2869 * cause only 'n' 32 bit words to be dumped in a
2870 * single line.
2871 */
2872inline void
2873megasas_dump_reg_set(void __iomem *reg_set)
2874{
2875 unsigned int i, sz = 256;
2876 u32 __iomem *reg = (u32 __iomem *)reg_set;
2877
2878 for (i = 0; i < (sz / sizeof(u32)); i++)
2879 printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2880}
2881
2882/**
Shivasharan S96c96032019-05-07 10:05:37 -07002883 * megasas_dump_fusion_io - This function will print key details
2884 * of SCSI IO
2885 * @scmd: SCSI command pointer of SCSI IO
2886 */
2887void
2888megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2889{
2890 struct megasas_cmd_fusion *cmd;
2891 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2892 struct megasas_instance *instance;
2893
2894 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2895 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2896
2897 scmd_printk(KERN_INFO, scmd,
2898 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2899 scmd, scmd->retries, scmd->allowed);
2900 scsi_print_command(scmd);
2901
2902 if (cmd) {
2903 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2904 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2905 scmd_printk(KERN_INFO, scmd,
2906 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
2907 req_desc->SCSIIO.RequestFlags,
2908 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2909 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2910
2911 printk(KERN_INFO "IO request frame:\n");
2912 megasas_dump(cmd->io_request,
Shivasharan S4fe55032019-05-07 10:05:39 -07002913 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
Shivasharan S96c96032019-05-07 10:05:37 -07002914 printk(KERN_INFO "Chain frame:\n");
2915 megasas_dump(cmd->sg_frame,
Shivasharan S4fe55032019-05-07 10:05:39 -07002916 instance->max_chain_frame_sz, 8);
Shivasharan S96c96032019-05-07 10:05:37 -07002917 }
2918
2919}
2920
Shivasharan Scfb9a302019-05-07 10:05:40 -07002921/*
2922 * megasas_dump_sys_regs - This function will dump system registers through
2923 * sysfs.
2924 * @reg_set: Pointer to System register set.
2925 * @buf: Buffer to which output is to be written.
2926 * @return: Number of bytes written to buffer.
2927 */
2928static inline ssize_t
2929megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2930{
2931 unsigned int i, sz = 256;
2932 int bytes_wrote = 0;
2933 char *loc = (char *)buf;
2934 u32 __iomem *reg = (u32 __iomem *)reg_set;
2935
2936 for (i = 0; i < sz / sizeof(u32); i++) {
2937 bytes_wrote += snprintf(loc + bytes_wrote, PAGE_SIZE,
2938 "%08x: %08x\n", (i * 4),
2939 readl(&reg[i]));
2940 }
2941 return bytes_wrote;
2942}
2943
Shivasharan S96c96032019-05-07 10:05:37 -07002944/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002945 * megasas_reset_bus_host - Bus & host reset handler entry point
2946 */
2947static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2948{
2949 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002950 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002951
adam radford9c915a82010-12-21 13:34:31 -08002952 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002953
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002954 scmd_printk(KERN_INFO, scmd,
Shivasharan S96c96032019-05-07 10:05:37 -07002955 "OCR is requested due to IO timeout!!\n");
2956
2957 scmd_printk(KERN_INFO, scmd,
2958 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
2959 scmd->device->host->shost_state,
Ming Leic84b0232018-06-24 22:03:26 +08002960 scsi_host_busy(scmd->device->host),
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002961 atomic_read(&instance->fw_outstanding));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002963 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002964 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002965 if (instance->adapter_type == MFI_SERIES) {
2966 ret = megasas_generic_reset(scmd);
2967 } else {
Shivasharan S96c96032019-05-07 10:05:37 -07002968 megasas_dump_fusion_io(scmd);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002969 ret = megasas_reset_fusion(scmd->device->host,
2970 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002971 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002972
2973 return ret;
2974}
2975
2976/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002977 * megasas_task_abort - Issues task abort request to firmware
2978 * (supported only for fusion adapters)
2979 * @scmd: SCSI command pointer
2980 */
2981static int megasas_task_abort(struct scsi_cmnd *scmd)
2982{
2983 int ret;
2984 struct megasas_instance *instance;
2985
2986 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2987
Shivasharan Se7d36b82017-10-19 02:48:50 -07002988 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002989 ret = megasas_task_abort_fusion(scmd);
2990 else {
2991 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2992 ret = FAILED;
2993 }
2994
2995 return ret;
2996}
2997
2998/**
2999 * megasas_reset_target: Issues target reset request to firmware
3000 * (supported only for fusion adapters)
3001 * @scmd: SCSI command pointer
3002 */
3003static int megasas_reset_target(struct scsi_cmnd *scmd)
3004{
3005 int ret;
3006 struct megasas_instance *instance;
3007
3008 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3009
Shivasharan Se7d36b82017-10-19 02:48:50 -07003010 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003011 ret = megasas_reset_target_fusion(scmd);
3012 else {
3013 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3014 ret = FAILED;
3015 }
3016
3017 return ret;
3018}
3019
3020/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003021 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003022 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003023 * @bdev: block device
3024 * @capacity: drive capacity
3025 * @geom: geometry parameters
3026 */
3027static int
3028megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3029 sector_t capacity, int geom[])
3030{
3031 int heads;
3032 int sectors;
3033 sector_t cylinders;
3034 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003035
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003036 /* Default heads (64) & sectors (32) */
3037 heads = 64;
3038 sectors = 32;
3039
3040 tmp = heads * sectors;
3041 cylinders = capacity;
3042
3043 sector_div(cylinders, tmp);
3044
3045 /*
3046 * Handle extended translation size for logical drives > 1Gb
3047 */
3048
3049 if (capacity >= 0x200000) {
3050 heads = 255;
3051 sectors = 63;
3052 tmp = heads*sectors;
3053 cylinders = capacity;
3054 sector_div(cylinders, tmp);
3055 }
3056
3057 geom[0] = heads;
3058 geom[1] = sectors;
3059 geom[2] = cylinders;
3060
3061 return 0;
3062}
3063
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003064static void megasas_aen_polling(struct work_struct *work);
3065
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003066/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003067 * megasas_service_aen - Processes an event notification
3068 * @instance: Adapter soft state
3069 * @cmd: AEN command completed by the ISR
3070 *
3071 * For AEN, driver sends a command down to FW that is held by the FW till an
3072 * event occurs. When an event of interest occurs, FW completes the command
3073 * that it was previously holding.
3074 *
3075 * This routines sends SIGIO signal to processes that have registered with the
3076 * driver for AEN.
3077 */
3078static void
3079megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3080{
Yang, Boc3518832009-10-06 14:18:02 -06003081 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003082
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003083 /*
3084 * Don't signal app if it is just an aborted previously registered aen
3085 */
Yang, Boc3518832009-10-06 14:18:02 -06003086 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3087 spin_lock_irqsave(&poll_aen_lock, flags);
3088 megasas_poll_wait_aen = 1;
3089 spin_unlock_irqrestore(&poll_aen_lock, flags);
3090 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003091 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06003092 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003093 else
3094 cmd->abort_aen = 0;
3095
3096 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303097
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303098 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003099
bo yang39a98552010-09-22 22:36:29 -04003100 if ((instance->unload == 0) &&
3101 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003102 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003103
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003104 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3105 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003106 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003107 } else {
3108 ev->instance = instance;
3109 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08003110 INIT_DELAYED_WORK(&ev->hotplug_work,
3111 megasas_aen_polling);
3112 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003113 }
3114 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003115}
3116
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303117static ssize_t
3118megasas_fw_crash_buffer_store(struct device *cdev,
3119 struct device_attribute *attr, const char *buf, size_t count)
3120{
3121 struct Scsi_Host *shost = class_to_shost(cdev);
3122 struct megasas_instance *instance =
3123 (struct megasas_instance *) shost->hostdata;
3124 int val = 0;
3125 unsigned long flags;
3126
3127 if (kstrtoint(buf, 0, &val) != 0)
3128 return -EINVAL;
3129
3130 spin_lock_irqsave(&instance->crashdump_lock, flags);
3131 instance->fw_crash_buffer_offset = val;
3132 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3133 return strlen(buf);
3134}
3135
3136static ssize_t
3137megasas_fw_crash_buffer_show(struct device *cdev,
3138 struct device_attribute *attr, char *buf)
3139{
3140 struct Scsi_Host *shost = class_to_shost(cdev);
3141 struct megasas_instance *instance =
3142 (struct megasas_instance *) shost->hostdata;
3143 u32 size;
3144 unsigned long buff_addr;
3145 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3146 unsigned long src_addr;
3147 unsigned long flags;
3148 u32 buff_offset;
3149
3150 spin_lock_irqsave(&instance->crashdump_lock, flags);
3151 buff_offset = instance->fw_crash_buffer_offset;
3152 if (!instance->crash_dump_buf &&
3153 !((instance->fw_crash_state == AVAILABLE) ||
3154 (instance->fw_crash_state == COPYING))) {
3155 dev_err(&instance->pdev->dev,
3156 "Firmware crash dump is not available\n");
3157 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3158 return -EINVAL;
3159 }
3160
3161 buff_addr = (unsigned long) buf;
3162
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003163 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303164 dev_err(&instance->pdev->dev,
3165 "Firmware crash dump offset is out of range\n");
3166 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3167 return 0;
3168 }
3169
3170 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3171 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3172
3173 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3174 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003175 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303176 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3177
3178 return size;
3179}
3180
3181static ssize_t
3182megasas_fw_crash_buffer_size_show(struct device *cdev,
3183 struct device_attribute *attr, char *buf)
3184{
3185 struct Scsi_Host *shost = class_to_shost(cdev);
3186 struct megasas_instance *instance =
3187 (struct megasas_instance *) shost->hostdata;
3188
3189 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3190 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3191}
3192
3193static ssize_t
3194megasas_fw_crash_state_store(struct device *cdev,
3195 struct device_attribute *attr, const char *buf, size_t count)
3196{
3197 struct Scsi_Host *shost = class_to_shost(cdev);
3198 struct megasas_instance *instance =
3199 (struct megasas_instance *) shost->hostdata;
3200 int val = 0;
3201 unsigned long flags;
3202
3203 if (kstrtoint(buf, 0, &val) != 0)
3204 return -EINVAL;
3205
3206 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3207 dev_err(&instance->pdev->dev, "application updates invalid "
3208 "firmware crash state\n");
3209 return -EINVAL;
3210 }
3211
3212 instance->fw_crash_state = val;
3213
3214 if ((val == COPIED) || (val == COPY_ERROR)) {
3215 spin_lock_irqsave(&instance->crashdump_lock, flags);
3216 megasas_free_host_crash_buffer(instance);
3217 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3218 if (val == COPY_ERROR)
3219 dev_info(&instance->pdev->dev, "application failed to "
3220 "copy Firmware crash dump\n");
3221 else
3222 dev_info(&instance->pdev->dev, "Firmware crash dump "
3223 "copied successfully\n");
3224 }
3225 return strlen(buf);
3226}
3227
3228static ssize_t
3229megasas_fw_crash_state_show(struct device *cdev,
3230 struct device_attribute *attr, char *buf)
3231{
3232 struct Scsi_Host *shost = class_to_shost(cdev);
3233 struct megasas_instance *instance =
3234 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003235
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303236 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3237}
3238
3239static ssize_t
3240megasas_page_size_show(struct device *cdev,
3241 struct device_attribute *attr, char *buf)
3242{
3243 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3244}
3245
Sumit Saxena308ec452016-01-28 21:04:30 +05303246static ssize_t
3247megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3248 char *buf)
3249{
3250 struct Scsi_Host *shost = class_to_shost(cdev);
3251 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3252
3253 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3254}
3255
Shivasharan S88d155c2018-01-05 05:27:46 -08003256static ssize_t
3257megasas_fw_cmds_outstanding_show(struct device *cdev,
3258 struct device_attribute *attr, char *buf)
3259{
3260 struct Scsi_Host *shost = class_to_shost(cdev);
3261 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3262
3263 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3264}
3265
Shivasharan Scfb9a302019-05-07 10:05:40 -07003266static ssize_t
3267megasas_dump_system_regs_show(struct device *cdev,
3268 struct device_attribute *attr, char *buf)
3269{
3270 struct Scsi_Host *shost = class_to_shost(cdev);
3271 struct megasas_instance *instance =
3272 (struct megasas_instance *)shost->hostdata;
3273
3274 return megasas_dump_sys_regs(instance->reg_set, buf);
3275}
3276
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303277static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3278 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3279static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3280 megasas_fw_crash_buffer_size_show, NULL);
3281static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3282 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3283static DEVICE_ATTR(page_size, S_IRUGO,
3284 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303285static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3286 megasas_ldio_outstanding_show, NULL);
Shivasharan S88d155c2018-01-05 05:27:46 -08003287static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3288 megasas_fw_cmds_outstanding_show, NULL);
Shivasharan Scfb9a302019-05-07 10:05:40 -07003289static DEVICE_ATTR(dump_system_regs, S_IRUGO,
3290 megasas_dump_system_regs_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303291
3292struct device_attribute *megaraid_host_attrs[] = {
3293 &dev_attr_fw_crash_buffer_size,
3294 &dev_attr_fw_crash_buffer,
3295 &dev_attr_fw_crash_state,
3296 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303297 &dev_attr_ldio_outstanding,
Shivasharan S88d155c2018-01-05 05:27:46 -08003298 &dev_attr_fw_cmds_outstanding,
Shivasharan Scfb9a302019-05-07 10:05:40 -07003299 &dev_attr_dump_system_regs,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303300 NULL,
3301};
3302
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003303/*
3304 * Scsi host template for megaraid_sas driver
3305 */
3306static struct scsi_host_template megasas_template = {
3307
3308 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303309 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003310 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003311 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003312 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303313 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003314 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003315 .eh_target_reset_handler = megasas_reset_target,
3316 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003317 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003318 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303319 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003320 .bios_param = megasas_bios_param,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003321 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003322 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003323};
3324
3325/**
3326 * megasas_complete_int_cmd - Completes an internal command
3327 * @instance: Adapter soft state
3328 * @cmd: Command to be completed
3329 *
3330 * The megasas_issue_blocked_cmd() function waits for a command to complete
3331 * after it issues a command. This function wakes up that waiting routine by
3332 * calling wake_up() on the wait queue.
3333 */
3334static void
3335megasas_complete_int_cmd(struct megasas_instance *instance,
3336 struct megasas_cmd *cmd)
3337{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303338 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003339 wake_up(&instance->int_cmd_wait_q);
3340}
3341
3342/**
3343 * megasas_complete_abort - Completes aborting a command
3344 * @instance: Adapter soft state
3345 * @cmd: Cmd that was issued to abort another cmd
3346 *
adam radford0d490162010-12-14 19:17:17 -08003347 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3348 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003349 * wakes up all functions waiting on the same wait queue.
3350 */
3351static void
3352megasas_complete_abort(struct megasas_instance *instance,
3353 struct megasas_cmd *cmd)
3354{
3355 if (cmd->sync_cmd) {
3356 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303357 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003358 wake_up(&instance->abort_cmd_wait_q);
3359 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003360}
3361
3362/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003363 * megasas_complete_cmd - Completes a command
3364 * @instance: Adapter soft state
3365 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003366 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003367 * SCSI mid-layer instead of the value returned
3368 * by the FW. This should be used if caller wants
3369 * an alternate status (as in the case of aborted
3370 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371 */
adam radford9c915a82010-12-21 13:34:31 -08003372void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003373megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3374 u8 alt_status)
3375{
3376 int exception = 0;
3377 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003378 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003379 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303380 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003381
bo yang39a98552010-09-22 22:36:29 -04003382 /* flag for the retry reset */
3383 cmd->retry_for_fw_reset = 0;
3384
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003385 if (cmd->scmd)
3386 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003387
3388 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003389 case MFI_CMD_INVALID:
3390 /* Some older 1068 controller FW may keep a pended
3391 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3392 when booting the kdump kernel. Ignore this command to
3393 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003394 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3395 "completed\n");
3396 dev_warn(&instance->pdev->dev, "If you have a controller "
3397 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003398 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003399 case MFI_CMD_PD_SCSI_IO:
3400 case MFI_CMD_LD_SCSI_IO:
3401
3402 /*
3403 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3404 * issued either through an IO path or an IOCTL path. If it
3405 * was via IOCTL, we will send it to internal completion.
3406 */
3407 if (cmd->sync_cmd) {
3408 cmd->sync_cmd = 0;
3409 megasas_complete_int_cmd(instance, cmd);
3410 break;
3411 }
Gustavo A. R. Silva8d07f7d2018-11-27 22:32:27 -06003412 /* fall through */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003413
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003414 case MFI_CMD_LD_READ:
3415 case MFI_CMD_LD_WRITE:
3416
3417 if (alt_status) {
3418 cmd->scmd->result = alt_status << 16;
3419 exception = 1;
3420 }
3421
3422 if (exception) {
3423
Sumant Patroe4a082c2006-05-30 12:03:37 -07003424 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003425
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003426 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003427 cmd->scmd->scsi_done(cmd->scmd);
3428 megasas_return_cmd(instance, cmd);
3429
3430 break;
3431 }
3432
3433 switch (hdr->cmd_status) {
3434
3435 case MFI_STAT_OK:
3436 cmd->scmd->result = DID_OK << 16;
3437 break;
3438
3439 case MFI_STAT_SCSI_IO_FAILED:
3440 case MFI_STAT_LD_INIT_IN_PROGRESS:
3441 cmd->scmd->result =
3442 (DID_ERROR << 16) | hdr->scsi_status;
3443 break;
3444
3445 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3446
3447 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3448
3449 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3450 memset(cmd->scmd->sense_buffer, 0,
3451 SCSI_SENSE_BUFFERSIZE);
3452 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3453 hdr->sense_len);
3454
3455 cmd->scmd->result |= DRIVER_SENSE << 24;
3456 }
3457
3458 break;
3459
3460 case MFI_STAT_LD_OFFLINE:
3461 case MFI_STAT_DEVICE_NOT_FOUND:
3462 cmd->scmd->result = DID_BAD_TARGET << 16;
3463 break;
3464
3465 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003466 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003467 hdr->cmd_status);
3468 cmd->scmd->result = DID_ERROR << 16;
3469 break;
3470 }
3471
Sumant Patroe4a082c2006-05-30 12:03:37 -07003472 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003473
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003474 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003475 cmd->scmd->scsi_done(cmd->scmd);
3476 megasas_return_cmd(instance, cmd);
3477
3478 break;
3479
3480 case MFI_CMD_SMP:
3481 case MFI_CMD_STP:
Shivasharan Sf870bcb2018-01-05 05:33:04 -08003482 case MFI_CMD_NVME:
Shivasharan S82add4e2017-10-19 02:49:02 -07003483 megasas_complete_int_cmd(instance, cmd);
3484 break;
3485
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003486 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303487 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003488 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303489 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3490 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303491 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003492 spin_lock_irqsave(instance->host->host_lock, flags);
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003493 status = cmd->frame->hdr.cmd_status;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303494 instance->map_update_cmd = NULL;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003495 if (status != MFI_STAT_OK) {
3496 if (status != MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003497 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003498 cmd->frame->hdr.cmd_status);
3499 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303500 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003501 spin_unlock_irqrestore(
3502 instance->host->host_lock,
3503 flags);
3504 break;
3505 }
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003506 }
3507
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303508 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303509
3510 /*
3511 * Set fast path IO to ZERO.
3512 * Validate Map will set proper value.
3513 * Meanwhile all IOs will go as LD IO.
3514 */
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003515 if (status == MFI_STAT_OK &&
3516 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3517 instance->map_id++;
adam radford9c915a82010-12-21 13:34:31 -08003518 fusion->fast_path_io = 1;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003519 } else {
adam radford9c915a82010-12-21 13:34:31 -08003520 fusion->fast_path_io = 0;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003521 }
3522
adam radford9c915a82010-12-21 13:34:31 -08003523 megasas_sync_map_info(instance);
3524 spin_unlock_irqrestore(instance->host->host_lock,
3525 flags);
3526 break;
3527 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303528 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3529 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003530 spin_lock_irqsave(&poll_aen_lock, flags);
3531 megasas_poll_wait_aen = 0;
3532 spin_unlock_irqrestore(&poll_aen_lock, flags);
3533 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003534
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303535 /* FW has an updated PD sequence */
3536 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3537 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3538
3539 spin_lock_irqsave(instance->host->host_lock, flags);
3540 status = cmd->frame->hdr.cmd_status;
3541 instance->jbod_seq_cmd = NULL;
3542 megasas_return_cmd(instance, cmd);
3543
3544 if (status == MFI_STAT_OK) {
3545 instance->pd_seq_map_id++;
3546 /* Re-register a pd sync seq num cmd */
3547 if (megasas_sync_pd_seq_num(instance, true))
3548 instance->use_seqnum_jbod_fp = false;
3549 } else
3550 instance->use_seqnum_jbod_fp = false;
3551
3552 spin_unlock_irqrestore(instance->host->host_lock, flags);
3553 break;
3554 }
3555
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003556 /*
3557 * See if got an event notification
3558 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303559 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003560 megasas_service_aen(instance, cmd);
3561 else
3562 megasas_complete_int_cmd(instance, cmd);
3563
3564 break;
3565
3566 case MFI_CMD_ABORT:
3567 /*
3568 * Cmd issued to abort another cmd returned
3569 */
3570 megasas_complete_abort(instance, cmd);
3571 break;
3572
3573 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003574 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003575 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003576 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003577 break;
3578 }
3579}
3580
3581/**
bo yang39a98552010-09-22 22:36:29 -04003582 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003583 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003584 * @instance: Adapter soft state
3585 */
3586static inline void
3587megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3588{
3589 struct megasas_cmd *cmd;
3590 struct list_head clist_local;
3591 union megasas_evt_class_locale class_locale;
3592 unsigned long flags;
3593 u32 seq_num;
3594
3595 INIT_LIST_HEAD(&clist_local);
3596 spin_lock_irqsave(&instance->hba_lock, flags);
3597 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3598 spin_unlock_irqrestore(&instance->hba_lock, flags);
3599
3600 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003601 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003602 struct megasas_cmd, list);
3603 list_del_init(&cmd->list);
3604
3605 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003606 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3607 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003608 cmd, cmd->scmd, cmd->sync_cmd);
3609
3610 cmd->retry_for_fw_reset++;
3611
3612 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003613 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003614 "was tried multiple times during reset."
3615 "Shutting down the HBA\n",
3616 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303617 instance->instancet->disable_intr(instance);
3618 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003619 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003620 return;
3621 }
3622 }
3623
3624 if (cmd->sync_cmd == 1) {
3625 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003626 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003627 "cmd attached to internal command!\n");
3628 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003629 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003630 "on the internal reset queue,"
3631 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303632 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003633 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003634 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003635 0, instance->reg_set);
3636 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003637 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003638 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003639 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003640
3641 atomic_inc(&instance->fw_outstanding);
3642 instance->instancet->fire_cmd(instance,
3643 cmd->frame_phys_addr,
3644 cmd->frame_count-1, instance->reg_set);
3645 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003646 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003647 "internal reset defer list while re-issue!!\n",
3648 cmd);
3649 }
3650 }
3651
3652 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003653 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003654 megasas_return_cmd(instance, instance->aen_cmd);
3655
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003656 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003657 }
3658
3659 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003660 * Initiate AEN (Asynchronous Event Notification)
3661 */
bo yang39a98552010-09-22 22:36:29 -04003662 seq_num = instance->last_seq_num;
3663 class_locale.members.reserved = 0;
3664 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3665 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3666
3667 megasas_register_aen(instance, seq_num, class_locale.word);
3668}
3669
3670/**
3671 * Move the internal reset pending commands to a deferred queue.
3672 *
3673 * We move the commands pending at internal reset time to a
3674 * pending queue. This queue would be flushed after successful
3675 * completion of the internal reset sequence. if the internal reset
3676 * did not complete in time, the kernel reset handler would flush
3677 * these commands.
3678 **/
3679static void
3680megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3681{
3682 struct megasas_cmd *cmd;
3683 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003684 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003685 u32 defer_index;
3686 unsigned long flags;
3687
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003688 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303689 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003690 for (i = 0; i < max_cmd; i++) {
3691 cmd = instance->cmd_list[i];
3692 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003693 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003694 "on the defer queue as internal\n",
3695 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3696
3697 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003698 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003699 " moving this cmd:%p, %d %p, it was"
3700 "discovered on some list?\n",
3701 cmd, cmd->sync_cmd, cmd->scmd);
3702
3703 list_del_init(&cmd->list);
3704 }
3705 defer_index++;
3706 list_add_tail(&cmd->list,
3707 &instance->internal_reset_pending_q);
3708 }
3709 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303710 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003711}
3712
3713
3714static void
3715process_fw_state_change_wq(struct work_struct *work)
3716{
3717 struct megasas_instance *instance =
3718 container_of(work, struct megasas_instance, work_init);
3719 u32 wait;
3720 unsigned long flags;
3721
Sumit Saxena8a01a412016-01-28 21:04:32 +05303722 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003723 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303724 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003725 return ;
3726 }
3727
Sumit Saxena8a01a412016-01-28 21:04:32 +05303728 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003729 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003730 "state, restarting it...\n");
3731
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303732 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003733 atomic_set(&instance->fw_outstanding, 0);
3734
3735 atomic_set(&instance->fw_reset_no_pci_access, 1);
3736 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003737 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003738
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003739 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003740 "initiating next stage...\n");
3741
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003742 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003743 "state 2 starting...\n");
3744
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003745 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003746 for (wait = 0; wait < 30; wait++) {
3747 msleep(1000);
3748 }
3749
adam radford058a8fa2011-10-08 18:14:27 -07003750 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003751 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003752
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303753 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003754 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003755 return ;
3756 }
3757
3758 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3759 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3760 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3761 ) {
3762 *instance->consumer = *instance->producer;
3763 } else {
3764 *instance->consumer = 0;
3765 *instance->producer = 0;
3766 }
3767
3768 megasas_issue_init_mfi(instance);
3769
3770 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303771 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003772 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303773 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003774
3775 megasas_issue_pending_cmds_again(instance);
3776 instance->issuepend_done = 1;
3777 }
bo yang39a98552010-09-22 22:36:29 -04003778}
3779
3780/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003781 * megasas_deplete_reply_queue - Processes all completed commands
3782 * @instance: Adapter soft state
3783 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003784 * SCSI mid-layer instead of the status
3785 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003786 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003787 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003788static int
bo yang39a98552010-09-22 22:36:29 -04003789megasas_deplete_reply_queue(struct megasas_instance *instance,
3790 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003791{
bo yang39a98552010-09-22 22:36:29 -04003792 u32 mfiStatus;
3793 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003794
bo yang39a98552010-09-22 22:36:29 -04003795 if ((mfiStatus = instance->instancet->check_reset(instance,
3796 instance->reg_set)) == 1) {
3797 return IRQ_HANDLED;
3798 }
3799
Shivasharan Sde516372018-12-17 00:47:39 -08003800 mfiStatus = instance->instancet->clear_intr(instance);
3801 if (mfiStatus == 0) {
adam radforde1419192011-02-24 20:56:21 -08003802 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003803 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003804 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003805 }
3806
3807 instance->mfiStatus = mfiStatus;
3808
3809 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3810 fw_state = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08003811 instance) & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003812
3813 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003814 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003815 fw_state);
3816 }
3817
3818 if ((fw_state == MFI_STATE_FAULT) &&
3819 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003820 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003821
3822 if ((instance->pdev->device ==
3823 PCI_DEVICE_ID_LSI_SAS1064R) ||
3824 (instance->pdev->device ==
3825 PCI_DEVICE_ID_DELL_PERC5) ||
3826 (instance->pdev->device ==
3827 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3828
3829 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303830 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003831 }
3832
3833
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303834 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303835 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003836 instance->issuepend_done = 0;
3837
3838 atomic_set(&instance->fw_outstanding, 0);
3839 megasas_internal_reset_defer_cmds(instance);
3840
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003841 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303842 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003843
3844 schedule_work(&instance->work_init);
3845 return IRQ_HANDLED;
3846
3847 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003848 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003849 fw_state, instance->disableOnlineCtrlReset);
3850 }
3851 }
3852
Sumant Patro5d018ad2006-10-03 13:13:18 -07003853 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003854 return IRQ_HANDLED;
3855}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003856/**
3857 * megasas_isr - isr entry point
3858 */
David Howells7d12e782006-10-05 14:55:46 +01003859static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003860{
adam radfordc8e858f2011-10-08 18:15:13 -07003861 struct megasas_irq_context *irq_context = devp;
3862 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003863 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003864 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003865
adam radfordc8e858f2011-10-08 18:15:13 -07003866 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003867 return IRQ_HANDLED;
3868
bo yang39a98552010-09-22 22:36:29 -04003869 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003870 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003871 spin_unlock_irqrestore(&instance->hba_lock, flags);
3872
3873 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003874}
3875
3876/**
3877 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003878 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003879 *
3880 * During the initialization, FW passes can potentially be in any one of
3881 * several possible states. If the FW in operational, waiting-for-handshake
3882 * states, driver must take steps to bring it to ready state. Otherwise, it
3883 * has to wait for the ready state.
3884 */
adam radford9c915a82010-12-21 13:34:31 -08003885int
adam radford058a8fa2011-10-08 18:14:27 -07003886megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003887{
3888 int i;
3889 u8 max_wait;
3890 u32 fw_state;
3891 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003892 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003893
Shivasharan Sde516372018-12-17 00:47:39 -08003894 abs_state = instance->instancet->read_fw_status_reg(instance);
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003895 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003896
Sumant Patroe3bbff92006-10-03 12:28:49 -07003897 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003898 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003899 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003900
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003901 while (fw_state != MFI_STATE_READY) {
3902
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003903 switch (fw_state) {
3904
3905 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003906 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003907 if (ocr) {
3908 max_wait = MEGASAS_RESET_WAIT_TIME;
3909 cur_state = MFI_STATE_FAULT;
3910 break;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003911 } else {
3912 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
3913 megasas_dump_reg_set(instance->reg_set);
adam radford058a8fa2011-10-08 18:14:27 -07003914 return -ENODEV;
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07003915 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916
3917 case MFI_STATE_WAIT_HANDSHAKE:
3918 /*
3919 * Set the CLR bit in inbound doorbell
3920 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003921 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003922 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3923 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003924 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003925 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003926 writel(
3927 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003928 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303929 else
Yang, Bo87911122009-10-06 14:31:54 -06003930 writel(
3931 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3932 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003933
Yang, Bo7218df62009-10-06 14:52:20 -06003934 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003935 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3936 break;
3937
Sumant Patroe3bbff92006-10-03 12:28:49 -07003938 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003939 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003940 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3941 (instance->pdev->device ==
3942 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003943 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003944 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003945 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303946 else
Yang, Bo87911122009-10-06 14:31:54 -06003947 writel(MFI_INIT_HOTPLUG,
3948 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003949
Yang, Bo7218df62009-10-06 14:52:20 -06003950 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003951 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3952 break;
3953
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003954 case MFI_STATE_OPERATIONAL:
3955 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003956 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003957 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303958 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003959 if ((instance->pdev->device ==
3960 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3961 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003962 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003963 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003964 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003965 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303966
Shivasharan Se7d36b82017-10-19 02:48:50 -07003967 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003968 for (i = 0; i < (10 * 1000); i += 20) {
Shivasharan S272652f2018-12-17 00:47:40 -08003969 if (megasas_readl(
3970 instance,
adam radford9c915a82010-12-21 13:34:31 -08003971 &instance->
3972 reg_set->
3973 doorbell) & 1)
3974 msleep(20);
3975 else
3976 break;
3977 }
3978 }
Yang, Bo87911122009-10-06 14:31:54 -06003979 } else
3980 writel(MFI_RESET_FLAGS,
3981 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003982
Yang, Bo7218df62009-10-06 14:52:20 -06003983 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003984 cur_state = MFI_STATE_OPERATIONAL;
3985 break;
3986
3987 case MFI_STATE_UNDEFINED:
3988 /*
3989 * This state should not last for more than 2 seconds
3990 */
Yang, Bo7218df62009-10-06 14:52:20 -06003991 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003992 cur_state = MFI_STATE_UNDEFINED;
3993 break;
3994
3995 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003996 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003997 cur_state = MFI_STATE_BB_INIT;
3998 break;
3999
4000 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06004001 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004002 cur_state = MFI_STATE_FW_INIT;
4003 break;
4004
4005 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06004006 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004007 cur_state = MFI_STATE_FW_INIT_2;
4008 break;
4009
4010 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06004011 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004012 cur_state = MFI_STATE_DEVICE_SCAN;
4013 break;
4014
4015 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06004016 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004017 cur_state = MFI_STATE_FLUSH_CACHE;
4018 break;
4019
4020 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004021 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004022 fw_state);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004023 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4024 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004025 return -ENODEV;
4026 }
4027
4028 /*
4029 * The cur_state should not last for more than max_wait secs
4030 */
Steve Sistare31b6a052019-03-01 06:46:28 -08004031 for (i = 0; i < max_wait * 50; i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004032 curr_abs_state = instance->instancet->
Shivasharan Sde516372018-12-17 00:47:39 -08004033 read_fw_status_reg(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004034
Yang, Bo7218df62009-10-06 14:52:20 -06004035 if (abs_state == curr_abs_state) {
Steve Sistare31b6a052019-03-01 06:46:28 -08004036 msleep(20);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004037 } else
4038 break;
4039 }
4040
4041 /*
4042 * Return error if fw_state hasn't changed after max_wait
4043 */
Yang, Bo7218df62009-10-06 14:52:20 -06004044 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004045 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004046 "in %d secs\n", fw_state, max_wait);
Shivasharan S3d1d9eb2019-05-07 10:05:41 -07004047 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4048 megasas_dump_reg_set(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004049 return -ENODEV;
4050 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02004051
4052 abs_state = curr_abs_state;
4053 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04004054 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004055 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004056
4057 return 0;
4058}
4059
4060/**
4061 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4062 * @instance: Adapter soft state
4063 */
4064static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4065{
4066 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004067 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004068 struct megasas_cmd *cmd;
4069
4070 if (!instance->frame_dma_pool)
4071 return;
4072
4073 /*
4074 * Return all frames to pool
4075 */
4076 for (i = 0; i < max_cmd; i++) {
4077
4078 cmd = instance->cmd_list[i];
4079
4080 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02004081 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004082 cmd->frame_phys_addr);
4083
4084 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02004085 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004086 cmd->sense_phys_addr);
4087 }
4088
4089 /*
4090 * Now destroy the pool itself
4091 */
Romain Perierfc69d862017-07-06 10:13:06 +02004092 dma_pool_destroy(instance->frame_dma_pool);
4093 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004094
4095 instance->frame_dma_pool = NULL;
4096 instance->sense_dma_pool = NULL;
4097}
4098
4099/**
4100 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4101 * @instance: Adapter soft state
4102 *
4103 * Each command packet has an embedded DMA memory buffer that is used for
4104 * filling MFI frame and the SG list that immediately follows the frame. This
4105 * function creates those DMA memory buffers for each command packet by using
4106 * PCI pool facility.
4107 */
4108static int megasas_create_frame_pool(struct megasas_instance *instance)
4109{
4110 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004111 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004112 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004113 u32 frame_count;
4114 struct megasas_cmd *cmd;
4115
adam radford9c915a82010-12-21 13:34:31 -08004116 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004117
4118 /*
4119 * Size of our frame is 64 bytes for MFI frame, followed by max SG
4120 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4121 */
4122 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4123 sizeof(struct megasas_sge32);
4124
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004125 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06004126 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06004127
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004128 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05304129 * For MFI controllers.
4130 * max_num_sge = 60
4131 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4132 * Total 960 byte (15 MFI frame of 64 byte)
4133 *
4134 * Fusion adapter require only 3 extra frame.
4135 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4136 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4137 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004138 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07004139 frame_count = (instance->adapter_type == MFI_SERIES) ?
4140 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08004141 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004142 /*
4143 * Use DMA pool facility provided by PCI layer
4144 */
Romain Perierfc69d862017-07-06 10:13:06 +02004145 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4146 &instance->pdev->dev,
4147 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004148
4149 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004150 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004151 return -ENOMEM;
4152 }
4153
Romain Perierfc69d862017-07-06 10:13:06 +02004154 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4155 &instance->pdev->dev, 128,
4156 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004157
4158 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004159 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004160
Romain Perierfc69d862017-07-06 10:13:06 +02004161 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004162 instance->frame_dma_pool = NULL;
4163
4164 return -ENOMEM;
4165 }
4166
4167 /*
4168 * Allocate and attach a frame to each of the commands in cmd_list.
4169 * By making cmd->index as the context instead of the &cmd, we can
4170 * always use 32bit context regardless of the architecture
4171 */
4172 for (i = 0; i < max_cmd; i++) {
4173
4174 cmd = instance->cmd_list[i];
4175
Souptick Joarder61b142a2018-02-15 21:55:06 +05304176 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004177 GFP_KERNEL, &cmd->frame_phys_addr);
4178
Romain Perierfc69d862017-07-06 10:13:06 +02004179 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004180 GFP_KERNEL, &cmd->sense_phys_addr);
4181
4182 /*
4183 * megasas_teardown_frame_pool() takes care of freeing
4184 * whatever has been allocated
4185 */
4186 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004187 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004188 megasas_teardown_frame_pool(instance);
4189 return -ENOMEM;
4190 }
4191
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304192 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004193 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004194 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004195 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004196 }
4197
4198 return 0;
4199}
4200
4201/**
4202 * megasas_free_cmds - Free all the cmds in the free cmd pool
4203 * @instance: Adapter soft state
4204 */
adam radford9c915a82010-12-21 13:34:31 -08004205void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004206{
4207 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004208
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004209 /* First free the MFI frame pool */
4210 megasas_teardown_frame_pool(instance);
4211
4212 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004213 for (i = 0; i < instance->max_mfi_cmds; i++)
4214
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004215 kfree(instance->cmd_list[i]);
4216
4217 /* Free the cmd_list buffer itself */
4218 kfree(instance->cmd_list);
4219 instance->cmd_list = NULL;
4220
4221 INIT_LIST_HEAD(&instance->cmd_pool);
4222}
4223
4224/**
4225 * megasas_alloc_cmds - Allocates the command packets
4226 * @instance: Adapter soft state
4227 *
4228 * Each command that is issued to the FW, whether IO commands from the OS or
4229 * internal commands like IOCTLs, are wrapped in local data structure called
4230 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4231 * the FW.
4232 *
4233 * Each frame has a 32-bit field called context (tag). This context is used
4234 * to get back the megasas_cmd from the frame when a frame gets completed in
4235 * the ISR. Typically the address of the megasas_cmd itself would be used as
4236 * the context. But we wanted to keep the differences between 32 and 64 bit
4237 * systems to the mininum. We always use 32 bit integers for the context. In
4238 * this driver, the 32 bit values are the indices into an array cmd_list.
4239 * This array is used only to look up the megasas_cmd given the context. The
4240 * free commands themselves are maintained in a linked list called cmd_pool.
4241 */
adam radford9c915a82010-12-21 13:34:31 -08004242int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004243{
4244 int i;
4245 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004246 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004247 struct megasas_cmd *cmd;
4248
adam radford9c915a82010-12-21 13:34:31 -08004249 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004250
4251 /*
4252 * instance->cmd_list is an array of struct megasas_cmd pointers.
4253 * Allocate the dynamic array first and then allocate individual
4254 * commands.
4255 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004256 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004257
4258 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004259 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004260 return -ENOMEM;
4261 }
4262
adam radford9c915a82010-12-21 13:34:31 -08004263 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004264
4265 for (i = 0; i < max_cmd; i++) {
4266 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4267 GFP_KERNEL);
4268
4269 if (!instance->cmd_list[i]) {
4270
4271 for (j = 0; j < i; j++)
4272 kfree(instance->cmd_list[j]);
4273
4274 kfree(instance->cmd_list);
4275 instance->cmd_list = NULL;
4276
4277 return -ENOMEM;
4278 }
4279 }
4280
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004281 for (i = 0; i < max_cmd; i++) {
4282 cmd = instance->cmd_list[i];
4283 memset(cmd, 0, sizeof(struct megasas_cmd));
4284 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004285 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004286 cmd->instance = instance;
4287
4288 list_add_tail(&cmd->list, &instance->cmd_pool);
4289 }
4290
4291 /*
4292 * Create a frame pool and assign one frame to each cmd
4293 */
4294 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004295 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004296 megasas_free_cmds(instance);
Jason Yanbcf3b672019-02-15 19:50:27 +08004297 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004298 }
4299
4300 return 0;
4301}
4302
Yang, Bo81e403c2009-10-06 14:27:54 -06004303/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304304 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4305 * @instance: Adapter soft state
4306 *
4307 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4308 * or FW is not under OCR.
4309 */
4310inline int
4311dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4312
Shivasharan Se7d36b82017-10-19 02:48:50 -07004313 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304314 return KILL_ADAPTER;
4315 else if (instance->unload ||
4316 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4317 return IGNORE_TIMEOUT;
4318 else
4319 return INITIATE_OCR;
4320}
4321
Shivasharan S15dd0382017-02-10 00:59:10 -08004322static void
4323megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304324{
4325 int ret;
4326 struct megasas_cmd *cmd;
4327 struct megasas_dcmd_frame *dcmd;
4328
Shivasharan S15dd0382017-02-10 00:59:10 -08004329 struct MR_PRIV_DEVICE *mr_device_priv_data;
4330 u16 device_id = 0;
4331
4332 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304333 cmd = megasas_get_cmd(instance);
4334
4335 if (!cmd) {
4336 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004337 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304338 }
4339
4340 dcmd = &cmd->frame->dcmd;
4341
4342 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4343 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4344
4345 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4346 dcmd->cmd = MFI_CMD_DCMD;
4347 dcmd->cmd_status = 0xFF;
4348 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004349 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304350 dcmd->timeout = 0;
4351 dcmd->pad_0 = 0;
4352 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4353 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304354
Shivasharan S107a60d2017-10-19 02:49:05 -07004355 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4356 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304357
Shivasharan Se7d36b82017-10-19 02:48:50 -07004358 if ((instance->adapter_type != MFI_SERIES) &&
4359 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304360 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4361 else
4362 ret = megasas_issue_polled(instance, cmd);
4363
4364 switch (ret) {
4365 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004366 mr_device_priv_data = sdev->hostdata;
4367 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4368 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304369 instance->pd_info->state.ddf.pdType.intf;
4370 break;
4371
4372 case DCMD_TIMEOUT:
4373
4374 switch (dcmd_timeout_ocr_possible(instance)) {
4375 case INITIATE_OCR:
4376 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4377 megasas_reset_fusion(instance->host,
4378 MFI_IO_TIMEOUT_OCR);
4379 break;
4380 case KILL_ADAPTER:
4381 megaraid_sas_kill_hba(instance);
4382 break;
4383 case IGNORE_TIMEOUT:
4384 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4385 __func__, __LINE__);
4386 break;
4387 }
4388
4389 break;
4390 }
4391
4392 if (ret != DCMD_TIMEOUT)
4393 megasas_return_cmd(instance, cmd);
4394
Shivasharan S15dd0382017-02-10 00:59:10 -08004395 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304396}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304397/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004398 * megasas_get_pd_list_info - Returns FW's pd_list structure
4399 * @instance: Adapter soft state
4400 * @pd_list: pd_list structure
4401 *
4402 * Issues an internal command (DCMD) to get the FW's controller PD
4403 * list structure. This information is mainly used to find out SYSTEM
4404 * supported by the FW.
4405 */
4406static int
4407megasas_get_pd_list(struct megasas_instance *instance)
4408{
4409 int ret = 0, pd_index = 0;
4410 struct megasas_cmd *cmd;
4411 struct megasas_dcmd_frame *dcmd;
4412 struct MR_PD_LIST *ci;
4413 struct MR_PD_ADDRESS *pd_addr;
4414 dma_addr_t ci_h = 0;
4415
Sumit Saxenad9083162016-07-08 03:30:16 -07004416 if (instance->pd_list_not_supported) {
4417 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4418 "not supported by firmware\n");
4419 return ret;
4420 }
4421
Shivasharan S9b3d0282017-10-19 02:48:56 -07004422 ci = instance->pd_list_buf;
4423 ci_h = instance->pd_list_buf_h;
4424
Yang, Bo81e403c2009-10-06 14:27:54 -06004425 cmd = megasas_get_cmd(instance);
4426
4427 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004428 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004429 return -ENOMEM;
4430 }
4431
4432 dcmd = &cmd->frame->dcmd;
4433
Yang, Bo81e403c2009-10-06 14:27:54 -06004434 memset(ci, 0, sizeof(*ci));
4435 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4436
4437 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4438 dcmd->mbox.b[1] = 0;
4439 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304440 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004441 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004442 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004443 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004444 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304445 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4446 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004447
Shivasharan S107a60d2017-10-19 02:49:05 -07004448 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4449 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004450
Shivasharan Se7d36b82017-10-19 02:48:50 -07004451 if ((instance->adapter_type != MFI_SERIES) &&
4452 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304453 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304454 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304455 else
4456 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004457
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304458 switch (ret) {
4459 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004460 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4461 "failed/not supported by firmware\n");
4462
Shivasharan Se7d36b82017-10-19 02:48:50 -07004463 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004464 megaraid_sas_kill_hba(instance);
4465 else
4466 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304467 break;
4468 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004469
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304470 switch (dcmd_timeout_ocr_possible(instance)) {
4471 case INITIATE_OCR:
4472 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4473 /*
4474 * DCMD failed from AEN path.
4475 * AEN path already hold reset_mutex to avoid PCI access
4476 * while OCR is in progress.
4477 */
4478 mutex_unlock(&instance->reset_mutex);
4479 megasas_reset_fusion(instance->host,
4480 MFI_IO_TIMEOUT_OCR);
4481 mutex_lock(&instance->reset_mutex);
4482 break;
4483 case KILL_ADAPTER:
4484 megaraid_sas_kill_hba(instance);
4485 break;
4486 case IGNORE_TIMEOUT:
4487 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4488 __func__, __LINE__);
4489 break;
4490 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004491
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304492 break;
4493
4494 case DCMD_SUCCESS:
4495 pd_addr = ci->addr;
4496
4497 if ((le32_to_cpu(ci->count) >
4498 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4499 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004500
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304501 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304502 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004503
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304504 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304505 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304506 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304507 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304508 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304509 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304510 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004511 pd_addr++;
4512 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304513
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304514 memcpy(instance->pd_list, instance->local_pd_list,
4515 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304516 break;
4517
Yang, Bo81e403c2009-10-06 14:27:54 -06004518 }
4519
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304520 if (ret != DCMD_TIMEOUT)
4521 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004522
4523 return ret;
4524}
4525
Yang, Bobdc6fb82009-12-06 08:30:19 -07004526/*
4527 * megasas_get_ld_list_info - Returns FW's ld_list structure
4528 * @instance: Adapter soft state
4529 * @ld_list: ld_list structure
4530 *
4531 * Issues an internal command (DCMD) to get the FW's controller PD
4532 * list structure. This information is mainly used to find out SYSTEM
4533 * supported by the FW.
4534 */
4535static int
4536megasas_get_ld_list(struct megasas_instance *instance)
4537{
4538 int ret = 0, ld_index = 0, ids = 0;
4539 struct megasas_cmd *cmd;
4540 struct megasas_dcmd_frame *dcmd;
4541 struct MR_LD_LIST *ci;
4542 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304543 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004544
Shivasharan S9b3d0282017-10-19 02:48:56 -07004545 ci = instance->ld_list_buf;
4546 ci_h = instance->ld_list_buf_h;
4547
Yang, Bobdc6fb82009-12-06 08:30:19 -07004548 cmd = megasas_get_cmd(instance);
4549
4550 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004551 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004552 return -ENOMEM;
4553 }
4554
4555 dcmd = &cmd->frame->dcmd;
4556
Yang, Bobdc6fb82009-12-06 08:30:19 -07004557 memset(ci, 0, sizeof(*ci));
4558 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4559
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304560 if (instance->supportmax256vd)
4561 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004562 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304563 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004564 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004565 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004566 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304567 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4568 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004569 dcmd->pad_0 = 0;
4570
Shivasharan S107a60d2017-10-19 02:49:05 -07004571 megasas_set_dma_settings(instance, dcmd, ci_h,
4572 sizeof(struct MR_LD_LIST));
4573
Shivasharan Se7d36b82017-10-19 02:48:50 -07004574 if ((instance->adapter_type != MFI_SERIES) &&
4575 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304576 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304577 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304578 else
4579 ret = megasas_issue_polled(instance, cmd);
4580
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304581 ld_count = le32_to_cpu(ci->ldCount);
4582
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304583 switch (ret) {
4584 case DCMD_FAILED:
4585 megaraid_sas_kill_hba(instance);
4586 break;
4587 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004588
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304589 switch (dcmd_timeout_ocr_possible(instance)) {
4590 case INITIATE_OCR:
4591 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4592 /*
4593 * DCMD failed from AEN path.
4594 * AEN path already hold reset_mutex to avoid PCI access
4595 * while OCR is in progress.
4596 */
4597 mutex_unlock(&instance->reset_mutex);
4598 megasas_reset_fusion(instance->host,
4599 MFI_IO_TIMEOUT_OCR);
4600 mutex_lock(&instance->reset_mutex);
4601 break;
4602 case KILL_ADAPTER:
4603 megaraid_sas_kill_hba(instance);
4604 break;
4605 case IGNORE_TIMEOUT:
4606 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4607 __func__, __LINE__);
4608 break;
4609 }
4610
4611 break;
4612
4613 case DCMD_SUCCESS:
4614 if (ld_count > instance->fw_supported_vd_count)
4615 break;
4616
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304617 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004618
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304619 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004620 if (ci->ldList[ld_index].state != 0) {
4621 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304622 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004623 }
4624 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304625
4626 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004627 }
4628
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304629 if (ret != DCMD_TIMEOUT)
4630 megasas_return_cmd(instance, cmd);
4631
Yang, Bobdc6fb82009-12-06 08:30:19 -07004632 return ret;
4633}
4634
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004635/**
adam radford21c9e162013-09-06 15:27:14 -07004636 * megasas_ld_list_query - Returns FW's ld_list structure
4637 * @instance: Adapter soft state
4638 * @ld_list: ld_list structure
4639 *
4640 * Issues an internal command (DCMD) to get the FW's controller PD
4641 * list structure. This information is mainly used to find out SYSTEM
4642 * supported by the FW.
4643 */
4644static int
4645megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4646{
4647 int ret = 0, ld_index = 0, ids = 0;
4648 struct megasas_cmd *cmd;
4649 struct megasas_dcmd_frame *dcmd;
4650 struct MR_LD_TARGETID_LIST *ci;
4651 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304652 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004653
Shivasharan S9b3d0282017-10-19 02:48:56 -07004654 ci = instance->ld_targetid_list_buf;
4655 ci_h = instance->ld_targetid_list_buf_h;
4656
adam radford21c9e162013-09-06 15:27:14 -07004657 cmd = megasas_get_cmd(instance);
4658
4659 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004660 dev_warn(&instance->pdev->dev,
4661 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004662 return -ENOMEM;
4663 }
4664
4665 dcmd = &cmd->frame->dcmd;
4666
adam radford21c9e162013-09-06 15:27:14 -07004667 memset(ci, 0, sizeof(*ci));
4668 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4669
4670 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304671 if (instance->supportmax256vd)
4672 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004673
4674 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304675 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004676 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004677 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004678 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304679 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4680 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004681 dcmd->pad_0 = 0;
4682
Shivasharan S107a60d2017-10-19 02:49:05 -07004683 megasas_set_dma_settings(instance, dcmd, ci_h,
4684 sizeof(struct MR_LD_TARGETID_LIST));
4685
Shivasharan Se7d36b82017-10-19 02:48:50 -07004686 if ((instance->adapter_type != MFI_SERIES) &&
4687 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304688 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304689 else
4690 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004691
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304692 switch (ret) {
4693 case DCMD_FAILED:
4694 dev_info(&instance->pdev->dev,
4695 "DCMD not supported by firmware - %s %d\n",
4696 __func__, __LINE__);
4697 ret = megasas_get_ld_list(instance);
4698 break;
4699 case DCMD_TIMEOUT:
4700 switch (dcmd_timeout_ocr_possible(instance)) {
4701 case INITIATE_OCR:
4702 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4703 /*
4704 * DCMD failed from AEN path.
4705 * AEN path already hold reset_mutex to avoid PCI access
4706 * while OCR is in progress.
4707 */
4708 mutex_unlock(&instance->reset_mutex);
4709 megasas_reset_fusion(instance->host,
4710 MFI_IO_TIMEOUT_OCR);
4711 mutex_lock(&instance->reset_mutex);
4712 break;
4713 case KILL_ADAPTER:
4714 megaraid_sas_kill_hba(instance);
4715 break;
4716 case IGNORE_TIMEOUT:
4717 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4718 __func__, __LINE__);
4719 break;
4720 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304721
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304722 break;
4723 case DCMD_SUCCESS:
4724 tgtid_count = le32_to_cpu(ci->count);
4725
4726 if ((tgtid_count > (instance->fw_supported_vd_count)))
4727 break;
4728
adam radford21c9e162013-09-06 15:27:14 -07004729 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304730 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004731 ids = ci->targetId[ld_index];
4732 instance->ld_ids[ids] = ci->targetId[ld_index];
4733 }
4734
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304735 break;
adam radford21c9e162013-09-06 15:27:14 -07004736 }
4737
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304738 if (ret != DCMD_TIMEOUT)
4739 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004740
4741 return ret;
4742}
4743
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004744/**
4745 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4746 * dcmd.mbox - reserved
4747 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4748 * Desc: This DCMD will return the combined device list
4749 * Status: MFI_STAT_OK - List returned successfully
4750 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4751 * disabled
4752 * @instance: Adapter soft state
4753 * @is_probe: Driver probe check
4754 * Return: 0 if DCMD succeeded
4755 * non-zero if failed
4756 */
YueHaibing7c3f8ca2019-04-13 22:21:38 +08004757static int
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004758megasas_host_device_list_query(struct megasas_instance *instance,
4759 bool is_probe)
4760{
4761 int ret, i, target_id;
4762 struct megasas_cmd *cmd;
4763 struct megasas_dcmd_frame *dcmd;
4764 struct MR_HOST_DEVICE_LIST *ci;
4765 u32 count;
4766 dma_addr_t ci_h;
4767
4768 ci = instance->host_device_list_buf;
4769 ci_h = instance->host_device_list_buf_h;
4770
4771 cmd = megasas_get_cmd(instance);
4772
4773 if (!cmd) {
4774 dev_warn(&instance->pdev->dev,
4775 "%s: failed to get cmd\n",
4776 __func__);
4777 return -ENOMEM;
4778 }
4779
4780 dcmd = &cmd->frame->dcmd;
4781
4782 memset(ci, 0, sizeof(*ci));
4783 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4784
4785 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4786 dcmd->cmd = MFI_CMD_DCMD;
4787 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4788 dcmd->sge_count = 1;
4789 dcmd->flags = MFI_FRAME_DIR_READ;
4790 dcmd->timeout = 0;
4791 dcmd->pad_0 = 0;
4792 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4793 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4794
4795 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4796
4797 if (!instance->mask_interrupts) {
4798 ret = megasas_issue_blocked_cmd(instance, cmd,
4799 MFI_IO_TIMEOUT_SECS);
4800 } else {
4801 ret = megasas_issue_polled(instance, cmd);
4802 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4803 }
4804
4805 switch (ret) {
4806 case DCMD_SUCCESS:
4807 /* Fill the internal pd_list and ld_ids array based on
4808 * targetIds returned by FW
4809 */
4810 count = le32_to_cpu(ci->count);
4811
4812 memset(instance->local_pd_list, 0,
4813 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4814 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4815 for (i = 0; i < count; i++) {
4816 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4817 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4818 instance->local_pd_list[target_id].tid = target_id;
4819 instance->local_pd_list[target_id].driveType =
4820 ci->host_device_list[i].scsi_type;
4821 instance->local_pd_list[target_id].driveState =
4822 MR_PD_STATE_SYSTEM;
4823 } else {
4824 instance->ld_ids[target_id] = target_id;
4825 }
4826 }
4827
4828 memcpy(instance->pd_list, instance->local_pd_list,
4829 sizeof(instance->pd_list));
4830 break;
4831
4832 case DCMD_TIMEOUT:
4833 switch (dcmd_timeout_ocr_possible(instance)) {
4834 case INITIATE_OCR:
4835 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4836 megasas_reset_fusion(instance->host,
4837 MFI_IO_TIMEOUT_OCR);
4838 break;
4839 case KILL_ADAPTER:
4840 megaraid_sas_kill_hba(instance);
4841 break;
4842 case IGNORE_TIMEOUT:
4843 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4844 __func__, __LINE__);
4845 break;
4846 }
4847 break;
4848 case DCMD_FAILED:
4849 dev_err(&instance->pdev->dev,
4850 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4851 __func__);
4852 break;
4853 }
4854
4855 if (ret != DCMD_TIMEOUT)
4856 megasas_return_cmd(instance, cmd);
4857
4858 return ret;
4859}
4860
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304861/*
4862 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4863 * instance : Controller's instance
4864*/
4865static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4866{
4867 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004868 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304869
4870 fusion = instance->ctrl_context;
4871 /* For MFI based controllers return dummy success */
4872 if (!fusion)
4873 return;
4874
4875 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004876 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304877 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004878 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304879 instance->supportmax256vd = 1;
4880
4881 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4882 * MEGASAS_MAX_DEV_PER_CHANNEL;
4883 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4884 * MEGASAS_MAX_DEV_PER_CHANNEL;
4885 if (instance->supportmax256vd) {
4886 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4887 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4888 } else {
4889 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4890 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4891 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304892
4893 dev_info(&instance->pdev->dev,
Shivasharan Scba67d92018-10-16 23:37:56 -07004894 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4895 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4896 instance->ctrl_info_buf->max_lds);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304897
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004898 if (instance->max_raid_mapsize) {
4899 ventura_map_sz = instance->max_raid_mapsize *
4900 MR_MIN_MAP_SIZE; /* 64k */
4901 fusion->current_map_sz = ventura_map_sz;
4902 fusion->max_map_sz = ventura_map_sz;
4903 } else {
4904 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4905 (sizeof(struct MR_LD_SPAN_MAP) *
4906 (instance->fw_supported_vd_count - 1));
4907 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304908
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004909 fusion->max_map_sz =
4910 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304911
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004912 if (instance->supportmax256vd)
4913 fusion->current_map_sz = fusion->new_map_sz;
4914 else
4915 fusion->current_map_sz = fusion->old_map_sz;
4916 }
4917 /* irrespective of FW raid maps, driver raid map is constant */
4918 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304919}
4920
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004921/*
4922 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4923 * dcmd.hdr.length - number of bytes to read
4924 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4925 * Desc: Fill in snapdump properties
4926 * Status: MFI_STAT_OK- Command successful
4927 */
4928void megasas_get_snapdump_properties(struct megasas_instance *instance)
4929{
4930 int ret = 0;
4931 struct megasas_cmd *cmd;
4932 struct megasas_dcmd_frame *dcmd;
4933 struct MR_SNAPDUMP_PROPERTIES *ci;
4934 dma_addr_t ci_h = 0;
4935
4936 ci = instance->snapdump_prop;
4937 ci_h = instance->snapdump_prop_h;
4938
4939 if (!ci)
4940 return;
4941
4942 cmd = megasas_get_cmd(instance);
4943
4944 if (!cmd) {
4945 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4946 return;
4947 }
4948
4949 dcmd = &cmd->frame->dcmd;
4950
4951 memset(ci, 0, sizeof(*ci));
4952 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4953
4954 dcmd->cmd = MFI_CMD_DCMD;
4955 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4956 dcmd->sge_count = 1;
4957 dcmd->flags = MFI_FRAME_DIR_READ;
4958 dcmd->timeout = 0;
4959 dcmd->pad_0 = 0;
4960 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
4961 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
4962
4963 megasas_set_dma_settings(instance, dcmd, ci_h,
4964 sizeof(struct MR_SNAPDUMP_PROPERTIES));
4965
4966 if (!instance->mask_interrupts) {
4967 ret = megasas_issue_blocked_cmd(instance, cmd,
4968 MFI_IO_TIMEOUT_SECS);
4969 } else {
4970 ret = megasas_issue_polled(instance, cmd);
4971 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4972 }
4973
4974 switch (ret) {
4975 case DCMD_SUCCESS:
4976 instance->snapdump_wait_time =
4977 min_t(u8, ci->trigger_min_num_sec_before_ocr,
4978 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
4979 break;
4980
4981 case DCMD_TIMEOUT:
4982 switch (dcmd_timeout_ocr_possible(instance)) {
4983 case INITIATE_OCR:
4984 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4985 megasas_reset_fusion(instance->host,
4986 MFI_IO_TIMEOUT_OCR);
4987 break;
4988 case KILL_ADAPTER:
4989 megaraid_sas_kill_hba(instance);
4990 break;
4991 case IGNORE_TIMEOUT:
4992 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4993 __func__, __LINE__);
4994 break;
4995 }
4996 }
4997
4998 if (ret != DCMD_TIMEOUT)
4999 megasas_return_cmd(instance, cmd);
5000}
5001
adam radford21c9e162013-09-06 15:27:14 -07005002/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005003 * megasas_get_controller_info - Returns FW's controller structure
5004 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005005 *
5006 * Issues an internal command (DCMD) to get the FW's controller structure.
5007 * This information is mainly used to find out the maximum IO transfer per
5008 * command supported by the FW.
5009 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305010int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305011megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005012{
5013 int ret = 0;
5014 struct megasas_cmd *cmd;
5015 struct megasas_dcmd_frame *dcmd;
5016 struct megasas_ctrl_info *ci;
5017 dma_addr_t ci_h = 0;
5018
Shivasharan S9b3d0282017-10-19 02:48:56 -07005019 ci = instance->ctrl_info_buf;
5020 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305021
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005022 cmd = megasas_get_cmd(instance);
5023
5024 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005025 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005026 return -ENOMEM;
5027 }
5028
5029 dcmd = &cmd->frame->dcmd;
5030
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005031 memset(ci, 0, sizeof(*ci));
5032 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5033
5034 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305035 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005036 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005037 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005038 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005039 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305040 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5041 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305042 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005043
Shivasharan S107a60d2017-10-19 02:49:05 -07005044 megasas_set_dma_settings(instance, dcmd, ci_h,
5045 sizeof(struct megasas_ctrl_info));
5046
Shivasharan Se7d36b82017-10-19 02:48:50 -07005047 if ((instance->adapter_type != MFI_SERIES) &&
Shivasharan S54b28042018-01-05 05:27:47 -08005048 !instance->mask_interrupts) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305049 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Shivasharan S54b28042018-01-05 05:27:47 -08005050 } else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305051 ret = megasas_issue_polled(instance, cmd);
Shivasharan S54b28042018-01-05 05:27:47 -08005052 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5053 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305054
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305055 switch (ret) {
5056 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305057 /* Save required controller information in
5058 * CPU endianness format.
5059 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005060 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005061 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
Shivasharan S9ad18a92017-10-19 02:48:57 -07005062 le32_to_cpus((u32 *)&ci->adapterOperations2);
5063 le32_to_cpus((u32 *)&ci->adapterOperations3);
5064 le16_to_cpus((u16 *)&ci->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305065
5066 /* Update the latest Ext VD info.
5067 * From Init path, store current firmware details.
5068 * From OCR path, detect any firmware properties changes.
5069 * in case of Firmware upgrade without system reboot.
5070 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305071 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305072 instance->use_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005073 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05005074 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005075 ci->adapter_operations4.support_pd_map_target_id;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005076 instance->support_nvme_passthru =
5077 ci->adapter_operations4.support_nvme_passthru;
Shivasharan Se9495e22018-06-04 03:45:12 -07005078 instance->task_abort_tmo = ci->TaskAbortTO;
5079 instance->max_reset_tmo = ci->MaxResetTO;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305080
5081 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07005082 instance->is_imr = (ci->memory_size ? 0 : 1);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005083
5084 instance->snapdump_wait_time =
5085 (ci->properties.on_off_properties2.enable_snap_dump ?
5086 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5087
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005088 instance->enable_fw_dev_list =
5089 ci->properties.on_off_properties2.enable_fw_dev_list;
5090
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305091 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305092 "controller type\t: %s(%dMB)\n",
5093 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07005094 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305095
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05305096 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005097 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305098 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07005099 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305100 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5101 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05305102 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5103 instance->secure_jbod_support ? "Yes" : "No");
Shivasharan Sf870bcb2018-01-05 05:33:04 -08005104 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5105 instance->support_nvme_passthru ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07005106 dev_info(&instance->pdev->dev,
5107 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5108 instance->task_abort_tmo, instance->max_reset_tmo);
5109
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305110 break;
5111
5112 case DCMD_TIMEOUT:
5113 switch (dcmd_timeout_ocr_possible(instance)) {
5114 case INITIATE_OCR:
5115 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5116 megasas_reset_fusion(instance->host,
5117 MFI_IO_TIMEOUT_OCR);
5118 break;
5119 case KILL_ADAPTER:
5120 megaraid_sas_kill_hba(instance);
5121 break;
5122 case IGNORE_TIMEOUT:
5123 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5124 __func__, __LINE__);
5125 break;
5126 }
Shivasharan S2747e6b2018-06-04 03:45:09 -07005127 break;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305128 case DCMD_FAILED:
5129 megaraid_sas_kill_hba(instance);
5130 break;
5131
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305132 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005133
Shivasharan S2747e6b2018-06-04 03:45:09 -07005134 if (ret != DCMD_TIMEOUT)
5135 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305136
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005137 return ret;
5138}
5139
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305140/*
5141 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5142 * to firmware
5143 *
5144 * @instance: Adapter soft state
5145 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5146 MR_CRASH_BUF_TURN_OFF = 0
5147 MR_CRASH_BUF_TURN_ON = 1
5148 * @return 0 on success non-zero on failure.
5149 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5150 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5151 * that driver supports crash dump feature. This DCMD will be sent only if
5152 * crash dump feature is supported by the FW.
5153 *
5154 */
5155int megasas_set_crash_dump_params(struct megasas_instance *instance,
5156 u8 crash_buf_state)
5157{
5158 int ret = 0;
5159 struct megasas_cmd *cmd;
5160 struct megasas_dcmd_frame *dcmd;
5161
5162 cmd = megasas_get_cmd(instance);
5163
5164 if (!cmd) {
5165 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5166 return -ENOMEM;
5167 }
5168
5169
5170 dcmd = &cmd->frame->dcmd;
5171
5172 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5173 dcmd->mbox.b[0] = crash_buf_state;
5174 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305175 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305176 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005177 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305178 dcmd->timeout = 0;
5179 dcmd->pad_0 = 0;
5180 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5181 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305182
Shivasharan S107a60d2017-10-19 02:49:05 -07005183 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5184 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305185
Shivasharan Se7d36b82017-10-19 02:48:50 -07005186 if ((instance->adapter_type != MFI_SERIES) &&
5187 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305188 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305189 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305190 ret = megasas_issue_polled(instance, cmd);
5191
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305192 if (ret == DCMD_TIMEOUT) {
5193 switch (dcmd_timeout_ocr_possible(instance)) {
5194 case INITIATE_OCR:
5195 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5196 megasas_reset_fusion(instance->host,
5197 MFI_IO_TIMEOUT_OCR);
5198 break;
5199 case KILL_ADAPTER:
5200 megaraid_sas_kill_hba(instance);
5201 break;
5202 case IGNORE_TIMEOUT:
5203 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5204 __func__, __LINE__);
5205 break;
5206 }
5207 } else
5208 megasas_return_cmd(instance, cmd);
5209
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305210 return ret;
5211}
5212
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005213/**
bo yang31ea7082007-11-07 12:09:50 -05005214 * megasas_issue_init_mfi - Initializes the FW
5215 * @instance: Adapter soft state
5216 *
5217 * Issues the INIT MFI cmd
5218 */
5219static int
5220megasas_issue_init_mfi(struct megasas_instance *instance)
5221{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05305222 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05005223 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05005224 struct megasas_init_frame *init_frame;
5225 struct megasas_init_queue_info *initq_info;
5226 dma_addr_t init_frame_h;
5227 dma_addr_t initq_info_h;
5228
5229 /*
5230 * Prepare a init frame. Note the init frame points to queue info
5231 * structure. Each frame has SGL allocated after first 64 bytes. For
5232 * this frame - since we don't need any SGL - we use SGL's space as
5233 * queue info structure
5234 *
5235 * We will not get a NULL command below. We just created the pool.
5236 */
5237 cmd = megasas_get_cmd(instance);
5238
5239 init_frame = (struct megasas_init_frame *)cmd->frame;
5240 initq_info = (struct megasas_init_queue_info *)
5241 ((unsigned long)init_frame + 64);
5242
5243 init_frame_h = cmd->frame_phys_addr;
5244 initq_info_h = init_frame_h + 64;
5245
5246 context = init_frame->context;
5247 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5248 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5249 init_frame->context = context;
5250
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305251 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5252 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05005253
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305254 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5255 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05005256
5257 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305258 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305259 init_frame->queue_info_new_phys_addr_lo =
5260 cpu_to_le32(lower_32_bits(initq_info_h));
5261 init_frame->queue_info_new_phys_addr_hi =
5262 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05005263
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305264 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05005265
5266 /*
5267 * disable the intr before firing the init frame to FW
5268 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305269 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05005270
5271 /*
5272 * Issue the init frame in polled mode
5273 */
5274
5275 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005276 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05005277 megasas_return_cmd(instance, cmd);
5278 goto fail_fw_init;
5279 }
5280
5281 megasas_return_cmd(instance, cmd);
5282
5283 return 0;
5284
5285fail_fw_init:
5286 return -EINVAL;
5287}
5288
adam radfordcd50ba82010-12-21 10:23:23 -08005289static u32
5290megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005291{
5292 u32 context_sz;
5293 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005294
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005295 /*
5296 * Get various operational parameters from status register
5297 */
Shivasharan Sde516372018-12-17 00:47:39 -08005298 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07005299 /*
5300 * Reduce the max supported cmds by 1. This is to ensure that the
5301 * reply_q_sz (1 more than the max cmd that driver may send)
5302 * does not exceed max cmds that the FW can support
5303 */
5304 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08005305 instance->max_mfi_cmds = instance->max_fw_cmds;
Shivasharan Sde516372018-12-17 00:47:39 -08005306 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08005307 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005308 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305309 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5310 * are reserved for IOCTL + driver's internal DCMDs.
5311 */
5312 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5313 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5314 instance->max_scsi_cmds = (instance->max_fw_cmds -
5315 MEGASAS_SKINNY_INT_CMDS);
5316 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5317 } else {
5318 instance->max_scsi_cmds = (instance->max_fw_cmds -
5319 MEGASAS_INT_CMDS);
5320 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5321 }
5322
Sumit Saxena308ec452016-01-28 21:04:30 +05305323 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305324 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005325 * Create a pool of commands
5326 */
5327 if (megasas_alloc_cmds(instance))
5328 goto fail_alloc_cmds;
5329
5330 /*
5331 * Allocate memory for reply queue. Length of reply queue should
5332 * be _one_ more than the maximum commands handled by the firmware.
5333 *
5334 * Note: When FW completes commands, it places corresponding contex
5335 * values in this circular reply queue. This circular queue is a fairly
5336 * typical producer-consumer queue. FW is the producer (of completed
5337 * commands) and the driver is the consumer.
5338 */
5339 context_sz = sizeof(u32);
5340 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5341
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005342 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5343 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005344
5345 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005346 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005347 goto fail_reply_queue;
5348 }
5349
bo yang31ea7082007-11-07 12:09:50 -05005350 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005351 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005352
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305353 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305354 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5355 "Fail from %s %d\n", instance->unique_id,
5356 __func__, __LINE__);
5357 goto fail_fw_init;
5358 }
5359
bo yang39a98552010-09-22 22:36:29 -04005360 instance->fw_support_ieee = 0;
5361 instance->fw_support_ieee =
Shivasharan Sde516372018-12-17 00:47:39 -08005362 (instance->instancet->read_fw_status_reg(instance) &
bo yang39a98552010-09-22 22:36:29 -04005363 0x04000000);
5364
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005365 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04005366 instance->fw_support_ieee);
5367
5368 if (instance->fw_support_ieee)
5369 instance->flag_ieee = 1;
5370
adam radfordcd50ba82010-12-21 10:23:23 -08005371 return 0;
5372
5373fail_fw_init:
5374
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005375 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
adam radfordcd50ba82010-12-21 10:23:23 -08005376 instance->reply_queue, instance->reply_queue_h);
5377fail_reply_queue:
5378 megasas_free_cmds(instance);
5379
5380fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08005381 return 1;
5382}
5383
Shivasharan S62a04f82019-05-07 10:05:35 -07005384static
5385void megasas_setup_irq_poll(struct megasas_instance *instance)
5386{
5387 struct megasas_irq_context *irq_ctx;
5388 u32 count, i;
5389
5390 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5391
5392 /* Initialize IRQ poll */
5393 for (i = 0; i < count; i++) {
5394 irq_ctx = &instance->irq_context[i];
5395 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5396 irq_ctx->irq_poll_scheduled = false;
5397 irq_poll_init(&irq_ctx->irqpoll,
5398 instance->threshold_reply_count,
5399 megasas_irqpoll);
5400 }
5401}
5402
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305403/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01005404 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305405 * @instance: Adapter soft state
5406 *
5407 * Do not enable interrupt, only setup ISRs.
5408 *
5409 * Return 0 on success.
5410 */
5411static int
5412megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5413{
5414 struct pci_dev *pdev;
5415
5416 pdev = instance->pdev;
5417 instance->irq_context[0].instance = instance;
5418 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005419 if (request_irq(pci_irq_vector(pdev, 0),
5420 instance->instancet->service_isr, IRQF_SHARED,
5421 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305422 dev_err(&instance->pdev->dev,
5423 "Failed to register IRQ from %s %d\n",
5424 __func__, __LINE__);
5425 return -1;
5426 }
5427 return 0;
5428}
5429
5430/**
5431 * megasas_setup_irqs_msix - register MSI-x interrupts.
5432 * @instance: Adapter soft state
5433 * @is_probe: Driver probe check
5434 *
5435 * Do not enable interrupt, only setup ISRs.
5436 *
5437 * Return 0 on success.
5438 */
5439static int
5440megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5441{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005442 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305443 struct pci_dev *pdev;
5444
5445 pdev = instance->pdev;
5446
5447 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305448 for (i = 0; i < instance->msix_vectors; i++) {
5449 instance->irq_context[i].instance = instance;
5450 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005451 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305452 instance->instancet->service_isr, 0, "megasas",
5453 &instance->irq_context[i])) {
5454 dev_err(&instance->pdev->dev,
5455 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005456 for (j = 0; j < i; j++)
5457 free_irq(pci_irq_vector(pdev, j),
5458 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305459 /* Retry irq register for IO_APIC*/
5460 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005461 instance->msix_load_balance = false;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005462 if (is_probe) {
5463 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305464 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005465 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305466 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005467 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305468 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305469 }
Shivasharan S1d15d902019-05-07 10:05:36 -07005470
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305471 return 0;
5472}
5473
5474/*
5475 * megasas_destroy_irqs- unregister interrupts.
5476 * @instance: Adapter soft state
5477 * return: void
5478 */
5479static void
5480megasas_destroy_irqs(struct megasas_instance *instance) {
5481
5482 int i;
Shivasharan S62a04f82019-05-07 10:05:35 -07005483 int count;
5484 struct megasas_irq_context *irq_ctx;
5485
5486 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5487 if (instance->adapter_type != MFI_SERIES) {
5488 for (i = 0; i < count; i++) {
5489 irq_ctx = &instance->irq_context[i];
5490 irq_poll_disable(&irq_ctx->irqpoll);
5491 }
5492 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305493
5494 if (instance->msix_vectors)
5495 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005496 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305497 &instance->irq_context[i]);
5498 }
5499 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005500 free_irq(pci_irq_vector(instance->pdev, 0),
5501 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305502}
5503
adam radfordcd50ba82010-12-21 10:23:23 -08005504/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305505 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5506 * @instance: Adapter soft state
5507 * @is_probe: Driver probe check
5508 *
5509 * Return 0 on success.
5510 */
5511void
5512megasas_setup_jbod_map(struct megasas_instance *instance)
5513{
5514 int i;
5515 struct fusion_context *fusion = instance->ctrl_context;
5516 u32 pd_seq_map_sz;
5517
5518 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5519 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5520
5521 if (reset_devices || !fusion ||
Shivasharan S9ad18a92017-10-19 02:48:57 -07005522 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305523 dev_info(&instance->pdev->dev,
5524 "Jbod map is not supported %s %d\n",
5525 __func__, __LINE__);
5526 instance->use_seqnum_jbod_fp = false;
5527 return;
5528 }
5529
5530 if (fusion->pd_seq_sync[0])
5531 goto skip_alloc;
5532
5533 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5534 fusion->pd_seq_sync[i] = dma_alloc_coherent
5535 (&instance->pdev->dev, pd_seq_map_sz,
5536 &fusion->pd_seq_phys[i], GFP_KERNEL);
5537 if (!fusion->pd_seq_sync[i]) {
5538 dev_err(&instance->pdev->dev,
5539 "Failed to allocate memory from %s %d\n",
5540 __func__, __LINE__);
5541 if (i == 1) {
5542 dma_free_coherent(&instance->pdev->dev,
5543 pd_seq_map_sz, fusion->pd_seq_sync[0],
5544 fusion->pd_seq_phys[0]);
5545 fusion->pd_seq_sync[0] = NULL;
5546 }
5547 instance->use_seqnum_jbod_fp = false;
5548 return;
5549 }
5550 }
5551
5552skip_alloc:
5553 if (!megasas_sync_pd_seq_num(instance, false) &&
5554 !megasas_sync_pd_seq_num(instance, true))
5555 instance->use_seqnum_jbod_fp = true;
5556 else
5557 instance->use_seqnum_jbod_fp = false;
5558}
5559
Ming Leiadbe5522018-03-13 17:42:40 +08005560static void megasas_setup_reply_map(struct megasas_instance *instance)
5561{
5562 const struct cpumask *mask;
5563 unsigned int queue, cpu;
5564
5565 for (queue = 0; queue < instance->msix_vectors; queue++) {
5566 mask = pci_irq_get_affinity(instance->pdev, queue);
5567 if (!mask)
5568 goto fallback;
5569
5570 for_each_cpu(cpu, mask)
5571 instance->reply_map[cpu] = queue;
5572 }
5573 return;
5574
5575fallback:
5576 for_each_possible_cpu(cpu)
5577 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5578}
5579
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305580/**
Shivasharan Sdaa06812019-01-29 01:38:12 -08005581 * megasas_get_device_list - Get the PD and LD device list from FW.
5582 * @instance: Adapter soft state
5583 * @return: Success or failure
5584 *
5585 * Issue DCMDs to Firmware to get the PD and LD list.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005586 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5587 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan Sdaa06812019-01-29 01:38:12 -08005588 */
5589static
5590int megasas_get_device_list(struct megasas_instance *instance)
5591{
5592 memset(instance->pd_list, 0,
5593 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5594 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5595
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005596 if (instance->enable_fw_dev_list) {
5597 if (megasas_host_device_list_query(instance, true))
5598 return FAILED;
5599 } else {
5600 if (megasas_get_pd_list(instance) < 0) {
5601 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5602 return FAILED;
5603 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005604
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005605 if (megasas_ld_list_query(instance,
5606 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5607 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5608 return FAILED;
5609 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005610 }
5611
5612 return SUCCESS;
5613}
5614/**
adam radfordcd50ba82010-12-21 10:23:23 -08005615 * megasas_init_fw - Initializes the FW
5616 * @instance: Adapter soft state
5617 *
5618 * This is the main function for initializing firmware
5619 */
5620
5621static int megasas_init_fw(struct megasas_instance *instance)
5622{
5623 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005624 u32 max_sectors_2, tmp_sectors, msix_enable;
Shivasharan S81b76452018-10-16 23:37:51 -07005625 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005626 resource_size_t base_addr;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305627 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005628 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005629 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005630 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305631 struct fusion_context *fusion;
5632
5633 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005634
5635 /* Find first memory bar */
5636 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005637 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005638 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005639 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005640 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005641 return -EBUSY;
5642 }
5643
Ben Collins11f8a7b2013-09-13 12:46:44 -04005644 base_addr = pci_resource_start(instance->pdev, instance->bar);
5645 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005646
5647 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005648 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005649 goto fail_ioremap;
5650 }
5651
Shivasharan Se7d36b82017-10-19 02:48:50 -07005652 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005653 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005654 else {
5655 switch (instance->pdev->device) {
5656 case PCI_DEVICE_ID_LSI_SAS1078R:
5657 case PCI_DEVICE_ID_LSI_SAS1078DE:
5658 instance->instancet = &megasas_instance_template_ppc;
5659 break;
5660 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5661 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5662 instance->instancet = &megasas_instance_template_gen2;
5663 break;
5664 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5665 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5666 instance->instancet = &megasas_instance_template_skinny;
5667 break;
5668 case PCI_DEVICE_ID_LSI_SAS1064R:
5669 case PCI_DEVICE_ID_DELL_PERC5:
5670 default:
5671 instance->instancet = &megasas_instance_template_xscale;
5672 instance->pd_list_not_supported = 1;
5673 break;
5674 }
adam radfordcd50ba82010-12-21 10:23:23 -08005675 }
5676
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305677 if (megasas_transition_to_ready(instance, 0)) {
Shivasharan S78409d42019-05-07 10:05:34 -07005678 dev_info(&instance->pdev->dev,
5679 "Failed to transition controller to ready from %s!\n",
5680 __func__);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005681 if (instance->adapter_type != MFI_SERIES) {
Shivasharan Sde93b402018-10-16 23:37:42 -07005682 status_reg = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08005683 instance);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005684 if (status_reg & MFI_RESET_ADAPTER) {
Shivasharan S78409d42019-05-07 10:05:34 -07005685 if (megasas_adp_reset_wait_for_ready
5686 (instance, true, 0) == FAILED)
Shivasharan Sf10fb852019-05-07 10:05:32 -07005687 goto fail_ready_state;
5688 } else {
5689 goto fail_ready_state;
5690 }
5691 } else {
Shivasharan Sde93b402018-10-16 23:37:42 -07005692 atomic_set(&instance->fw_reset_no_pci_access, 1);
5693 instance->instancet->adp_reset
5694 (instance, instance->reg_set);
5695 atomic_set(&instance->fw_reset_no_pci_access, 0);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305696
Shivasharan Sde93b402018-10-16 23:37:42 -07005697 /*waiting for about 30 second before retry*/
5698 ssleep(30);
5699
5700 if (megasas_transition_to_ready(instance, 0))
5701 goto fail_ready_state;
Shivasharan Sde93b402018-10-16 23:37:42 -07005702 }
Shivasharan S78409d42019-05-07 10:05:34 -07005703
5704 dev_info(&instance->pdev->dev,
5705 "FW restarted successfully from %s!\n",
5706 __func__);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305707 }
adam radfordcd50ba82010-12-21 10:23:23 -08005708
Shivasharan Se5d65b42017-10-19 02:48:59 -07005709 megasas_init_ctrl_params(instance);
5710
Shivasharan S107a60d2017-10-19 02:49:05 -07005711 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005712 goto fail_ready_state;
5713
5714 if (megasas_alloc_ctrl_mem(instance))
5715 goto fail_alloc_dma_buf;
5716
5717 if (megasas_alloc_ctrl_dma_buffers(instance))
5718 goto fail_alloc_dma_buf;
5719
5720 fusion = instance->ctrl_context;
5721
Shivasharan S630d42b2018-12-17 00:47:37 -08005722 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005723 scratch_pad_2 =
Shivasharan S272652f2018-12-17 00:47:40 -08005724 megasas_readl(instance,
5725 &instance->reg_set->outbound_scratch_pad_2);
Shivasharan S81b76452018-10-16 23:37:51 -07005726 instance->max_raid_mapsize = ((scratch_pad_2 >>
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005727 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5728 MR_MAX_RAID_MAP_SIZE_MASK);
5729 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305730
adam radford3f1abce2011-05-11 18:33:47 -07005731 /* Check if MSI-X is supported while in ready state */
Shivasharan Sde516372018-12-17 00:47:39 -08005732 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
adam radford3f1abce2011-05-11 18:33:47 -07005733 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005734 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005735 int irq_flags = PCI_IRQ_MSIX;
5736
Shivasharan S272652f2018-12-17 00:47:40 -08005737 scratch_pad_1 = megasas_readl
5738 (instance, &instance->reg_set->outbound_scratch_pad_1);
adam radfordc8e858f2011-10-08 18:15:13 -07005739 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305740 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005741 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5742 /* Thunderbolt Series*/
Shivasharan S81b76452018-10-16 23:37:51 -07005743 instance->msix_vectors = (scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305744 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5745 fw_msix_count = instance->msix_vectors;
Shivasharan Se29c3222018-10-16 23:37:46 -07005746 } else {
Shivasharan S81b76452018-10-16 23:37:51 -07005747 instance->msix_vectors = ((scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305748 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5749 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005750
5751 /*
5752 * For Invader series, > 8 MSI-x vectors
5753 * supported by FW/HW implies combined
5754 * reply queue mode is enabled.
5755 * For Ventura series, > 16 MSI-x vectors
5756 * supported by FW/HW implies combined
5757 * reply queue mode is enabled.
5758 */
5759 switch (instance->adapter_type) {
5760 case INVADER_SERIES:
5761 if (instance->msix_vectors > 8)
5762 instance->msix_combined = true;
5763 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08005764 case AERO_SERIES:
Shivasharan Se29c3222018-10-16 23:37:46 -07005765 case VENTURA_SERIES:
5766 if (instance->msix_vectors > 16)
5767 instance->msix_combined = true;
5768 break;
5769 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005770
Sumit Saxena179ac142016-01-28 21:04:28 +05305771 if (rdpq_enable)
Shivasharan S81b76452018-10-16 23:37:51 -07005772 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
Sumit Saxena179ac142016-01-28 21:04:28 +05305773 1 : 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005774
5775 if (!instance->msix_combined) {
5776 instance->msix_load_balance = true;
5777 instance->smp_affinity_enable = false;
5778 }
5779
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305780 fw_msix_count = instance->msix_vectors;
5781 /* Save 1-15 reply post index address to local memory
5782 * Index 0 is already saved from reg offset
5783 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5784 */
5785 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5786 instance->reply_post_host_index_addr[loop] =
5787 (u32 __iomem *)
5788 ((u8 __iomem *)instance->reg_set +
5789 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5790 + (loop * 0x10));
5791 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305792 }
5793 if (msix_vectors)
5794 instance->msix_vectors = min(msix_vectors,
5795 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305796 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005797 instance->msix_vectors = 1;
Shivasharan S1d15d902019-05-07 10:05:36 -07005798
adam radfordc8e858f2011-10-08 18:15:13 -07005799 /* Don't bother allocating more MSI-X vectors than cpus */
5800 instance->msix_vectors = min(instance->msix_vectors,
5801 (unsigned int)num_online_cpus());
Shivasharan S1d15d902019-05-07 10:05:36 -07005802 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005803 irq_flags |= PCI_IRQ_AFFINITY;
5804 i = pci_alloc_irq_vectors(instance->pdev, 1,
5805 instance->msix_vectors, irq_flags);
Shivasharan S1d15d902019-05-07 10:05:36 -07005806 if (i > 0) {
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005807 instance->msix_vectors = i;
Shivasharan S1d15d902019-05-07 10:05:36 -07005808 } else {
adam radfordc8e858f2011-10-08 18:15:13 -07005809 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005810 instance->msix_load_balance = false;
5811 }
adam radfordc8e858f2011-10-08 18:15:13 -07005812 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005813 /*
5814 * MSI-X host index 0 is common for all adapter.
5815 * It is used for all MPT based Adapters.
5816 */
5817 if (instance->msix_combined) {
5818 instance->reply_post_host_index_addr[0] =
5819 (u32 *)((u8 *)instance->reg_set +
5820 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5821 } else {
5822 instance->reply_post_host_index_addr[0] =
5823 (u32 *)((u8 *)instance->reg_set +
5824 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5825 }
5826
Shivasharan S64ff64b2017-03-10 03:22:12 -08005827 if (!instance->msix_vectors) {
5828 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5829 if (i < 0)
Shivasharan S8a25fa12018-10-16 23:37:44 -07005830 goto fail_init_adapter;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005831 }
adam radford3f1abce2011-05-11 18:33:47 -07005832
Ming Leiadbe5522018-03-13 17:42:40 +08005833 megasas_setup_reply_map(instance);
5834
Tomas Henzl258c3af2015-06-02 16:09:46 +05305835 dev_info(&instance->pdev->dev,
5836 "firmware supports msix\t: (%d)", fw_msix_count);
5837 dev_info(&instance->pdev->dev,
5838 "current msix/online cpus\t: (%d/%d)\n",
5839 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305840 dev_info(&instance->pdev->dev,
5841 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305842
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305843 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5844 (unsigned long)instance);
5845
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305846 /*
5847 * Below are default value for legacy Firmware.
5848 * non-fusion based controllers
5849 */
5850 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5851 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005852 /* Get operational params, sge flags, send init cmd to controller */
5853 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005854 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005855
Shivasharan S630d42b2018-12-17 00:47:37 -08005856 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005857 scratch_pad_3 =
Shivasharan S272652f2018-12-17 00:47:40 -08005858 megasas_readl(instance,
5859 &instance->reg_set->outbound_scratch_pad_3);
Shivasharan S81b76452018-10-16 23:37:51 -07005860 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
Shivasharan S15dd0382017-02-10 00:59:10 -08005861 MR_DEFAULT_NVME_PAGE_SHIFT)
5862 instance->nvme_page_size =
Shivasharan S81b76452018-10-16 23:37:51 -07005863 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
Shivasharan S15dd0382017-02-10 00:59:10 -08005864
5865 dev_info(&instance->pdev->dev,
5866 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5867 }
5868
Tomas Henzl18103ef2016-11-01 17:32:02 +01005869 if (instance->msix_vectors ?
5870 megasas_setup_irqs_msix(instance, 1) :
5871 megasas_setup_irqs_ioapic(instance))
5872 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305873
Shivasharan S62a04f82019-05-07 10:05:35 -07005874 if (instance->adapter_type != MFI_SERIES)
5875 megasas_setup_irq_poll(instance);
5876
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305877 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005878
Andy Lutomirski13f30772016-05-03 10:24:31 -07005879 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005880
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305881 megasas_setup_jbod_map(instance);
5882
Shivasharan Sdaa06812019-01-29 01:38:12 -08005883 if (megasas_get_device_list(instance) != SUCCESS) {
5884 dev_err(&instance->pdev->dev,
5885 "%s: megasas_get_device_list failed\n",
5886 __func__);
Shivasharan S72bff2d2017-02-10 00:59:33 -08005887 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005888 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005889
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005890 /* stream detection initialization */
Shivasharan S630d42b2018-12-17 00:47:37 -08005891 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005892 fusion->stream_detect_by_ld =
Kees Cook6396bb22018-06-12 14:03:40 -07005893 kcalloc(MAX_LOGICAL_DRIVES_EXT,
5894 sizeof(struct LD_STREAM_DETECT *),
5895 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005896 if (!fusion->stream_detect_by_ld) {
5897 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005898 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005899 goto fail_get_ld_pd_list;
5900 }
5901 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5902 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08005903 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005904 GFP_KERNEL);
5905 if (!fusion->stream_detect_by_ld[i]) {
5906 dev_err(&instance->pdev->dev,
5907 "unable to allocate stream detect by LD\n ");
5908 for (j = 0; j < i; ++j)
5909 kfree(fusion->stream_detect_by_ld[j]);
5910 kfree(fusion->stream_detect_by_ld);
5911 fusion->stream_detect_by_ld = NULL;
5912 goto fail_get_ld_pd_list;
5913 }
5914 fusion->stream_detect_by_ld[i]->mru_bit_map
5915 = MR_STREAM_BITMAP;
5916 }
5917 }
5918
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005919 /*
5920 * Compute the max allowed sectors per IO: The controller info has two
5921 * limits on max sectors. Driver should use the minimum of these two.
5922 *
5923 * 1 << stripe_sz_ops.min = max sectors per strip
5924 *
5925 * Note that older firmwares ( < FW ver 30) didn't report information
5926 * to calculate max_sectors_1. So the number ended up as zero always.
5927 */
bo yang14faea92007-11-09 04:14:00 -05005928 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07005929 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005930
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305931 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5932 le16_to_cpu(ctrl_info->max_strips_per_io);
5933 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005934
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005935 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305936
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305937 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5938 instance->passive = ctrl_info->cluster.passive;
5939 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305940 instance->UnevenSpanSupport =
5941 ctrl_info->adapterOperations2.supportUnevenSpans;
5942 if (instance->UnevenSpanSupport) {
5943 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08005944 if (MR_ValidateMapInfo(instance, instance->map_id))
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305945 fusion->fast_path_io = 1;
5946 else
5947 fusion->fast_path_io = 0;
5948
5949 }
5950 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305951 instance->requestorId = ctrl_info->iov.requestorId;
5952 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5953 if (!ctrl_info->adapterOperations2.activePassive)
5954 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305955
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305956 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5957 instance->PlasmaFW111 ? "1.11" : "new");
5958
5959 if (instance->PlasmaFW111) {
5960 iovPtr = (struct IOV_111 *)
5961 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5962 instance->requestorId = iovPtr->requestorId;
5963 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305964 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305965 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5966 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305967 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305968
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305969 instance->crash_dump_fw_support =
5970 ctrl_info->adapterOperations3.supportCrashDump;
5971 instance->crash_dump_drv_support =
5972 (instance->crash_dump_fw_support &&
5973 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305974 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305975 megasas_set_crash_dump_params(instance,
5976 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005977
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305978 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305979 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005980 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305981 CRASH_DMA_BUF_SIZE,
5982 instance->crash_dump_buf,
5983 instance->crash_dump_h);
5984 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005985 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305986
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005987 if (instance->snapdump_wait_time) {
5988 megasas_get_snapdump_properties(instance);
5989 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
5990 instance->snapdump_wait_time);
5991 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305992
5993 dev_info(&instance->pdev->dev,
5994 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5995 le16_to_cpu(ctrl_info->pci.vendor_id),
5996 le16_to_cpu(ctrl_info->pci.device_id),
5997 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5998 le16_to_cpu(ctrl_info->pci.sub_device_id));
5999 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6000 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306001 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6002 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306003 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
6004 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05306005
bo yang14faea92007-11-09 04:14:00 -05006006 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05306007 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05006008 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6009 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006010
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306011 /* Check for valid throttlequeuedepth module parameter */
6012 if (throttlequeuedepth &&
6013 throttlequeuedepth <= instance->max_scsi_cmds)
6014 instance->throttlequeuedepth = throttlequeuedepth;
6015 else
6016 instance->throttlequeuedepth =
6017 MEGASAS_THROTTLE_QUEUE_DEPTH;
6018
Shivasharan Se636a7a2017-08-23 04:46:56 -07006019 if ((resetwaittime < 1) ||
6020 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05306021 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6022
6023 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6024 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05006025
adam radford229fe472014-03-10 02:51:56 -07006026 /* Launch SR-IOV heartbeat timer */
6027 if (instance->requestorId) {
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006028 if (!megasas_sriov_start_heartbeat(instance, 1)) {
Kees Cookc251a7b2017-10-22 15:30:04 -07006029 megasas_start_timer(instance);
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006030 } else {
adam radford229fe472014-03-10 02:51:56 -07006031 instance->skip_heartbeat_timer_del = 1;
Shivasharan S2e47e4e2018-10-16 23:37:48 -07006032 goto fail_get_ld_pd_list;
6033 }
adam radford229fe472014-03-10 02:51:56 -07006034 }
6035
Shivasharan S3f6194a2018-10-16 23:37:39 -07006036 /*
6037 * Create and start watchdog thread which will monitor
6038 * controller state every 1 sec and trigger OCR when
6039 * it enters fault state
6040 */
6041 if (instance->adapter_type != MFI_SERIES)
6042 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6043 goto fail_start_watchdog;
6044
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006045 return 0;
6046
Shivasharan S3f6194a2018-10-16 23:37:39 -07006047fail_start_watchdog:
6048 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6049 del_timer_sync(&instance->sriov_heartbeat_timer);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006050fail_get_ld_pd_list:
6051 instance->instancet->disable_intr(instance);
Hannes Reineckefad119b2016-12-02 12:52:23 +01006052 megasas_destroy_irqs(instance);
Shivasharan S8a25fa12018-10-16 23:37:44 -07006053fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306054 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006055 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306056 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07006057fail_alloc_dma_buf:
6058 megasas_free_ctrl_dma_buffers(instance);
6059 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08006060fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006061 iounmap(instance->reg_set);
6062
Shivasharan S72bff2d2017-02-10 00:59:33 -08006063fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07006064 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006065
Shivasharan S72bff2d2017-02-10 00:59:33 -08006066 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6067 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006068 return -EINVAL;
6069}
6070
6071/**
6072 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01006073 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006074 */
6075static void megasas_release_mfi(struct megasas_instance *instance)
6076{
adam radford9c915a82010-12-21 13:34:31 -08006077 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006078
adam radford9c915a82010-12-21 13:34:31 -08006079 if (instance->reply_queue)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006080 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006081 instance->reply_queue, instance->reply_queue_h);
6082
6083 megasas_free_cmds(instance);
6084
6085 iounmap(instance->reg_set);
6086
Yinghai Lue7f85162016-08-05 23:37:34 -07006087 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006088}
6089
6090/**
6091 * megasas_get_seq_num - Gets latest event sequence numbers
6092 * @instance: Adapter soft state
6093 * @eli: FW event log sequence numbers information
6094 *
6095 * FW maintains a log of all events in a non-volatile area. Upper layers would
6096 * usually find out the latest sequence number of the events, the seq number at
6097 * the boot etc. They would "read" all the events below the latest seq number
6098 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6099 * number), they would subsribe to AEN (asynchronous event notification) and
6100 * wait for the events to happen.
6101 */
6102static int
6103megasas_get_seq_num(struct megasas_instance *instance,
6104 struct megasas_evt_log_info *eli)
6105{
6106 struct megasas_cmd *cmd;
6107 struct megasas_dcmd_frame *dcmd;
6108 struct megasas_evt_log_info *el_info;
6109 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08006110 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006111
6112 cmd = megasas_get_cmd(instance);
6113
6114 if (!cmd) {
6115 return -ENOMEM;
6116 }
6117
6118 dcmd = &cmd->frame->dcmd;
Luis Chamberlain750afb02019-01-04 09:23:09 +01006119 el_info = dma_alloc_coherent(&instance->pdev->dev,
6120 sizeof(struct megasas_evt_log_info),
6121 &el_info_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006122 if (!el_info) {
6123 megasas_return_cmd(instance, cmd);
6124 return -ENOMEM;
6125 }
6126
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006127 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6128
6129 dcmd->cmd = MFI_CMD_DCMD;
6130 dcmd->cmd_status = 0x0;
6131 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006132 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006133 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006134 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306135 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6136 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07006137
6138 megasas_set_dma_settings(instance, dcmd, el_info_h,
6139 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006140
Shivasharan Sb051cc62018-01-05 05:27:38 -08006141 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6142 if (ret != DCMD_SUCCESS) {
6143 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6144 __func__, __LINE__);
6145 goto dcmd_failed;
6146 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006147
Shivasharan Sb051cc62018-01-05 05:27:38 -08006148 /*
6149 * Copy the data back into callers buffer
6150 */
6151 eli->newest_seq_num = el_info->newest_seq_num;
6152 eli->oldest_seq_num = el_info->oldest_seq_num;
6153 eli->clear_seq_num = el_info->clear_seq_num;
6154 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6155 eli->boot_seq_num = el_info->boot_seq_num;
6156
6157dcmd_failed:
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006158 dma_free_coherent(&instance->pdev->dev,
6159 sizeof(struct megasas_evt_log_info),
6160 el_info, el_info_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006161
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306162 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006163
Shivasharan Sb051cc62018-01-05 05:27:38 -08006164 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006165}
6166
6167/**
6168 * megasas_register_aen - Registers for asynchronous event notification
6169 * @instance: Adapter soft state
6170 * @seq_num: The starting sequence number
6171 * @class_locale: Class of the event
6172 *
6173 * This function subscribes for AEN for events beyond the @seq_num. It requests
6174 * to be notified if and only if the event is of type @class_locale
6175 */
6176static int
6177megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6178 u32 class_locale_word)
6179{
6180 int ret_val;
6181 struct megasas_cmd *cmd;
6182 struct megasas_dcmd_frame *dcmd;
6183 union megasas_evt_class_locale curr_aen;
6184 union megasas_evt_class_locale prev_aen;
6185
6186 /*
6187 * If there an AEN pending already (aen_cmd), check if the
6188 * class_locale of that pending AEN is inclusive of the new
6189 * AEN request we currently have. If it is, then we don't have
6190 * to do anything. In other words, whichever events the current
6191 * AEN request is subscribing to, have already been subscribed
6192 * to.
6193 *
6194 * If the old_cmd is _not_ inclusive, then we have to abort
6195 * that command, form a class_locale that is superset of both
6196 * old and current and re-issue to the FW
6197 */
6198
6199 curr_aen.word = class_locale_word;
6200
6201 if (instance->aen_cmd) {
6202
Christoph Hellwiga9555532015-04-23 16:34:24 +05306203 prev_aen.word =
6204 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006205
Shivasharan S91b3d9f2017-08-23 04:47:01 -07006206 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6207 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6208 dev_info(&instance->pdev->dev,
6209 "%s %d out of range class %d send by application\n",
6210 __func__, __LINE__, curr_aen.members.class);
6211 return 0;
6212 }
6213
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006214 /*
6215 * A class whose enum value is smaller is inclusive of all
6216 * higher values. If a PROGRESS (= -1) was previously
6217 * registered, then a new registration requests for higher
6218 * classes need not be sent to FW. They are automatically
6219 * included.
6220 *
6221 * Locale numbers don't have such hierarchy. They are bitmap
6222 * values
6223 */
6224 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306225 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006226 curr_aen.members.locale)) {
6227 /*
6228 * Previously issued event registration includes
6229 * current request. Nothing to do.
6230 */
6231 return 0;
6232 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306233 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006234
6235 if (prev_aen.members.class < curr_aen.members.class)
6236 curr_aen.members.class = prev_aen.members.class;
6237
6238 instance->aen_cmd->abort_aen = 1;
6239 ret_val = megasas_issue_blocked_abort_cmd(instance,
6240 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306241 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006242
6243 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006244 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006245 "previous AEN command\n");
6246 return ret_val;
6247 }
6248 }
6249 }
6250
6251 cmd = megasas_get_cmd(instance);
6252
6253 if (!cmd)
6254 return -ENOMEM;
6255
6256 dcmd = &cmd->frame->dcmd;
6257
6258 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6259
6260 /*
6261 * Prepare DCMD for aen registration
6262 */
6263 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6264
6265 dcmd->cmd = MFI_CMD_DCMD;
6266 dcmd->cmd_status = 0x0;
6267 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006268 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006269 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006270 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306271 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6272 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6273 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04006274 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306275 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07006276
6277 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6278 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006279
Yang, Bof4c9a132009-10-06 14:43:28 -06006280 if (instance->aen_cmd != NULL) {
6281 megasas_return_cmd(instance, cmd);
6282 return 0;
6283 }
6284
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006285 /*
6286 * Store reference to the cmd used to register for AEN. When an
6287 * application wants us to register for AEN, we have to abort this
6288 * cmd and re-register with a new EVENT LOCALE supplied by that app
6289 */
6290 instance->aen_cmd = cmd;
6291
6292 /*
6293 * Issue the aen registration frame
6294 */
adam radford9c915a82010-12-21 13:34:31 -08006295 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006296
6297 return 0;
6298}
6299
Shivasharan S96188a82017-02-10 00:59:11 -08006300/* megasas_get_target_prop - Send DCMD with below details to firmware.
6301 *
6302 * This DCMD will fetch few properties of LD/system PD defined
6303 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6304 *
6305 * DCMD send by drivers whenever new target is added to the OS.
6306 *
6307 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6308 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6309 * 0 = system PD, 1 = LD.
6310 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6311 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6312 *
6313 * @instance: Adapter soft state
6314 * @sdev: OS provided scsi device
6315 *
6316 * Returns 0 on success non-zero on failure.
6317 */
Shivasharan Se9495e22018-06-04 03:45:12 -07006318int
Shivasharan S96188a82017-02-10 00:59:11 -08006319megasas_get_target_prop(struct megasas_instance *instance,
6320 struct scsi_device *sdev)
6321{
6322 int ret;
6323 struct megasas_cmd *cmd;
6324 struct megasas_dcmd_frame *dcmd;
6325 u16 targetId = (sdev->channel % 2) + sdev->id;
6326
6327 cmd = megasas_get_cmd(instance);
6328
6329 if (!cmd) {
6330 dev_err(&instance->pdev->dev,
6331 "Failed to get cmd %s\n", __func__);
6332 return -ENOMEM;
6333 }
6334
6335 dcmd = &cmd->frame->dcmd;
6336
6337 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6338 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6339 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6340
6341 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6342 dcmd->cmd = MFI_CMD_DCMD;
6343 dcmd->cmd_status = 0xFF;
6344 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006345 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08006346 dcmd->timeout = 0;
6347 dcmd->pad_0 = 0;
6348 dcmd->data_xfer_len =
6349 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6350 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08006351
Shivasharan S107a60d2017-10-19 02:49:05 -07006352 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6353 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08006354
Shivasharan Se7d36b82017-10-19 02:48:50 -07006355 if ((instance->adapter_type != MFI_SERIES) &&
6356 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08006357 ret = megasas_issue_blocked_cmd(instance,
6358 cmd, MFI_IO_TIMEOUT_SECS);
6359 else
6360 ret = megasas_issue_polled(instance, cmd);
6361
6362 switch (ret) {
6363 case DCMD_TIMEOUT:
6364 switch (dcmd_timeout_ocr_possible(instance)) {
6365 case INITIATE_OCR:
6366 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6367 megasas_reset_fusion(instance->host,
6368 MFI_IO_TIMEOUT_OCR);
6369 break;
6370 case KILL_ADAPTER:
6371 megaraid_sas_kill_hba(instance);
6372 break;
6373 case IGNORE_TIMEOUT:
6374 dev_info(&instance->pdev->dev,
6375 "Ignore DCMD timeout: %s %d\n",
6376 __func__, __LINE__);
6377 break;
6378 }
6379 break;
6380
6381 default:
6382 megasas_return_cmd(instance, cmd);
6383 }
6384 if (ret != DCMD_SUCCESS)
6385 dev_err(&instance->pdev->dev,
6386 "return from %s %d return value %d\n",
6387 __func__, __LINE__, ret);
6388
6389 return ret;
6390}
6391
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006392/**
6393 * megasas_start_aen - Subscribes to AEN during driver load time
6394 * @instance: Adapter soft state
6395 */
6396static int megasas_start_aen(struct megasas_instance *instance)
6397{
6398 struct megasas_evt_log_info eli;
6399 union megasas_evt_class_locale class_locale;
6400
6401 /*
6402 * Get the latest sequence number from FW
6403 */
6404 memset(&eli, 0, sizeof(eli));
6405
6406 if (megasas_get_seq_num(instance, &eli))
6407 return -1;
6408
6409 /*
6410 * Register AEN with FW for latest sequence number plus 1
6411 */
6412 class_locale.members.reserved = 0;
6413 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6414 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6415
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306416 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05306417 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306418 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006419}
6420
6421/**
6422 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6423 * @instance: Adapter soft state
6424 */
6425static int megasas_io_attach(struct megasas_instance *instance)
6426{
6427 struct Scsi_Host *host = instance->host;
6428
6429 /*
6430 * Export parameters required by SCSI mid-layer
6431 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006432 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306433 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006434 host->this_id = instance->init_id;
6435 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08006436
6437 if (instance->fw_support_ieee)
6438 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6439
Yang, Bo1fd10682010-10-12 07:18:50 -06006440 /*
6441 * Check if the module parameter value for max_sectors can be used
6442 */
6443 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6444 instance->max_sectors_per_req = max_sectors;
6445 else {
6446 if (max_sectors) {
6447 if (((instance->pdev->device ==
6448 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6449 (instance->pdev->device ==
6450 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6451 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6452 instance->max_sectors_per_req = max_sectors;
6453 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006454 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06006455 "and <= %d (or < 1MB for GEN2 controller)\n",
6456 instance->max_sectors_per_req);
6457 }
6458 }
6459 }
6460
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006461 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08006462 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006463 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6464 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6465 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08006466 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006467
6468 /*
6469 * Notify the mid-layer about the new controller
6470 */
6471 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306472 dev_err(&instance->pdev->dev,
6473 "Failed to add host from %s %d\n",
6474 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006475 return -ENODEV;
6476 }
6477
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006478 return 0;
6479}
6480
Shivasharan S107a60d2017-10-19 02:49:05 -07006481/**
6482 * megasas_set_dma_mask - Set DMA mask for supported controllers
6483 *
6484 * @instance: Adapter soft state
6485 * Description:
6486 *
Shivasharan S894169d2018-12-18 05:59:54 -08006487 * For Ventura, driver/FW will operate in 63bit DMA addresses.
Shivasharan S107a60d2017-10-19 02:49:05 -07006488 *
6489 * For invader-
6490 * By default, driver/FW will operate in 32bit DMA addresses
6491 * for consistent DMA mapping but if 32 bit consistent
Shivasharan S894169d2018-12-18 05:59:54 -08006492 * DMA mask fails, driver will try with 63 bit consistent
6493 * mask provided FW is true 63bit DMA capable
Shivasharan S107a60d2017-10-19 02:49:05 -07006494 *
6495 * For older controllers(Thunderbolt and MFI based adapters)-
6496 * driver/FW will operate in 32 bit consistent DMA addresses.
6497 */
bo yang31ea7082007-11-07 12:09:50 -05006498static int
Shivasharan S107a60d2017-10-19 02:49:05 -07006499megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05006500{
Shivasharan S107a60d2017-10-19 02:49:05 -07006501 u64 consistent_mask;
6502 struct pci_dev *pdev;
Shivasharan S81b76452018-10-16 23:37:51 -07006503 u32 scratch_pad_1;
bo yang31ea7082007-11-07 12:09:50 -05006504
Shivasharan S107a60d2017-10-19 02:49:05 -07006505 pdev = instance->pdev;
Shivasharan S630d42b2018-12-17 00:47:37 -08006506 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
Shivasharan S894169d2018-12-18 05:59:54 -08006507 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
Shivasharan S107a60d2017-10-19 02:49:05 -07006508
6509 if (IS_DMA64) {
Shivasharan S894169d2018-12-18 05:59:54 -08006510 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006511 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6512 goto fail_set_dma_mask;
6513
Shivasharan S894169d2018-12-18 05:59:54 -08006514 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006515 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6516 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6517 /*
6518 * If 32 bit DMA mask fails, then try for 64 bit mask
6519 * for FW capable of handling 64 bit DMA.
6520 */
Shivasharan S272652f2018-12-17 00:47:40 -08006521 scratch_pad_1 = megasas_readl
6522 (instance, &instance->reg_set->outbound_scratch_pad_1);
Shivasharan S107a60d2017-10-19 02:49:05 -07006523
Shivasharan S81b76452018-10-16 23:37:51 -07006524 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
Shivasharan S107a60d2017-10-19 02:49:05 -07006525 goto fail_set_dma_mask;
6526 else if (dma_set_mask_and_coherent(&pdev->dev,
Shivasharan S894169d2018-12-18 05:59:54 -08006527 DMA_BIT_MASK(63)))
bo yang31ea7082007-11-07 12:09:50 -05006528 goto fail_set_dma_mask;
6529 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006530 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6531 goto fail_set_dma_mask;
6532
6533 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6534 instance->consistent_mask_64bit = false;
6535 else
6536 instance->consistent_mask_64bit = true;
6537
6538 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
Tomas Henzld1f38d92019-01-02 16:07:25 +01006539 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
Shivasharan S894169d2018-12-18 05:59:54 -08006540 (instance->consistent_mask_64bit ? "63" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306541
bo yang31ea7082007-11-07 12:09:50 -05006542 return 0;
6543
6544fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006545 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6546 return -1;
6547
bo yang31ea7082007-11-07 12:09:50 -05006548}
6549
Shivasharan Sc3651782017-10-19 02:48:48 -07006550/*
6551 * megasas_set_adapter_type - Set adapter type.
6552 * Supported controllers can be divided in
Shivasharan S154a7cd2018-12-17 00:47:38 -08006553 * different categories-
6554 * enum MR_ADAPTER_TYPE {
6555 * MFI_SERIES = 1,
6556 * THUNDERBOLT_SERIES = 2,
6557 * INVADER_SERIES = 3,
6558 * VENTURA_SERIES = 4,
6559 * AERO_SERIES = 5,
6560 * };
Shivasharan Sc3651782017-10-19 02:48:48 -07006561 * @instance: Adapter soft state
6562 * return: void
6563 */
6564static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6565{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006566 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6567 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006568 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006569 } else {
6570 switch (instance->pdev->device) {
Shivasharan S469f72d2018-11-09 09:47:20 -08006571 case PCI_DEVICE_ID_LSI_AERO_10E1:
6572 case PCI_DEVICE_ID_LSI_AERO_10E2:
6573 case PCI_DEVICE_ID_LSI_AERO_10E5:
6574 case PCI_DEVICE_ID_LSI_AERO_10E6:
Shivasharan S154a7cd2018-12-17 00:47:38 -08006575 instance->adapter_type = AERO_SERIES;
6576 break;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006577 case PCI_DEVICE_ID_LSI_VENTURA:
6578 case PCI_DEVICE_ID_LSI_CRUSADER:
6579 case PCI_DEVICE_ID_LSI_HARPOON:
6580 case PCI_DEVICE_ID_LSI_TOMCAT:
6581 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6582 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6583 instance->adapter_type = VENTURA_SERIES;
6584 break;
6585 case PCI_DEVICE_ID_LSI_FUSION:
6586 case PCI_DEVICE_ID_LSI_PLASMA:
6587 instance->adapter_type = THUNDERBOLT_SERIES;
6588 break;
6589 case PCI_DEVICE_ID_LSI_INVADER:
6590 case PCI_DEVICE_ID_LSI_INTRUDER:
6591 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6592 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6593 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6594 case PCI_DEVICE_ID_LSI_FURY:
6595 instance->adapter_type = INVADER_SERIES;
6596 break;
6597 default: /* For all other supported controllers */
6598 instance->adapter_type = MFI_SERIES;
6599 break;
6600 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006601 }
6602}
6603
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006604static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6605{
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006606 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6607 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6608 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6609 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006610
6611 if (!instance->producer || !instance->consumer) {
6612 dev_err(&instance->pdev->dev,
6613 "Failed to allocate memory for producer, consumer\n");
6614 return -1;
6615 }
6616
6617 *instance->producer = 0;
6618 *instance->consumer = 0;
6619 return 0;
6620}
6621
6622/**
6623 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6624 * structures which are not common across MFI
6625 * adapters and fusion adapters.
6626 * For MFI based adapters, allocate producer and
6627 * consumer buffers. For fusion adapters, allocate
6628 * memory for fusion context.
6629 * @instance: Adapter soft state
6630 * return: 0 for SUCCESS
6631 */
6632static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6633{
Kees Cook6396bb22018-06-12 14:03:40 -07006634 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
Ming Leiadbe5522018-03-13 17:42:40 +08006635 GFP_KERNEL);
6636 if (!instance->reply_map)
6637 return -ENOMEM;
6638
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006639 switch (instance->adapter_type) {
6640 case MFI_SERIES:
6641 if (megasas_alloc_mfi_ctrl_mem(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006642 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006643 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08006644 case AERO_SERIES:
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006645 case VENTURA_SERIES:
6646 case THUNDERBOLT_SERIES:
6647 case INVADER_SERIES:
6648 if (megasas_alloc_fusion_context(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006649 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006650 break;
6651 }
6652
6653 return 0;
Ming Leiadbe5522018-03-13 17:42:40 +08006654 fail:
6655 kfree(instance->reply_map);
6656 instance->reply_map = NULL;
6657 return -ENOMEM;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006658}
6659
6660/*
6661 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6662 * producer, consumer buffers for MFI adapters
6663 *
6664 * @instance - Adapter soft instance
6665 *
6666 */
6667static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6668{
Ming Leiadbe5522018-03-13 17:42:40 +08006669 kfree(instance->reply_map);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006670 if (instance->adapter_type == MFI_SERIES) {
6671 if (instance->producer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006672 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006673 instance->producer,
6674 instance->producer_h);
6675 if (instance->consumer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006676 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006677 instance->consumer,
6678 instance->consumer_h);
6679 } else {
6680 megasas_free_fusion_context(instance);
6681 }
6682}
6683
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006684/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006685 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6686 * driver load time
6687 *
6688 * @instance- Adapter soft instance
6689 * @return- O for SUCCESS
6690 */
6691static inline
6692int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6693{
6694 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006695 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006696
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006697 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6698 sizeof(struct megasas_evt_detail),
6699 &instance->evt_detail_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006700
6701 if (!instance->evt_detail) {
6702 dev_err(&instance->pdev->dev,
6703 "Failed to allocate event detail buffer\n");
6704 return -ENOMEM;
6705 }
6706
Shivasharan S9b3d0282017-10-19 02:48:56 -07006707 if (fusion) {
6708 fusion->ioc_init_request =
6709 dma_alloc_coherent(&pdev->dev,
6710 sizeof(struct MPI2_IOC_INIT_REQUEST),
6711 &fusion->ioc_init_request_phys,
6712 GFP_KERNEL);
6713
6714 if (!fusion->ioc_init_request) {
6715 dev_err(&pdev->dev,
6716 "Failed to allocate PD list buffer\n");
6717 return -ENOMEM;
6718 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006719
6720 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6721 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6722 &instance->snapdump_prop_h, GFP_KERNEL);
6723
6724 if (!instance->snapdump_prop)
6725 dev_err(&pdev->dev,
6726 "Failed to allocate snapdump properties buffer\n");
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006727
6728 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6729 HOST_DEVICE_LIST_SZ,
6730 &instance->host_device_list_buf_h,
6731 GFP_KERNEL);
6732
6733 if (!instance->host_device_list_buf) {
6734 dev_err(&pdev->dev,
6735 "Failed to allocate targetid list buffer\n");
6736 return -ENOMEM;
6737 }
6738
Shivasharan S9b3d0282017-10-19 02:48:56 -07006739 }
6740
6741 instance->pd_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006742 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006743 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006744 &instance->pd_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006745
6746 if (!instance->pd_list_buf) {
6747 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6748 return -ENOMEM;
6749 }
6750
6751 instance->ctrl_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006752 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006753 sizeof(struct megasas_ctrl_info),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006754 &instance->ctrl_info_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006755
6756 if (!instance->ctrl_info_buf) {
6757 dev_err(&pdev->dev,
6758 "Failed to allocate controller info buffer\n");
6759 return -ENOMEM;
6760 }
6761
6762 instance->ld_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006763 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006764 sizeof(struct MR_LD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006765 &instance->ld_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006766
6767 if (!instance->ld_list_buf) {
6768 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6769 return -ENOMEM;
6770 }
6771
6772 instance->ld_targetid_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006773 dma_alloc_coherent(&pdev->dev,
6774 sizeof(struct MR_LD_TARGETID_LIST),
6775 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006776
6777 if (!instance->ld_targetid_list_buf) {
6778 dev_err(&pdev->dev,
6779 "Failed to allocate LD targetid list buffer\n");
6780 return -ENOMEM;
6781 }
6782
Shivasharan S1b4bed22017-10-19 02:48:55 -07006783 if (!reset_devices) {
6784 instance->system_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006785 dma_alloc_coherent(&pdev->dev,
6786 sizeof(struct MR_DRV_SYSTEM_INFO),
6787 &instance->system_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006788 instance->pd_info =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006789 dma_alloc_coherent(&pdev->dev,
6790 sizeof(struct MR_PD_INFO),
6791 &instance->pd_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006792 instance->tgt_prop =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006793 dma_alloc_coherent(&pdev->dev,
6794 sizeof(struct MR_TARGET_PROPERTIES),
6795 &instance->tgt_prop_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006796 instance->crash_dump_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006797 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6798 &instance->crash_dump_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006799
6800 if (!instance->system_info_buf)
6801 dev_err(&instance->pdev->dev,
6802 "Failed to allocate system info buffer\n");
6803
6804 if (!instance->pd_info)
6805 dev_err(&instance->pdev->dev,
6806 "Failed to allocate pd_info buffer\n");
6807
6808 if (!instance->tgt_prop)
6809 dev_err(&instance->pdev->dev,
6810 "Failed to allocate tgt_prop buffer\n");
6811
6812 if (!instance->crash_dump_buf)
6813 dev_err(&instance->pdev->dev,
6814 "Failed to allocate crash dump buffer\n");
6815 }
6816
6817 return 0;
6818}
6819
6820/*
6821 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6822 * during driver load time
6823 *
6824 * @instance- Adapter soft instance
6825 *
6826 */
6827static inline
6828void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6829{
6830 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006831 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006832
6833 if (instance->evt_detail)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006834 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006835 instance->evt_detail,
6836 instance->evt_detail_h);
6837
Shivasharan S9b3d0282017-10-19 02:48:56 -07006838 if (fusion && fusion->ioc_init_request)
6839 dma_free_coherent(&pdev->dev,
6840 sizeof(struct MPI2_IOC_INIT_REQUEST),
6841 fusion->ioc_init_request,
6842 fusion->ioc_init_request_phys);
6843
6844 if (instance->pd_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006845 dma_free_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006846 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6847 instance->pd_list_buf,
6848 instance->pd_list_buf_h);
6849
6850 if (instance->ld_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006851 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006852 instance->ld_list_buf,
6853 instance->ld_list_buf_h);
6854
6855 if (instance->ld_targetid_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006856 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006857 instance->ld_targetid_list_buf,
6858 instance->ld_targetid_list_buf_h);
6859
6860 if (instance->ctrl_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006861 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006862 instance->ctrl_info_buf,
6863 instance->ctrl_info_buf_h);
6864
Shivasharan S1b4bed22017-10-19 02:48:55 -07006865 if (instance->system_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006866 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006867 instance->system_info_buf,
6868 instance->system_info_h);
6869
6870 if (instance->pd_info)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006871 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006872 instance->pd_info, instance->pd_info_h);
6873
6874 if (instance->tgt_prop)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006875 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006876 instance->tgt_prop, instance->tgt_prop_h);
6877
6878 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006879 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
Shivasharan S1b4bed22017-10-19 02:48:55 -07006880 instance->crash_dump_buf,
6881 instance->crash_dump_h);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006882
6883 if (instance->snapdump_prop)
6884 dma_free_coherent(&pdev->dev,
6885 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6886 instance->snapdump_prop,
6887 instance->snapdump_prop_h);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006888
6889 if (instance->host_device_list_buf)
6890 dma_free_coherent(&pdev->dev,
6891 HOST_DEVICE_LIST_SZ,
6892 instance->host_device_list_buf,
6893 instance->host_device_list_buf_h);
6894
Shivasharan S1b4bed22017-10-19 02:48:55 -07006895}
6896
Shivasharan S7535f272017-10-19 02:48:58 -07006897/*
6898 * megasas_init_ctrl_params - Initialize controller's instance
6899 * parameters before FW init
6900 * @instance - Adapter soft instance
6901 * @return - void
6902 */
6903static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6904{
6905 instance->fw_crash_state = UNAVAILABLE;
6906
6907 megasas_poll_wait_aen = 0;
6908 instance->issuepend_done = 1;
6909 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6910
6911 /*
6912 * Initialize locks and queues
6913 */
6914 INIT_LIST_HEAD(&instance->cmd_pool);
6915 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6916
6917 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S1d15d902019-05-07 10:05:36 -07006918 atomic64_set(&instance->total_io_count, 0);
Shivasharan S7535f272017-10-19 02:48:58 -07006919
6920 init_waitqueue_head(&instance->int_cmd_wait_q);
6921 init_waitqueue_head(&instance->abort_cmd_wait_q);
6922
6923 spin_lock_init(&instance->crashdump_lock);
6924 spin_lock_init(&instance->mfi_pool_lock);
6925 spin_lock_init(&instance->hba_lock);
6926 spin_lock_init(&instance->stream_lock);
6927 spin_lock_init(&instance->completion_lock);
6928
Shivasharan S7535f272017-10-19 02:48:58 -07006929 mutex_init(&instance->reset_mutex);
6930
6931 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6932 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6933 instance->flag_ieee = 1;
6934
6935 megasas_dbg_lvl = 0;
6936 instance->flag = 0;
6937 instance->unload = 1;
6938 instance->last_time = 0;
6939 instance->disableOnlineCtrlReset = 1;
6940 instance->UnevenSpanSupport = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07006941 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
6942 instance->msix_load_balance = false;
Shivasharan S7535f272017-10-19 02:48:58 -07006943
Shivasharan S3f6194a2018-10-16 23:37:39 -07006944 if (instance->adapter_type != MFI_SERIES)
Shivasharan S7535f272017-10-19 02:48:58 -07006945 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Shivasharan S3f6194a2018-10-16 23:37:39 -07006946 else
Shivasharan S7535f272017-10-19 02:48:58 -07006947 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006948}
6949
6950/**
6951 * megasas_probe_one - PCI hotplug entry point
6952 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006953 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006954 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006955static int megasas_probe_one(struct pci_dev *pdev,
6956 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006957{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306958 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006959 struct Scsi_Host *host;
6960 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006961 u16 control = 0;
6962
Shivasharan S469f72d2018-11-09 09:47:20 -08006963 switch (pdev->device) {
6964 case PCI_DEVICE_ID_LSI_AERO_10E1:
6965 case PCI_DEVICE_ID_LSI_AERO_10E5:
6966 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
6967 break;
6968 }
6969
adam radford66192dfe2011-02-24 20:56:28 -08006970 /* Reset MSI-X in the kdump kernel */
6971 if (reset_devices) {
6972 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6973 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006974 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006975 &control);
6976 if (control & PCI_MSIX_FLAGS_ENABLE) {
6977 dev_info(&pdev->dev, "resetting MSI-X\n");
6978 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006979 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006980 control &
6981 ~PCI_MSIX_FLAGS_ENABLE);
6982 }
6983 }
6984 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006985
6986 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006987 * PCI prepping: enable device set bus mastering and dma mask
6988 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006989 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006990
6991 if (rval) {
6992 return rval;
6993 }
6994
6995 pci_set_master(pdev);
6996
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006997 host = scsi_host_alloc(&megasas_template,
6998 sizeof(struct megasas_instance));
6999
7000 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007001 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007002 goto fail_alloc_instance;
7003 }
7004
7005 instance = (struct megasas_instance *)host->hostdata;
7006 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007007 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007008
7009 /*
7010 * Initialize PCI related and misc parameters
7011 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007012 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007013 instance->host = host;
7014 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7015 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7016
Shivasharan Sc3651782017-10-19 02:48:48 -07007017 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07007018
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007019 /*
adam radford0a770662011-02-24 20:56:12 -08007020 * Initialize MFI Firmware
7021 */
7022 if (megasas_init_fw(instance))
7023 goto fail_init_mfi;
7024
adam radford229fe472014-03-10 02:51:56 -07007025 if (instance->requestorId) {
7026 if (instance->PlasmaFW111) {
7027 instance->vf_affiliation_111 =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007028 dma_alloc_coherent(&pdev->dev,
7029 sizeof(struct MR_LD_VF_AFFILIATION_111),
7030 &instance->vf_affiliation_111_h,
7031 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007032 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007033 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007034 "memory for VF affiliation buffer\n");
7035 } else {
7036 instance->vf_affiliation =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007037 dma_alloc_coherent(&pdev->dev,
7038 (MAX_LOGICAL_DRIVES + 1) *
7039 sizeof(struct MR_LD_VF_AFFILIATION),
7040 &instance->vf_affiliation_h,
7041 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07007042 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007043 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07007044 "memory for VF affiliation buffer\n");
7045 }
7046 }
7047
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007048 /*
7049 * Store instance in PCI softstate
7050 */
7051 pci_set_drvdata(pdev, instance);
7052
7053 /*
7054 * Add this controller to megasas_mgmt_info structure so that it
7055 * can be exported to management applications
7056 */
7057 megasas_mgmt_info.count++;
7058 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7059 megasas_mgmt_info.max_index++;
7060
7061 /*
adam radford541f90b2011-05-11 18:34:29 -07007062 * Register with SCSI mid-layer
7063 */
7064 if (megasas_io_attach(instance))
7065 goto fail_io_attach;
7066
7067 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307068 /*
7069 * Trigger SCSI to scan our drives
7070 */
Shivasharan Sf6fe5732019-01-29 01:38:14 -08007071 if (!instance->enable_fw_dev_list ||
7072 (instance->host_device_list_buf->count > 0))
7073 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07007074
7075 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007076 * Initiate AEN (Asynchronous Event Notification)
7077 */
7078 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007079 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007080 goto fail_start_aen;
7081 }
7082
Adam Radford9ea81f82014-07-09 15:17:57 -07007083 /* Get current SR-IOV LD/VF affiliation */
7084 if (instance->requestorId)
7085 megasas_get_ld_vf_affiliation(instance, 1);
7086
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007087 return 0;
7088
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007089fail_start_aen:
7090fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007091 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007092 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08007093 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007094
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307095 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307096 megasas_destroy_irqs(instance);
7097
Shivasharan Se7d36b82017-10-19 02:48:50 -07007098 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08007099 megasas_release_fusion(instance);
7100 else
7101 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007102 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007103 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307104fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007105 scsi_host_put(host);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007106fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007107 pci_disable_device(pdev);
7108
7109 return -ENODEV;
7110}
7111
7112/**
7113 * megasas_flush_cache - Requests FW to flush all its caches
7114 * @instance: Adapter soft state
7115 */
7116static void megasas_flush_cache(struct megasas_instance *instance)
7117{
7118 struct megasas_cmd *cmd;
7119 struct megasas_dcmd_frame *dcmd;
7120
Sumit Saxena8a01a412016-01-28 21:04:32 +05307121 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007122 return;
7123
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007124 cmd = megasas_get_cmd(instance);
7125
7126 if (!cmd)
7127 return;
7128
7129 dcmd = &cmd->frame->dcmd;
7130
7131 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7132
7133 dcmd->cmd = MFI_CMD_DCMD;
7134 dcmd->cmd_status = 0x0;
7135 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307136 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007137 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007138 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007139 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307140 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007141 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7142
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307143 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7144 != DCMD_SUCCESS) {
7145 dev_err(&instance->pdev->dev,
7146 "return from %s %d\n", __func__, __LINE__);
7147 return;
7148 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007149
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307150 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007151}
7152
7153/**
7154 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7155 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05007156 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007157 */
bo yang31ea7082007-11-07 12:09:50 -05007158static void megasas_shutdown_controller(struct megasas_instance *instance,
7159 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007160{
7161 struct megasas_cmd *cmd;
7162 struct megasas_dcmd_frame *dcmd;
7163
Sumit Saxena8a01a412016-01-28 21:04:32 +05307164 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007165 return;
7166
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007167 cmd = megasas_get_cmd(instance);
7168
7169 if (!cmd)
7170 return;
7171
7172 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307173 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307174 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08007175 if (instance->map_update_cmd)
7176 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307177 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307178 if (instance->jbod_seq_cmd)
7179 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307180 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307181
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007182 dcmd = &cmd->frame->dcmd;
7183
7184 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7185
7186 dcmd->cmd = MFI_CMD_DCMD;
7187 dcmd->cmd_status = 0x0;
7188 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307189 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007190 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007191 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007192 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307193 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007194
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307195 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7196 != DCMD_SUCCESS) {
7197 dev_err(&instance->pdev->dev,
7198 "return from %s %d\n", __func__, __LINE__);
7199 return;
7200 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007201
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307202 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007203}
7204
Jiri Slaby33139b22008-05-01 17:56:02 +02007205#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007206/**
bo yangad84db22007-11-09 04:40:16 -05007207 * megasas_suspend - driver suspend entry point
7208 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05007209 * @state: PCI power state to suspend routine
7210 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007211static int
bo yang31ea7082007-11-07 12:09:50 -05007212megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7213{
7214 struct Scsi_Host *host;
7215 struct megasas_instance *instance;
7216
7217 instance = pci_get_drvdata(pdev);
7218 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06007219 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05007220
adam radford229fe472014-03-10 02:51:56 -07007221 /* Shutdown SR-IOV heartbeat timer */
7222 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7223 del_timer_sync(&instance->sriov_heartbeat_timer);
7224
Shivasharan S3f6194a2018-10-16 23:37:39 -07007225 /* Stop the FW fault detection watchdog */
7226 if (instance->adapter_type != MFI_SERIES)
7227 megasas_fusion_stop_watchdog(instance);
7228
bo yang31ea7082007-11-07 12:09:50 -05007229 megasas_flush_cache(instance);
7230 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007231
7232 /* cancel the delayed work if this work still in queue */
7233 if (instance->ev != NULL) {
7234 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007235 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007236 instance->ev = NULL;
7237 }
7238
bo yang31ea7082007-11-07 12:09:50 -05007239 tasklet_kill(&instance->isr_tasklet);
7240
7241 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307242 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007243
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307244 megasas_destroy_irqs(instance);
7245
adam radfordc8e858f2011-10-08 18:15:13 -07007246 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007247 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05007248
7249 pci_save_state(pdev);
7250 pci_disable_device(pdev);
7251
7252 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7253
7254 return 0;
7255}
7256
7257/**
7258 * megasas_resume- driver resume entry point
7259 * @pdev: PCI device structure
7260 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007261static int
bo yang31ea7082007-11-07 12:09:50 -05007262megasas_resume(struct pci_dev *pdev)
7263{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307264 int rval;
bo yang31ea7082007-11-07 12:09:50 -05007265 struct Scsi_Host *host;
7266 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01007267 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05007268
7269 instance = pci_get_drvdata(pdev);
7270 host = instance->host;
7271 pci_set_power_state(pdev, PCI_D0);
7272 pci_enable_wake(pdev, PCI_D0, 0);
7273 pci_restore_state(pdev);
7274
7275 /*
7276 * PCI prepping: enable device set bus mastering and dma mask
7277 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007278 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05007279
7280 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007281 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05007282 return rval;
7283 }
7284
7285 pci_set_master(pdev);
7286
Shivasharan S107a60d2017-10-19 02:49:05 -07007287 /*
7288 * We expect the FW state to be READY
7289 */
7290 if (megasas_transition_to_ready(instance, 0))
7291 goto fail_ready_state;
7292
7293 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05007294 goto fail_set_dma_mask;
7295
7296 /*
7297 * Initialize MFI Firmware
7298 */
7299
bo yang31ea7082007-11-07 12:09:50 -05007300 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08007301 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05007302
adam radford3f1abce2011-05-11 18:33:47 -07007303 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01007304 if (instance->msix_vectors) {
7305 irq_flags = PCI_IRQ_MSIX;
Shivasharan S1d15d902019-05-07 10:05:36 -07007306 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007307 irq_flags |= PCI_IRQ_AFFINITY;
7308 }
7309 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7310 instance->msix_vectors ?
7311 instance->msix_vectors : 1, irq_flags);
7312 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02007313 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07007314
Ming Leiadbe5522018-03-13 17:42:40 +08007315 megasas_setup_reply_map(instance);
7316
Shivasharan Se7d36b82017-10-19 02:48:50 -07007317 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007318 megasas_reset_reply_desc(instance);
7319 if (megasas_ioc_init_fusion(instance)) {
7320 megasas_free_cmds(instance);
7321 megasas_free_cmds_fusion(instance);
7322 goto fail_init_mfi;
7323 }
7324 if (!megasas_get_map_info(instance))
7325 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307326 } else {
adam radford9c915a82010-12-21 13:34:31 -08007327 *instance->producer = 0;
7328 *instance->consumer = 0;
7329 if (megasas_issue_init_mfi(instance))
7330 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08007331 }
bo yang31ea7082007-11-07 12:09:50 -05007332
Shivasharan Sc3b10a52018-06-04 03:45:10 -07007333 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7334 goto fail_init_mfi;
7335
adam radford9c915a82010-12-21 13:34:31 -08007336 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7337 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05007338
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307339 if (instance->msix_vectors ?
7340 megasas_setup_irqs_msix(instance, 0) :
7341 megasas_setup_irqs_ioapic(instance))
7342 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05007343
Shivasharan S62a04f82019-05-07 10:05:35 -07007344 if (instance->adapter_type != MFI_SERIES)
7345 megasas_setup_irq_poll(instance);
7346
adam radford229fe472014-03-10 02:51:56 -07007347 /* Re-launch SR-IOV heartbeat timer */
7348 if (instance->requestorId) {
7349 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07007350 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307351 else {
adam radford229fe472014-03-10 02:51:56 -07007352 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307353 goto fail_init_mfi;
7354 }
adam radford229fe472014-03-10 02:51:56 -07007355 }
7356
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307357 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307358 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06007359 instance->unload = 0;
7360
adam radford541f90b2011-05-11 18:34:29 -07007361 /*
7362 * Initiate AEN (Asynchronous Event Notification)
7363 */
7364 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007365 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07007366
Shivasharan S3f6194a2018-10-16 23:37:39 -07007367 /* Re-launch FW fault watchdog */
7368 if (instance->adapter_type != MFI_SERIES)
7369 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7370 goto fail_start_watchdog;
7371
bo yang31ea7082007-11-07 12:09:50 -05007372 return 0;
7373
Shivasharan S3f6194a2018-10-16 23:37:39 -07007374fail_start_watchdog:
7375 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7376 del_timer_sync(&instance->sriov_heartbeat_timer);
bo yang31ea7082007-11-07 12:09:50 -05007377fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07007378 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007379 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05007380 scsi_host_put(host);
7381
Shivasharan S107a60d2017-10-19 02:49:05 -07007382fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05007383fail_set_dma_mask:
7384fail_ready_state:
7385
7386 pci_disable_device(pdev);
7387
7388 return -ENODEV;
7389}
Jiri Slaby33139b22008-05-01 17:56:02 +02007390#else
7391#define megasas_suspend NULL
7392#define megasas_resume NULL
7393#endif
bo yang31ea7082007-11-07 12:09:50 -05007394
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007395static inline int
7396megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7397{
7398 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7399 int i;
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007400 u8 adp_state;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007401
7402 for (i = 0; i < wait_time; i++) {
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007403 adp_state = atomic_read(&instance->adprecovery);
7404 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7405 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007406 break;
7407
7408 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7409 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7410
7411 msleep(1000);
7412 }
7413
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007414 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7415 dev_info(&instance->pdev->dev,
7416 "%s HBA failed to become operational, adp_state %d\n",
7417 __func__, adp_state);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007418 return 1;
7419 }
7420
7421 return 0;
7422}
7423
bo yang31ea7082007-11-07 12:09:50 -05007424/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007425 * megasas_detach_one - PCI hot"un"plug entry point
7426 * @pdev: PCI device structure
7427 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007428static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007429{
7430 int i;
7431 struct Scsi_Host *host;
7432 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08007433 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307434 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007435
7436 instance = pci_get_drvdata(pdev);
7437 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08007438 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007439
adam radford229fe472014-03-10 02:51:56 -07007440 /* Shutdown SR-IOV heartbeat timer */
7441 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7442 del_timer_sync(&instance->sriov_heartbeat_timer);
7443
Shivasharan S3f6194a2018-10-16 23:37:39 -07007444 /* Stop the FW fault detection watchdog */
7445 if (instance->adapter_type != MFI_SERIES)
7446 megasas_fusion_stop_watchdog(instance);
7447
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307448 if (instance->fw_crash_state != UNAVAILABLE)
7449 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007450 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08007451 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007452
7453 if (megasas_wait_for_adapter_operational(instance))
7454 goto skip_firing_dcmds;
7455
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007456 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05007457 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007458
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007459skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007460 /* cancel the delayed work if this work still in queue*/
7461 if (instance->ev != NULL) {
7462 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007463 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007464 instance->ev = NULL;
7465 }
7466
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307467 /* cancel all wait events */
7468 wake_up_all(&instance->int_cmd_wait_q);
7469
Sumant Patro5d018ad2006-10-03 13:13:18 -07007470 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007471
7472 /*
7473 * Take the instance off the instance array. Note that we will not
7474 * decrement the max_index. We let this array be sparse array
7475 */
7476 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7477 if (megasas_mgmt_info.instance[i] == instance) {
7478 megasas_mgmt_info.count--;
7479 megasas_mgmt_info.instance[i] = NULL;
7480
7481 break;
7482 }
7483 }
7484
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307485 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007486
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307487 megasas_destroy_irqs(instance);
7488
adam radfordc8e858f2011-10-08 18:15:13 -07007489 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007490 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007491
Shivasharan S630d42b2018-12-17 00:47:37 -08007492 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05007493 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7494 kfree(fusion->stream_detect_by_ld[i]);
7495 kfree(fusion->stream_detect_by_ld);
7496 fusion->stream_detect_by_ld = NULL;
7497 }
7498
7499
Shivasharan Se7d36b82017-10-19 02:48:50 -07007500 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007501 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307502 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7503 (sizeof(struct MR_PD_CFG_SEQ) *
7504 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307505 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08007506 if (fusion->ld_map[i])
7507 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307508 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08007509 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307510 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07007511 if (fusion->ld_drv_map[i]) {
7512 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7513 vfree(fusion->ld_drv_map[i]);
7514 else
7515 free_pages((ulong)fusion->ld_drv_map[i],
7516 fusion->drv_map_pages);
7517 }
7518
Maurizio Lombardi546e5592016-01-22 13:41:42 +01007519 if (fusion->pd_seq_sync[i])
7520 dma_free_coherent(&instance->pdev->dev,
7521 pd_seq_map_sz,
7522 fusion->pd_seq_sync[i],
7523 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307524 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307525 } else {
adam radford9c915a82010-12-21 13:34:31 -08007526 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08007527 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007528
adam radford229fe472014-03-10 02:51:56 -07007529 if (instance->vf_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007530 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
adam radford229fe472014-03-10 02:51:56 -07007531 sizeof(struct MR_LD_VF_AFFILIATION),
7532 instance->vf_affiliation,
7533 instance->vf_affiliation_h);
7534
7535 if (instance->vf_affiliation_111)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007536 dma_free_coherent(&pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07007537 sizeof(struct MR_LD_VF_AFFILIATION_111),
7538 instance->vf_affiliation_111,
7539 instance->vf_affiliation_111_h);
7540
7541 if (instance->hb_host_mem)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007542 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
adam radford229fe472014-03-10 02:51:56 -07007543 instance->hb_host_mem,
7544 instance->hb_host_mem_h);
7545
Shivasharan S1b4bed22017-10-19 02:48:55 -07007546 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307547
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007548 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307549
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007550 scsi_host_put(host);
7551
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007552 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007553}
7554
7555/**
7556 * megasas_shutdown - Shutdown entry point
7557 * @device: Generic device structure
7558 */
7559static void megasas_shutdown(struct pci_dev *pdev)
7560{
7561 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07007562
Yang, Bo0c79e682009-10-06 14:47:35 -06007563 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007564
7565 if (megasas_wait_for_adapter_operational(instance))
7566 goto skip_firing_dcmds;
7567
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007568 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07007569 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007570
7571skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307572 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307573 megasas_destroy_irqs(instance);
7574
adam radfordc8e858f2011-10-08 18:15:13 -07007575 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007576 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007577}
7578
7579/**
7580 * megasas_mgmt_open - char node "open" entry point
7581 */
7582static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7583{
7584 /*
7585 * Allow only those users with admin rights
7586 */
7587 if (!capable(CAP_SYS_ADMIN))
7588 return -EACCES;
7589
7590 return 0;
7591}
7592
7593/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007594 * megasas_mgmt_fasync - Async notifier registration from applications
7595 *
7596 * This function adds the calling process to a driver global queue. When an
7597 * event occurs, SIGIO will be sent to all processes in this queue.
7598 */
7599static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7600{
7601 int rc;
7602
Arjan van de Ven0b950672006-01-11 13:16:10 +01007603 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007604
7605 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7606
Arjan van de Ven0b950672006-01-11 13:16:10 +01007607 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007608
7609 if (rc >= 0) {
7610 /* For sanity check when we get ioctl */
7611 filep->private_data = filep;
7612 return 0;
7613 }
7614
7615 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7616
7617 return rc;
7618}
7619
7620/**
Yang, Boc3518832009-10-06 14:18:02 -06007621 * megasas_mgmt_poll - char node "poll" entry point
7622 * */
Al Viroafc9a422017-07-03 06:39:46 -04007623static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
Yang, Boc3518832009-10-06 14:18:02 -06007624{
Al Viroafc9a422017-07-03 06:39:46 -04007625 __poll_t mask;
Yang, Boc3518832009-10-06 14:18:02 -06007626 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007627
Yang, Boc3518832009-10-06 14:18:02 -06007628 poll_wait(file, &megasas_poll_wait, wait);
7629 spin_lock_irqsave(&poll_aen_lock, flags);
7630 if (megasas_poll_wait_aen)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007631 mask = (EPOLLIN | EPOLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007632 else
7633 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307634 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007635 spin_unlock_irqrestore(&poll_aen_lock, flags);
7636 return mask;
7637}
7638
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307639/*
7640 * megasas_set_crash_dump_params_ioctl:
7641 * Send CRASH_DUMP_MODE DCMD to all controllers
7642 * @cmd: MFI command frame
7643 */
7644
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007645static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307646{
7647 struct megasas_instance *local_instance;
7648 int i, error = 0;
7649 int crash_support;
7650
7651 crash_support = cmd->frame->dcmd.mbox.w[0];
7652
7653 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7654 local_instance = megasas_mgmt_info.instance[i];
7655 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307656 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307657 MEGASAS_HBA_OPERATIONAL) &&
7658 !megasas_set_crash_dump_params(local_instance,
7659 crash_support)) {
7660 local_instance->crash_dump_app_support =
7661 crash_support;
7662 dev_info(&local_instance->pdev->dev,
7663 "Application firmware crash "
7664 "dump mode set success\n");
7665 error = 0;
7666 } else {
7667 dev_info(&local_instance->pdev->dev,
7668 "Application firmware crash "
7669 "dump mode set failed\n");
7670 error = -1;
7671 }
7672 }
7673 }
7674 return error;
7675}
7676
Yang, Boc3518832009-10-06 14:18:02 -06007677/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007678 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7679 * @instance: Adapter soft state
7680 * @argp: User's ioctl packet
7681 */
7682static int
7683megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7684 struct megasas_iocpacket __user * user_ioc,
7685 struct megasas_iocpacket *ioc)
7686{
Shivasharan S107a60d2017-10-19 02:49:05 -07007687 struct megasas_sge64 *kern_sge64 = NULL;
7688 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007689 struct megasas_cmd *cmd;
7690 void *kbuff_arr[MAX_IOCTL_SGE];
7691 dma_addr_t buf_handle = 0;
7692 int error = 0, i;
7693 void *sense = NULL;
7694 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007695 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007696 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007697
7698 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7699
7700 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007701 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007702 ioc->sge_count, MAX_IOCTL_SGE);
7703 return -EINVAL;
7704 }
7705
Shivasharan Sf870bcb2018-01-05 05:33:04 -08007706 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7707 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7708 !instance->support_nvme_passthru)) {
Shivasharan S82add4e2017-10-19 02:49:02 -07007709 dev_err(&instance->pdev->dev,
7710 "Received invalid ioctl command 0x%x\n",
7711 ioc->frame.hdr.cmd);
7712 return -ENOTSUPP;
7713 }
7714
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007715 cmd = megasas_get_cmd(instance);
7716 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007717 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007718 return -ENOMEM;
7719 }
7720
7721 /*
7722 * User's IOCTL packet has 2 frames (maximum). Copy those two
7723 * frames into our cmd's frames. cmd->frame's context will get
7724 * overwritten when we copy from user's frames. So set that value
7725 * alone separately
7726 */
7727 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307728 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007729 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07007730
7731 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7732
7733 if (instance->consistent_mask_64bit)
7734 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7735 MFI_FRAME_SENSE64));
7736 else
7737 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307738 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07007739
7740 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7741 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007742
Shivasharan S8823abe2017-08-23 04:47:00 -07007743 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08007744 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7745 megasas_return_cmd(instance, cmd);
7746 return -1;
7747 }
7748 }
7749
Shivasharan S8823abe2017-08-23 04:47:00 -07007750 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307751 error = megasas_set_crash_dump_params_ioctl(cmd);
7752 megasas_return_cmd(instance, cmd);
7753 return error;
7754 }
7755
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007756 /*
7757 * The management interface between applications and the fw uses
7758 * MFI frames. E.g, RAID configuration changes, LD property changes
7759 * etc are accomplishes through different kinds of MFI frames. The
7760 * driver needs to care only about substituting user buffers with
7761 * kernel buffers in SGLs. The location of SGL is embedded in the
7762 * struct iocpacket itself.
7763 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007764 if (instance->consistent_mask_64bit)
7765 kern_sge64 = (struct megasas_sge64 *)
7766 ((unsigned long)cmd->frame + ioc->sgl_off);
7767 else
7768 kern_sge32 = (struct megasas_sge32 *)
7769 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007770
7771 /*
7772 * For each user buffer, create a mirror buffer and copy in
7773 */
7774 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01007775 if (!ioc->sgl[i].iov_len)
7776 continue;
7777
Sumant Patro9f35fa82007-02-14 12:55:45 -08007778 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007779 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08007780 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007781 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007782 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7783 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007784 error = -ENOMEM;
7785 goto out;
7786 }
7787
7788 /*
7789 * We don't change the dma_coherent_mask, so
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007790 * dma_alloc_coherent only returns 32bit addresses
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007791 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007792 if (instance->consistent_mask_64bit) {
7793 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7794 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7795 } else {
7796 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7797 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7798 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007799
7800 /*
7801 * We created a kernel buffer corresponding to the
7802 * user buffer. Now copy in from the user buffer
7803 */
7804 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7805 (u32) (ioc->sgl[i].iov_len))) {
7806 error = -EFAULT;
7807 goto out;
7808 }
7809 }
7810
7811 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007812 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7813 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007814 if (!sense) {
7815 error = -ENOMEM;
7816 goto out;
7817 }
7818
7819 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007820 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07007821 if (instance->consistent_mask_64bit)
7822 *sense_ptr = cpu_to_le64(sense_handle);
7823 else
7824 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007825 }
7826
7827 /*
7828 * Set the sync_cmd flag so that the ISR knows not to complete this
7829 * cmd to the SCSI mid-layer
7830 */
7831 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307832 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7833 cmd->sync_cmd = 0;
7834 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07007835 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7836 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7837 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307838 return -EBUSY;
7839 }
7840
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007841 cmd->sync_cmd = 0;
7842
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307843 if (instance->unload == 1) {
7844 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7845 "don't submit data to application\n");
7846 goto out;
7847 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007848 /*
7849 * copy out the kernel buffers to user buffers
7850 */
7851 for (i = 0; i < ioc->sge_count; i++) {
7852 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7853 ioc->sgl[i].iov_len)) {
7854 error = -EFAULT;
7855 goto out;
7856 }
7857 }
7858
7859 /*
7860 * copy out the sense
7861 */
7862 if (ioc->sense_len) {
7863 /*
bo yangb70a41e2008-03-18 03:13:06 -04007864 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007865 * sense buffer address
7866 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007867 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7868 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007869
Shivasharan S318aaef2017-02-10 00:59:22 -08007870 if (copy_to_user((void __user *)((unsigned long)
7871 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007872 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007873 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007874 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007875 error = -EFAULT;
7876 goto out;
7877 }
7878 }
7879
7880 /*
7881 * copy the status codes returned by the fw
7882 */
7883 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7884 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007885 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007886 error = -EFAULT;
7887 }
7888
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007889out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007890 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007891 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007892 sense, sense_handle);
7893 }
7894
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007895 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007896 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07007897 if (instance->consistent_mask_64bit)
7898 dma_free_coherent(&instance->pdev->dev,
7899 le32_to_cpu(kern_sge64[i].length),
7900 kbuff_arr[i],
7901 le64_to_cpu(kern_sge64[i].phys_addr));
7902 else
7903 dma_free_coherent(&instance->pdev->dev,
7904 le32_to_cpu(kern_sge32[i].length),
7905 kbuff_arr[i],
7906 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307907 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007908 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007909 }
7910
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307911 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007912 return error;
7913}
7914
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007915static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7916{
7917 struct megasas_iocpacket __user *user_ioc =
7918 (struct megasas_iocpacket __user *)arg;
7919 struct megasas_iocpacket *ioc;
7920 struct megasas_instance *instance;
7921 int error;
7922
Markus Elfring709ab232016-08-21 10:39:04 +02007923 ioc = memdup_user(user_ioc, sizeof(*ioc));
7924 if (IS_ERR(ioc))
7925 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007926
7927 instance = megasas_lookup_instance(ioc->host_no);
7928 if (!instance) {
7929 error = -ENODEV;
7930 goto out_kfree_ioc;
7931 }
7932
adam radford229fe472014-03-10 02:51:56 -07007933 /* Block ioctls in VF mode */
7934 if (instance->requestorId && !allow_vf_ioctls) {
7935 error = -ENODEV;
7936 goto out_kfree_ioc;
7937 }
7938
Sumit Saxena8a01a412016-01-28 21:04:32 +05307939 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007940 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007941 error = -ENODEV;
7942 goto out_kfree_ioc;
7943 }
7944
7945 if (instance->unload == 1) {
7946 error = -ENODEV;
7947 goto out_kfree_ioc;
7948 }
7949
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007950 if (down_interruptible(&instance->ioctl_sem)) {
7951 error = -ERESTARTSYS;
7952 goto out_kfree_ioc;
7953 }
bo yang39a98552010-09-22 22:36:29 -04007954
Shivasharan S619831f2018-01-05 05:27:43 -08007955 if (megasas_wait_for_adapter_operational(instance)) {
bo yang39a98552010-09-22 22:36:29 -04007956 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007957 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007958 }
bo yang39a98552010-09-22 22:36:29 -04007959
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007960 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007961out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007962 up(&instance->ioctl_sem);
7963
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007964out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007965 kfree(ioc);
7966 return error;
7967}
7968
7969static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7970{
7971 struct megasas_instance *instance;
7972 struct megasas_aen aen;
7973 int error;
7974
7975 if (file->private_data != file) {
7976 printk(KERN_DEBUG "megasas: fasync_helper was not "
7977 "called first\n");
7978 return -EINVAL;
7979 }
7980
7981 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7982 return -EFAULT;
7983
7984 instance = megasas_lookup_instance(aen.host_no);
7985
7986 if (!instance)
7987 return -ENODEV;
7988
Sumit Saxena8a01a412016-01-28 21:04:32 +05307989 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007990 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007991 }
7992
7993 if (instance->unload == 1) {
7994 return -ENODEV;
7995 }
7996
Shivasharan S619831f2018-01-05 05:27:43 -08007997 if (megasas_wait_for_adapter_operational(instance))
bo yang39a98552010-09-22 22:36:29 -04007998 return -ENODEV;
bo yang39a98552010-09-22 22:36:29 -04007999
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308000 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008001 error = megasas_register_aen(instance, aen.seq_num,
8002 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308003 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008004 return error;
8005}
8006
8007/**
8008 * megasas_mgmt_ioctl - char node ioctl entry point
8009 */
8010static long
8011megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8012{
8013 switch (cmd) {
8014 case MEGASAS_IOC_FIRMWARE:
8015 return megasas_mgmt_ioctl_fw(file, arg);
8016
8017 case MEGASAS_IOC_GET_AEN:
8018 return megasas_mgmt_ioctl_aen(file, arg);
8019 }
8020
8021 return -ENOTTY;
8022}
8023
8024#ifdef CONFIG_COMPAT
8025static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8026{
8027 struct compat_megasas_iocpacket __user *cioc =
8028 (struct compat_megasas_iocpacket __user *)arg;
8029 struct megasas_iocpacket __user *ioc =
8030 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8031 int i;
8032 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008033 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308034 u32 local_sense_off;
8035 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308036 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008037
Jeff Garzik83aabc12006-10-04 06:34:03 -04008038 if (clear_user(ioc, sizeof(*ioc)))
8039 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008040
8041 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8042 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8043 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8044 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8045 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8046 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8047 return -EFAULT;
8048
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008049 /*
8050 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8051 * sense_len is not null, so prepare the 64bit value under
8052 * the same condition.
8053 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308054 if (get_user(local_sense_off, &ioc->sense_off) ||
8055 get_user(local_sense_len, &ioc->sense_len) ||
8056 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308057 return -EFAULT;
8058
Wenwen Wang47db7872018-10-06 13:34:21 -05008059 if (local_sense_off != user_sense_off)
8060 return -EINVAL;
8061
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05308062 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008063 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308064 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008065 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05308066 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008067 if (get_user(ptr, sense_cioc_ptr) ||
8068 put_user(compat_ptr(ptr), sense_ioc_ptr))
8069 return -EFAULT;
8070 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008071
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01008072 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008073 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8074 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8075 copy_in_user(&ioc->sgl[i].iov_len,
8076 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8077 return -EFAULT;
8078 }
8079
8080 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8081
8082 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8083 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8084 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8085 return -EFAULT;
8086 }
8087 return error;
8088}
8089
8090static long
8091megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8092 unsigned long arg)
8093{
8094 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08008095 case MEGASAS_IOC_FIRMWARE32:
8096 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008097 case MEGASAS_IOC_GET_AEN:
8098 return megasas_mgmt_ioctl_aen(file, arg);
8099 }
8100
8101 return -ENOTTY;
8102}
8103#endif
8104
8105/*
8106 * File operations structure for management interface
8107 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08008108static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008109 .owner = THIS_MODULE,
8110 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008111 .fasync = megasas_mgmt_fasync,
8112 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06008113 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008114#ifdef CONFIG_COMPAT
8115 .compat_ioctl = megasas_mgmt_compat_ioctl,
8116#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02008117 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008118};
8119
8120/*
8121 * PCI hotplug support registration structure
8122 */
8123static struct pci_driver megasas_pci_driver = {
8124
8125 .name = "megaraid_sas",
8126 .id_table = megasas_pci_table,
8127 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008128 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05008129 .suspend = megasas_suspend,
8130 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008131 .shutdown = megasas_shutdown,
8132};
8133
8134/*
8135 * Sysfs driver attributes
8136 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008137static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008138{
8139 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8140 MEGASAS_VERSION);
8141}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008142static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008143
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008144static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308145{
8146 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8147 MEGASAS_RELDATE);
8148}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008149static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308150
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008151static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06008152{
8153 return sprintf(buf, "%u\n", support_poll_for_event);
8154}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008155static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008156
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008157static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06008158{
8159 return sprintf(buf, "%u\n", support_device_change);
8160}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008161static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06008162
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008163static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07008164{
bo yangad84db22007-11-09 04:40:16 -05008165 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07008166}
8167
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008168static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8169 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07008170{
8171 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008172
8173 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07008174 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8175 retval = -EINVAL;
8176 }
8177 return retval;
8178}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008179static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05008180
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008181static ssize_t
8182support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8183{
8184 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8185}
8186
8187static DRIVER_ATTR_RO(support_nvme_encapsulation);
8188
Shivasharan Sb4a42212017-02-10 00:59:16 -08008189static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8190{
8191 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8192 scsi_remove_device(sdev);
8193 scsi_device_put(sdev);
8194}
8195
Shivasharan S44abbaf2019-01-29 01:38:13 -08008196/**
8197 * megasas_update_device_list - Update the PD and LD device list from FW
8198 * after an AEN event notification
8199 * @instance: Adapter soft state
8200 * @event_type: Indicates type of event (PD or LD event)
8201 *
8202 * @return: Success or failure
8203 *
8204 * Issue DCMDs to Firmware to update the internal device list in driver.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008205 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8206 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan S44abbaf2019-01-29 01:38:13 -08008207 */
8208static
8209int megasas_update_device_list(struct megasas_instance *instance,
8210 int event_type)
8211{
8212 int dcmd_ret = DCMD_SUCCESS;
8213
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008214 if (instance->enable_fw_dev_list) {
8215 dcmd_ret = megasas_host_device_list_query(instance, false);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008216 if (dcmd_ret != DCMD_SUCCESS)
8217 goto out;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008218 } else {
8219 if (event_type & SCAN_PD_CHANNEL) {
8220 dcmd_ret = megasas_get_pd_list(instance);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008221
Shivasharan S44abbaf2019-01-29 01:38:13 -08008222 if (dcmd_ret != DCMD_SUCCESS)
8223 goto out;
8224 }
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008225
8226 if (event_type & SCAN_VD_CHANNEL) {
8227 if (!instance->requestorId ||
8228 (instance->requestorId &&
8229 megasas_get_ld_vf_affiliation(instance, 0))) {
8230 dcmd_ret = megasas_ld_list_query(instance,
8231 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8232 if (dcmd_ret != DCMD_SUCCESS)
8233 goto out;
8234 }
8235 }
Shivasharan S44abbaf2019-01-29 01:38:13 -08008236 }
8237
8238out:
8239 return dcmd_ret;
8240}
8241
8242/**
8243 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8244 * after an AEN event notification
8245 * @instance: Adapter soft state
8246 * @scan_type: Indicates type of devices (PD/LD) to add
8247 * @return void
8248 */
8249static
8250void megasas_add_remove_devices(struct megasas_instance *instance,
8251 int scan_type)
8252{
8253 int i, j;
8254 u16 pd_index = 0;
8255 u16 ld_index = 0;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008256 u16 channel = 0, id = 0;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008257 struct Scsi_Host *host;
8258 struct scsi_device *sdev1;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008259 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8260 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008261
8262 host = instance->host;
8263
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008264 if (instance->enable_fw_dev_list) {
8265 targetid_list = instance->host_device_list_buf;
8266 for (i = 0; i < targetid_list->count; i++) {
8267 targetid_entry = &targetid_list->host_device_list[i];
8268 if (targetid_entry->flags.u.bits.is_sys_pd) {
8269 channel = le16_to_cpu(targetid_entry->target_id) /
8270 MEGASAS_MAX_DEV_PER_CHANNEL;
8271 id = le16_to_cpu(targetid_entry->target_id) %
8272 MEGASAS_MAX_DEV_PER_CHANNEL;
8273 } else {
8274 channel = MEGASAS_MAX_PD_CHANNELS +
8275 (le16_to_cpu(targetid_entry->target_id) /
8276 MEGASAS_MAX_DEV_PER_CHANNEL);
8277 id = le16_to_cpu(targetid_entry->target_id) %
8278 MEGASAS_MAX_DEV_PER_CHANNEL;
8279 }
8280 sdev1 = scsi_device_lookup(host, channel, id, 0);
8281 if (!sdev1) {
8282 scsi_add_device(host, channel, id, 0);
8283 } else {
8284 scsi_device_put(sdev1);
8285 }
8286 }
8287 }
8288
Shivasharan S44abbaf2019-01-29 01:38:13 -08008289 if (scan_type & SCAN_PD_CHANNEL) {
8290 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8291 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8292 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8293 sdev1 = scsi_device_lookup(host, i, j, 0);
8294 if (instance->pd_list[pd_index].driveState ==
8295 MR_PD_STATE_SYSTEM) {
8296 if (!sdev1)
8297 scsi_add_device(host, i, j, 0);
8298 else
8299 scsi_device_put(sdev1);
8300 } else {
8301 if (sdev1)
8302 megasas_remove_scsi_device(sdev1);
8303 }
8304 }
8305 }
8306 }
8307
8308 if (scan_type & SCAN_VD_CHANNEL) {
8309 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8310 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8311 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8312 sdev1 = scsi_device_lookup(host,
8313 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8314 if (instance->ld_ids[ld_index] != 0xff) {
8315 if (!sdev1)
8316 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8317 else
8318 scsi_device_put(sdev1);
8319 } else {
8320 if (sdev1)
8321 megasas_remove_scsi_device(sdev1);
8322 }
8323 }
8324 }
8325 }
8326
8327}
8328
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008329static void
8330megasas_aen_polling(struct work_struct *work)
8331{
8332 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08008333 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008334 struct megasas_instance *instance = ev->instance;
8335 union megasas_evt_class_locale class_locale;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008336 int event_type = 0;
adam radford229fe472014-03-10 02:51:56 -07008337 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008338 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308339 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008340
8341 if (!instance) {
8342 printk(KERN_ERR "invalid instance!\n");
8343 kfree(ev);
8344 return;
8345 }
adam radford229fe472014-03-10 02:51:56 -07008346
8347 /* Adjust event workqueue thread wait time for VF mode */
8348 if (instance->requestorId)
8349 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8350
8351 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308352 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07008353
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008354 instance->ev = NULL;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008355 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05308356 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008357
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308358 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308359
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008360 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308361 case MR_EVT_PD_REMOVED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008362 event_type = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07008363 break;
8364
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308365 case MR_EVT_LD_OFFLINE:
8366 case MR_EVT_CFG_CLEARED:
8367 case MR_EVT_LD_DELETED:
8368 case MR_EVT_LD_CREATED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008369 event_type = SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308370 break;
8371
8372 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8373 case MR_EVT_FOREIGN_CFG_IMPORTED:
8374 case MR_EVT_LD_STATE_CHANGE:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008375 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308376 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8377 instance->host->host_no);
8378 break;
8379
8380 case MR_EVT_CTRL_PROP_CHANGED:
Shivasharan Sf0c21df2018-10-16 23:37:40 -07008381 dcmd_ret = megasas_get_ctrl_info(instance);
8382 if (dcmd_ret == DCMD_SUCCESS &&
8383 instance->snapdump_wait_time) {
8384 megasas_get_snapdump_properties(instance);
8385 dev_info(&instance->pdev->dev,
8386 "Snap dump wait time\t: %d\n",
8387 instance->snapdump_wait_time);
8388 }
8389 break;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308390 default:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008391 event_type = 0;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308392 break;
8393 }
8394 } else {
8395 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8396 mutex_unlock(&instance->reset_mutex);
8397 kfree(ev);
8398 return;
8399 }
8400
Shivasharan S44abbaf2019-01-29 01:38:13 -08008401 if (event_type)
8402 dcmd_ret = megasas_update_device_list(instance, event_type);
8403
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308404 mutex_unlock(&instance->reset_mutex);
8405
Shivasharan S44abbaf2019-01-29 01:38:13 -08008406 if (event_type && dcmd_ret == DCMD_SUCCESS)
8407 megasas_add_remove_devices(instance, event_type);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008408
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308409 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308410 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8411 else
8412 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008413
8414 /* Register AEN with FW for latest sequence number plus 1 */
8415 class_locale.members.reserved = 0;
8416 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8417 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308418
8419 if (instance->aen_cmd != NULL) {
8420 kfree(ev);
8421 return;
8422 }
8423
8424 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008425 error = megasas_register_aen(instance, seq_num,
8426 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008427 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308428 dev_err(&instance->pdev->dev,
8429 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008430
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308431 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008432 kfree(ev);
8433}
8434
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008435/**
8436 * megasas_init - Driver load entry point
8437 */
8438static int __init megasas_init(void)
8439{
8440 int rval;
8441
8442 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07008443 * Booted in kdump kernel, minimize memory footprints by
8444 * disabling few features
8445 */
8446 if (reset_devices) {
8447 msix_vectors = 1;
8448 rdpq_enable = 0;
8449 dual_qdepth_disable = 1;
8450 }
8451
8452 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008453 * Announce driver version and other information
8454 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05308455 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008456
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07008457 spin_lock_init(&poll_aen_lock);
8458
Yang, Bo72c4fd32009-10-06 14:20:59 -06008459 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06008460 support_device_change = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008461 support_nvme_encapsulation = true;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008462
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008463 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8464
8465 /*
8466 * Register character device node
8467 */
8468 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8469
8470 if (rval < 0) {
8471 printk(KERN_DEBUG "megasas: failed to open device node\n");
8472 return rval;
8473 }
8474
8475 megasas_mgmt_majorno = rval;
8476
8477 /*
8478 * Register ourselves as PCI hotplug module
8479 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00008480 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008481
8482 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09008483 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04008484 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008485 }
8486
Jeff Garzik83aabc12006-10-04 06:34:03 -04008487 rval = driver_create_file(&megasas_pci_driver.driver,
8488 &driver_attr_version);
8489 if (rval)
8490 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008491
8492 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308493 &driver_attr_release_date);
8494 if (rval)
8495 goto err_dcf_rel_date;
8496
8497 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008498 &driver_attr_support_poll_for_event);
8499 if (rval)
8500 goto err_dcf_support_poll_for_event;
8501
Jeff Garzik83aabc12006-10-04 06:34:03 -04008502 rval = driver_create_file(&megasas_pci_driver.driver,
8503 &driver_attr_dbg_lvl);
8504 if (rval)
8505 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05008506 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008507 &driver_attr_support_device_change);
8508 if (rval)
8509 goto err_dcf_support_device_change;
8510
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008511 rval = driver_create_file(&megasas_pci_driver.driver,
8512 &driver_attr_support_nvme_encapsulation);
8513 if (rval)
8514 goto err_dcf_support_nvme_encapsulation;
8515
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008516 return rval;
bo yangad84db22007-11-09 04:40:16 -05008517
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008518err_dcf_support_nvme_encapsulation:
8519 driver_remove_file(&megasas_pci_driver.driver,
8520 &driver_attr_support_device_change);
8521
Yang, Bo837f5fe2010-10-11 06:59:20 -06008522err_dcf_support_device_change:
8523 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05008524 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008525err_dcf_dbg_lvl:
8526 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008527 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008528err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308529 driver_remove_file(&megasas_pci_driver.driver,
8530 &driver_attr_release_date);
8531err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04008532 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8533err_dcf_attr_ver:
8534 pci_unregister_driver(&megasas_pci_driver);
8535err_pcidrv:
8536 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08008537 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008538}
8539
8540/**
8541 * megasas_exit - Driver unload entry point
8542 */
8543static void __exit megasas_exit(void)
8544{
Sumant Patro658dced2006-10-03 13:09:14 -07008545 driver_remove_file(&megasas_pci_driver.driver,
8546 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008547 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008548 &driver_attr_support_poll_for_event);
8549 driver_remove_file(&megasas_pci_driver.driver,
8550 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308551 driver_remove_file(&megasas_pci_driver.driver,
8552 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008553 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008554 driver_remove_file(&megasas_pci_driver.driver,
8555 &driver_attr_support_nvme_encapsulation);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008556
8557 pci_unregister_driver(&megasas_pci_driver);
8558 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8559}
8560
8561module_init(megasas_init);
8562module_exit(megasas_exit);