blob: 34abd5194b0b87209a37b489eea9c9c060a7f432 [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>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070056#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080057#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058#include "megaraid_sas.h"
59
bo yangad84db22007-11-09 04:40:16 -050060/*
Yang, Bo1fd10682010-10-12 07:18:50 -060061 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
63 */
64static unsigned int max_sectors;
65module_param_named(max_sectors, max_sectors, int, 0);
66MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
68
adam radford80d9da92010-12-21 10:17:40 -080069static int msix_disable;
70module_param(msix_disable, int, S_IRUGO);
71MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72
adam radford079eadd2012-10-01 19:26:59 -070073static unsigned int msix_vectors;
74module_param(msix_vectors, int, S_IRUGO);
75MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76
adam radford229fe472014-03-10 02:51:56 -070077static int allow_vf_ioctls;
78module_param(allow_vf_ioctls, int, S_IRUGO);
79MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053081static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070082module_param(throttlequeuedepth, int, S_IRUGO);
83MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
85
Sumit Saxenae3d178c2016-01-28 21:04:34 +053086unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070087module_param(resetwaittime, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070088MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
adam radfordc007b8b2012-07-17 18:20:24 -070089
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053090int smp_affinity_enable = 1;
91module_param(smp_affinity_enable, int, S_IRUGO);
Colin Ian King55d9a1d2018-04-29 13:25:32 +010092MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053093
Sumit Saxena179ac142016-01-28 21:04:28 +053094int rdpq_enable = 1;
95module_param(rdpq_enable, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070096MODULE_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 +053097
Sumit Saxena308ec452016-01-28 21:04:30 +053098unsigned int dual_qdepth_disable;
99module_param(dual_qdepth_disable, int, S_IRUGO);
100MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
101
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530102unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
103module_param(scmd_timeout, int, S_IRUGO);
104MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
105
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400106MODULE_LICENSE("GPL");
107MODULE_VERSION(MEGASAS_VERSION);
Shivasharan S365597c2018-10-16 23:37:43 -0700108MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
109MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400110
adam radford058a8fa2011-10-08 18:14:27 -0700111int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400112static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700113static int megasas_ld_list_query(struct megasas_instance *instance,
114 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400115static int megasas_issue_init_mfi(struct megasas_instance *instance);
116static int megasas_register_aen(struct megasas_instance *instance,
117 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800118static void megasas_get_pd_info(struct megasas_instance *instance,
119 struct scsi_device *sdev);
Shivasharan Se9495e22018-06-04 03:45:12 -0700120
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400121/*
122 * PCI ID table for all supported controllers
123 */
124static struct pci_device_id megasas_pci_table[] = {
125
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
127 /* xscale IOP */
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
129 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
131 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
133 /* gen2*/
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
135 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
137 /* skinny*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
139 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
141 /* xscale IOP, vega */
142 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
143 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
145 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
147 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
149 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
151 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
153 /* Intruder */
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
155 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500158 /* VENTURA */
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Shivasharan S469f72d2018-11-09 09:47:20 -0800165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200169 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400170};
171
172MODULE_DEVICE_TABLE(pci, megasas_pci_table);
173
174static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700175struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100177static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400178
Yang, Boc3518832009-10-06 14:18:02 -0600179static int megasas_poll_wait_aen;
180static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600181static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800182u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600183static u32 support_device_change;
Shivasharan Sf870bcb2018-01-05 05:33:04 -0800184static bool support_nvme_encapsulation;
Sumant Patro658dced2006-10-03 13:09:14 -0700185
Yang, Boc3518832009-10-06 14:18:02 -0600186/* define lock for aen poll */
187spinlock_t poll_aen_lock;
188
adam radford9c915a82010-12-21 13:34:31 -0800189void
bo yang7343eb62007-11-09 04:35:44 -0500190megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
191 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800192static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800193megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
adam radfordebf054b2011-02-24 20:57:15 -0800194static int
195megasas_adp_reset_gen2(struct megasas_instance *instance,
196 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800197static irqreturn_t megasas_isr(int irq, void *devp);
198static u32
199megasas_init_adapter_mfi(struct megasas_instance *instance);
200u32
201megasas_build_and_issue_cmd(struct megasas_instance *instance,
202 struct scsi_cmnd *scmd);
203static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800204int
adam radford229fe472014-03-10 02:51:56 -0700205wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
206 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800207void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700208static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
209 int initial);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700210static int
Shivasharan S107a60d2017-10-19 02:49:05 -0700211megasas_set_dma_mask(struct megasas_instance *instance);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700212static int
213megasas_alloc_ctrl_mem(struct megasas_instance *instance);
214static inline void
215megasas_free_ctrl_mem(struct megasas_instance *instance);
216static inline int
217megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
218static inline void
219megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
220static inline void
221megasas_init_ctrl_params(struct megasas_instance *instance);
adam radfordcd50ba82010-12-21 10:23:23 -0800222
Shivasharan S272652f2018-12-17 00:47:40 -0800223u32 megasas_readl(struct megasas_instance *instance,
224 const volatile void __iomem *addr)
225{
226 u32 i = 0, ret_val;
227 /*
228 * Due to a HW errata in Aero controllers, reads to certain
229 * Fusion registers could intermittently return all zeroes.
230 * This behavior is transient in nature and subsequent reads will
231 * return valid value. As a workaround in driver, retry readl for
232 * upto three times until a non-zero value is read.
233 */
234 if (instance->adapter_type == AERO_SERIES) {
235 do {
236 ret_val = readl(addr);
237 i++;
238 } while (ret_val == 0 && i < 3);
239 return ret_val;
240 } else {
241 return readl(addr);
242 }
243}
244
Shivasharan S107a60d2017-10-19 02:49:05 -0700245/**
246 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
247 * @instance: Adapter soft state
248 * @dcmd: DCMD frame inside MFI command
249 * @dma_addr: DMA address of buffer to be passed to FW
250 * @dma_len: Length of DMA buffer to be passed to FW
251 * @return: void
252 */
253void megasas_set_dma_settings(struct megasas_instance *instance,
254 struct megasas_dcmd_frame *dcmd,
255 dma_addr_t dma_addr, u32 dma_len)
256{
257 if (instance->consistent_mask_64bit) {
258 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
259 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
260 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
261
262 } else {
263 dcmd->sgl.sge32[0].phys_addr =
264 cpu_to_le32(lower_32_bits(dma_addr));
265 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
266 dcmd->flags = cpu_to_le16(dcmd->flags);
267 }
268}
adam radfordcd50ba82010-12-21 10:23:23 -0800269
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800270void
adam radfordcd50ba82010-12-21 10:23:23 -0800271megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
272{
273 instance->instancet->fire_cmd(instance,
274 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800275 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800276}
bo yang7343eb62007-11-09 04:35:44 -0500277
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400278/**
279 * megasas_get_cmd - Get a command from the free pool
280 * @instance: Adapter soft state
281 *
282 * Returns a free command from the pool
283 */
adam radford9c915a82010-12-21 13:34:31 -0800284struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400285 *instance)
286{
287 unsigned long flags;
288 struct megasas_cmd *cmd = NULL;
289
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530290 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400291
292 if (!list_empty(&instance->cmd_pool)) {
293 cmd = list_entry((&instance->cmd_pool)->next,
294 struct megasas_cmd, list);
295 list_del_init(&cmd->list);
296 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500297 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400298 }
299
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530300 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400301 return cmd;
302}
303
304/**
305 * megasas_return_cmd - Return a cmd to free command pool
306 * @instance: Adapter soft state
307 * @cmd: Command packet to be returned to free command pool
308 */
Andi Kleen68b43742017-05-08 15:58:53 -0700309void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400310megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
311{
312 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530313 u32 blk_tags;
314 struct megasas_cmd_fusion *cmd_fusion;
315 struct fusion_context *fusion = instance->ctrl_context;
316
317 /* This flag is used only for fusion adapter.
318 * Wait for Interrupt for Polled mode DCMD
319 */
320 if (cmd->flags & DRV_DCMD_POLLED_MODE)
321 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400322
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530323 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400324
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530325 if (fusion) {
326 blk_tags = instance->max_scsi_cmds + cmd->index;
327 cmd_fusion = fusion->cmd_list[blk_tags];
328 megasas_return_cmd_fusion(instance, cmd_fusion);
329 }
330 cmd->scmd = NULL;
331 cmd->frame_count = 0;
332 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800333 memset(cmd->frame, 0, instance->mfi_frame_size);
334 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530335 if (!fusion && reset_devices)
336 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
337 list_add(&cmd->list, (&instance->cmd_pool)->next);
338
339 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
340
341}
Sumant Patro1341c932006-01-25 12:02:40 -0800342
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530343static const char *
344format_timestamp(uint32_t timestamp)
345{
346 static char buffer[32];
347
348 if ((timestamp & 0xff000000) == 0xff000000)
349 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
350 0x00ffffff);
351 else
352 snprintf(buffer, sizeof(buffer), "%us", timestamp);
353 return buffer;
354}
355
356static const char *
357format_class(int8_t class)
358{
359 static char buffer[6];
360
361 switch (class) {
362 case MFI_EVT_CLASS_DEBUG:
363 return "debug";
364 case MFI_EVT_CLASS_PROGRESS:
365 return "progress";
366 case MFI_EVT_CLASS_INFO:
367 return "info";
368 case MFI_EVT_CLASS_WARNING:
369 return "WARN";
370 case MFI_EVT_CLASS_CRITICAL:
371 return "CRIT";
372 case MFI_EVT_CLASS_FATAL:
373 return "FATAL";
374 case MFI_EVT_CLASS_DEAD:
375 return "DEAD";
376 default:
377 snprintf(buffer, sizeof(buffer), "%d", class);
378 return buffer;
379 }
380}
381
382/**
383 * megasas_decode_evt: Decode FW AEN event and print critical event
384 * for information.
385 * @instance: Adapter soft state
386 */
387static void
388megasas_decode_evt(struct megasas_instance *instance)
389{
390 struct megasas_evt_detail *evt_detail = instance->evt_detail;
391 union megasas_evt_class_locale class_locale;
392 class_locale.word = le32_to_cpu(evt_detail->cl.word);
393
394 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
395 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
396 le32_to_cpu(evt_detail->seq_num),
397 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
398 (class_locale.members.locale),
399 format_class(class_locale.members.class),
400 evt_detail->description);
401}
402
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400403/**
adam radford0d490162010-12-14 19:17:17 -0800404* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800405* (deviceid : 1064R, PERC5) controllers
406*/
407
408/**
409 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400410 * @regs: MFI register set
411 */
412static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530413megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400414{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530415 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500416
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530417 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400418 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400419
420 /* Dummy readl to force pci flush */
421 readl(&regs->outbound_intr_mask);
422}
423
424/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700425 * megasas_disable_intr_xscale -Disables interrupt
426 * @regs: MFI register set
427 */
428static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530429megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700430{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530431 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700432 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500433
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530434 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700435 writel(mask, &regs->outbound_intr_mask);
436 /* Dummy readl to force pci flush */
437 readl(&regs->outbound_intr_mask);
438}
439
440/**
Sumant Patro1341c932006-01-25 12:02:40 -0800441 * megasas_read_fw_status_reg_xscale - returns the current FW status value
442 * @regs: MFI register set
443 */
444static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800445megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800446{
Shivasharan Sde516372018-12-17 00:47:39 -0800447 return readl(&instance->reg_set->outbound_msg_0);
Sumant Patro1341c932006-01-25 12:02:40 -0800448}
449/**
450 * megasas_clear_interrupt_xscale - Check & clear interrupt
451 * @regs: MFI register set
452 */
adam radford0d490162010-12-14 19:17:17 -0800453static int
Shivasharan Sde516372018-12-17 00:47:39 -0800454megasas_clear_intr_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800455{
456 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400457 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800458 struct megasas_register_set __iomem *regs;
459 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500460
Sumant Patro1341c932006-01-25 12:02:40 -0800461 /*
462 * Check if it is our interrupt
463 */
464 status = readl(&regs->outbound_intr_status);
465
bo yang39a98552010-09-22 22:36:29 -0400466 if (status & MFI_OB_INTR_STATUS_MASK)
467 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
468 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
469 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800470
471 /*
472 * Clear the interrupt by writing back the same value
473 */
bo yang39a98552010-09-22 22:36:29 -0400474 if (mfiStatus)
475 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800476
Yang, Bo06f579d2008-08-10 12:42:37 -0700477 /* Dummy readl to force pci flush */
478 readl(&regs->outbound_intr_status);
479
bo yang39a98552010-09-22 22:36:29 -0400480 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800481}
482
483/**
484 * megasas_fire_cmd_xscale - Sends command to the FW
485 * @frame_phys_addr : Physical address of cmd
486 * @frame_count : Number of frames for the command
487 * @regs : MFI register set
488 */
adam radford0d490162010-12-14 19:17:17 -0800489static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600490megasas_fire_cmd_xscale(struct megasas_instance *instance,
491 dma_addr_t frame_phys_addr,
492 u32 frame_count,
493 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800494{
bo yang39a98552010-09-22 22:36:29 -0400495 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500496
bo yang39a98552010-09-22 22:36:29 -0400497 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800498 writel((frame_phys_addr >> 3)|(frame_count),
499 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400500 spin_unlock_irqrestore(&instance->hba_lock, flags);
501}
502
503/**
504 * megasas_adp_reset_xscale - For controller reset
505 * @regs: MFI register set
506 */
507static int
508megasas_adp_reset_xscale(struct megasas_instance *instance,
509 struct megasas_register_set __iomem *regs)
510{
511 u32 i;
512 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500513
bo yang39a98552010-09-22 22:36:29 -0400514 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
515
516 for (i = 0; i < 3; i++)
517 msleep(1000); /* sleep for 3 secs */
518 pcidata = 0;
519 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500520 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400521 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500522 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400523 pcidata &= ~0x2;
524 pci_write_config_dword(instance->pdev,
525 MFI_1068_PCSR_OFFSET, pcidata);
526
527 for (i = 0; i < 2; i++)
528 msleep(1000); /* need to wait 2 secs again */
529
530 pcidata = 0;
531 pci_read_config_dword(instance->pdev,
532 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500533 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400534 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500535 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400536 pcidata = 0;
537 pci_write_config_dword(instance->pdev,
538 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
539 }
540 }
541 return 0;
542}
543
544/**
545 * megasas_check_reset_xscale - For controller reset check
546 * @regs: MFI register set
547 */
548static int
549megasas_check_reset_xscale(struct megasas_instance *instance,
550 struct megasas_register_set __iomem *regs)
551{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530552 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530553 (le32_to_cpu(*instance->consumer) ==
554 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400555 return 1;
bo yang39a98552010-09-22 22:36:29 -0400556 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800557}
558
559static struct megasas_instance_template megasas_instance_template_xscale = {
560
561 .fire_cmd = megasas_fire_cmd_xscale,
562 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700563 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800564 .clear_intr = megasas_clear_intr_xscale,
565 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400566 .adp_reset = megasas_adp_reset_xscale,
567 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800568 .service_isr = megasas_isr,
569 .tasklet = megasas_complete_cmd_dpc,
570 .init_adapter = megasas_init_adapter_mfi,
571 .build_and_issue_cmd = megasas_build_and_issue_cmd,
572 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800573};
574
575/**
adam radford0d490162010-12-14 19:17:17 -0800576* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800577* to xscale (deviceid : 1064R, PERC5) controllers
578*/
579
580/**
adam radford0d490162010-12-14 19:17:17 -0800581* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500582* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800583*/
584
585/**
586 * megasas_enable_intr_ppc - Enables interrupts
587 * @regs: MFI register set
588 */
589static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530590megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800591{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530592 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500593
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530594 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800595 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800596
bo yang39a98552010-09-22 22:36:29 -0400597 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800598
599 /* Dummy readl to force pci flush */
600 readl(&regs->outbound_intr_mask);
601}
602
603/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700604 * megasas_disable_intr_ppc - Disable interrupt
605 * @regs: MFI register set
606 */
607static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530608megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700609{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530610 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700611 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500612
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530613 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700614 writel(mask, &regs->outbound_intr_mask);
615 /* Dummy readl to force pci flush */
616 readl(&regs->outbound_intr_mask);
617}
618
619/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800620 * megasas_read_fw_status_reg_ppc - returns the current FW status value
621 * @regs: MFI register set
622 */
623static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800624megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800625{
Shivasharan Sde516372018-12-17 00:47:39 -0800626 return readl(&instance->reg_set->outbound_scratch_pad_0);
Sumant Patrof9876f02006-02-03 15:34:35 -0800627}
628
629/**
630 * megasas_clear_interrupt_ppc - Check & clear interrupt
631 * @regs: MFI register set
632 */
adam radford0d490162010-12-14 19:17:17 -0800633static int
Shivasharan Sde516372018-12-17 00:47:39 -0800634megasas_clear_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800635{
adam radford3cc6851f92011-05-11 18:34:52 -0700636 u32 status, mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800637 struct megasas_register_set __iomem *regs;
638 regs = instance->reg_set;
adam radford3cc6851f92011-05-11 18:34:52 -0700639
Sumant Patrof9876f02006-02-03 15:34:35 -0800640 /*
641 * Check if it is our interrupt
642 */
643 status = readl(&regs->outbound_intr_status);
644
adam radford3cc6851f92011-05-11 18:34:52 -0700645 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
646 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
647
648 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
649 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800650
651 /*
652 * Clear the interrupt by writing back the same value
653 */
654 writel(status, &regs->outbound_doorbell_clear);
655
Yang, Bo06f579d2008-08-10 12:42:37 -0700656 /* Dummy readl to force pci flush */
657 readl(&regs->outbound_doorbell_clear);
658
adam radford3cc6851f92011-05-11 18:34:52 -0700659 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800660}
adam radford3cc6851f92011-05-11 18:34:52 -0700661
Sumant Patrof9876f02006-02-03 15:34:35 -0800662/**
663 * megasas_fire_cmd_ppc - Sends command to the FW
664 * @frame_phys_addr : Physical address of cmd
665 * @frame_count : Number of frames for the command
666 * @regs : MFI register set
667 */
adam radford0d490162010-12-14 19:17:17 -0800668static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600669megasas_fire_cmd_ppc(struct megasas_instance *instance,
670 dma_addr_t frame_phys_addr,
671 u32 frame_count,
672 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800673{
bo yang39a98552010-09-22 22:36:29 -0400674 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500675
bo yang39a98552010-09-22 22:36:29 -0400676 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800677 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800678 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400679 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800680}
681
bo yang39a98552010-09-22 22:36:29 -0400682/**
bo yang39a98552010-09-22 22:36:29 -0400683 * megasas_check_reset_ppc - For controller reset check
684 * @regs: MFI register set
685 */
686static int
687megasas_check_reset_ppc(struct megasas_instance *instance,
688 struct megasas_register_set __iomem *regs)
689{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530690 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700691 return 1;
692
bo yang39a98552010-09-22 22:36:29 -0400693 return 0;
694}
adam radford3cc6851f92011-05-11 18:34:52 -0700695
Sumant Patrof9876f02006-02-03 15:34:35 -0800696static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800697
Sumant Patrof9876f02006-02-03 15:34:35 -0800698 .fire_cmd = megasas_fire_cmd_ppc,
699 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700700 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800701 .clear_intr = megasas_clear_intr_ppc,
702 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700703 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400704 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800705 .service_isr = megasas_isr,
706 .tasklet = megasas_complete_cmd_dpc,
707 .init_adapter = megasas_init_adapter_mfi,
708 .build_and_issue_cmd = megasas_build_and_issue_cmd,
709 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800710};
711
712/**
Yang, Bo87911122009-10-06 14:31:54 -0600713 * megasas_enable_intr_skinny - Enables interrupts
714 * @regs: MFI register set
715 */
716static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530717megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600718{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530719 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500720
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530721 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600722 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
723
724 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
725
726 /* Dummy readl to force pci flush */
727 readl(&regs->outbound_intr_mask);
728}
729
730/**
731 * megasas_disable_intr_skinny - Disables interrupt
732 * @regs: MFI register set
733 */
734static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530735megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600736{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530737 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600738 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500739
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530740 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600741 writel(mask, &regs->outbound_intr_mask);
742 /* Dummy readl to force pci flush */
743 readl(&regs->outbound_intr_mask);
744}
745
746/**
747 * megasas_read_fw_status_reg_skinny - returns the current FW status value
748 * @regs: MFI register set
749 */
750static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800751megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600752{
Shivasharan Sde516372018-12-17 00:47:39 -0800753 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo87911122009-10-06 14:31:54 -0600754}
755
756/**
757 * megasas_clear_interrupt_skinny - Check & clear interrupt
758 * @regs: MFI register set
759 */
760static int
Shivasharan Sde516372018-12-17 00:47:39 -0800761megasas_clear_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600762{
763 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800764 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800765 struct megasas_register_set __iomem *regs;
766 regs = instance->reg_set;
adam radfordebf054b2011-02-24 20:57:15 -0800767
Yang, Bo87911122009-10-06 14:31:54 -0600768 /*
769 * Check if it is our interrupt
770 */
771 status = readl(&regs->outbound_intr_status);
772
773 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400774 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600775 }
776
777 /*
adam radfordebf054b2011-02-24 20:57:15 -0800778 * Check if it is our interrupt
779 */
Shivasharan Sde516372018-12-17 00:47:39 -0800780 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800781 MFI_STATE_FAULT) {
782 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
783 } else
784 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
785
786 /*
Yang, Bo87911122009-10-06 14:31:54 -0600787 * Clear the interrupt by writing back the same value
788 */
789 writel(status, &regs->outbound_intr_status);
790
791 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500792 * dummy read to flush PCI
793 */
Yang, Bo87911122009-10-06 14:31:54 -0600794 readl(&regs->outbound_intr_status);
795
adam radfordebf054b2011-02-24 20:57:15 -0800796 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600797}
798
799/**
800 * megasas_fire_cmd_skinny - Sends command to the FW
801 * @frame_phys_addr : Physical address of cmd
802 * @frame_count : Number of frames for the command
803 * @regs : MFI register set
804 */
805static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600806megasas_fire_cmd_skinny(struct megasas_instance *instance,
807 dma_addr_t frame_phys_addr,
808 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600809 struct megasas_register_set __iomem *regs)
810{
Yang, Bo0c79e682009-10-06 14:47:35 -0600811 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500812
bo yang39a98552010-09-22 22:36:29 -0400813 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530814 writel(upper_32_bits(frame_phys_addr),
815 &(regs)->inbound_high_queue_port);
816 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
817 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400818 spin_unlock_irqrestore(&instance->hba_lock, flags);
819}
820
821/**
bo yang39a98552010-09-22 22:36:29 -0400822 * megasas_check_reset_skinny - For controller reset check
823 * @regs: MFI register set
824 */
825static int
826megasas_check_reset_skinny(struct megasas_instance *instance,
827 struct megasas_register_set __iomem *regs)
828{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530829 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700830 return 1;
831
bo yang39a98552010-09-22 22:36:29 -0400832 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600833}
834
835static struct megasas_instance_template megasas_instance_template_skinny = {
836
837 .fire_cmd = megasas_fire_cmd_skinny,
838 .enable_intr = megasas_enable_intr_skinny,
839 .disable_intr = megasas_disable_intr_skinny,
840 .clear_intr = megasas_clear_intr_skinny,
841 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800842 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400843 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800844 .service_isr = megasas_isr,
845 .tasklet = megasas_complete_cmd_dpc,
846 .init_adapter = megasas_init_adapter_mfi,
847 .build_and_issue_cmd = megasas_build_and_issue_cmd,
848 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600849};
850
851
852/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700853* The following functions are defined for gen2 (deviceid : 0x78 0x79)
854* controllers
855*/
856
857/**
858 * megasas_enable_intr_gen2 - Enables interrupts
859 * @regs: MFI register set
860 */
861static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530862megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700863{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530864 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500865
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530866 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700867 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
868
869 /* write ~0x00000005 (4 & 1) to the intr mask*/
870 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
871
872 /* Dummy readl to force pci flush */
873 readl(&regs->outbound_intr_mask);
874}
875
876/**
877 * megasas_disable_intr_gen2 - Disables interrupt
878 * @regs: MFI register set
879 */
880static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530881megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700882{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530883 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700884 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500885
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530886 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700887 writel(mask, &regs->outbound_intr_mask);
888 /* Dummy readl to force pci flush */
889 readl(&regs->outbound_intr_mask);
890}
891
892/**
893 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
894 * @regs: MFI register set
895 */
896static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800897megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700898{
Shivasharan Sde516372018-12-17 00:47:39 -0800899 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700900}
901
902/**
903 * megasas_clear_interrupt_gen2 - Check & clear interrupt
904 * @regs: MFI register set
905 */
906static int
Shivasharan Sde516372018-12-17 00:47:39 -0800907megasas_clear_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700908{
909 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400910 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800911 struct megasas_register_set __iomem *regs;
912 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500913
Yang, Bo6610a6b2008-08-10 12:42:38 -0700914 /*
915 * Check if it is our interrupt
916 */
917 status = readl(&regs->outbound_intr_status);
918
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530919 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400920 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
921 }
922 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
923 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
924 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700925
926 /*
927 * Clear the interrupt by writing back the same value
928 */
bo yang39a98552010-09-22 22:36:29 -0400929 if (mfiStatus)
930 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700931
932 /* Dummy readl to force pci flush */
933 readl(&regs->outbound_intr_status);
934
bo yang39a98552010-09-22 22:36:29 -0400935 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700936}
937/**
938 * megasas_fire_cmd_gen2 - Sends command to the FW
939 * @frame_phys_addr : Physical address of cmd
940 * @frame_count : Number of frames for the command
941 * @regs : MFI register set
942 */
943static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600944megasas_fire_cmd_gen2(struct megasas_instance *instance,
945 dma_addr_t frame_phys_addr,
946 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700947 struct megasas_register_set __iomem *regs)
948{
bo yang39a98552010-09-22 22:36:29 -0400949 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500950
bo yang39a98552010-09-22 22:36:29 -0400951 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700952 writel((frame_phys_addr | (frame_count<<1))|1,
953 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400954 spin_unlock_irqrestore(&instance->hba_lock, flags);
955}
956
957/**
958 * megasas_adp_reset_gen2 - For controller reset
959 * @regs: MFI register set
960 */
961static int
962megasas_adp_reset_gen2(struct megasas_instance *instance,
963 struct megasas_register_set __iomem *reg_set)
964{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500965 u32 retry = 0 ;
966 u32 HostDiag;
967 u32 __iomem *seq_offset = &reg_set->seq_offset;
968 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400969
adam radfordebf054b2011-02-24 20:57:15 -0800970 if (instance->instancet == &megasas_instance_template_skinny) {
971 seq_offset = &reg_set->fusion_seq_offset;
972 hostdiag_offset = &reg_set->fusion_host_diag;
973 }
974
975 writel(0, seq_offset);
976 writel(4, seq_offset);
977 writel(0xb, seq_offset);
978 writel(2, seq_offset);
979 writel(7, seq_offset);
980 writel(0xd, seq_offset);
981
bo yang39a98552010-09-22 22:36:29 -0400982 msleep(1000);
983
adam radfordebf054b2011-02-24 20:57:15 -0800984 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400985
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500986 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400987 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800988 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500989 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400990 retry, HostDiag);
991
992 if (retry++ >= 100)
993 return 1;
994
995 }
996
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500997 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400998
adam radfordebf054b2011-02-24 20:57:15 -0800999 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -04001000
1001 ssleep(10);
1002
adam radfordebf054b2011-02-24 20:57:15 -08001003 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001004 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -04001005 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -08001006 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001007 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -04001008 retry, HostDiag);
1009
1010 if (retry++ >= 1000)
1011 return 1;
1012
1013 }
1014 return 0;
1015}
1016
1017/**
1018 * megasas_check_reset_gen2 - For controller reset check
1019 * @regs: MFI register set
1020 */
1021static int
1022megasas_check_reset_gen2(struct megasas_instance *instance,
1023 struct megasas_register_set __iomem *regs)
1024{
Sumit Saxena8a01a412016-01-28 21:04:32 +05301025 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -06001026 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -06001027
bo yang39a98552010-09-22 22:36:29 -04001028 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -07001029}
1030
1031static struct megasas_instance_template megasas_instance_template_gen2 = {
1032
1033 .fire_cmd = megasas_fire_cmd_gen2,
1034 .enable_intr = megasas_enable_intr_gen2,
1035 .disable_intr = megasas_disable_intr_gen2,
1036 .clear_intr = megasas_clear_intr_gen2,
1037 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -04001038 .adp_reset = megasas_adp_reset_gen2,
1039 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -08001040 .service_isr = megasas_isr,
1041 .tasklet = megasas_complete_cmd_dpc,
1042 .init_adapter = megasas_init_adapter_mfi,
1043 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1044 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -07001045};
1046
1047/**
Sumant Patrof9876f02006-02-03 15:34:35 -08001048* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -04001049* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -08001050*/
1051
adam radford9c915a82010-12-21 13:34:31 -08001052/*
1053 * Template added for TB (Fusion)
1054 */
1055extern struct megasas_instance_template megasas_instance_template_fusion;
1056
Sumant Patrof9876f02006-02-03 15:34:35 -08001057/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001058 * megasas_issue_polled - Issues a polling command
1059 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -08001060 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001061 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301062 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063 */
adam radford9c915a82010-12-21 13:34:31 -08001064int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001065megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1066{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001067 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1068
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301069 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301070 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001071
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001072 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301073 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1074 __func__, __LINE__);
1075 return DCMD_NOT_FIRED;
1076 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001077
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001078 instance->instancet->issue_dcmd(instance, cmd);
1079
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301080 return wait_and_poll(instance, cmd, instance->requestorId ?
1081 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001082}
1083
1084/**
1085 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1086 * @instance: Adapter soft state
1087 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301088 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001089 *
1090 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001091 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001092 * Used to issue ioctl commands.
1093 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301094int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001095megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301096 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001097{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301098 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301099 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001100
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001101 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301102 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1103 __func__, __LINE__);
1104 return DCMD_NOT_FIRED;
1105 }
1106
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001107 instance->instancet->issue_dcmd(instance, cmd);
1108
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301109 if (timeout) {
1110 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301111 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301112 if (!ret) {
1113 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1114 __func__, __LINE__);
1115 return DCMD_TIMEOUT;
1116 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301117 } else
1118 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301119 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001120
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301121 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301122 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001123}
1124
1125/**
1126 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1127 * @instance: Adapter soft state
1128 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301129 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001130 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301131 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001132 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001133 * cmd and waits for return status.
1134 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135 */
1136static int
1137megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301138 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001139{
1140 struct megasas_cmd *cmd;
1141 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301142 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001143
1144 cmd = megasas_get_cmd(instance);
1145
1146 if (!cmd)
1147 return -1;
1148
1149 abort_fr = &cmd->frame->abort;
1150
1151 /*
1152 * Prepare and issue the abort frame
1153 */
1154 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301155 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301156 abort_fr->flags = cpu_to_le16(0);
1157 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1158 abort_fr->abort_mfi_phys_addr_lo =
1159 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1160 abort_fr->abort_mfi_phys_addr_hi =
1161 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001162
1163 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301164 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001165
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001166 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301167 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1168 __func__, __LINE__);
1169 return DCMD_NOT_FIRED;
1170 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001172 instance->instancet->issue_dcmd(instance, cmd);
1173
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301174 if (timeout) {
1175 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301176 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301177 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301178 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1179 __func__, __LINE__);
1180 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301181 }
1182 } else
1183 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301184 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301185
bo yang39a98552010-09-22 22:36:29 -04001186 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001187
1188 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301189 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1190 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001191}
1192
1193/**
1194 * megasas_make_sgl32 - Prepares 32-bit SGL
1195 * @instance: Adapter soft state
1196 * @scp: SCSI command from the mid-layer
1197 * @mfi_sgl: SGL to be filled in
1198 *
1199 * If successful, this function returns the number of SG elements. Otherwise,
1200 * it returnes -1.
1201 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001202static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001203megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1204 union megasas_sgl *mfi_sgl)
1205{
1206 int i;
1207 int sge_count;
1208 struct scatterlist *os_sgl;
1209
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001210 sge_count = scsi_dma_map(scp);
1211 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001212
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001213 if (sge_count) {
1214 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301215 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1216 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001217 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001218 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001219 return sge_count;
1220}
1221
1222/**
1223 * megasas_make_sgl64 - Prepares 64-bit SGL
1224 * @instance: Adapter soft state
1225 * @scp: SCSI command from the mid-layer
1226 * @mfi_sgl: SGL to be filled in
1227 *
1228 * If successful, this function returns the number of SG elements. Otherwise,
1229 * it returnes -1.
1230 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001231static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001232megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1233 union megasas_sgl *mfi_sgl)
1234{
1235 int i;
1236 int sge_count;
1237 struct scatterlist *os_sgl;
1238
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001239 sge_count = scsi_dma_map(scp);
1240 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001241
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001242 if (sge_count) {
1243 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301244 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1245 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001246 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001247 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001248 return sge_count;
1249}
1250
Yang, Bof4c9a132009-10-06 14:43:28 -06001251/**
1252 * megasas_make_sgl_skinny - Prepares IEEE SGL
1253 * @instance: Adapter soft state
1254 * @scp: SCSI command from the mid-layer
1255 * @mfi_sgl: SGL to be filled in
1256 *
1257 * If successful, this function returns the number of SG elements. Otherwise,
1258 * it returnes -1.
1259 */
1260static int
1261megasas_make_sgl_skinny(struct megasas_instance *instance,
1262 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1263{
1264 int i;
1265 int sge_count;
1266 struct scatterlist *os_sgl;
1267
1268 sge_count = scsi_dma_map(scp);
1269
1270 if (sge_count) {
1271 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301272 mfi_sgl->sge_skinny[i].length =
1273 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001274 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301275 cpu_to_le64(sg_dma_address(os_sgl));
1276 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001277 }
1278 }
1279 return sge_count;
1280}
1281
Sumant Patrob1df99d2006-10-03 12:40:47 -07001282 /**
1283 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001284 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001285 * @sge_count : number of sg elements
1286 *
1287 * Returns the number of frames required for numnber of sge's (sge_count)
1288 */
1289
Yang, Bof4c9a132009-10-06 14:43:28 -06001290static u32 megasas_get_frame_count(struct megasas_instance *instance,
1291 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001292{
1293 int num_cnt;
1294 int sge_bytes;
1295 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001296 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001297
1298 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1299 sizeof(struct megasas_sge32);
1300
Yang, Bof4c9a132009-10-06 14:43:28 -06001301 if (instance->flag_ieee) {
1302 sge_sz = sizeof(struct megasas_sge_skinny);
1303 }
1304
Sumant Patrob1df99d2006-10-03 12:40:47 -07001305 /*
bo yangd532dbe2008-03-17 03:36:43 -04001306 * Main frame can contain 2 SGEs for 64-bit SGLs and
1307 * 3 SGEs for 32-bit SGLs for ldio &
1308 * 1 SGEs for 64-bit SGLs and
1309 * 2 SGEs for 32-bit SGLs for pthru frame
1310 */
1311 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001312 if (instance->flag_ieee == 1) {
1313 num_cnt = sge_count - 1;
1314 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001315 num_cnt = sge_count - 1;
1316 else
1317 num_cnt = sge_count - 2;
1318 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001319 if (instance->flag_ieee == 1) {
1320 num_cnt = sge_count - 1;
1321 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001322 num_cnt = sge_count - 2;
1323 else
1324 num_cnt = sge_count - 3;
1325 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001326
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001327 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001328 sge_bytes = sge_sz * num_cnt;
1329
1330 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1331 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1332 }
1333 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001334 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001335
1336 if (frame_count > 7)
1337 frame_count = 8;
1338 return frame_count;
1339}
1340
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001341/**
1342 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1343 * @instance: Adapter soft state
1344 * @scp: SCSI command
1345 * @cmd: Command to be prepared in
1346 *
1347 * This function prepares CDB commands. These are typcially pass-through
1348 * commands to the devices.
1349 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001350static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001351megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1352 struct megasas_cmd *cmd)
1353{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001354 u32 is_logical;
1355 u32 device_id;
1356 u16 flags = 0;
1357 struct megasas_pthru_frame *pthru;
1358
Shivasharan S3cabd162017-02-10 00:59:05 -08001359 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301360 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001361 pthru = (struct megasas_pthru_frame *)cmd->frame;
1362
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001363 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001364 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001365 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001366 flags = MFI_FRAME_DIR_READ;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001367 else if (scp->sc_data_direction == DMA_NONE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001368 flags = MFI_FRAME_DIR_NONE;
1369
Yang, Bof4c9a132009-10-06 14:43:28 -06001370 if (instance->flag_ieee == 1) {
1371 flags |= MFI_FRAME_IEEE;
1372 }
1373
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001374 /*
1375 * Prepare the DCDB frame
1376 */
1377 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1378 pthru->cmd_status = 0x0;
1379 pthru->scsi_status = 0x0;
1380 pthru->target_id = device_id;
1381 pthru->lun = scp->device->lun;
1382 pthru->cdb_len = scp->cmd_len;
1383 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001384 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301385 pthru->flags = cpu_to_le16(flags);
1386 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001387
1388 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1389
1390 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001391 * If the command is for the tape device, set the
1392 * pthru timeout to the os layer timeout value.
1393 */
Yang, Bo8d568252009-10-06 14:12:21 -06001394 if (scp->device->type == TYPE_TAPE) {
1395 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301396 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001397 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301398 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001399 }
1400
1401 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001402 * Construct SGL
1403 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001404 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301405 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001406 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1407 &pthru->sgl);
1408 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301409 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001410 pthru->sge_count = megasas_make_sgl64(instance, scp,
1411 &pthru->sgl);
1412 } else
1413 pthru->sge_count = megasas_make_sgl32(instance, scp,
1414 &pthru->sgl);
1415
Yang, Bobdc6fb82009-12-06 08:30:19 -07001416 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001417 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001418 pthru->sge_count);
1419 return 0;
1420 }
1421
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001422 /*
1423 * Sense info specific
1424 */
1425 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301426 pthru->sense_buf_phys_addr_hi =
1427 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1428 pthru->sense_buf_phys_addr_lo =
1429 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001430
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001431 /*
1432 * Compute the total number of frames this command consumes. FW uses
1433 * this number to pull sufficient number of frames from host memory.
1434 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001435 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001436 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001437
1438 return cmd->frame_count;
1439}
1440
1441/**
1442 * megasas_build_ldio - Prepares IOs to logical devices
1443 * @instance: Adapter soft state
1444 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001445 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001446 *
1447 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1448 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001449static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001450megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1451 struct megasas_cmd *cmd)
1452{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001453 u32 device_id;
1454 u8 sc = scp->cmnd[0];
1455 u16 flags = 0;
1456 struct megasas_io_frame *ldio;
1457
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301458 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459 ldio = (struct megasas_io_frame *)cmd->frame;
1460
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001461 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001462 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001463 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464 flags = MFI_FRAME_DIR_READ;
1465
Yang, Bof4c9a132009-10-06 14:43:28 -06001466 if (instance->flag_ieee == 1) {
1467 flags |= MFI_FRAME_IEEE;
1468 }
1469
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001470 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001471 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001472 */
1473 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1474 ldio->cmd_status = 0x0;
1475 ldio->scsi_status = 0x0;
1476 ldio->target_id = device_id;
1477 ldio->timeout = 0;
1478 ldio->reserved_0 = 0;
1479 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301480 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001481 ldio->start_lba_hi = 0;
1482 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1483
1484 /*
1485 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1486 */
1487 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301488 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1489 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1490 ((u32) scp->cmnd[2] << 8) |
1491 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001492
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301493 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001494 }
1495
1496 /*
1497 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1498 */
1499 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301500 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1501 ((u32) scp->cmnd[7] << 8));
1502 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1503 ((u32) scp->cmnd[3] << 16) |
1504 ((u32) scp->cmnd[4] << 8) |
1505 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506 }
1507
1508 /*
1509 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1510 */
1511 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301512 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1513 ((u32) scp->cmnd[7] << 16) |
1514 ((u32) scp->cmnd[8] << 8) |
1515 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001516
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301517 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1518 ((u32) scp->cmnd[3] << 16) |
1519 ((u32) scp->cmnd[4] << 8) |
1520 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001521 }
1522
1523 /*
1524 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1525 */
1526 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301527 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1528 ((u32) scp->cmnd[11] << 16) |
1529 ((u32) scp->cmnd[12] << 8) |
1530 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001531
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301532 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1533 ((u32) scp->cmnd[7] << 16) |
1534 ((u32) scp->cmnd[8] << 8) |
1535 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001536
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301537 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1538 ((u32) scp->cmnd[3] << 16) |
1539 ((u32) scp->cmnd[4] << 8) |
1540 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001541
1542 }
1543
1544 /*
1545 * Construct SGL
1546 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001547 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301548 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001549 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1550 &ldio->sgl);
1551 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301552 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001553 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1554 } else
1555 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1556
Yang, Bobdc6fb82009-12-06 08:30:19 -07001557 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001558 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001559 ldio->sge_count);
1560 return 0;
1561 }
1562
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001563 /*
1564 * Sense info specific
1565 */
1566 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1567 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301568 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001569
Sumant Patrob1df99d2006-10-03 12:40:47 -07001570 /*
1571 * Compute the total number of frames this command consumes. FW uses
1572 * this number to pull sufficient number of frames from host memory.
1573 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001574 cmd->frame_count = megasas_get_frame_count(instance,
1575 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001576
1577 return cmd->frame_count;
1578}
1579
1580/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301581 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1582 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001583 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001584 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001585 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301586inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001587{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301588 int ret;
1589
Sumant Patrocb59aa62006-01-25 11:53:25 -08001590 switch (cmd->cmnd[0]) {
1591 case READ_10:
1592 case WRITE_10:
1593 case READ_12:
1594 case WRITE_12:
1595 case READ_6:
1596 case WRITE_6:
1597 case READ_16:
1598 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001599 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301600 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1601 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001602 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001603 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301604 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001605 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301606 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001607}
1608
Sumant Patro658dced2006-10-03 13:09:14 -07001609 /**
1610 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001611 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001612 * @instance: Adapter soft state
1613 */
1614static inline void
1615megasas_dump_pending_frames(struct megasas_instance *instance)
1616{
1617 struct megasas_cmd *cmd;
1618 int i,n;
1619 union megasas_sgl *mfi_sgl;
1620 struct megasas_io_frame *ldio;
1621 struct megasas_pthru_frame *pthru;
1622 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001623 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001624
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001625 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1626 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 -07001627 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001628 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 -07001629 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001630 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 -07001631
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001632 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001633 for (i = 0; i < max_cmd; i++) {
1634 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001635 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001636 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001637 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 +05301638 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001639 ldio = (struct megasas_io_frame *)cmd->frame;
1640 mfi_sgl = &ldio->sgl;
1641 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001642 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 +05301643 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1644 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1645 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1646 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001647 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001648 pthru = (struct megasas_pthru_frame *) cmd->frame;
1649 mfi_sgl = &pthru->sgl;
1650 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001651 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 +05301652 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1653 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1654 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1655 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001656 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001657 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1658 for (n = 0; n < sgcount; n++) {
1659 if (IS_DMA64)
1660 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1661 le32_to_cpu(mfi_sgl->sge64[n].length),
1662 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1663 else
1664 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1665 le32_to_cpu(mfi_sgl->sge32[n].length),
1666 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001667 }
1668 }
Sumant Patro658dced2006-10-03 13:09:14 -07001669 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001670 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001671 for (i = 0; i < max_cmd; i++) {
1672
1673 cmd = instance->cmd_list[i];
1674
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001675 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001676 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001677 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001678 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001679}
1680
adam radfordcd50ba82010-12-21 10:23:23 -08001681u32
1682megasas_build_and_issue_cmd(struct megasas_instance *instance,
1683 struct scsi_cmnd *scmd)
1684{
1685 struct megasas_cmd *cmd;
1686 u32 frame_count;
1687
1688 cmd = megasas_get_cmd(instance);
1689 if (!cmd)
1690 return SCSI_MLQUEUE_HOST_BUSY;
1691
1692 /*
1693 * Logical drive command
1694 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301695 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001696 frame_count = megasas_build_ldio(instance, scmd, cmd);
1697 else
1698 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1699
1700 if (!frame_count)
1701 goto out_return_cmd;
1702
1703 cmd->scmd = scmd;
1704 scmd->SCp.ptr = (char *)cmd;
1705
1706 /*
1707 * Issue the command to the FW
1708 */
1709 atomic_inc(&instance->fw_outstanding);
1710
1711 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1712 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001713
1714 return 0;
1715out_return_cmd:
1716 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301717 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001718}
1719
1720
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001721/**
1722 * megasas_queue_command - Queue entry point
1723 * @scmd: SCSI command to be queued
1724 * @done: Callback entry point
1725 */
1726static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301727megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001728{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001729 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301730 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001731
1732 instance = (struct megasas_instance *)
1733 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001734
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301735 if (instance->unload == 1) {
1736 scmd->result = DID_NO_CONNECT << 16;
1737 scmd->scsi_done(scmd);
1738 return 0;
1739 }
1740
bo yang39a98552010-09-22 22:36:29 -04001741 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001742 return SCSI_MLQUEUE_HOST_BUSY;
1743
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301744
adam radford229fe472014-03-10 02:51:56 -07001745 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301746 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001747 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001748 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001749 return SCSI_MLQUEUE_HOST_BUSY;
1750 } else {
adam radford229fe472014-03-10 02:51:56 -07001751 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301752 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001753 return 0;
1754 }
1755 }
1756
Sumit Saxena8a01a412016-01-28 21:04:32 +05301757 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001758 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301759 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301760 return 0;
1761 }
1762
Sumit Saxena18365b12016-01-28 21:04:25 +05301763 mr_device_priv_data = scmd->device->hostdata;
1764 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301765 scmd->result = DID_NO_CONNECT << 16;
1766 scmd->scsi_done(scmd);
1767 return 0;
1768 }
1769
Sumit Saxena8a01a412016-01-28 21:04:32 +05301770 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001771 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001772
Sumit Saxena8a01a412016-01-28 21:04:32 +05301773 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301774 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301775
bo yang39a98552010-09-22 22:36:29 -04001776
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001777 scmd->result = 0;
1778
Shivasharan S3cabd162017-02-10 00:59:05 -08001779 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301780 (scmd->device->id >= instance->fw_supported_vd_count ||
1781 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001782 scmd->result = DID_BAD_TARGET << 16;
1783 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001784 }
1785
Shivasharan S3cabd162017-02-10 00:59:05 -08001786 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1787 MEGASAS_IS_LOGICAL(scmd->device) &&
1788 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001789 scmd->result = DID_OK << 16;
1790 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001791 }
1792
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301793 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001794
Sumant Patrocb59aa62006-01-25 11:53:25 -08001795 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301796 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001797 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001798}
1799
Yang, Bo044833b2009-10-06 14:33:06 -06001800static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1801{
1802 int i;
1803
1804 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1805
1806 if ((megasas_mgmt_info.instance[i]) &&
1807 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1808 return megasas_mgmt_info.instance[i];
1809 }
1810
1811 return NULL;
1812}
1813
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301814/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001815* megasas_set_dynamic_target_properties -
1816* Device property set by driver may not be static and it is required to be
1817* updated after OCR
1818*
1819* set tm_capable.
1820* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301821*
1822* @sdev: OS provided scsi device
1823*
1824* Returns void
1825*/
Shivasharan Se9495e22018-06-04 03:45:12 -07001826void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1827 bool is_target_prop)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301828{
Shivasharan S15dd0382017-02-10 00:59:10 -08001829 u16 pd_index = 0, ld;
1830 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301831 struct megasas_instance *instance;
1832 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301833 struct MR_PRIV_DEVICE *mr_device_priv_data;
1834 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301835 struct MR_LD_RAID *raid;
1836 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1837
1838 instance = megasas_lookup_instance(sdev->host->host_no);
1839 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301840 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301841
Shivasharan Sed981b82017-02-10 00:59:06 -08001842 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301843 return;
1844
Shivasharan Sed981b82017-02-10 00:59:06 -08001845 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301846 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1847 + sdev->id;
1848 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1849 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001850 if (ld >= instance->fw_supported_vd_count)
1851 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301852 raid = MR_LdRaidGet(ld, local_map_ptr);
1853
1854 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001855 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001856
Sumit Saxena18365b12016-01-28 21:04:25 +05301857 mr_device_priv_data->is_tm_capable =
1858 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001859 } else if (instance->use_seqnum_jbod_fp) {
1860 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001861 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001862 pd_sync = (void *)fusion->pd_seq_sync
1863 [(instance->pd_seq_map_id - 1) & 1];
1864 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001865 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301866 }
Shivasharan Se9495e22018-06-04 03:45:12 -07001867
1868 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1869 /*
1870 * If FW provides a target reset timeout value, driver will use
1871 * it. If not set, fallback to default values.
1872 */
1873 mr_device_priv_data->target_reset_tmo =
1874 min_t(u8, instance->max_reset_tmo,
1875 instance->tgt_prop->reset_tmo);
1876 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1877 } else {
1878 mr_device_priv_data->target_reset_tmo =
1879 MEGASAS_DEFAULT_TM_TIMEOUT;
1880 mr_device_priv_data->task_abort_tmo =
1881 MEGASAS_DEFAULT_TM_TIMEOUT;
1882 }
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301883}
1884
Shivasharan S15dd0382017-02-10 00:59:10 -08001885/*
1886 * megasas_set_nvme_device_properties -
1887 * set nomerges=2
1888 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1889 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1890 *
1891 * MR firmware provides value in KB. Caller of this function converts
1892 * kb into bytes.
1893 *
1894 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1895 * MR firmware provides value 128 as (32 * 4K) = 128K.
1896 *
1897 * @sdev: scsi device
1898 * @max_io_size: maximum io transfer size
1899 *
1900 */
1901static inline void
1902megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301903{
Sumit Saxena2216c302016-01-28 21:04:26 +05301904 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001905 u32 mr_nvme_pg_size;
1906
1907 instance = (struct megasas_instance *)sdev->host->hostdata;
1908 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1909 MR_DEFAULT_NVME_PAGE_SIZE);
1910
1911 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1912
Bart Van Assche8b904b52018-03-07 17:10:10 -08001913 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
Shivasharan S15dd0382017-02-10 00:59:10 -08001914 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1915}
1916
1917
1918/*
1919 * megasas_set_static_target_properties -
1920 * Device property set by driver are static and it is not required to be
1921 * updated after OCR.
1922 *
1923 * set io timeout
1924 * set device queue depth
1925 * set nvme device properties. see - megasas_set_nvme_device_properties
1926 *
1927 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001928 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001929 */
Shivasharan S96188a82017-02-10 00:59:11 -08001930static void megasas_set_static_target_properties(struct scsi_device *sdev,
1931 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001932{
Shivasharan S15dd0382017-02-10 00:59:10 -08001933 u8 interface_type;
1934 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1935 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001936 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001937 struct megasas_instance *instance;
1938 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301939
1940 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001941 mr_device_priv_data = sdev->hostdata;
1942 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301943
Shivasharan S15dd0382017-02-10 00:59:10 -08001944 /*
1945 * The RAID firmware may require extended timeouts.
1946 */
1947 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301948
Shivasharan S15dd0382017-02-10 00:59:10 -08001949 switch (interface_type) {
1950 case SAS_PD:
1951 device_qd = MEGASAS_SAS_QD;
1952 break;
1953 case SATA_PD:
1954 device_qd = MEGASAS_SATA_QD;
1955 break;
1956 case NVME_PD:
1957 device_qd = MEGASAS_NVME_QD;
1958 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301959 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001960
Shivasharan S96188a82017-02-10 00:59:11 -08001961 if (is_target_prop) {
1962 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1963 if (tgt_device_qd &&
1964 (tgt_device_qd <= instance->host->can_queue))
1965 device_qd = tgt_device_qd;
1966
1967 /* max_io_size_kb will be set to non zero for
1968 * nvme based vd and syspd.
1969 */
1970 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1971 }
1972
Shivasharan S15dd0382017-02-10 00:59:10 -08001973 if (instance->nvme_page_size && max_io_size_kb)
1974 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1975
1976 scsi_change_queue_depth(sdev, device_qd);
1977
Sumit Saxena2216c302016-01-28 21:04:26 +05301978}
1979
Sumit Saxena18365b12016-01-28 21:04:25 +05301980
Christoph Hellwig147aab62006-02-17 12:13:48 +01001981static int megasas_slave_configure(struct scsi_device *sdev)
1982{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301983 u16 pd_index = 0;
1984 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001985 int ret_target_prop = DCMD_FAILED;
1986 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301987
1988 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001989 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001990 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301991 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1992 sdev->id;
1993 if (instance->pd_list[pd_index].driveState !=
1994 MR_PD_STATE_SYSTEM)
1995 return -ENXIO;
1996 }
1997 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301998
Shivasharan S149c5752018-01-05 05:27:42 -08001999 mutex_lock(&instance->reset_mutex);
Shivasharan S15dd0382017-02-10 00:59:10 -08002000 /* Send DCMD to Firmware and cache the information */
2001 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2002 megasas_get_pd_info(instance, sdev);
2003
Shivasharan S96188a82017-02-10 00:59:11 -08002004 /* Some ventura firmware may not have instance->nvme_page_size set.
2005 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2006 */
2007 if ((instance->tgt_prop) && (instance->nvme_page_size))
2008 ret_target_prop = megasas_get_target_prop(instance, sdev);
2009
2010 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2011 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08002012
Shivasharan S15dd0382017-02-10 00:59:10 -08002013 /* This sdev property may change post OCR */
Shivasharan Se9495e22018-06-04 03:45:12 -07002014 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2015
2016 mutex_unlock(&instance->reset_mutex);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05302017
Yang, Bo044833b2009-10-06 14:33:06 -06002018 return 0;
2019}
2020
2021static int megasas_slave_alloc(struct scsi_device *sdev)
2022{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002023 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06002024 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05302025 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002026
Yang, Bo044833b2009-10-06 14:33:06 -06002027 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08002028 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06002029 /*
2030 * Open the OS scan to the SYSTEM PD
2031 */
2032 pd_index =
2033 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2034 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08002035 if ((instance->pd_list_not_supported ||
2036 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302037 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05302038 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06002039 }
2040 return -ENXIO;
2041 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302042
2043scan_target:
2044 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2045 GFP_KERNEL);
2046 if (!mr_device_priv_data)
2047 return -ENOMEM;
2048 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08002049
2050 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2051 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01002052 return 0;
2053}
2054
Sumit Saxena18365b12016-01-28 21:04:25 +05302055static void megasas_slave_destroy(struct scsi_device *sdev)
2056{
2057 kfree(sdev->hostdata);
2058 sdev->hostdata = NULL;
2059}
2060
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302061/*
2062* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2063* kill adapter
2064* @instance: Adapter soft state
2065*
2066*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05302067static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302068{
2069 int i;
2070 struct megasas_cmd *cmd_mfi;
2071 struct megasas_cmd_fusion *cmd_fusion;
2072 struct fusion_context *fusion = instance->ctrl_context;
2073
2074 /* Find all outstanding ioctls */
2075 if (fusion) {
2076 for (i = 0; i < instance->max_fw_cmds; i++) {
2077 cmd_fusion = fusion->cmd_list[i];
2078 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2079 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2080 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002081 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2082 cmd_mfi->frame->hdr.cmd_status =
2083 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302084 megasas_complete_cmd(instance,
2085 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002086 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302087 }
2088 }
2089 } else {
2090 for (i = 0; i < instance->max_fw_cmds; i++) {
2091 cmd_mfi = instance->cmd_list[i];
2092 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2093 MFI_CMD_ABORT)
2094 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2095 }
2096 }
2097}
2098
2099
adam radford9c915a82010-12-21 13:34:31 -08002100void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002101{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302102 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302103 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302104 /* Wait 1 second to ensure IO or ioctls in build have posted */
2105 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002106 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302107 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002108 (instance->adapter_type != MFI_SERIES)) {
Shivasharan S5acad9b2018-10-16 23:37:47 -07002109 if (!instance->requestorId) {
2110 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2111 /* Flush */
2112 readl(&instance->reg_set->doorbell);
2113 }
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302114 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002115 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002116 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302117 writel(MFI_STOP_ADP,
2118 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002119 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302120 /* Complete outstanding ioctls when adapter is killed */
2121 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002122}
2123
2124 /**
2125 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2126 * restored to max value
2127 * @instance: Adapter soft state
2128 *
2129 */
2130void
2131megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2132{
2133 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302134
adam radford9c915a82010-12-21 13:34:31 -08002135 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002136 && time_after(jiffies, instance->last_time + 5 * HZ)
2137 && atomic_read(&instance->fw_outstanding) <
2138 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002139
2140 spin_lock_irqsave(instance->host->host_lock, flags);
2141 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002142
Sumit Saxena308ec452016-01-28 21:04:30 +05302143 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002144 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002145 }
2146}
2147
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002148/**
bo yang7343eb62007-11-09 04:35:44 -05002149 * megasas_complete_cmd_dpc - Returns FW's controller structure
2150 * @instance_addr: Address of adapter soft state
2151 *
2152 * Tasklet to complete cmds
2153 */
2154static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2155{
2156 u32 producer;
2157 u32 consumer;
2158 u32 context;
2159 struct megasas_cmd *cmd;
2160 struct megasas_instance *instance =
2161 (struct megasas_instance *)instance_addr;
2162 unsigned long flags;
2163
2164 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302165 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002166 return;
2167
2168 spin_lock_irqsave(&instance->completion_lock, flags);
2169
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302170 producer = le32_to_cpu(*instance->producer);
2171 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002172
2173 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302174 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002175 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002176 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002177 context);
2178 BUG();
2179 }
bo yang7343eb62007-11-09 04:35:44 -05002180
2181 cmd = instance->cmd_list[context];
2182
2183 megasas_complete_cmd(instance, cmd, DID_OK);
2184
2185 consumer++;
2186 if (consumer == (instance->max_fw_cmds + 1)) {
2187 consumer = 0;
2188 }
2189 }
2190
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302191 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002192
2193 spin_unlock_irqrestore(&instance->completion_lock, flags);
2194
2195 /*
2196 * Check if we can restore can_queue
2197 */
adam radford9c915a82010-12-21 13:34:31 -08002198 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002199}
2200
Kees Cookc251a7b2017-10-22 15:30:04 -07002201static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2202
adam radford229fe472014-03-10 02:51:56 -07002203/**
Kees Cookc251a7b2017-10-22 15:30:04 -07002204 * megasas_start_timer - Initializes sriov heartbeat timer object
adam radford229fe472014-03-10 02:51:56 -07002205 * @instance: Adapter soft state
adam radford229fe472014-03-10 02:51:56 -07002206 *
2207 */
Kees Cookc251a7b2017-10-22 15:30:04 -07002208void megasas_start_timer(struct megasas_instance *instance)
adam radford229fe472014-03-10 02:51:56 -07002209{
Kees Cookc251a7b2017-10-22 15:30:04 -07002210 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2211
2212 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2213 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
adam radford229fe472014-03-10 02:51:56 -07002214 add_timer(timer);
2215}
2216
Yang, Bo707e09b2010-10-12 07:20:27 -06002217static void
2218megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2219
2220static void
2221process_fw_state_change_wq(struct work_struct *work);
2222
2223void megasas_do_ocr(struct megasas_instance *instance)
2224{
2225 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2226 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2227 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302228 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002229 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302230 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302231 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002232 instance->issuepend_done = 0;
2233
2234 atomic_set(&instance->fw_outstanding, 0);
2235 megasas_internal_reset_defer_cmds(instance);
2236 process_fw_state_change_wq(&instance->work_init);
2237}
2238
Adam Radford4cbfea82014-07-09 15:17:56 -07002239static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2240 int initial)
adam radford229fe472014-03-10 02:51:56 -07002241{
2242 struct megasas_cmd *cmd;
2243 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002244 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002245 dma_addr_t new_affiliation_111_h;
2246 int ld, retval = 0;
2247 u8 thisVf;
2248
2249 cmd = megasas_get_cmd(instance);
2250
2251 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002252 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2253 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002254 instance->host->host_no);
2255 return -ENOMEM;
2256 }
2257
2258 dcmd = &cmd->frame->dcmd;
2259
Adam Radford4cbfea82014-07-09 15:17:56 -07002260 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002261 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2262 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002263 megasas_return_cmd(instance, cmd);
2264 return -ENOMEM;
2265 }
2266
2267 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002268 memset(instance->vf_affiliation_111, 0,
2269 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002270 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002271 new_affiliation_111 =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002272 dma_alloc_coherent(&instance->pdev->dev,
2273 sizeof(struct MR_LD_VF_AFFILIATION_111),
2274 &new_affiliation_111_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002275 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002276 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2277 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002278 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002279 megasas_return_cmd(instance, cmd);
2280 return -ENOMEM;
2281 }
adam radford229fe472014-03-10 02:51:56 -07002282 }
2283
2284 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2285
2286 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302287 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002288 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302289 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002290 dcmd->timeout = 0;
2291 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302292 dcmd->data_xfer_len =
2293 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2294 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002295
Adam Radford4cbfea82014-07-09 15:17:56 -07002296 if (initial)
2297 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302298 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002299 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302300 dcmd->sgl.sge32[0].phys_addr =
2301 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002302
Christoph Hellwig2213a462015-04-23 16:33:54 +05302303 dcmd->sgl.sge32[0].length = cpu_to_le32(
2304 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002305
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002306 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002307 "scsi%d\n", instance->host->host_no);
2308
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302309 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002310 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2311 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002312 dcmd->cmd_status, instance->host->host_no);
2313 retval = 1; /* Do a scan if we couldn't get affiliation */
2314 goto out;
2315 }
2316
2317 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002318 thisVf = new_affiliation_111->thisVf;
2319 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2320 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2321 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002322 dev_warn(&instance->pdev->dev, "SR-IOV: "
2323 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002324 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002325 memcpy(instance->vf_affiliation_111,
2326 new_affiliation_111,
2327 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002328 retval = 1;
2329 goto out;
2330 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002331 }
2332out:
2333 if (new_affiliation_111) {
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002334 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002335 sizeof(struct MR_LD_VF_AFFILIATION_111),
2336 new_affiliation_111,
2337 new_affiliation_111_h);
2338 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302339
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302340 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002341
2342 return retval;
2343}
2344
2345static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2346 int initial)
2347{
2348 struct megasas_cmd *cmd;
2349 struct megasas_dcmd_frame *dcmd;
2350 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2351 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2352 dma_addr_t new_affiliation_h;
2353 int i, j, retval = 0, found = 0, doscan = 0;
2354 u8 thisVf;
2355
2356 cmd = megasas_get_cmd(instance);
2357
2358 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002359 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2360 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002361 instance->host->host_no);
2362 return -ENOMEM;
2363 }
2364
2365 dcmd = &cmd->frame->dcmd;
2366
2367 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002368 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2369 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002370 megasas_return_cmd(instance, cmd);
2371 return -ENOMEM;
2372 }
2373
2374 if (initial)
2375 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2376 sizeof(struct MR_LD_VF_AFFILIATION));
2377 else {
2378 new_affiliation =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002379 dma_alloc_coherent(&instance->pdev->dev,
2380 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2381 &new_affiliation_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002382 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002383 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2384 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002385 instance->host->host_no);
2386 megasas_return_cmd(instance, cmd);
2387 return -ENOMEM;
2388 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002389 }
2390
2391 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2392
2393 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302394 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002395 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302396 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002397 dcmd->timeout = 0;
2398 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302399 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2400 sizeof(struct MR_LD_VF_AFFILIATION));
2401 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002402
2403 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302404 dcmd->sgl.sge32[0].phys_addr =
2405 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002406 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302407 dcmd->sgl.sge32[0].phys_addr =
2408 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002409
Christoph Hellwig2213a462015-04-23 16:33:54 +05302410 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2411 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002412
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002413 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002414 "scsi%d\n", instance->host->host_no);
2415
Adam Radford4cbfea82014-07-09 15:17:56 -07002416
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302417 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002418 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2419 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002420 dcmd->cmd_status, instance->host->host_no);
2421 retval = 1; /* Do a scan if we couldn't get affiliation */
2422 goto out;
2423 }
2424
2425 if (!initial) {
2426 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002427 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2428 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002429 instance->host->host_no);
2430 retval = 1;
2431 goto out;
2432 }
2433 newmap = new_affiliation->map;
2434 savedmap = instance->vf_affiliation->map;
2435 thisVf = new_affiliation->thisVf;
2436 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2437 found = 0;
2438 for (j = 0; j < instance->vf_affiliation->ldCount;
2439 j++) {
2440 if (newmap->ref.targetId ==
2441 savedmap->ref.targetId) {
2442 found = 1;
2443 if (newmap->policy[thisVf] !=
2444 savedmap->policy[thisVf]) {
2445 doscan = 1;
2446 goto out;
2447 }
adam radford229fe472014-03-10 02:51:56 -07002448 }
2449 savedmap = (struct MR_LD_VF_MAP *)
2450 ((unsigned char *)savedmap +
2451 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002452 }
2453 if (!found && newmap->policy[thisVf] !=
2454 MR_LD_ACCESS_HIDDEN) {
2455 doscan = 1;
2456 goto out;
2457 }
2458 newmap = (struct MR_LD_VF_MAP *)
2459 ((unsigned char *)newmap + newmap->size);
2460 }
2461
2462 newmap = new_affiliation->map;
2463 savedmap = instance->vf_affiliation->map;
2464
2465 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2466 found = 0;
2467 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2468 if (savedmap->ref.targetId ==
2469 newmap->ref.targetId) {
2470 found = 1;
2471 if (savedmap->policy[thisVf] !=
2472 newmap->policy[thisVf]) {
2473 doscan = 1;
2474 goto out;
2475 }
2476 }
adam radford229fe472014-03-10 02:51:56 -07002477 newmap = (struct MR_LD_VF_MAP *)
2478 ((unsigned char *)newmap +
2479 newmap->size);
2480 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002481 if (!found && savedmap->policy[thisVf] !=
2482 MR_LD_ACCESS_HIDDEN) {
2483 doscan = 1;
2484 goto out;
2485 }
2486 savedmap = (struct MR_LD_VF_MAP *)
2487 ((unsigned char *)savedmap +
2488 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002489 }
2490 }
2491out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002492 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002493 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2494 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002495 memcpy(instance->vf_affiliation, new_affiliation,
2496 new_affiliation->size);
2497 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002498 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002499
2500 if (new_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002501 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002502 (MAX_LOGICAL_DRIVES + 1) *
2503 sizeof(struct MR_LD_VF_AFFILIATION),
2504 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302505 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002506
2507 return retval;
2508}
2509
Adam Radford4cbfea82014-07-09 15:17:56 -07002510/* This function will get the current SR-IOV LD/VF affiliation */
2511static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2512 int initial)
2513{
2514 int retval;
2515
2516 if (instance->PlasmaFW111)
2517 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2518 else
2519 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2520 return retval;
2521}
2522
adam radford229fe472014-03-10 02:51:56 -07002523/* This function will tell FW to start the SR-IOV heartbeat */
2524int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2525 int initial)
2526{
2527 struct megasas_cmd *cmd;
2528 struct megasas_dcmd_frame *dcmd;
2529 int retval = 0;
2530
2531 cmd = megasas_get_cmd(instance);
2532
2533 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002534 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2535 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002536 instance->host->host_no);
2537 return -ENOMEM;
2538 }
2539
2540 dcmd = &cmd->frame->dcmd;
2541
2542 if (initial) {
2543 instance->hb_host_mem =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002544 dma_alloc_coherent(&instance->pdev->dev,
2545 sizeof(struct MR_CTRL_HB_HOST_MEM),
2546 &instance->hb_host_mem_h,
2547 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07002548 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002549 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2550 " memory for heartbeat host memory for scsi%d\n",
2551 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002552 retval = -ENOMEM;
2553 goto out;
2554 }
adam radford229fe472014-03-10 02:51:56 -07002555 }
2556
2557 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2558
Christoph Hellwig2213a462015-04-23 16:33:54 +05302559 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002560 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302561 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002562 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302563 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002564 dcmd->timeout = 0;
2565 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302566 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2567 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
Shivasharan S107a60d2017-10-19 02:49:05 -07002568
2569 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2570 sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002571
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002572 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002573 instance->host->host_no);
2574
Shivasharan Se7d36b82017-10-19 02:48:50 -07002575 if ((instance->adapter_type != MFI_SERIES) &&
2576 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302577 retval = megasas_issue_blocked_cmd(instance, cmd,
2578 MEGASAS_ROUTINE_WAIT_TIME_VF);
2579 else
2580 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002581
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302582 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302583 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2584 "_MEM_ALLOC DCMD %s for scsi%d\n",
2585 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2586 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002587 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002588 }
2589
2590out:
2591 megasas_return_cmd(instance, cmd);
2592
2593 return retval;
2594}
2595
2596/* Handler for SR-IOV heartbeat */
Kees Cookc251a7b2017-10-22 15:30:04 -07002597static void megasas_sriov_heartbeat_handler(struct timer_list *t)
adam radford229fe472014-03-10 02:51:56 -07002598{
2599 struct megasas_instance *instance =
Kees Cookc251a7b2017-10-22 15:30:04 -07002600 from_timer(instance, t, sriov_heartbeat_timer);
adam radford229fe472014-03-10 02:51:56 -07002601
2602 if (instance->hb_host_mem->HB.fwCounter !=
2603 instance->hb_host_mem->HB.driverCounter) {
2604 instance->hb_host_mem->HB.driverCounter =
2605 instance->hb_host_mem->HB.fwCounter;
2606 mod_timer(&instance->sriov_heartbeat_timer,
2607 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2608 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002609 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002610 "completed for scsi%d\n", instance->host->host_no);
2611 schedule_work(&instance->work_init);
2612 }
2613}
2614
bo yang7343eb62007-11-09 04:35:44 -05002615/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002616 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2617 * @instance: Adapter soft state
2618 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002619 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002620 * complete all its outstanding commands. Returns error if one or more IOs
2621 * are pending after this time period. It also marks the controller dead.
2622 */
2623static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2624{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302625 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002626 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002627 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002628 unsigned long flags;
2629 struct list_head clist_local;
2630 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002631 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002632
Sumit Saxenaccc75072016-01-28 21:04:33 +05302633 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2634 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2635 __func__, __LINE__);
2636 return FAILED;
2637 }
bo yang39a98552010-09-22 22:36:29 -04002638
Sumit Saxena8a01a412016-01-28 21:04:32 +05302639 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002640
2641 INIT_LIST_HEAD(&clist_local);
2642 spin_lock_irqsave(&instance->hba_lock, flags);
2643 list_splice_init(&instance->internal_reset_pending_q,
2644 &clist_local);
2645 spin_unlock_irqrestore(&instance->hba_lock, flags);
2646
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002647 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002648 for (i = 0; i < wait_time; i++) {
2649 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302650 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002651 break;
2652 }
2653
Sumit Saxena8a01a412016-01-28 21:04:32 +05302654 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002655 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302656 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002657 return FAILED;
2658 }
2659
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002660 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002661 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002662 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002663 struct megasas_cmd, list);
2664 list_del_init(&reset_cmd->list);
2665 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002666 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002667 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002668 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002669 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002670
2671 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2672 megasas_return_cmd(instance, reset_cmd);
2673 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002674 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002675 "reset queue\n",
2676 reset_cmd);
2677
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302678 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002679 instance->instancet->fire_cmd(instance,
2680 reset_cmd->frame_phys_addr,
2681 0, instance->reg_set);
2682 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002683 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002684 "cmds lst\n",
2685 reset_cmd);
2686 }
2687 reset_index++;
2688 }
2689
2690 return SUCCESS;
2691 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002692
adam radfordc007b8b2012-07-17 18:20:24 -07002693 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302694 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002695
2696 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002697 break;
2698
2699 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002700 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002701 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002702 /*
2703 * Call cmd completion routine. Cmd to be
2704 * be completed directly without depending on isr.
2705 */
2706 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002707 }
2708
2709 msleep(1000);
2710 }
2711
Yang, Bo707e09b2010-10-12 07:20:27 -06002712 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302713 outstanding = atomic_read(&instance->fw_outstanding);
Shivasharan Sde516372018-12-17 00:47:39 -08002714 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002715
Sumit Saxenaccc75072016-01-28 21:04:33 +05302716 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2717 goto no_outstanding;
2718
2719 if (instance->disableOnlineCtrlReset)
2720 goto kill_hba_and_failed;
2721 do {
2722 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2723 dev_info(&instance->pdev->dev,
Colin Ian Kingefc372c2019-04-17 14:51:09 +01002724 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
Sumit Saxenaccc75072016-01-28 21:04:33 +05302725 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2726 if (i == 3)
2727 goto kill_hba_and_failed;
2728 megasas_do_ocr(instance);
2729
2730 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2731 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2732 __func__, __LINE__);
2733 return FAILED;
2734 }
2735 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2736 __func__, __LINE__);
2737
2738 for (sl = 0; sl < 10; sl++)
2739 msleep(500);
2740
2741 outstanding = atomic_read(&instance->fw_outstanding);
2742
Shivasharan Sde516372018-12-17 00:47:39 -08002743 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302744 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2745 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002746 }
2747 i++;
2748 } while (i <= 3);
2749
Sumit Saxenaccc75072016-01-28 21:04:33 +05302750no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002751
Sumit Saxenaccc75072016-01-28 21:04:33 +05302752 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2753 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002754 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302755
2756kill_hba_and_failed:
2757
2758 /* Reset not supported, kill adapter */
2759 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2760 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2761 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2762 atomic_read(&instance->fw_outstanding));
2763 megasas_dump_pending_frames(instance);
2764 megaraid_sas_kill_hba(instance);
2765
2766 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002767}
2768
2769/**
2770 * megasas_generic_reset - Generic reset routine
2771 * @scmd: Mid-layer SCSI command
2772 *
2773 * This routine implements a generic reset handler for device, bus and host
2774 * reset requests. Device, bus and host specific reset handlers can use this
2775 * function after they do their specific tasks.
2776 */
2777static int megasas_generic_reset(struct scsi_cmnd *scmd)
2778{
2779 int ret_val;
2780 struct megasas_instance *instance;
2781
2782 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2783
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002784 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2785 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002786
Sumit Saxena8a01a412016-01-28 21:04:32 +05302787 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002788 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002789 return FAILED;
2790 }
2791
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002792 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002794 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002795 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002796 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002797
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002798 return ret_val;
2799}
2800
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002801/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002802 * megasas_reset_timer - quiesce the adapter if required
2803 * @scmd: scsi cmnd
2804 *
2805 * Sets the FW busy flag and reduces the host->can_queue if the
2806 * cmd has not been completed within the timeout period.
2807 */
2808static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002809blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002810{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002811 struct megasas_instance *instance;
2812 unsigned long flags;
2813
2814 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302815 (scmd_timeout * 2) * HZ)) {
Christoph Hellwig66005932018-05-29 15:52:29 +02002816 return BLK_EH_DONE;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002817 }
2818
adam radfordf575c5d2011-10-13 16:01:12 -07002819 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002820 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2821 /* FW is busy, throttle IO */
2822 spin_lock_irqsave(instance->host->host_lock, flags);
2823
adam radfordc5daa6a2012-07-17 18:20:03 -07002824 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002825 instance->last_time = jiffies;
2826 instance->flag |= MEGASAS_FW_BUSY;
2827
2828 spin_unlock_irqrestore(instance->host->host_lock, flags);
2829 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002830 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002831}
2832
2833/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002834 * megasas_dump_frame - This function will dump MPT/MFI frame
2835 */
2836static inline void
2837megasas_dump_frame(void *mpi_request, int sz)
2838{
2839 int i;
2840 __le32 *mfp = (__le32 *)mpi_request;
2841
2842 printk(KERN_INFO "IO request frame:\n\t");
Dan Carpenter40a4c2c2017-02-14 19:38:55 +03002843 for (i = 0; i < sz / sizeof(__le32); i++) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002844 if (i && ((i % 8) == 0))
2845 printk("\n\t");
2846 printk("%08x ", le32_to_cpu(mfp[i]));
2847 }
2848 printk("\n");
2849}
2850
2851/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002852 * megasas_reset_bus_host - Bus & host reset handler entry point
2853 */
2854static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2855{
2856 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002857 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002858
adam radford9c915a82010-12-21 13:34:31 -08002859 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002860
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002861 scmd_printk(KERN_INFO, scmd,
2862 "Controller reset is requested due to IO timeout\n"
2863 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2864 " SCSI host busy: %d\t FW outstanding: %d\n",
2865 scmd, scmd->device->host->shost_state,
Ming Leic84b0232018-06-24 22:03:26 +08002866 scsi_host_busy(scmd->device->host),
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002867 atomic_read(&instance->fw_outstanding));
2868
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002869 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002870 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002871 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002872 if (instance->adapter_type == MFI_SERIES) {
2873 ret = megasas_generic_reset(scmd);
2874 } else {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002875 struct megasas_cmd_fusion *cmd;
2876 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2877 if (cmd)
2878 megasas_dump_frame(cmd->io_request,
Shivasharan S5dd977e2017-08-23 04:47:07 -07002879 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002880 ret = megasas_reset_fusion(scmd->device->host,
2881 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002882 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002883
2884 return ret;
2885}
2886
2887/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002888 * megasas_task_abort - Issues task abort request to firmware
2889 * (supported only for fusion adapters)
2890 * @scmd: SCSI command pointer
2891 */
2892static int megasas_task_abort(struct scsi_cmnd *scmd)
2893{
2894 int ret;
2895 struct megasas_instance *instance;
2896
2897 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2898
Shivasharan Se7d36b82017-10-19 02:48:50 -07002899 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002900 ret = megasas_task_abort_fusion(scmd);
2901 else {
2902 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2903 ret = FAILED;
2904 }
2905
2906 return ret;
2907}
2908
2909/**
2910 * megasas_reset_target: Issues target reset request to firmware
2911 * (supported only for fusion adapters)
2912 * @scmd: SCSI command pointer
2913 */
2914static int megasas_reset_target(struct scsi_cmnd *scmd)
2915{
2916 int ret;
2917 struct megasas_instance *instance;
2918
2919 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2920
Shivasharan Se7d36b82017-10-19 02:48:50 -07002921 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002922 ret = megasas_reset_target_fusion(scmd);
2923 else {
2924 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2925 ret = FAILED;
2926 }
2927
2928 return ret;
2929}
2930
2931/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002932 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002933 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002934 * @bdev: block device
2935 * @capacity: drive capacity
2936 * @geom: geometry parameters
2937 */
2938static int
2939megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2940 sector_t capacity, int geom[])
2941{
2942 int heads;
2943 int sectors;
2944 sector_t cylinders;
2945 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002946
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002947 /* Default heads (64) & sectors (32) */
2948 heads = 64;
2949 sectors = 32;
2950
2951 tmp = heads * sectors;
2952 cylinders = capacity;
2953
2954 sector_div(cylinders, tmp);
2955
2956 /*
2957 * Handle extended translation size for logical drives > 1Gb
2958 */
2959
2960 if (capacity >= 0x200000) {
2961 heads = 255;
2962 sectors = 63;
2963 tmp = heads*sectors;
2964 cylinders = capacity;
2965 sector_div(cylinders, tmp);
2966 }
2967
2968 geom[0] = heads;
2969 geom[1] = sectors;
2970 geom[2] = cylinders;
2971
2972 return 0;
2973}
2974
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002975static void megasas_aen_polling(struct work_struct *work);
2976
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002977/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002978 * megasas_service_aen - Processes an event notification
2979 * @instance: Adapter soft state
2980 * @cmd: AEN command completed by the ISR
2981 *
2982 * For AEN, driver sends a command down to FW that is held by the FW till an
2983 * event occurs. When an event of interest occurs, FW completes the command
2984 * that it was previously holding.
2985 *
2986 * This routines sends SIGIO signal to processes that have registered with the
2987 * driver for AEN.
2988 */
2989static void
2990megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2991{
Yang, Boc3518832009-10-06 14:18:02 -06002992 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002993
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002994 /*
2995 * Don't signal app if it is just an aborted previously registered aen
2996 */
Yang, Boc3518832009-10-06 14:18:02 -06002997 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2998 spin_lock_irqsave(&poll_aen_lock, flags);
2999 megasas_poll_wait_aen = 1;
3000 spin_unlock_irqrestore(&poll_aen_lock, flags);
3001 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003002 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06003003 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003004 else
3005 cmd->abort_aen = 0;
3006
3007 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303008
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303009 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003010
bo yang39a98552010-09-22 22:36:29 -04003011 if ((instance->unload == 0) &&
3012 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003013 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003014
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003015 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3016 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003017 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003018 } else {
3019 ev->instance = instance;
3020 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08003021 INIT_DELAYED_WORK(&ev->hotplug_work,
3022 megasas_aen_polling);
3023 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003024 }
3025 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003026}
3027
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303028static ssize_t
3029megasas_fw_crash_buffer_store(struct device *cdev,
3030 struct device_attribute *attr, const char *buf, size_t count)
3031{
3032 struct Scsi_Host *shost = class_to_shost(cdev);
3033 struct megasas_instance *instance =
3034 (struct megasas_instance *) shost->hostdata;
3035 int val = 0;
3036 unsigned long flags;
3037
3038 if (kstrtoint(buf, 0, &val) != 0)
3039 return -EINVAL;
3040
3041 spin_lock_irqsave(&instance->crashdump_lock, flags);
3042 instance->fw_crash_buffer_offset = val;
3043 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3044 return strlen(buf);
3045}
3046
3047static ssize_t
3048megasas_fw_crash_buffer_show(struct device *cdev,
3049 struct device_attribute *attr, char *buf)
3050{
3051 struct Scsi_Host *shost = class_to_shost(cdev);
3052 struct megasas_instance *instance =
3053 (struct megasas_instance *) shost->hostdata;
3054 u32 size;
3055 unsigned long buff_addr;
3056 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3057 unsigned long src_addr;
3058 unsigned long flags;
3059 u32 buff_offset;
3060
3061 spin_lock_irqsave(&instance->crashdump_lock, flags);
3062 buff_offset = instance->fw_crash_buffer_offset;
3063 if (!instance->crash_dump_buf &&
3064 !((instance->fw_crash_state == AVAILABLE) ||
3065 (instance->fw_crash_state == COPYING))) {
3066 dev_err(&instance->pdev->dev,
3067 "Firmware crash dump is not available\n");
3068 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3069 return -EINVAL;
3070 }
3071
3072 buff_addr = (unsigned long) buf;
3073
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003074 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303075 dev_err(&instance->pdev->dev,
3076 "Firmware crash dump offset is out of range\n");
3077 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3078 return 0;
3079 }
3080
3081 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3082 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3083
3084 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3085 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003086 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303087 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3088
3089 return size;
3090}
3091
3092static ssize_t
3093megasas_fw_crash_buffer_size_show(struct device *cdev,
3094 struct device_attribute *attr, char *buf)
3095{
3096 struct Scsi_Host *shost = class_to_shost(cdev);
3097 struct megasas_instance *instance =
3098 (struct megasas_instance *) shost->hostdata;
3099
3100 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3101 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3102}
3103
3104static ssize_t
3105megasas_fw_crash_state_store(struct device *cdev,
3106 struct device_attribute *attr, const char *buf, size_t count)
3107{
3108 struct Scsi_Host *shost = class_to_shost(cdev);
3109 struct megasas_instance *instance =
3110 (struct megasas_instance *) shost->hostdata;
3111 int val = 0;
3112 unsigned long flags;
3113
3114 if (kstrtoint(buf, 0, &val) != 0)
3115 return -EINVAL;
3116
3117 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3118 dev_err(&instance->pdev->dev, "application updates invalid "
3119 "firmware crash state\n");
3120 return -EINVAL;
3121 }
3122
3123 instance->fw_crash_state = val;
3124
3125 if ((val == COPIED) || (val == COPY_ERROR)) {
3126 spin_lock_irqsave(&instance->crashdump_lock, flags);
3127 megasas_free_host_crash_buffer(instance);
3128 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3129 if (val == COPY_ERROR)
3130 dev_info(&instance->pdev->dev, "application failed to "
3131 "copy Firmware crash dump\n");
3132 else
3133 dev_info(&instance->pdev->dev, "Firmware crash dump "
3134 "copied successfully\n");
3135 }
3136 return strlen(buf);
3137}
3138
3139static ssize_t
3140megasas_fw_crash_state_show(struct device *cdev,
3141 struct device_attribute *attr, char *buf)
3142{
3143 struct Scsi_Host *shost = class_to_shost(cdev);
3144 struct megasas_instance *instance =
3145 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003146
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303147 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3148}
3149
3150static ssize_t
3151megasas_page_size_show(struct device *cdev,
3152 struct device_attribute *attr, char *buf)
3153{
3154 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3155}
3156
Sumit Saxena308ec452016-01-28 21:04:30 +05303157static ssize_t
3158megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3159 char *buf)
3160{
3161 struct Scsi_Host *shost = class_to_shost(cdev);
3162 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3163
3164 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3165}
3166
Shivasharan S88d155c2018-01-05 05:27:46 -08003167static ssize_t
3168megasas_fw_cmds_outstanding_show(struct device *cdev,
3169 struct device_attribute *attr, char *buf)
3170{
3171 struct Scsi_Host *shost = class_to_shost(cdev);
3172 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3173
3174 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3175}
3176
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303177static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3178 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3179static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3180 megasas_fw_crash_buffer_size_show, NULL);
3181static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3182 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3183static DEVICE_ATTR(page_size, S_IRUGO,
3184 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303185static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3186 megasas_ldio_outstanding_show, NULL);
Shivasharan S88d155c2018-01-05 05:27:46 -08003187static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3188 megasas_fw_cmds_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303189
3190struct device_attribute *megaraid_host_attrs[] = {
3191 &dev_attr_fw_crash_buffer_size,
3192 &dev_attr_fw_crash_buffer,
3193 &dev_attr_fw_crash_state,
3194 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303195 &dev_attr_ldio_outstanding,
Shivasharan S88d155c2018-01-05 05:27:46 -08003196 &dev_attr_fw_cmds_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303197 NULL,
3198};
3199
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003200/*
3201 * Scsi host template for megaraid_sas driver
3202 */
3203static struct scsi_host_template megasas_template = {
3204
3205 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303206 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003207 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003208 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003209 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303210 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003211 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003212 .eh_target_reset_handler = megasas_reset_target,
3213 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003214 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003215 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303216 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003217 .bios_param = megasas_bios_param,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003218 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003219 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003220};
3221
3222/**
3223 * megasas_complete_int_cmd - Completes an internal command
3224 * @instance: Adapter soft state
3225 * @cmd: Command to be completed
3226 *
3227 * The megasas_issue_blocked_cmd() function waits for a command to complete
3228 * after it issues a command. This function wakes up that waiting routine by
3229 * calling wake_up() on the wait queue.
3230 */
3231static void
3232megasas_complete_int_cmd(struct megasas_instance *instance,
3233 struct megasas_cmd *cmd)
3234{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303235 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003236 wake_up(&instance->int_cmd_wait_q);
3237}
3238
3239/**
3240 * megasas_complete_abort - Completes aborting a command
3241 * @instance: Adapter soft state
3242 * @cmd: Cmd that was issued to abort another cmd
3243 *
adam radford0d490162010-12-14 19:17:17 -08003244 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3245 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003246 * wakes up all functions waiting on the same wait queue.
3247 */
3248static void
3249megasas_complete_abort(struct megasas_instance *instance,
3250 struct megasas_cmd *cmd)
3251{
3252 if (cmd->sync_cmd) {
3253 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303254 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003255 wake_up(&instance->abort_cmd_wait_q);
3256 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003257}
3258
3259/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003260 * megasas_complete_cmd - Completes a command
3261 * @instance: Adapter soft state
3262 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003263 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003264 * SCSI mid-layer instead of the value returned
3265 * by the FW. This should be used if caller wants
3266 * an alternate status (as in the case of aborted
3267 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003268 */
adam radford9c915a82010-12-21 13:34:31 -08003269void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003270megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3271 u8 alt_status)
3272{
3273 int exception = 0;
3274 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003275 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003276 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303277 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003278
bo yang39a98552010-09-22 22:36:29 -04003279 /* flag for the retry reset */
3280 cmd->retry_for_fw_reset = 0;
3281
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003282 if (cmd->scmd)
3283 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003284
3285 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003286 case MFI_CMD_INVALID:
3287 /* Some older 1068 controller FW may keep a pended
3288 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3289 when booting the kdump kernel. Ignore this command to
3290 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003291 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3292 "completed\n");
3293 dev_warn(&instance->pdev->dev, "If you have a controller "
3294 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003295 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003296 case MFI_CMD_PD_SCSI_IO:
3297 case MFI_CMD_LD_SCSI_IO:
3298
3299 /*
3300 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3301 * issued either through an IO path or an IOCTL path. If it
3302 * was via IOCTL, we will send it to internal completion.
3303 */
3304 if (cmd->sync_cmd) {
3305 cmd->sync_cmd = 0;
3306 megasas_complete_int_cmd(instance, cmd);
3307 break;
3308 }
Gustavo A. R. Silva8d07f7d2018-11-27 22:32:27 -06003309 /* fall through */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003310
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003311 case MFI_CMD_LD_READ:
3312 case MFI_CMD_LD_WRITE:
3313
3314 if (alt_status) {
3315 cmd->scmd->result = alt_status << 16;
3316 exception = 1;
3317 }
3318
3319 if (exception) {
3320
Sumant Patroe4a082c2006-05-30 12:03:37 -07003321 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003322
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003323 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003324 cmd->scmd->scsi_done(cmd->scmd);
3325 megasas_return_cmd(instance, cmd);
3326
3327 break;
3328 }
3329
3330 switch (hdr->cmd_status) {
3331
3332 case MFI_STAT_OK:
3333 cmd->scmd->result = DID_OK << 16;
3334 break;
3335
3336 case MFI_STAT_SCSI_IO_FAILED:
3337 case MFI_STAT_LD_INIT_IN_PROGRESS:
3338 cmd->scmd->result =
3339 (DID_ERROR << 16) | hdr->scsi_status;
3340 break;
3341
3342 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3343
3344 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3345
3346 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3347 memset(cmd->scmd->sense_buffer, 0,
3348 SCSI_SENSE_BUFFERSIZE);
3349 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3350 hdr->sense_len);
3351
3352 cmd->scmd->result |= DRIVER_SENSE << 24;
3353 }
3354
3355 break;
3356
3357 case MFI_STAT_LD_OFFLINE:
3358 case MFI_STAT_DEVICE_NOT_FOUND:
3359 cmd->scmd->result = DID_BAD_TARGET << 16;
3360 break;
3361
3362 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003363 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003364 hdr->cmd_status);
3365 cmd->scmd->result = DID_ERROR << 16;
3366 break;
3367 }
3368
Sumant Patroe4a082c2006-05-30 12:03:37 -07003369 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003370
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003371 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003372 cmd->scmd->scsi_done(cmd->scmd);
3373 megasas_return_cmd(instance, cmd);
3374
3375 break;
3376
3377 case MFI_CMD_SMP:
3378 case MFI_CMD_STP:
Shivasharan Sf870bcb2018-01-05 05:33:04 -08003379 case MFI_CMD_NVME:
Shivasharan S82add4e2017-10-19 02:49:02 -07003380 megasas_complete_int_cmd(instance, cmd);
3381 break;
3382
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003383 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303384 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003385 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303386 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3387 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303388 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003389 spin_lock_irqsave(instance->host->host_lock, flags);
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003390 status = cmd->frame->hdr.cmd_status;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303391 instance->map_update_cmd = NULL;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003392 if (status != MFI_STAT_OK) {
3393 if (status != MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003394 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003395 cmd->frame->hdr.cmd_status);
3396 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303397 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003398 spin_unlock_irqrestore(
3399 instance->host->host_lock,
3400 flags);
3401 break;
3402 }
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003403 }
3404
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303405 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303406
3407 /*
3408 * Set fast path IO to ZERO.
3409 * Validate Map will set proper value.
3410 * Meanwhile all IOs will go as LD IO.
3411 */
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003412 if (status == MFI_STAT_OK &&
3413 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3414 instance->map_id++;
adam radford9c915a82010-12-21 13:34:31 -08003415 fusion->fast_path_io = 1;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003416 } else {
adam radford9c915a82010-12-21 13:34:31 -08003417 fusion->fast_path_io = 0;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003418 }
3419
adam radford9c915a82010-12-21 13:34:31 -08003420 megasas_sync_map_info(instance);
3421 spin_unlock_irqrestore(instance->host->host_lock,
3422 flags);
3423 break;
3424 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303425 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3426 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003427 spin_lock_irqsave(&poll_aen_lock, flags);
3428 megasas_poll_wait_aen = 0;
3429 spin_unlock_irqrestore(&poll_aen_lock, flags);
3430 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003431
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303432 /* FW has an updated PD sequence */
3433 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3434 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3435
3436 spin_lock_irqsave(instance->host->host_lock, flags);
3437 status = cmd->frame->hdr.cmd_status;
3438 instance->jbod_seq_cmd = NULL;
3439 megasas_return_cmd(instance, cmd);
3440
3441 if (status == MFI_STAT_OK) {
3442 instance->pd_seq_map_id++;
3443 /* Re-register a pd sync seq num cmd */
3444 if (megasas_sync_pd_seq_num(instance, true))
3445 instance->use_seqnum_jbod_fp = false;
3446 } else
3447 instance->use_seqnum_jbod_fp = false;
3448
3449 spin_unlock_irqrestore(instance->host->host_lock, flags);
3450 break;
3451 }
3452
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003453 /*
3454 * See if got an event notification
3455 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303456 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003457 megasas_service_aen(instance, cmd);
3458 else
3459 megasas_complete_int_cmd(instance, cmd);
3460
3461 break;
3462
3463 case MFI_CMD_ABORT:
3464 /*
3465 * Cmd issued to abort another cmd returned
3466 */
3467 megasas_complete_abort(instance, cmd);
3468 break;
3469
3470 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003471 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003472 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003473 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003474 break;
3475 }
3476}
3477
3478/**
bo yang39a98552010-09-22 22:36:29 -04003479 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003480 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003481 * @instance: Adapter soft state
3482 */
3483static inline void
3484megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3485{
3486 struct megasas_cmd *cmd;
3487 struct list_head clist_local;
3488 union megasas_evt_class_locale class_locale;
3489 unsigned long flags;
3490 u32 seq_num;
3491
3492 INIT_LIST_HEAD(&clist_local);
3493 spin_lock_irqsave(&instance->hba_lock, flags);
3494 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3495 spin_unlock_irqrestore(&instance->hba_lock, flags);
3496
3497 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003498 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003499 struct megasas_cmd, list);
3500 list_del_init(&cmd->list);
3501
3502 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003503 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3504 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003505 cmd, cmd->scmd, cmd->sync_cmd);
3506
3507 cmd->retry_for_fw_reset++;
3508
3509 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003510 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003511 "was tried multiple times during reset."
3512 "Shutting down the HBA\n",
3513 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303514 instance->instancet->disable_intr(instance);
3515 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003516 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003517 return;
3518 }
3519 }
3520
3521 if (cmd->sync_cmd == 1) {
3522 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003523 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003524 "cmd attached to internal command!\n");
3525 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003526 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003527 "on the internal reset queue,"
3528 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303529 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003530 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003531 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003532 0, instance->reg_set);
3533 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003534 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003535 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003536 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003537
3538 atomic_inc(&instance->fw_outstanding);
3539 instance->instancet->fire_cmd(instance,
3540 cmd->frame_phys_addr,
3541 cmd->frame_count-1, instance->reg_set);
3542 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003543 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003544 "internal reset defer list while re-issue!!\n",
3545 cmd);
3546 }
3547 }
3548
3549 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003550 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003551 megasas_return_cmd(instance, instance->aen_cmd);
3552
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003553 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003554 }
3555
3556 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003557 * Initiate AEN (Asynchronous Event Notification)
3558 */
bo yang39a98552010-09-22 22:36:29 -04003559 seq_num = instance->last_seq_num;
3560 class_locale.members.reserved = 0;
3561 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3562 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3563
3564 megasas_register_aen(instance, seq_num, class_locale.word);
3565}
3566
3567/**
3568 * Move the internal reset pending commands to a deferred queue.
3569 *
3570 * We move the commands pending at internal reset time to a
3571 * pending queue. This queue would be flushed after successful
3572 * completion of the internal reset sequence. if the internal reset
3573 * did not complete in time, the kernel reset handler would flush
3574 * these commands.
3575 **/
3576static void
3577megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3578{
3579 struct megasas_cmd *cmd;
3580 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003581 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003582 u32 defer_index;
3583 unsigned long flags;
3584
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003585 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303586 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003587 for (i = 0; i < max_cmd; i++) {
3588 cmd = instance->cmd_list[i];
3589 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003590 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003591 "on the defer queue as internal\n",
3592 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3593
3594 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003595 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003596 " moving this cmd:%p, %d %p, it was"
3597 "discovered on some list?\n",
3598 cmd, cmd->sync_cmd, cmd->scmd);
3599
3600 list_del_init(&cmd->list);
3601 }
3602 defer_index++;
3603 list_add_tail(&cmd->list,
3604 &instance->internal_reset_pending_q);
3605 }
3606 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303607 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003608}
3609
3610
3611static void
3612process_fw_state_change_wq(struct work_struct *work)
3613{
3614 struct megasas_instance *instance =
3615 container_of(work, struct megasas_instance, work_init);
3616 u32 wait;
3617 unsigned long flags;
3618
Sumit Saxena8a01a412016-01-28 21:04:32 +05303619 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003620 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303621 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003622 return ;
3623 }
3624
Sumit Saxena8a01a412016-01-28 21:04:32 +05303625 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003626 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003627 "state, restarting it...\n");
3628
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303629 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003630 atomic_set(&instance->fw_outstanding, 0);
3631
3632 atomic_set(&instance->fw_reset_no_pci_access, 1);
3633 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003634 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003635
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003636 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003637 "initiating next stage...\n");
3638
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003639 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003640 "state 2 starting...\n");
3641
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003642 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003643 for (wait = 0; wait < 30; wait++) {
3644 msleep(1000);
3645 }
3646
adam radford058a8fa2011-10-08 18:14:27 -07003647 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003648 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003649
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303650 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003651 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003652 return ;
3653 }
3654
3655 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3656 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3657 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3658 ) {
3659 *instance->consumer = *instance->producer;
3660 } else {
3661 *instance->consumer = 0;
3662 *instance->producer = 0;
3663 }
3664
3665 megasas_issue_init_mfi(instance);
3666
3667 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303668 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003669 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303670 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003671
3672 megasas_issue_pending_cmds_again(instance);
3673 instance->issuepend_done = 1;
3674 }
bo yang39a98552010-09-22 22:36:29 -04003675}
3676
3677/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003678 * megasas_deplete_reply_queue - Processes all completed commands
3679 * @instance: Adapter soft state
3680 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003681 * SCSI mid-layer instead of the status
3682 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003683 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003684 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003685static int
bo yang39a98552010-09-22 22:36:29 -04003686megasas_deplete_reply_queue(struct megasas_instance *instance,
3687 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003688{
bo yang39a98552010-09-22 22:36:29 -04003689 u32 mfiStatus;
3690 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003691
bo yang39a98552010-09-22 22:36:29 -04003692 if ((mfiStatus = instance->instancet->check_reset(instance,
3693 instance->reg_set)) == 1) {
3694 return IRQ_HANDLED;
3695 }
3696
Shivasharan Sde516372018-12-17 00:47:39 -08003697 mfiStatus = instance->instancet->clear_intr(instance);
3698 if (mfiStatus == 0) {
adam radforde1419192011-02-24 20:56:21 -08003699 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003700 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003701 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003702 }
3703
3704 instance->mfiStatus = mfiStatus;
3705
3706 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3707 fw_state = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08003708 instance) & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003709
3710 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003711 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003712 fw_state);
3713 }
3714
3715 if ((fw_state == MFI_STATE_FAULT) &&
3716 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003717 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003718
3719 if ((instance->pdev->device ==
3720 PCI_DEVICE_ID_LSI_SAS1064R) ||
3721 (instance->pdev->device ==
3722 PCI_DEVICE_ID_DELL_PERC5) ||
3723 (instance->pdev->device ==
3724 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3725
3726 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303727 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003728 }
3729
3730
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303731 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303732 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003733 instance->issuepend_done = 0;
3734
3735 atomic_set(&instance->fw_outstanding, 0);
3736 megasas_internal_reset_defer_cmds(instance);
3737
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003738 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303739 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003740
3741 schedule_work(&instance->work_init);
3742 return IRQ_HANDLED;
3743
3744 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003745 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003746 fw_state, instance->disableOnlineCtrlReset);
3747 }
3748 }
3749
Sumant Patro5d018ad2006-10-03 13:13:18 -07003750 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003751 return IRQ_HANDLED;
3752}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003753/**
3754 * megasas_isr - isr entry point
3755 */
David Howells7d12e782006-10-05 14:55:46 +01003756static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003757{
adam radfordc8e858f2011-10-08 18:15:13 -07003758 struct megasas_irq_context *irq_context = devp;
3759 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003760 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003761 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003762
adam radfordc8e858f2011-10-08 18:15:13 -07003763 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003764 return IRQ_HANDLED;
3765
bo yang39a98552010-09-22 22:36:29 -04003766 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003767 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003768 spin_unlock_irqrestore(&instance->hba_lock, flags);
3769
3770 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003771}
3772
3773/**
3774 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003775 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003776 *
3777 * During the initialization, FW passes can potentially be in any one of
3778 * several possible states. If the FW in operational, waiting-for-handshake
3779 * states, driver must take steps to bring it to ready state. Otherwise, it
3780 * has to wait for the ready state.
3781 */
adam radford9c915a82010-12-21 13:34:31 -08003782int
adam radford058a8fa2011-10-08 18:14:27 -07003783megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003784{
3785 int i;
3786 u8 max_wait;
3787 u32 fw_state;
3788 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003789 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003790
Shivasharan Sde516372018-12-17 00:47:39 -08003791 abs_state = instance->instancet->read_fw_status_reg(instance);
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003792 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003793
Sumant Patroe3bbff92006-10-03 12:28:49 -07003794 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003795 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003796 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003797
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003798 while (fw_state != MFI_STATE_READY) {
3799
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003800 switch (fw_state) {
3801
3802 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003803 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003804 if (ocr) {
3805 max_wait = MEGASAS_RESET_WAIT_TIME;
3806 cur_state = MFI_STATE_FAULT;
3807 break;
3808 } else
3809 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003810
3811 case MFI_STATE_WAIT_HANDSHAKE:
3812 /*
3813 * Set the CLR bit in inbound doorbell
3814 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003815 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003816 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3817 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003818 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003819 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003820 writel(
3821 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003822 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303823 else
Yang, Bo87911122009-10-06 14:31:54 -06003824 writel(
3825 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3826 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003827
Yang, Bo7218df62009-10-06 14:52:20 -06003828 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003829 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3830 break;
3831
Sumant Patroe3bbff92006-10-03 12:28:49 -07003832 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003833 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003834 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3835 (instance->pdev->device ==
3836 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003837 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003838 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003839 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303840 else
Yang, Bo87911122009-10-06 14:31:54 -06003841 writel(MFI_INIT_HOTPLUG,
3842 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003843
Yang, Bo7218df62009-10-06 14:52:20 -06003844 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003845 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3846 break;
3847
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003848 case MFI_STATE_OPERATIONAL:
3849 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003850 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003851 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303852 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003853 if ((instance->pdev->device ==
3854 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3855 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003856 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003857 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003858 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003859 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303860
Shivasharan Se7d36b82017-10-19 02:48:50 -07003861 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003862 for (i = 0; i < (10 * 1000); i += 20) {
Shivasharan S272652f2018-12-17 00:47:40 -08003863 if (megasas_readl(
3864 instance,
adam radford9c915a82010-12-21 13:34:31 -08003865 &instance->
3866 reg_set->
3867 doorbell) & 1)
3868 msleep(20);
3869 else
3870 break;
3871 }
3872 }
Yang, Bo87911122009-10-06 14:31:54 -06003873 } else
3874 writel(MFI_RESET_FLAGS,
3875 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003876
Yang, Bo7218df62009-10-06 14:52:20 -06003877 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003878 cur_state = MFI_STATE_OPERATIONAL;
3879 break;
3880
3881 case MFI_STATE_UNDEFINED:
3882 /*
3883 * This state should not last for more than 2 seconds
3884 */
Yang, Bo7218df62009-10-06 14:52:20 -06003885 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003886 cur_state = MFI_STATE_UNDEFINED;
3887 break;
3888
3889 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003890 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003891 cur_state = MFI_STATE_BB_INIT;
3892 break;
3893
3894 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003895 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003896 cur_state = MFI_STATE_FW_INIT;
3897 break;
3898
3899 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003900 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003901 cur_state = MFI_STATE_FW_INIT_2;
3902 break;
3903
3904 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003905 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906 cur_state = MFI_STATE_DEVICE_SCAN;
3907 break;
3908
3909 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003910 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003911 cur_state = MFI_STATE_FLUSH_CACHE;
3912 break;
3913
3914 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003915 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916 fw_state);
3917 return -ENODEV;
3918 }
3919
3920 /*
3921 * The cur_state should not last for more than max_wait secs
3922 */
Steve Sistare31b6a052019-03-01 06:46:28 -08003923 for (i = 0; i < max_wait * 50; i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003924 curr_abs_state = instance->instancet->
Shivasharan Sde516372018-12-17 00:47:39 -08003925 read_fw_status_reg(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003926
Yang, Bo7218df62009-10-06 14:52:20 -06003927 if (abs_state == curr_abs_state) {
Steve Sistare31b6a052019-03-01 06:46:28 -08003928 msleep(20);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003929 } else
3930 break;
3931 }
3932
3933 /*
3934 * Return error if fw_state hasn't changed after max_wait
3935 */
Yang, Bo7218df62009-10-06 14:52:20 -06003936 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003937 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003938 "in %d secs\n", fw_state, max_wait);
3939 return -ENODEV;
3940 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003941
3942 abs_state = curr_abs_state;
3943 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003944 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003945 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003946
3947 return 0;
3948}
3949
3950/**
3951 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3952 * @instance: Adapter soft state
3953 */
3954static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3955{
3956 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003957 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003958 struct megasas_cmd *cmd;
3959
3960 if (!instance->frame_dma_pool)
3961 return;
3962
3963 /*
3964 * Return all frames to pool
3965 */
3966 for (i = 0; i < max_cmd; i++) {
3967
3968 cmd = instance->cmd_list[i];
3969
3970 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02003971 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003972 cmd->frame_phys_addr);
3973
3974 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02003975 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003976 cmd->sense_phys_addr);
3977 }
3978
3979 /*
3980 * Now destroy the pool itself
3981 */
Romain Perierfc69d862017-07-06 10:13:06 +02003982 dma_pool_destroy(instance->frame_dma_pool);
3983 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003984
3985 instance->frame_dma_pool = NULL;
3986 instance->sense_dma_pool = NULL;
3987}
3988
3989/**
3990 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3991 * @instance: Adapter soft state
3992 *
3993 * Each command packet has an embedded DMA memory buffer that is used for
3994 * filling MFI frame and the SG list that immediately follows the frame. This
3995 * function creates those DMA memory buffers for each command packet by using
3996 * PCI pool facility.
3997 */
3998static int megasas_create_frame_pool(struct megasas_instance *instance)
3999{
4000 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004001 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004002 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004003 u32 frame_count;
4004 struct megasas_cmd *cmd;
4005
adam radford9c915a82010-12-21 13:34:31 -08004006 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004007
4008 /*
4009 * Size of our frame is 64 bytes for MFI frame, followed by max SG
4010 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4011 */
4012 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4013 sizeof(struct megasas_sge32);
4014
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004015 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06004016 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06004017
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004018 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05304019 * For MFI controllers.
4020 * max_num_sge = 60
4021 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4022 * Total 960 byte (15 MFI frame of 64 byte)
4023 *
4024 * Fusion adapter require only 3 extra frame.
4025 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4026 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4027 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004028 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07004029 frame_count = (instance->adapter_type == MFI_SERIES) ?
4030 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08004031 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004032 /*
4033 * Use DMA pool facility provided by PCI layer
4034 */
Romain Perierfc69d862017-07-06 10:13:06 +02004035 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4036 &instance->pdev->dev,
4037 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004038
4039 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004040 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004041 return -ENOMEM;
4042 }
4043
Romain Perierfc69d862017-07-06 10:13:06 +02004044 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4045 &instance->pdev->dev, 128,
4046 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004047
4048 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004049 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004050
Romain Perierfc69d862017-07-06 10:13:06 +02004051 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004052 instance->frame_dma_pool = NULL;
4053
4054 return -ENOMEM;
4055 }
4056
4057 /*
4058 * Allocate and attach a frame to each of the commands in cmd_list.
4059 * By making cmd->index as the context instead of the &cmd, we can
4060 * always use 32bit context regardless of the architecture
4061 */
4062 for (i = 0; i < max_cmd; i++) {
4063
4064 cmd = instance->cmd_list[i];
4065
Souptick Joarder61b142a2018-02-15 21:55:06 +05304066 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004067 GFP_KERNEL, &cmd->frame_phys_addr);
4068
Romain Perierfc69d862017-07-06 10:13:06 +02004069 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004070 GFP_KERNEL, &cmd->sense_phys_addr);
4071
4072 /*
4073 * megasas_teardown_frame_pool() takes care of freeing
4074 * whatever has been allocated
4075 */
4076 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004077 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004078 megasas_teardown_frame_pool(instance);
4079 return -ENOMEM;
4080 }
4081
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304082 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004083 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004084 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004085 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004086 }
4087
4088 return 0;
4089}
4090
4091/**
4092 * megasas_free_cmds - Free all the cmds in the free cmd pool
4093 * @instance: Adapter soft state
4094 */
adam radford9c915a82010-12-21 13:34:31 -08004095void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004096{
4097 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004098
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004099 /* First free the MFI frame pool */
4100 megasas_teardown_frame_pool(instance);
4101
4102 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004103 for (i = 0; i < instance->max_mfi_cmds; i++)
4104
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004105 kfree(instance->cmd_list[i]);
4106
4107 /* Free the cmd_list buffer itself */
4108 kfree(instance->cmd_list);
4109 instance->cmd_list = NULL;
4110
4111 INIT_LIST_HEAD(&instance->cmd_pool);
4112}
4113
4114/**
4115 * megasas_alloc_cmds - Allocates the command packets
4116 * @instance: Adapter soft state
4117 *
4118 * Each command that is issued to the FW, whether IO commands from the OS or
4119 * internal commands like IOCTLs, are wrapped in local data structure called
4120 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4121 * the FW.
4122 *
4123 * Each frame has a 32-bit field called context (tag). This context is used
4124 * to get back the megasas_cmd from the frame when a frame gets completed in
4125 * the ISR. Typically the address of the megasas_cmd itself would be used as
4126 * the context. But we wanted to keep the differences between 32 and 64 bit
4127 * systems to the mininum. We always use 32 bit integers for the context. In
4128 * this driver, the 32 bit values are the indices into an array cmd_list.
4129 * This array is used only to look up the megasas_cmd given the context. The
4130 * free commands themselves are maintained in a linked list called cmd_pool.
4131 */
adam radford9c915a82010-12-21 13:34:31 -08004132int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004133{
4134 int i;
4135 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004136 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004137 struct megasas_cmd *cmd;
4138
adam radford9c915a82010-12-21 13:34:31 -08004139 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004140
4141 /*
4142 * instance->cmd_list is an array of struct megasas_cmd pointers.
4143 * Allocate the dynamic array first and then allocate individual
4144 * commands.
4145 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004146 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004147
4148 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004149 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004150 return -ENOMEM;
4151 }
4152
adam radford9c915a82010-12-21 13:34:31 -08004153 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004154
4155 for (i = 0; i < max_cmd; i++) {
4156 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4157 GFP_KERNEL);
4158
4159 if (!instance->cmd_list[i]) {
4160
4161 for (j = 0; j < i; j++)
4162 kfree(instance->cmd_list[j]);
4163
4164 kfree(instance->cmd_list);
4165 instance->cmd_list = NULL;
4166
4167 return -ENOMEM;
4168 }
4169 }
4170
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004171 for (i = 0; i < max_cmd; i++) {
4172 cmd = instance->cmd_list[i];
4173 memset(cmd, 0, sizeof(struct megasas_cmd));
4174 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004175 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004176 cmd->instance = instance;
4177
4178 list_add_tail(&cmd->list, &instance->cmd_pool);
4179 }
4180
4181 /*
4182 * Create a frame pool and assign one frame to each cmd
4183 */
4184 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004185 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004186 megasas_free_cmds(instance);
Jason Yanbcf3b672019-02-15 19:50:27 +08004187 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004188 }
4189
4190 return 0;
4191}
4192
Yang, Bo81e403c2009-10-06 14:27:54 -06004193/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304194 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4195 * @instance: Adapter soft state
4196 *
4197 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4198 * or FW is not under OCR.
4199 */
4200inline int
4201dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4202
Shivasharan Se7d36b82017-10-19 02:48:50 -07004203 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304204 return KILL_ADAPTER;
4205 else if (instance->unload ||
4206 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4207 return IGNORE_TIMEOUT;
4208 else
4209 return INITIATE_OCR;
4210}
4211
Shivasharan S15dd0382017-02-10 00:59:10 -08004212static void
4213megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304214{
4215 int ret;
4216 struct megasas_cmd *cmd;
4217 struct megasas_dcmd_frame *dcmd;
4218
Shivasharan S15dd0382017-02-10 00:59:10 -08004219 struct MR_PRIV_DEVICE *mr_device_priv_data;
4220 u16 device_id = 0;
4221
4222 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304223 cmd = megasas_get_cmd(instance);
4224
4225 if (!cmd) {
4226 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004227 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304228 }
4229
4230 dcmd = &cmd->frame->dcmd;
4231
4232 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4233 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4234
4235 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4236 dcmd->cmd = MFI_CMD_DCMD;
4237 dcmd->cmd_status = 0xFF;
4238 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004239 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304240 dcmd->timeout = 0;
4241 dcmd->pad_0 = 0;
4242 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4243 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304244
Shivasharan S107a60d2017-10-19 02:49:05 -07004245 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4246 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304247
Shivasharan Se7d36b82017-10-19 02:48:50 -07004248 if ((instance->adapter_type != MFI_SERIES) &&
4249 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304250 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4251 else
4252 ret = megasas_issue_polled(instance, cmd);
4253
4254 switch (ret) {
4255 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004256 mr_device_priv_data = sdev->hostdata;
4257 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4258 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304259 instance->pd_info->state.ddf.pdType.intf;
4260 break;
4261
4262 case DCMD_TIMEOUT:
4263
4264 switch (dcmd_timeout_ocr_possible(instance)) {
4265 case INITIATE_OCR:
4266 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4267 megasas_reset_fusion(instance->host,
4268 MFI_IO_TIMEOUT_OCR);
4269 break;
4270 case KILL_ADAPTER:
4271 megaraid_sas_kill_hba(instance);
4272 break;
4273 case IGNORE_TIMEOUT:
4274 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4275 __func__, __LINE__);
4276 break;
4277 }
4278
4279 break;
4280 }
4281
4282 if (ret != DCMD_TIMEOUT)
4283 megasas_return_cmd(instance, cmd);
4284
Shivasharan S15dd0382017-02-10 00:59:10 -08004285 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304286}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304287/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004288 * megasas_get_pd_list_info - Returns FW's pd_list structure
4289 * @instance: Adapter soft state
4290 * @pd_list: pd_list structure
4291 *
4292 * Issues an internal command (DCMD) to get the FW's controller PD
4293 * list structure. This information is mainly used to find out SYSTEM
4294 * supported by the FW.
4295 */
4296static int
4297megasas_get_pd_list(struct megasas_instance *instance)
4298{
4299 int ret = 0, pd_index = 0;
4300 struct megasas_cmd *cmd;
4301 struct megasas_dcmd_frame *dcmd;
4302 struct MR_PD_LIST *ci;
4303 struct MR_PD_ADDRESS *pd_addr;
4304 dma_addr_t ci_h = 0;
4305
Sumit Saxenad9083162016-07-08 03:30:16 -07004306 if (instance->pd_list_not_supported) {
4307 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4308 "not supported by firmware\n");
4309 return ret;
4310 }
4311
Shivasharan S9b3d0282017-10-19 02:48:56 -07004312 ci = instance->pd_list_buf;
4313 ci_h = instance->pd_list_buf_h;
4314
Yang, Bo81e403c2009-10-06 14:27:54 -06004315 cmd = megasas_get_cmd(instance);
4316
4317 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004318 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004319 return -ENOMEM;
4320 }
4321
4322 dcmd = &cmd->frame->dcmd;
4323
Yang, Bo81e403c2009-10-06 14:27:54 -06004324 memset(ci, 0, sizeof(*ci));
4325 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4326
4327 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4328 dcmd->mbox.b[1] = 0;
4329 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304330 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004331 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004332 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004333 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004334 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304335 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4336 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004337
Shivasharan S107a60d2017-10-19 02:49:05 -07004338 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4339 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004340
Shivasharan Se7d36b82017-10-19 02:48:50 -07004341 if ((instance->adapter_type != MFI_SERIES) &&
4342 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304343 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304344 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304345 else
4346 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004347
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304348 switch (ret) {
4349 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004350 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4351 "failed/not supported by firmware\n");
4352
Shivasharan Se7d36b82017-10-19 02:48:50 -07004353 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004354 megaraid_sas_kill_hba(instance);
4355 else
4356 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304357 break;
4358 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004359
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304360 switch (dcmd_timeout_ocr_possible(instance)) {
4361 case INITIATE_OCR:
4362 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4363 /*
4364 * DCMD failed from AEN path.
4365 * AEN path already hold reset_mutex to avoid PCI access
4366 * while OCR is in progress.
4367 */
4368 mutex_unlock(&instance->reset_mutex);
4369 megasas_reset_fusion(instance->host,
4370 MFI_IO_TIMEOUT_OCR);
4371 mutex_lock(&instance->reset_mutex);
4372 break;
4373 case KILL_ADAPTER:
4374 megaraid_sas_kill_hba(instance);
4375 break;
4376 case IGNORE_TIMEOUT:
4377 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4378 __func__, __LINE__);
4379 break;
4380 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004381
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304382 break;
4383
4384 case DCMD_SUCCESS:
4385 pd_addr = ci->addr;
4386
4387 if ((le32_to_cpu(ci->count) >
4388 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4389 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004390
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304391 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304392 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004393
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304394 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304395 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304396 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304397 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304398 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304399 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304400 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004401 pd_addr++;
4402 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304403
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304404 memcpy(instance->pd_list, instance->local_pd_list,
4405 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304406 break;
4407
Yang, Bo81e403c2009-10-06 14:27:54 -06004408 }
4409
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304410 if (ret != DCMD_TIMEOUT)
4411 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004412
4413 return ret;
4414}
4415
Yang, Bobdc6fb82009-12-06 08:30:19 -07004416/*
4417 * megasas_get_ld_list_info - Returns FW's ld_list structure
4418 * @instance: Adapter soft state
4419 * @ld_list: ld_list structure
4420 *
4421 * Issues an internal command (DCMD) to get the FW's controller PD
4422 * list structure. This information is mainly used to find out SYSTEM
4423 * supported by the FW.
4424 */
4425static int
4426megasas_get_ld_list(struct megasas_instance *instance)
4427{
4428 int ret = 0, ld_index = 0, ids = 0;
4429 struct megasas_cmd *cmd;
4430 struct megasas_dcmd_frame *dcmd;
4431 struct MR_LD_LIST *ci;
4432 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304433 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004434
Shivasharan S9b3d0282017-10-19 02:48:56 -07004435 ci = instance->ld_list_buf;
4436 ci_h = instance->ld_list_buf_h;
4437
Yang, Bobdc6fb82009-12-06 08:30:19 -07004438 cmd = megasas_get_cmd(instance);
4439
4440 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004441 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004442 return -ENOMEM;
4443 }
4444
4445 dcmd = &cmd->frame->dcmd;
4446
Yang, Bobdc6fb82009-12-06 08:30:19 -07004447 memset(ci, 0, sizeof(*ci));
4448 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4449
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304450 if (instance->supportmax256vd)
4451 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004452 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304453 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004454 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004455 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004456 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304457 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4458 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004459 dcmd->pad_0 = 0;
4460
Shivasharan S107a60d2017-10-19 02:49:05 -07004461 megasas_set_dma_settings(instance, dcmd, ci_h,
4462 sizeof(struct MR_LD_LIST));
4463
Shivasharan Se7d36b82017-10-19 02:48:50 -07004464 if ((instance->adapter_type != MFI_SERIES) &&
4465 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304466 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304467 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304468 else
4469 ret = megasas_issue_polled(instance, cmd);
4470
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304471 ld_count = le32_to_cpu(ci->ldCount);
4472
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304473 switch (ret) {
4474 case DCMD_FAILED:
4475 megaraid_sas_kill_hba(instance);
4476 break;
4477 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004478
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304479 switch (dcmd_timeout_ocr_possible(instance)) {
4480 case INITIATE_OCR:
4481 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4482 /*
4483 * DCMD failed from AEN path.
4484 * AEN path already hold reset_mutex to avoid PCI access
4485 * while OCR is in progress.
4486 */
4487 mutex_unlock(&instance->reset_mutex);
4488 megasas_reset_fusion(instance->host,
4489 MFI_IO_TIMEOUT_OCR);
4490 mutex_lock(&instance->reset_mutex);
4491 break;
4492 case KILL_ADAPTER:
4493 megaraid_sas_kill_hba(instance);
4494 break;
4495 case IGNORE_TIMEOUT:
4496 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4497 __func__, __LINE__);
4498 break;
4499 }
4500
4501 break;
4502
4503 case DCMD_SUCCESS:
4504 if (ld_count > instance->fw_supported_vd_count)
4505 break;
4506
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304507 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004508
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304509 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004510 if (ci->ldList[ld_index].state != 0) {
4511 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304512 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004513 }
4514 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304515
4516 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004517 }
4518
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304519 if (ret != DCMD_TIMEOUT)
4520 megasas_return_cmd(instance, cmd);
4521
Yang, Bobdc6fb82009-12-06 08:30:19 -07004522 return ret;
4523}
4524
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004525/**
adam radford21c9e162013-09-06 15:27:14 -07004526 * megasas_ld_list_query - Returns FW's ld_list structure
4527 * @instance: Adapter soft state
4528 * @ld_list: ld_list structure
4529 *
4530 * Issues an internal command (DCMD) to get the FW's controller PD
4531 * list structure. This information is mainly used to find out SYSTEM
4532 * supported by the FW.
4533 */
4534static int
4535megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4536{
4537 int ret = 0, ld_index = 0, ids = 0;
4538 struct megasas_cmd *cmd;
4539 struct megasas_dcmd_frame *dcmd;
4540 struct MR_LD_TARGETID_LIST *ci;
4541 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304542 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004543
Shivasharan S9b3d0282017-10-19 02:48:56 -07004544 ci = instance->ld_targetid_list_buf;
4545 ci_h = instance->ld_targetid_list_buf_h;
4546
adam radford21c9e162013-09-06 15:27:14 -07004547 cmd = megasas_get_cmd(instance);
4548
4549 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004550 dev_warn(&instance->pdev->dev,
4551 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004552 return -ENOMEM;
4553 }
4554
4555 dcmd = &cmd->frame->dcmd;
4556
adam radford21c9e162013-09-06 15:27:14 -07004557 memset(ci, 0, sizeof(*ci));
4558 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4559
4560 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304561 if (instance->supportmax256vd)
4562 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004563
4564 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304565 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004566 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004567 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004568 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304569 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4570 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004571 dcmd->pad_0 = 0;
4572
Shivasharan S107a60d2017-10-19 02:49:05 -07004573 megasas_set_dma_settings(instance, dcmd, ci_h,
4574 sizeof(struct MR_LD_TARGETID_LIST));
4575
Shivasharan Se7d36b82017-10-19 02:48:50 -07004576 if ((instance->adapter_type != MFI_SERIES) &&
4577 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304578 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304579 else
4580 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004581
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304582 switch (ret) {
4583 case DCMD_FAILED:
4584 dev_info(&instance->pdev->dev,
4585 "DCMD not supported by firmware - %s %d\n",
4586 __func__, __LINE__);
4587 ret = megasas_get_ld_list(instance);
4588 break;
4589 case DCMD_TIMEOUT:
4590 switch (dcmd_timeout_ocr_possible(instance)) {
4591 case INITIATE_OCR:
4592 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4593 /*
4594 * DCMD failed from AEN path.
4595 * AEN path already hold reset_mutex to avoid PCI access
4596 * while OCR is in progress.
4597 */
4598 mutex_unlock(&instance->reset_mutex);
4599 megasas_reset_fusion(instance->host,
4600 MFI_IO_TIMEOUT_OCR);
4601 mutex_lock(&instance->reset_mutex);
4602 break;
4603 case KILL_ADAPTER:
4604 megaraid_sas_kill_hba(instance);
4605 break;
4606 case IGNORE_TIMEOUT:
4607 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4608 __func__, __LINE__);
4609 break;
4610 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304611
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304612 break;
4613 case DCMD_SUCCESS:
4614 tgtid_count = le32_to_cpu(ci->count);
4615
4616 if ((tgtid_count > (instance->fw_supported_vd_count)))
4617 break;
4618
adam radford21c9e162013-09-06 15:27:14 -07004619 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304620 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004621 ids = ci->targetId[ld_index];
4622 instance->ld_ids[ids] = ci->targetId[ld_index];
4623 }
4624
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304625 break;
adam radford21c9e162013-09-06 15:27:14 -07004626 }
4627
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304628 if (ret != DCMD_TIMEOUT)
4629 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004630
4631 return ret;
4632}
4633
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004634/**
4635 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4636 * dcmd.mbox - reserved
4637 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4638 * Desc: This DCMD will return the combined device list
4639 * Status: MFI_STAT_OK - List returned successfully
4640 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4641 * disabled
4642 * @instance: Adapter soft state
4643 * @is_probe: Driver probe check
4644 * Return: 0 if DCMD succeeded
4645 * non-zero if failed
4646 */
YueHaibing7c3f8ca2019-04-13 22:21:38 +08004647static int
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004648megasas_host_device_list_query(struct megasas_instance *instance,
4649 bool is_probe)
4650{
4651 int ret, i, target_id;
4652 struct megasas_cmd *cmd;
4653 struct megasas_dcmd_frame *dcmd;
4654 struct MR_HOST_DEVICE_LIST *ci;
4655 u32 count;
4656 dma_addr_t ci_h;
4657
4658 ci = instance->host_device_list_buf;
4659 ci_h = instance->host_device_list_buf_h;
4660
4661 cmd = megasas_get_cmd(instance);
4662
4663 if (!cmd) {
4664 dev_warn(&instance->pdev->dev,
4665 "%s: failed to get cmd\n",
4666 __func__);
4667 return -ENOMEM;
4668 }
4669
4670 dcmd = &cmd->frame->dcmd;
4671
4672 memset(ci, 0, sizeof(*ci));
4673 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4674
4675 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4676 dcmd->cmd = MFI_CMD_DCMD;
4677 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4678 dcmd->sge_count = 1;
4679 dcmd->flags = MFI_FRAME_DIR_READ;
4680 dcmd->timeout = 0;
4681 dcmd->pad_0 = 0;
4682 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4683 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4684
4685 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4686
4687 if (!instance->mask_interrupts) {
4688 ret = megasas_issue_blocked_cmd(instance, cmd,
4689 MFI_IO_TIMEOUT_SECS);
4690 } else {
4691 ret = megasas_issue_polled(instance, cmd);
4692 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4693 }
4694
4695 switch (ret) {
4696 case DCMD_SUCCESS:
4697 /* Fill the internal pd_list and ld_ids array based on
4698 * targetIds returned by FW
4699 */
4700 count = le32_to_cpu(ci->count);
4701
4702 memset(instance->local_pd_list, 0,
4703 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4704 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4705 for (i = 0; i < count; i++) {
4706 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4707 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4708 instance->local_pd_list[target_id].tid = target_id;
4709 instance->local_pd_list[target_id].driveType =
4710 ci->host_device_list[i].scsi_type;
4711 instance->local_pd_list[target_id].driveState =
4712 MR_PD_STATE_SYSTEM;
4713 } else {
4714 instance->ld_ids[target_id] = target_id;
4715 }
4716 }
4717
4718 memcpy(instance->pd_list, instance->local_pd_list,
4719 sizeof(instance->pd_list));
4720 break;
4721
4722 case DCMD_TIMEOUT:
4723 switch (dcmd_timeout_ocr_possible(instance)) {
4724 case INITIATE_OCR:
4725 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4726 megasas_reset_fusion(instance->host,
4727 MFI_IO_TIMEOUT_OCR);
4728 break;
4729 case KILL_ADAPTER:
4730 megaraid_sas_kill_hba(instance);
4731 break;
4732 case IGNORE_TIMEOUT:
4733 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4734 __func__, __LINE__);
4735 break;
4736 }
4737 break;
4738 case DCMD_FAILED:
4739 dev_err(&instance->pdev->dev,
4740 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4741 __func__);
4742 break;
4743 }
4744
4745 if (ret != DCMD_TIMEOUT)
4746 megasas_return_cmd(instance, cmd);
4747
4748 return ret;
4749}
4750
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304751/*
4752 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4753 * instance : Controller's instance
4754*/
4755static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4756{
4757 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004758 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304759
4760 fusion = instance->ctrl_context;
4761 /* For MFI based controllers return dummy success */
4762 if (!fusion)
4763 return;
4764
4765 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004766 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304767 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004768 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304769 instance->supportmax256vd = 1;
4770
4771 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4772 * MEGASAS_MAX_DEV_PER_CHANNEL;
4773 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4774 * MEGASAS_MAX_DEV_PER_CHANNEL;
4775 if (instance->supportmax256vd) {
4776 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4777 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4778 } else {
4779 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4780 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4781 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304782
4783 dev_info(&instance->pdev->dev,
Shivasharan Scba67d92018-10-16 23:37:56 -07004784 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4785 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4786 instance->ctrl_info_buf->max_lds);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304787
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004788 if (instance->max_raid_mapsize) {
4789 ventura_map_sz = instance->max_raid_mapsize *
4790 MR_MIN_MAP_SIZE; /* 64k */
4791 fusion->current_map_sz = ventura_map_sz;
4792 fusion->max_map_sz = ventura_map_sz;
4793 } else {
4794 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4795 (sizeof(struct MR_LD_SPAN_MAP) *
4796 (instance->fw_supported_vd_count - 1));
4797 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304798
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004799 fusion->max_map_sz =
4800 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304801
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004802 if (instance->supportmax256vd)
4803 fusion->current_map_sz = fusion->new_map_sz;
4804 else
4805 fusion->current_map_sz = fusion->old_map_sz;
4806 }
4807 /* irrespective of FW raid maps, driver raid map is constant */
4808 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304809}
4810
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004811/*
4812 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4813 * dcmd.hdr.length - number of bytes to read
4814 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4815 * Desc: Fill in snapdump properties
4816 * Status: MFI_STAT_OK- Command successful
4817 */
4818void megasas_get_snapdump_properties(struct megasas_instance *instance)
4819{
4820 int ret = 0;
4821 struct megasas_cmd *cmd;
4822 struct megasas_dcmd_frame *dcmd;
4823 struct MR_SNAPDUMP_PROPERTIES *ci;
4824 dma_addr_t ci_h = 0;
4825
4826 ci = instance->snapdump_prop;
4827 ci_h = instance->snapdump_prop_h;
4828
4829 if (!ci)
4830 return;
4831
4832 cmd = megasas_get_cmd(instance);
4833
4834 if (!cmd) {
4835 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4836 return;
4837 }
4838
4839 dcmd = &cmd->frame->dcmd;
4840
4841 memset(ci, 0, sizeof(*ci));
4842 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4843
4844 dcmd->cmd = MFI_CMD_DCMD;
4845 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4846 dcmd->sge_count = 1;
4847 dcmd->flags = MFI_FRAME_DIR_READ;
4848 dcmd->timeout = 0;
4849 dcmd->pad_0 = 0;
4850 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
4851 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
4852
4853 megasas_set_dma_settings(instance, dcmd, ci_h,
4854 sizeof(struct MR_SNAPDUMP_PROPERTIES));
4855
4856 if (!instance->mask_interrupts) {
4857 ret = megasas_issue_blocked_cmd(instance, cmd,
4858 MFI_IO_TIMEOUT_SECS);
4859 } else {
4860 ret = megasas_issue_polled(instance, cmd);
4861 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4862 }
4863
4864 switch (ret) {
4865 case DCMD_SUCCESS:
4866 instance->snapdump_wait_time =
4867 min_t(u8, ci->trigger_min_num_sec_before_ocr,
4868 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
4869 break;
4870
4871 case DCMD_TIMEOUT:
4872 switch (dcmd_timeout_ocr_possible(instance)) {
4873 case INITIATE_OCR:
4874 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4875 megasas_reset_fusion(instance->host,
4876 MFI_IO_TIMEOUT_OCR);
4877 break;
4878 case KILL_ADAPTER:
4879 megaraid_sas_kill_hba(instance);
4880 break;
4881 case IGNORE_TIMEOUT:
4882 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4883 __func__, __LINE__);
4884 break;
4885 }
4886 }
4887
4888 if (ret != DCMD_TIMEOUT)
4889 megasas_return_cmd(instance, cmd);
4890}
4891
adam radford21c9e162013-09-06 15:27:14 -07004892/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004893 * megasas_get_controller_info - Returns FW's controller structure
4894 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004895 *
4896 * Issues an internal command (DCMD) to get the FW's controller structure.
4897 * This information is mainly used to find out the maximum IO transfer per
4898 * command supported by the FW.
4899 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304900int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304901megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004902{
4903 int ret = 0;
4904 struct megasas_cmd *cmd;
4905 struct megasas_dcmd_frame *dcmd;
4906 struct megasas_ctrl_info *ci;
4907 dma_addr_t ci_h = 0;
4908
Shivasharan S9b3d0282017-10-19 02:48:56 -07004909 ci = instance->ctrl_info_buf;
4910 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304911
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004912 cmd = megasas_get_cmd(instance);
4913
4914 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004915 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004916 return -ENOMEM;
4917 }
4918
4919 dcmd = &cmd->frame->dcmd;
4920
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004921 memset(ci, 0, sizeof(*ci));
4922 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4923
4924 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304925 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004926 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004927 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004928 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004929 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304930 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4931 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304932 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004933
Shivasharan S107a60d2017-10-19 02:49:05 -07004934 megasas_set_dma_settings(instance, dcmd, ci_h,
4935 sizeof(struct megasas_ctrl_info));
4936
Shivasharan Se7d36b82017-10-19 02:48:50 -07004937 if ((instance->adapter_type != MFI_SERIES) &&
Shivasharan S54b28042018-01-05 05:27:47 -08004938 !instance->mask_interrupts) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304939 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Shivasharan S54b28042018-01-05 05:27:47 -08004940 } else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304941 ret = megasas_issue_polled(instance, cmd);
Shivasharan S54b28042018-01-05 05:27:47 -08004942 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4943 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304944
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304945 switch (ret) {
4946 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304947 /* Save required controller information in
4948 * CPU endianness format.
4949 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004950 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004951 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
Shivasharan S9ad18a92017-10-19 02:48:57 -07004952 le32_to_cpus((u32 *)&ci->adapterOperations2);
4953 le32_to_cpus((u32 *)&ci->adapterOperations3);
4954 le16_to_cpus((u16 *)&ci->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304955
4956 /* Update the latest Ext VD info.
4957 * From Init path, store current firmware details.
4958 * From OCR path, detect any firmware properties changes.
4959 * in case of Firmware upgrade without system reboot.
4960 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304961 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304962 instance->use_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004963 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004964 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004965 ci->adapter_operations4.support_pd_map_target_id;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004966 instance->support_nvme_passthru =
4967 ci->adapter_operations4.support_nvme_passthru;
Shivasharan Se9495e22018-06-04 03:45:12 -07004968 instance->task_abort_tmo = ci->TaskAbortTO;
4969 instance->max_reset_tmo = ci->MaxResetTO;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304970
4971 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004972 instance->is_imr = (ci->memory_size ? 0 : 1);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004973
4974 instance->snapdump_wait_time =
4975 (ci->properties.on_off_properties2.enable_snap_dump ?
4976 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
4977
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004978 instance->enable_fw_dev_list =
4979 ci->properties.on_off_properties2.enable_fw_dev_list;
4980
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304981 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304982 "controller type\t: %s(%dMB)\n",
4983 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07004984 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304985
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304986 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004987 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304988 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004989 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304990 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4991 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304992 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4993 instance->secure_jbod_support ? "Yes" : "No");
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004994 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
4995 instance->support_nvme_passthru ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07004996 dev_info(&instance->pdev->dev,
4997 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
4998 instance->task_abort_tmo, instance->max_reset_tmo);
4999
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305000 break;
5001
5002 case DCMD_TIMEOUT:
5003 switch (dcmd_timeout_ocr_possible(instance)) {
5004 case INITIATE_OCR:
5005 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5006 megasas_reset_fusion(instance->host,
5007 MFI_IO_TIMEOUT_OCR);
5008 break;
5009 case KILL_ADAPTER:
5010 megaraid_sas_kill_hba(instance);
5011 break;
5012 case IGNORE_TIMEOUT:
5013 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5014 __func__, __LINE__);
5015 break;
5016 }
Shivasharan S2747e6b2018-06-04 03:45:09 -07005017 break;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305018 case DCMD_FAILED:
5019 megaraid_sas_kill_hba(instance);
5020 break;
5021
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305022 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005023
Shivasharan S2747e6b2018-06-04 03:45:09 -07005024 if (ret != DCMD_TIMEOUT)
5025 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305026
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005027 return ret;
5028}
5029
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305030/*
5031 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5032 * to firmware
5033 *
5034 * @instance: Adapter soft state
5035 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5036 MR_CRASH_BUF_TURN_OFF = 0
5037 MR_CRASH_BUF_TURN_ON = 1
5038 * @return 0 on success non-zero on failure.
5039 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5040 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5041 * that driver supports crash dump feature. This DCMD will be sent only if
5042 * crash dump feature is supported by the FW.
5043 *
5044 */
5045int megasas_set_crash_dump_params(struct megasas_instance *instance,
5046 u8 crash_buf_state)
5047{
5048 int ret = 0;
5049 struct megasas_cmd *cmd;
5050 struct megasas_dcmd_frame *dcmd;
5051
5052 cmd = megasas_get_cmd(instance);
5053
5054 if (!cmd) {
5055 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5056 return -ENOMEM;
5057 }
5058
5059
5060 dcmd = &cmd->frame->dcmd;
5061
5062 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5063 dcmd->mbox.b[0] = crash_buf_state;
5064 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305065 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305066 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005067 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305068 dcmd->timeout = 0;
5069 dcmd->pad_0 = 0;
5070 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5071 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305072
Shivasharan S107a60d2017-10-19 02:49:05 -07005073 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5074 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305075
Shivasharan Se7d36b82017-10-19 02:48:50 -07005076 if ((instance->adapter_type != MFI_SERIES) &&
5077 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305078 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305079 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305080 ret = megasas_issue_polled(instance, cmd);
5081
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305082 if (ret == DCMD_TIMEOUT) {
5083 switch (dcmd_timeout_ocr_possible(instance)) {
5084 case INITIATE_OCR:
5085 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5086 megasas_reset_fusion(instance->host,
5087 MFI_IO_TIMEOUT_OCR);
5088 break;
5089 case KILL_ADAPTER:
5090 megaraid_sas_kill_hba(instance);
5091 break;
5092 case IGNORE_TIMEOUT:
5093 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5094 __func__, __LINE__);
5095 break;
5096 }
5097 } else
5098 megasas_return_cmd(instance, cmd);
5099
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305100 return ret;
5101}
5102
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005103/**
bo yang31ea7082007-11-07 12:09:50 -05005104 * megasas_issue_init_mfi - Initializes the FW
5105 * @instance: Adapter soft state
5106 *
5107 * Issues the INIT MFI cmd
5108 */
5109static int
5110megasas_issue_init_mfi(struct megasas_instance *instance)
5111{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05305112 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05005113 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05005114 struct megasas_init_frame *init_frame;
5115 struct megasas_init_queue_info *initq_info;
5116 dma_addr_t init_frame_h;
5117 dma_addr_t initq_info_h;
5118
5119 /*
5120 * Prepare a init frame. Note the init frame points to queue info
5121 * structure. Each frame has SGL allocated after first 64 bytes. For
5122 * this frame - since we don't need any SGL - we use SGL's space as
5123 * queue info structure
5124 *
5125 * We will not get a NULL command below. We just created the pool.
5126 */
5127 cmd = megasas_get_cmd(instance);
5128
5129 init_frame = (struct megasas_init_frame *)cmd->frame;
5130 initq_info = (struct megasas_init_queue_info *)
5131 ((unsigned long)init_frame + 64);
5132
5133 init_frame_h = cmd->frame_phys_addr;
5134 initq_info_h = init_frame_h + 64;
5135
5136 context = init_frame->context;
5137 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5138 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5139 init_frame->context = context;
5140
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305141 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5142 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05005143
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305144 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5145 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05005146
5147 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305148 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305149 init_frame->queue_info_new_phys_addr_lo =
5150 cpu_to_le32(lower_32_bits(initq_info_h));
5151 init_frame->queue_info_new_phys_addr_hi =
5152 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05005153
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305154 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05005155
5156 /*
5157 * disable the intr before firing the init frame to FW
5158 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305159 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05005160
5161 /*
5162 * Issue the init frame in polled mode
5163 */
5164
5165 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005166 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05005167 megasas_return_cmd(instance, cmd);
5168 goto fail_fw_init;
5169 }
5170
5171 megasas_return_cmd(instance, cmd);
5172
5173 return 0;
5174
5175fail_fw_init:
5176 return -EINVAL;
5177}
5178
adam radfordcd50ba82010-12-21 10:23:23 -08005179static u32
5180megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005181{
5182 u32 context_sz;
5183 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005184
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005185 /*
5186 * Get various operational parameters from status register
5187 */
Shivasharan Sde516372018-12-17 00:47:39 -08005188 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07005189 /*
5190 * Reduce the max supported cmds by 1. This is to ensure that the
5191 * reply_q_sz (1 more than the max cmd that driver may send)
5192 * does not exceed max cmds that the FW can support
5193 */
5194 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08005195 instance->max_mfi_cmds = instance->max_fw_cmds;
Shivasharan Sde516372018-12-17 00:47:39 -08005196 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08005197 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005198 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305199 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5200 * are reserved for IOCTL + driver's internal DCMDs.
5201 */
5202 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5203 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5204 instance->max_scsi_cmds = (instance->max_fw_cmds -
5205 MEGASAS_SKINNY_INT_CMDS);
5206 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5207 } else {
5208 instance->max_scsi_cmds = (instance->max_fw_cmds -
5209 MEGASAS_INT_CMDS);
5210 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5211 }
5212
Sumit Saxena308ec452016-01-28 21:04:30 +05305213 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305214 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005215 * Create a pool of commands
5216 */
5217 if (megasas_alloc_cmds(instance))
5218 goto fail_alloc_cmds;
5219
5220 /*
5221 * Allocate memory for reply queue. Length of reply queue should
5222 * be _one_ more than the maximum commands handled by the firmware.
5223 *
5224 * Note: When FW completes commands, it places corresponding contex
5225 * values in this circular reply queue. This circular queue is a fairly
5226 * typical producer-consumer queue. FW is the producer (of completed
5227 * commands) and the driver is the consumer.
5228 */
5229 context_sz = sizeof(u32);
5230 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5231
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005232 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5233 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005234
5235 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005236 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005237 goto fail_reply_queue;
5238 }
5239
bo yang31ea7082007-11-07 12:09:50 -05005240 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005241 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005242
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305243 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305244 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5245 "Fail from %s %d\n", instance->unique_id,
5246 __func__, __LINE__);
5247 goto fail_fw_init;
5248 }
5249
bo yang39a98552010-09-22 22:36:29 -04005250 instance->fw_support_ieee = 0;
5251 instance->fw_support_ieee =
Shivasharan Sde516372018-12-17 00:47:39 -08005252 (instance->instancet->read_fw_status_reg(instance) &
bo yang39a98552010-09-22 22:36:29 -04005253 0x04000000);
5254
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005255 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04005256 instance->fw_support_ieee);
5257
5258 if (instance->fw_support_ieee)
5259 instance->flag_ieee = 1;
5260
adam radfordcd50ba82010-12-21 10:23:23 -08005261 return 0;
5262
5263fail_fw_init:
5264
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005265 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
adam radfordcd50ba82010-12-21 10:23:23 -08005266 instance->reply_queue, instance->reply_queue_h);
5267fail_reply_queue:
5268 megasas_free_cmds(instance);
5269
5270fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08005271 return 1;
5272}
5273
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305274/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01005275 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305276 * @instance: Adapter soft state
5277 *
5278 * Do not enable interrupt, only setup ISRs.
5279 *
5280 * Return 0 on success.
5281 */
5282static int
5283megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5284{
5285 struct pci_dev *pdev;
5286
5287 pdev = instance->pdev;
5288 instance->irq_context[0].instance = instance;
5289 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005290 if (request_irq(pci_irq_vector(pdev, 0),
5291 instance->instancet->service_isr, IRQF_SHARED,
5292 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305293 dev_err(&instance->pdev->dev,
5294 "Failed to register IRQ from %s %d\n",
5295 __func__, __LINE__);
5296 return -1;
5297 }
5298 return 0;
5299}
5300
5301/**
5302 * megasas_setup_irqs_msix - register MSI-x interrupts.
5303 * @instance: Adapter soft state
5304 * @is_probe: Driver probe check
5305 *
5306 * Do not enable interrupt, only setup ISRs.
5307 *
5308 * Return 0 on success.
5309 */
5310static int
5311megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5312{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005313 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305314 struct pci_dev *pdev;
5315
5316 pdev = instance->pdev;
5317
5318 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305319 for (i = 0; i < instance->msix_vectors; i++) {
5320 instance->irq_context[i].instance = instance;
5321 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005322 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305323 instance->instancet->service_isr, 0, "megasas",
5324 &instance->irq_context[i])) {
5325 dev_err(&instance->pdev->dev,
5326 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005327 for (j = 0; j < i; j++)
5328 free_irq(pci_irq_vector(pdev, j),
5329 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305330 /* Retry irq register for IO_APIC*/
5331 instance->msix_vectors = 0;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005332 if (is_probe) {
5333 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305334 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005335 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305336 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005337 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305338 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305339 }
5340 return 0;
5341}
5342
5343/*
5344 * megasas_destroy_irqs- unregister interrupts.
5345 * @instance: Adapter soft state
5346 * return: void
5347 */
5348static void
5349megasas_destroy_irqs(struct megasas_instance *instance) {
5350
5351 int i;
5352
5353 if (instance->msix_vectors)
5354 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005355 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305356 &instance->irq_context[i]);
5357 }
5358 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005359 free_irq(pci_irq_vector(instance->pdev, 0),
5360 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305361}
5362
adam radfordcd50ba82010-12-21 10:23:23 -08005363/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305364 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5365 * @instance: Adapter soft state
5366 * @is_probe: Driver probe check
5367 *
5368 * Return 0 on success.
5369 */
5370void
5371megasas_setup_jbod_map(struct megasas_instance *instance)
5372{
5373 int i;
5374 struct fusion_context *fusion = instance->ctrl_context;
5375 u32 pd_seq_map_sz;
5376
5377 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5378 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5379
5380 if (reset_devices || !fusion ||
Shivasharan S9ad18a92017-10-19 02:48:57 -07005381 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305382 dev_info(&instance->pdev->dev,
5383 "Jbod map is not supported %s %d\n",
5384 __func__, __LINE__);
5385 instance->use_seqnum_jbod_fp = false;
5386 return;
5387 }
5388
5389 if (fusion->pd_seq_sync[0])
5390 goto skip_alloc;
5391
5392 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5393 fusion->pd_seq_sync[i] = dma_alloc_coherent
5394 (&instance->pdev->dev, pd_seq_map_sz,
5395 &fusion->pd_seq_phys[i], GFP_KERNEL);
5396 if (!fusion->pd_seq_sync[i]) {
5397 dev_err(&instance->pdev->dev,
5398 "Failed to allocate memory from %s %d\n",
5399 __func__, __LINE__);
5400 if (i == 1) {
5401 dma_free_coherent(&instance->pdev->dev,
5402 pd_seq_map_sz, fusion->pd_seq_sync[0],
5403 fusion->pd_seq_phys[0]);
5404 fusion->pd_seq_sync[0] = NULL;
5405 }
5406 instance->use_seqnum_jbod_fp = false;
5407 return;
5408 }
5409 }
5410
5411skip_alloc:
5412 if (!megasas_sync_pd_seq_num(instance, false) &&
5413 !megasas_sync_pd_seq_num(instance, true))
5414 instance->use_seqnum_jbod_fp = true;
5415 else
5416 instance->use_seqnum_jbod_fp = false;
5417}
5418
Ming Leiadbe5522018-03-13 17:42:40 +08005419static void megasas_setup_reply_map(struct megasas_instance *instance)
5420{
5421 const struct cpumask *mask;
5422 unsigned int queue, cpu;
5423
5424 for (queue = 0; queue < instance->msix_vectors; queue++) {
5425 mask = pci_irq_get_affinity(instance->pdev, queue);
5426 if (!mask)
5427 goto fallback;
5428
5429 for_each_cpu(cpu, mask)
5430 instance->reply_map[cpu] = queue;
5431 }
5432 return;
5433
5434fallback:
5435 for_each_possible_cpu(cpu)
5436 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5437}
5438
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305439/**
Shivasharan Sdaa06812019-01-29 01:38:12 -08005440 * megasas_get_device_list - Get the PD and LD device list from FW.
5441 * @instance: Adapter soft state
5442 * @return: Success or failure
5443 *
5444 * Issue DCMDs to Firmware to get the PD and LD list.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005445 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5446 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan Sdaa06812019-01-29 01:38:12 -08005447 */
5448static
5449int megasas_get_device_list(struct megasas_instance *instance)
5450{
5451 memset(instance->pd_list, 0,
5452 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5453 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5454
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005455 if (instance->enable_fw_dev_list) {
5456 if (megasas_host_device_list_query(instance, true))
5457 return FAILED;
5458 } else {
5459 if (megasas_get_pd_list(instance) < 0) {
5460 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5461 return FAILED;
5462 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005463
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005464 if (megasas_ld_list_query(instance,
5465 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5466 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5467 return FAILED;
5468 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005469 }
5470
5471 return SUCCESS;
5472}
5473/**
adam radfordcd50ba82010-12-21 10:23:23 -08005474 * megasas_init_fw - Initializes the FW
5475 * @instance: Adapter soft state
5476 *
5477 * This is the main function for initializing firmware
5478 */
5479
5480static int megasas_init_fw(struct megasas_instance *instance)
5481{
5482 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005483 u32 max_sectors_2, tmp_sectors, msix_enable;
Shivasharan S81b76452018-10-16 23:37:51 -07005484 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005485 resource_size_t base_addr;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305486 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005487 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005488 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005489 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305490 struct fusion_context *fusion;
5491
5492 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005493
5494 /* Find first memory bar */
5495 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005496 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005497 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005498 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005499 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005500 return -EBUSY;
5501 }
5502
Ben Collins11f8a7b2013-09-13 12:46:44 -04005503 base_addr = pci_resource_start(instance->pdev, instance->bar);
5504 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005505
5506 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005507 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005508 goto fail_ioremap;
5509 }
5510
Shivasharan Se7d36b82017-10-19 02:48:50 -07005511 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005512 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005513 else {
5514 switch (instance->pdev->device) {
5515 case PCI_DEVICE_ID_LSI_SAS1078R:
5516 case PCI_DEVICE_ID_LSI_SAS1078DE:
5517 instance->instancet = &megasas_instance_template_ppc;
5518 break;
5519 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5520 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5521 instance->instancet = &megasas_instance_template_gen2;
5522 break;
5523 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5524 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5525 instance->instancet = &megasas_instance_template_skinny;
5526 break;
5527 case PCI_DEVICE_ID_LSI_SAS1064R:
5528 case PCI_DEVICE_ID_DELL_PERC5:
5529 default:
5530 instance->instancet = &megasas_instance_template_xscale;
5531 instance->pd_list_not_supported = 1;
5532 break;
5533 }
adam radfordcd50ba82010-12-21 10:23:23 -08005534 }
5535
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305536 if (megasas_transition_to_ready(instance, 0)) {
Shivasharan S78409d42019-05-07 10:05:34 -07005537 dev_info(&instance->pdev->dev,
5538 "Failed to transition controller to ready from %s!\n",
5539 __func__);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005540 if (instance->adapter_type != MFI_SERIES) {
Shivasharan Sde93b402018-10-16 23:37:42 -07005541 status_reg = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08005542 instance);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005543 if (status_reg & MFI_RESET_ADAPTER) {
Shivasharan S78409d42019-05-07 10:05:34 -07005544 if (megasas_adp_reset_wait_for_ready
5545 (instance, true, 0) == FAILED)
Shivasharan Sf10fb852019-05-07 10:05:32 -07005546 goto fail_ready_state;
5547 } else {
5548 goto fail_ready_state;
5549 }
5550 } else {
Shivasharan Sde93b402018-10-16 23:37:42 -07005551 atomic_set(&instance->fw_reset_no_pci_access, 1);
5552 instance->instancet->adp_reset
5553 (instance, instance->reg_set);
5554 atomic_set(&instance->fw_reset_no_pci_access, 0);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305555
Shivasharan Sde93b402018-10-16 23:37:42 -07005556 /*waiting for about 30 second before retry*/
5557 ssleep(30);
5558
5559 if (megasas_transition_to_ready(instance, 0))
5560 goto fail_ready_state;
Shivasharan Sde93b402018-10-16 23:37:42 -07005561 }
Shivasharan S78409d42019-05-07 10:05:34 -07005562
5563 dev_info(&instance->pdev->dev,
5564 "FW restarted successfully from %s!\n",
5565 __func__);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305566 }
adam radfordcd50ba82010-12-21 10:23:23 -08005567
Shivasharan Se5d65b42017-10-19 02:48:59 -07005568 megasas_init_ctrl_params(instance);
5569
Shivasharan S107a60d2017-10-19 02:49:05 -07005570 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005571 goto fail_ready_state;
5572
5573 if (megasas_alloc_ctrl_mem(instance))
5574 goto fail_alloc_dma_buf;
5575
5576 if (megasas_alloc_ctrl_dma_buffers(instance))
5577 goto fail_alloc_dma_buf;
5578
5579 fusion = instance->ctrl_context;
5580
Shivasharan S630d42b2018-12-17 00:47:37 -08005581 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005582 scratch_pad_2 =
Shivasharan S272652f2018-12-17 00:47:40 -08005583 megasas_readl(instance,
5584 &instance->reg_set->outbound_scratch_pad_2);
Shivasharan S81b76452018-10-16 23:37:51 -07005585 instance->max_raid_mapsize = ((scratch_pad_2 >>
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005586 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5587 MR_MAX_RAID_MAP_SIZE_MASK);
5588 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305589
adam radford3f1abce2011-05-11 18:33:47 -07005590 /* Check if MSI-X is supported while in ready state */
Shivasharan Sde516372018-12-17 00:47:39 -08005591 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
adam radford3f1abce2011-05-11 18:33:47 -07005592 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005593 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005594 int irq_flags = PCI_IRQ_MSIX;
5595
Shivasharan S272652f2018-12-17 00:47:40 -08005596 scratch_pad_1 = megasas_readl
5597 (instance, &instance->reg_set->outbound_scratch_pad_1);
adam radfordc8e858f2011-10-08 18:15:13 -07005598 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305599 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005600 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5601 /* Thunderbolt Series*/
Shivasharan S81b76452018-10-16 23:37:51 -07005602 instance->msix_vectors = (scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305603 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5604 fw_msix_count = instance->msix_vectors;
Shivasharan Se29c3222018-10-16 23:37:46 -07005605 } else {
Shivasharan S81b76452018-10-16 23:37:51 -07005606 instance->msix_vectors = ((scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305607 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5608 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005609
5610 /*
5611 * For Invader series, > 8 MSI-x vectors
5612 * supported by FW/HW implies combined
5613 * reply queue mode is enabled.
5614 * For Ventura series, > 16 MSI-x vectors
5615 * supported by FW/HW implies combined
5616 * reply queue mode is enabled.
5617 */
5618 switch (instance->adapter_type) {
5619 case INVADER_SERIES:
5620 if (instance->msix_vectors > 8)
5621 instance->msix_combined = true;
5622 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08005623 case AERO_SERIES:
Shivasharan Se29c3222018-10-16 23:37:46 -07005624 case VENTURA_SERIES:
5625 if (instance->msix_vectors > 16)
5626 instance->msix_combined = true;
5627 break;
5628 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005629
Sumit Saxena179ac142016-01-28 21:04:28 +05305630 if (rdpq_enable)
Shivasharan S81b76452018-10-16 23:37:51 -07005631 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
Sumit Saxena179ac142016-01-28 21:04:28 +05305632 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305633 fw_msix_count = instance->msix_vectors;
5634 /* Save 1-15 reply post index address to local memory
5635 * Index 0 is already saved from reg offset
5636 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5637 */
5638 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5639 instance->reply_post_host_index_addr[loop] =
5640 (u32 __iomem *)
5641 ((u8 __iomem *)instance->reg_set +
5642 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5643 + (loop * 0x10));
5644 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305645 }
5646 if (msix_vectors)
5647 instance->msix_vectors = min(msix_vectors,
5648 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305649 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005650 instance->msix_vectors = 1;
5651 /* Don't bother allocating more MSI-X vectors than cpus */
5652 instance->msix_vectors = min(instance->msix_vectors,
5653 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005654 if (smp_affinity_enable)
5655 irq_flags |= PCI_IRQ_AFFINITY;
5656 i = pci_alloc_irq_vectors(instance->pdev, 1,
5657 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005658 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005659 instance->msix_vectors = i;
5660 else
adam radfordc8e858f2011-10-08 18:15:13 -07005661 instance->msix_vectors = 0;
5662 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005663 /*
5664 * MSI-X host index 0 is common for all adapter.
5665 * It is used for all MPT based Adapters.
5666 */
5667 if (instance->msix_combined) {
5668 instance->reply_post_host_index_addr[0] =
5669 (u32 *)((u8 *)instance->reg_set +
5670 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5671 } else {
5672 instance->reply_post_host_index_addr[0] =
5673 (u32 *)((u8 *)instance->reg_set +
5674 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5675 }
5676
Shivasharan S64ff64b2017-03-10 03:22:12 -08005677 if (!instance->msix_vectors) {
5678 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5679 if (i < 0)
Shivasharan S8a25fa12018-10-16 23:37:44 -07005680 goto fail_init_adapter;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005681 }
adam radford3f1abce2011-05-11 18:33:47 -07005682
Ming Leiadbe5522018-03-13 17:42:40 +08005683 megasas_setup_reply_map(instance);
5684
Tomas Henzl258c3af2015-06-02 16:09:46 +05305685 dev_info(&instance->pdev->dev,
5686 "firmware supports msix\t: (%d)", fw_msix_count);
5687 dev_info(&instance->pdev->dev,
5688 "current msix/online cpus\t: (%d/%d)\n",
5689 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305690 dev_info(&instance->pdev->dev,
5691 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305692
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305693 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5694 (unsigned long)instance);
5695
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305696 /*
5697 * Below are default value for legacy Firmware.
5698 * non-fusion based controllers
5699 */
5700 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5701 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005702 /* Get operational params, sge flags, send init cmd to controller */
5703 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005704 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005705
Shivasharan S630d42b2018-12-17 00:47:37 -08005706 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005707 scratch_pad_3 =
Shivasharan S272652f2018-12-17 00:47:40 -08005708 megasas_readl(instance,
5709 &instance->reg_set->outbound_scratch_pad_3);
Shivasharan S81b76452018-10-16 23:37:51 -07005710 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
Shivasharan S15dd0382017-02-10 00:59:10 -08005711 MR_DEFAULT_NVME_PAGE_SHIFT)
5712 instance->nvme_page_size =
Shivasharan S81b76452018-10-16 23:37:51 -07005713 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
Shivasharan S15dd0382017-02-10 00:59:10 -08005714
5715 dev_info(&instance->pdev->dev,
5716 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5717 }
5718
Tomas Henzl18103ef2016-11-01 17:32:02 +01005719 if (instance->msix_vectors ?
5720 megasas_setup_irqs_msix(instance, 1) :
5721 megasas_setup_irqs_ioapic(instance))
5722 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305723
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305724 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005725
Andy Lutomirski13f30772016-05-03 10:24:31 -07005726 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005727
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305728 megasas_setup_jbod_map(instance);
5729
Shivasharan Sdaa06812019-01-29 01:38:12 -08005730 if (megasas_get_device_list(instance) != SUCCESS) {
5731 dev_err(&instance->pdev->dev,
5732 "%s: megasas_get_device_list failed\n",
5733 __func__);
Shivasharan S72bff2d2017-02-10 00:59:33 -08005734 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005735 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005736
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005737 /* stream detection initialization */
Shivasharan S630d42b2018-12-17 00:47:37 -08005738 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005739 fusion->stream_detect_by_ld =
Kees Cook6396bb22018-06-12 14:03:40 -07005740 kcalloc(MAX_LOGICAL_DRIVES_EXT,
5741 sizeof(struct LD_STREAM_DETECT *),
5742 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005743 if (!fusion->stream_detect_by_ld) {
5744 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005745 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005746 goto fail_get_ld_pd_list;
5747 }
5748 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5749 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08005750 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005751 GFP_KERNEL);
5752 if (!fusion->stream_detect_by_ld[i]) {
5753 dev_err(&instance->pdev->dev,
5754 "unable to allocate stream detect by LD\n ");
5755 for (j = 0; j < i; ++j)
5756 kfree(fusion->stream_detect_by_ld[j]);
5757 kfree(fusion->stream_detect_by_ld);
5758 fusion->stream_detect_by_ld = NULL;
5759 goto fail_get_ld_pd_list;
5760 }
5761 fusion->stream_detect_by_ld[i]->mru_bit_map
5762 = MR_STREAM_BITMAP;
5763 }
5764 }
5765
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005766 /*
5767 * Compute the max allowed sectors per IO: The controller info has two
5768 * limits on max sectors. Driver should use the minimum of these two.
5769 *
5770 * 1 << stripe_sz_ops.min = max sectors per strip
5771 *
5772 * Note that older firmwares ( < FW ver 30) didn't report information
5773 * to calculate max_sectors_1. So the number ended up as zero always.
5774 */
bo yang14faea92007-11-09 04:14:00 -05005775 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07005776 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005777
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305778 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5779 le16_to_cpu(ctrl_info->max_strips_per_io);
5780 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005781
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005782 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305783
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305784 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5785 instance->passive = ctrl_info->cluster.passive;
5786 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305787 instance->UnevenSpanSupport =
5788 ctrl_info->adapterOperations2.supportUnevenSpans;
5789 if (instance->UnevenSpanSupport) {
5790 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08005791 if (MR_ValidateMapInfo(instance, instance->map_id))
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305792 fusion->fast_path_io = 1;
5793 else
5794 fusion->fast_path_io = 0;
5795
5796 }
5797 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305798 instance->requestorId = ctrl_info->iov.requestorId;
5799 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5800 if (!ctrl_info->adapterOperations2.activePassive)
5801 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305802
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305803 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5804 instance->PlasmaFW111 ? "1.11" : "new");
5805
5806 if (instance->PlasmaFW111) {
5807 iovPtr = (struct IOV_111 *)
5808 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5809 instance->requestorId = iovPtr->requestorId;
5810 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305811 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305812 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5813 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305814 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305815
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305816 instance->crash_dump_fw_support =
5817 ctrl_info->adapterOperations3.supportCrashDump;
5818 instance->crash_dump_drv_support =
5819 (instance->crash_dump_fw_support &&
5820 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305821 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305822 megasas_set_crash_dump_params(instance,
5823 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005824
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305825 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305826 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005827 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305828 CRASH_DMA_BUF_SIZE,
5829 instance->crash_dump_buf,
5830 instance->crash_dump_h);
5831 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005832 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305833
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005834 if (instance->snapdump_wait_time) {
5835 megasas_get_snapdump_properties(instance);
5836 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
5837 instance->snapdump_wait_time);
5838 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305839
5840 dev_info(&instance->pdev->dev,
5841 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5842 le16_to_cpu(ctrl_info->pci.vendor_id),
5843 le16_to_cpu(ctrl_info->pci.device_id),
5844 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5845 le16_to_cpu(ctrl_info->pci.sub_device_id));
5846 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5847 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305848 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5849 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305850 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5851 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305852
bo yang14faea92007-11-09 04:14:00 -05005853 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305854 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005855 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5856 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005857
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305858 /* Check for valid throttlequeuedepth module parameter */
5859 if (throttlequeuedepth &&
5860 throttlequeuedepth <= instance->max_scsi_cmds)
5861 instance->throttlequeuedepth = throttlequeuedepth;
5862 else
5863 instance->throttlequeuedepth =
5864 MEGASAS_THROTTLE_QUEUE_DEPTH;
5865
Shivasharan Se636a7a2017-08-23 04:46:56 -07005866 if ((resetwaittime < 1) ||
5867 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305868 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5869
5870 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5871 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005872
adam radford229fe472014-03-10 02:51:56 -07005873 /* Launch SR-IOV heartbeat timer */
5874 if (instance->requestorId) {
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005875 if (!megasas_sriov_start_heartbeat(instance, 1)) {
Kees Cookc251a7b2017-10-22 15:30:04 -07005876 megasas_start_timer(instance);
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005877 } else {
adam radford229fe472014-03-10 02:51:56 -07005878 instance->skip_heartbeat_timer_del = 1;
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005879 goto fail_get_ld_pd_list;
5880 }
adam radford229fe472014-03-10 02:51:56 -07005881 }
5882
Shivasharan S3f6194a2018-10-16 23:37:39 -07005883 /*
5884 * Create and start watchdog thread which will monitor
5885 * controller state every 1 sec and trigger OCR when
5886 * it enters fault state
5887 */
5888 if (instance->adapter_type != MFI_SERIES)
5889 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
5890 goto fail_start_watchdog;
5891
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005892 return 0;
5893
Shivasharan S3f6194a2018-10-16 23:37:39 -07005894fail_start_watchdog:
5895 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5896 del_timer_sync(&instance->sriov_heartbeat_timer);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005897fail_get_ld_pd_list:
5898 instance->instancet->disable_intr(instance);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005899 megasas_destroy_irqs(instance);
Shivasharan S8a25fa12018-10-16 23:37:44 -07005900fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305901 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005902 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305903 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07005904fail_alloc_dma_buf:
5905 megasas_free_ctrl_dma_buffers(instance);
5906 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005907fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005908 iounmap(instance->reg_set);
5909
Shivasharan S72bff2d2017-02-10 00:59:33 -08005910fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005911 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005912
Shivasharan S72bff2d2017-02-10 00:59:33 -08005913 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5914 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005915 return -EINVAL;
5916}
5917
5918/**
5919 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005920 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005921 */
5922static void megasas_release_mfi(struct megasas_instance *instance)
5923{
adam radford9c915a82010-12-21 13:34:31 -08005924 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005925
adam radford9c915a82010-12-21 13:34:31 -08005926 if (instance->reply_queue)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005927 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005928 instance->reply_queue, instance->reply_queue_h);
5929
5930 megasas_free_cmds(instance);
5931
5932 iounmap(instance->reg_set);
5933
Yinghai Lue7f85162016-08-05 23:37:34 -07005934 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005935}
5936
5937/**
5938 * megasas_get_seq_num - Gets latest event sequence numbers
5939 * @instance: Adapter soft state
5940 * @eli: FW event log sequence numbers information
5941 *
5942 * FW maintains a log of all events in a non-volatile area. Upper layers would
5943 * usually find out the latest sequence number of the events, the seq number at
5944 * the boot etc. They would "read" all the events below the latest seq number
5945 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5946 * number), they would subsribe to AEN (asynchronous event notification) and
5947 * wait for the events to happen.
5948 */
5949static int
5950megasas_get_seq_num(struct megasas_instance *instance,
5951 struct megasas_evt_log_info *eli)
5952{
5953 struct megasas_cmd *cmd;
5954 struct megasas_dcmd_frame *dcmd;
5955 struct megasas_evt_log_info *el_info;
5956 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08005957 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005958
5959 cmd = megasas_get_cmd(instance);
5960
5961 if (!cmd) {
5962 return -ENOMEM;
5963 }
5964
5965 dcmd = &cmd->frame->dcmd;
Luis Chamberlain750afb02019-01-04 09:23:09 +01005966 el_info = dma_alloc_coherent(&instance->pdev->dev,
5967 sizeof(struct megasas_evt_log_info),
5968 &el_info_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005969 if (!el_info) {
5970 megasas_return_cmd(instance, cmd);
5971 return -ENOMEM;
5972 }
5973
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005974 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5975
5976 dcmd->cmd = MFI_CMD_DCMD;
5977 dcmd->cmd_status = 0x0;
5978 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005979 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005980 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005981 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305982 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5983 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07005984
5985 megasas_set_dma_settings(instance, dcmd, el_info_h,
5986 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005987
Shivasharan Sb051cc62018-01-05 05:27:38 -08005988 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5989 if (ret != DCMD_SUCCESS) {
5990 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5991 __func__, __LINE__);
5992 goto dcmd_failed;
5993 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005994
Shivasharan Sb051cc62018-01-05 05:27:38 -08005995 /*
5996 * Copy the data back into callers buffer
5997 */
5998 eli->newest_seq_num = el_info->newest_seq_num;
5999 eli->oldest_seq_num = el_info->oldest_seq_num;
6000 eli->clear_seq_num = el_info->clear_seq_num;
6001 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6002 eli->boot_seq_num = el_info->boot_seq_num;
6003
6004dcmd_failed:
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006005 dma_free_coherent(&instance->pdev->dev,
6006 sizeof(struct megasas_evt_log_info),
6007 el_info, el_info_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006008
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306009 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006010
Shivasharan Sb051cc62018-01-05 05:27:38 -08006011 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006012}
6013
6014/**
6015 * megasas_register_aen - Registers for asynchronous event notification
6016 * @instance: Adapter soft state
6017 * @seq_num: The starting sequence number
6018 * @class_locale: Class of the event
6019 *
6020 * This function subscribes for AEN for events beyond the @seq_num. It requests
6021 * to be notified if and only if the event is of type @class_locale
6022 */
6023static int
6024megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6025 u32 class_locale_word)
6026{
6027 int ret_val;
6028 struct megasas_cmd *cmd;
6029 struct megasas_dcmd_frame *dcmd;
6030 union megasas_evt_class_locale curr_aen;
6031 union megasas_evt_class_locale prev_aen;
6032
6033 /*
6034 * If there an AEN pending already (aen_cmd), check if the
6035 * class_locale of that pending AEN is inclusive of the new
6036 * AEN request we currently have. If it is, then we don't have
6037 * to do anything. In other words, whichever events the current
6038 * AEN request is subscribing to, have already been subscribed
6039 * to.
6040 *
6041 * If the old_cmd is _not_ inclusive, then we have to abort
6042 * that command, form a class_locale that is superset of both
6043 * old and current and re-issue to the FW
6044 */
6045
6046 curr_aen.word = class_locale_word;
6047
6048 if (instance->aen_cmd) {
6049
Christoph Hellwiga9555532015-04-23 16:34:24 +05306050 prev_aen.word =
6051 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006052
Shivasharan S91b3d9f2017-08-23 04:47:01 -07006053 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6054 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6055 dev_info(&instance->pdev->dev,
6056 "%s %d out of range class %d send by application\n",
6057 __func__, __LINE__, curr_aen.members.class);
6058 return 0;
6059 }
6060
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006061 /*
6062 * A class whose enum value is smaller is inclusive of all
6063 * higher values. If a PROGRESS (= -1) was previously
6064 * registered, then a new registration requests for higher
6065 * classes need not be sent to FW. They are automatically
6066 * included.
6067 *
6068 * Locale numbers don't have such hierarchy. They are bitmap
6069 * values
6070 */
6071 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306072 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006073 curr_aen.members.locale)) {
6074 /*
6075 * Previously issued event registration includes
6076 * current request. Nothing to do.
6077 */
6078 return 0;
6079 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306080 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006081
6082 if (prev_aen.members.class < curr_aen.members.class)
6083 curr_aen.members.class = prev_aen.members.class;
6084
6085 instance->aen_cmd->abort_aen = 1;
6086 ret_val = megasas_issue_blocked_abort_cmd(instance,
6087 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306088 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006089
6090 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006091 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006092 "previous AEN command\n");
6093 return ret_val;
6094 }
6095 }
6096 }
6097
6098 cmd = megasas_get_cmd(instance);
6099
6100 if (!cmd)
6101 return -ENOMEM;
6102
6103 dcmd = &cmd->frame->dcmd;
6104
6105 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6106
6107 /*
6108 * Prepare DCMD for aen registration
6109 */
6110 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6111
6112 dcmd->cmd = MFI_CMD_DCMD;
6113 dcmd->cmd_status = 0x0;
6114 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006115 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006116 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006117 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306118 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6119 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6120 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04006121 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306122 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07006123
6124 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6125 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006126
Yang, Bof4c9a132009-10-06 14:43:28 -06006127 if (instance->aen_cmd != NULL) {
6128 megasas_return_cmd(instance, cmd);
6129 return 0;
6130 }
6131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006132 /*
6133 * Store reference to the cmd used to register for AEN. When an
6134 * application wants us to register for AEN, we have to abort this
6135 * cmd and re-register with a new EVENT LOCALE supplied by that app
6136 */
6137 instance->aen_cmd = cmd;
6138
6139 /*
6140 * Issue the aen registration frame
6141 */
adam radford9c915a82010-12-21 13:34:31 -08006142 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006143
6144 return 0;
6145}
6146
Shivasharan S96188a82017-02-10 00:59:11 -08006147/* megasas_get_target_prop - Send DCMD with below details to firmware.
6148 *
6149 * This DCMD will fetch few properties of LD/system PD defined
6150 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6151 *
6152 * DCMD send by drivers whenever new target is added to the OS.
6153 *
6154 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6155 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6156 * 0 = system PD, 1 = LD.
6157 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6158 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6159 *
6160 * @instance: Adapter soft state
6161 * @sdev: OS provided scsi device
6162 *
6163 * Returns 0 on success non-zero on failure.
6164 */
Shivasharan Se9495e22018-06-04 03:45:12 -07006165int
Shivasharan S96188a82017-02-10 00:59:11 -08006166megasas_get_target_prop(struct megasas_instance *instance,
6167 struct scsi_device *sdev)
6168{
6169 int ret;
6170 struct megasas_cmd *cmd;
6171 struct megasas_dcmd_frame *dcmd;
6172 u16 targetId = (sdev->channel % 2) + sdev->id;
6173
6174 cmd = megasas_get_cmd(instance);
6175
6176 if (!cmd) {
6177 dev_err(&instance->pdev->dev,
6178 "Failed to get cmd %s\n", __func__);
6179 return -ENOMEM;
6180 }
6181
6182 dcmd = &cmd->frame->dcmd;
6183
6184 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6185 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6186 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6187
6188 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6189 dcmd->cmd = MFI_CMD_DCMD;
6190 dcmd->cmd_status = 0xFF;
6191 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006192 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08006193 dcmd->timeout = 0;
6194 dcmd->pad_0 = 0;
6195 dcmd->data_xfer_len =
6196 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6197 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08006198
Shivasharan S107a60d2017-10-19 02:49:05 -07006199 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6200 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08006201
Shivasharan Se7d36b82017-10-19 02:48:50 -07006202 if ((instance->adapter_type != MFI_SERIES) &&
6203 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08006204 ret = megasas_issue_blocked_cmd(instance,
6205 cmd, MFI_IO_TIMEOUT_SECS);
6206 else
6207 ret = megasas_issue_polled(instance, cmd);
6208
6209 switch (ret) {
6210 case DCMD_TIMEOUT:
6211 switch (dcmd_timeout_ocr_possible(instance)) {
6212 case INITIATE_OCR:
6213 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6214 megasas_reset_fusion(instance->host,
6215 MFI_IO_TIMEOUT_OCR);
6216 break;
6217 case KILL_ADAPTER:
6218 megaraid_sas_kill_hba(instance);
6219 break;
6220 case IGNORE_TIMEOUT:
6221 dev_info(&instance->pdev->dev,
6222 "Ignore DCMD timeout: %s %d\n",
6223 __func__, __LINE__);
6224 break;
6225 }
6226 break;
6227
6228 default:
6229 megasas_return_cmd(instance, cmd);
6230 }
6231 if (ret != DCMD_SUCCESS)
6232 dev_err(&instance->pdev->dev,
6233 "return from %s %d return value %d\n",
6234 __func__, __LINE__, ret);
6235
6236 return ret;
6237}
6238
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006239/**
6240 * megasas_start_aen - Subscribes to AEN during driver load time
6241 * @instance: Adapter soft state
6242 */
6243static int megasas_start_aen(struct megasas_instance *instance)
6244{
6245 struct megasas_evt_log_info eli;
6246 union megasas_evt_class_locale class_locale;
6247
6248 /*
6249 * Get the latest sequence number from FW
6250 */
6251 memset(&eli, 0, sizeof(eli));
6252
6253 if (megasas_get_seq_num(instance, &eli))
6254 return -1;
6255
6256 /*
6257 * Register AEN with FW for latest sequence number plus 1
6258 */
6259 class_locale.members.reserved = 0;
6260 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6261 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6262
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306263 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05306264 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306265 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006266}
6267
6268/**
6269 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6270 * @instance: Adapter soft state
6271 */
6272static int megasas_io_attach(struct megasas_instance *instance)
6273{
6274 struct Scsi_Host *host = instance->host;
6275
6276 /*
6277 * Export parameters required by SCSI mid-layer
6278 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006279 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306280 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006281 host->this_id = instance->init_id;
6282 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08006283
6284 if (instance->fw_support_ieee)
6285 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6286
Yang, Bo1fd10682010-10-12 07:18:50 -06006287 /*
6288 * Check if the module parameter value for max_sectors can be used
6289 */
6290 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6291 instance->max_sectors_per_req = max_sectors;
6292 else {
6293 if (max_sectors) {
6294 if (((instance->pdev->device ==
6295 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6296 (instance->pdev->device ==
6297 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6298 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6299 instance->max_sectors_per_req = max_sectors;
6300 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006301 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06006302 "and <= %d (or < 1MB for GEN2 controller)\n",
6303 instance->max_sectors_per_req);
6304 }
6305 }
6306 }
6307
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006308 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08006309 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006310 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6311 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6312 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08006313 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006314
6315 /*
6316 * Notify the mid-layer about the new controller
6317 */
6318 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306319 dev_err(&instance->pdev->dev,
6320 "Failed to add host from %s %d\n",
6321 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006322 return -ENODEV;
6323 }
6324
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006325 return 0;
6326}
6327
Shivasharan S107a60d2017-10-19 02:49:05 -07006328/**
6329 * megasas_set_dma_mask - Set DMA mask for supported controllers
6330 *
6331 * @instance: Adapter soft state
6332 * Description:
6333 *
Shivasharan S894169d2018-12-18 05:59:54 -08006334 * For Ventura, driver/FW will operate in 63bit DMA addresses.
Shivasharan S107a60d2017-10-19 02:49:05 -07006335 *
6336 * For invader-
6337 * By default, driver/FW will operate in 32bit DMA addresses
6338 * for consistent DMA mapping but if 32 bit consistent
Shivasharan S894169d2018-12-18 05:59:54 -08006339 * DMA mask fails, driver will try with 63 bit consistent
6340 * mask provided FW is true 63bit DMA capable
Shivasharan S107a60d2017-10-19 02:49:05 -07006341 *
6342 * For older controllers(Thunderbolt and MFI based adapters)-
6343 * driver/FW will operate in 32 bit consistent DMA addresses.
6344 */
bo yang31ea7082007-11-07 12:09:50 -05006345static int
Shivasharan S107a60d2017-10-19 02:49:05 -07006346megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05006347{
Shivasharan S107a60d2017-10-19 02:49:05 -07006348 u64 consistent_mask;
6349 struct pci_dev *pdev;
Shivasharan S81b76452018-10-16 23:37:51 -07006350 u32 scratch_pad_1;
bo yang31ea7082007-11-07 12:09:50 -05006351
Shivasharan S107a60d2017-10-19 02:49:05 -07006352 pdev = instance->pdev;
Shivasharan S630d42b2018-12-17 00:47:37 -08006353 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
Shivasharan S894169d2018-12-18 05:59:54 -08006354 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
Shivasharan S107a60d2017-10-19 02:49:05 -07006355
6356 if (IS_DMA64) {
Shivasharan S894169d2018-12-18 05:59:54 -08006357 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006358 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6359 goto fail_set_dma_mask;
6360
Shivasharan S894169d2018-12-18 05:59:54 -08006361 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006362 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6363 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6364 /*
6365 * If 32 bit DMA mask fails, then try for 64 bit mask
6366 * for FW capable of handling 64 bit DMA.
6367 */
Shivasharan S272652f2018-12-17 00:47:40 -08006368 scratch_pad_1 = megasas_readl
6369 (instance, &instance->reg_set->outbound_scratch_pad_1);
Shivasharan S107a60d2017-10-19 02:49:05 -07006370
Shivasharan S81b76452018-10-16 23:37:51 -07006371 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
Shivasharan S107a60d2017-10-19 02:49:05 -07006372 goto fail_set_dma_mask;
6373 else if (dma_set_mask_and_coherent(&pdev->dev,
Shivasharan S894169d2018-12-18 05:59:54 -08006374 DMA_BIT_MASK(63)))
bo yang31ea7082007-11-07 12:09:50 -05006375 goto fail_set_dma_mask;
6376 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006377 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6378 goto fail_set_dma_mask;
6379
6380 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6381 instance->consistent_mask_64bit = false;
6382 else
6383 instance->consistent_mask_64bit = true;
6384
6385 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
Tomas Henzld1f38d92019-01-02 16:07:25 +01006386 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
Shivasharan S894169d2018-12-18 05:59:54 -08006387 (instance->consistent_mask_64bit ? "63" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306388
bo yang31ea7082007-11-07 12:09:50 -05006389 return 0;
6390
6391fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006392 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6393 return -1;
6394
bo yang31ea7082007-11-07 12:09:50 -05006395}
6396
Shivasharan Sc3651782017-10-19 02:48:48 -07006397/*
6398 * megasas_set_adapter_type - Set adapter type.
6399 * Supported controllers can be divided in
Shivasharan S154a7cd2018-12-17 00:47:38 -08006400 * different categories-
6401 * enum MR_ADAPTER_TYPE {
6402 * MFI_SERIES = 1,
6403 * THUNDERBOLT_SERIES = 2,
6404 * INVADER_SERIES = 3,
6405 * VENTURA_SERIES = 4,
6406 * AERO_SERIES = 5,
6407 * };
Shivasharan Sc3651782017-10-19 02:48:48 -07006408 * @instance: Adapter soft state
6409 * return: void
6410 */
6411static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6412{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006413 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6414 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006415 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006416 } else {
6417 switch (instance->pdev->device) {
Shivasharan S469f72d2018-11-09 09:47:20 -08006418 case PCI_DEVICE_ID_LSI_AERO_10E1:
6419 case PCI_DEVICE_ID_LSI_AERO_10E2:
6420 case PCI_DEVICE_ID_LSI_AERO_10E5:
6421 case PCI_DEVICE_ID_LSI_AERO_10E6:
Shivasharan S154a7cd2018-12-17 00:47:38 -08006422 instance->adapter_type = AERO_SERIES;
6423 break;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006424 case PCI_DEVICE_ID_LSI_VENTURA:
6425 case PCI_DEVICE_ID_LSI_CRUSADER:
6426 case PCI_DEVICE_ID_LSI_HARPOON:
6427 case PCI_DEVICE_ID_LSI_TOMCAT:
6428 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6429 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6430 instance->adapter_type = VENTURA_SERIES;
6431 break;
6432 case PCI_DEVICE_ID_LSI_FUSION:
6433 case PCI_DEVICE_ID_LSI_PLASMA:
6434 instance->adapter_type = THUNDERBOLT_SERIES;
6435 break;
6436 case PCI_DEVICE_ID_LSI_INVADER:
6437 case PCI_DEVICE_ID_LSI_INTRUDER:
6438 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6439 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6440 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6441 case PCI_DEVICE_ID_LSI_FURY:
6442 instance->adapter_type = INVADER_SERIES;
6443 break;
6444 default: /* For all other supported controllers */
6445 instance->adapter_type = MFI_SERIES;
6446 break;
6447 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006448 }
6449}
6450
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006451static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6452{
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006453 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6454 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6455 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6456 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006457
6458 if (!instance->producer || !instance->consumer) {
6459 dev_err(&instance->pdev->dev,
6460 "Failed to allocate memory for producer, consumer\n");
6461 return -1;
6462 }
6463
6464 *instance->producer = 0;
6465 *instance->consumer = 0;
6466 return 0;
6467}
6468
6469/**
6470 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6471 * structures which are not common across MFI
6472 * adapters and fusion adapters.
6473 * For MFI based adapters, allocate producer and
6474 * consumer buffers. For fusion adapters, allocate
6475 * memory for fusion context.
6476 * @instance: Adapter soft state
6477 * return: 0 for SUCCESS
6478 */
6479static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6480{
Kees Cook6396bb22018-06-12 14:03:40 -07006481 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
Ming Leiadbe5522018-03-13 17:42:40 +08006482 GFP_KERNEL);
6483 if (!instance->reply_map)
6484 return -ENOMEM;
6485
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006486 switch (instance->adapter_type) {
6487 case MFI_SERIES:
6488 if (megasas_alloc_mfi_ctrl_mem(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006489 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006490 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08006491 case AERO_SERIES:
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006492 case VENTURA_SERIES:
6493 case THUNDERBOLT_SERIES:
6494 case INVADER_SERIES:
6495 if (megasas_alloc_fusion_context(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006496 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006497 break;
6498 }
6499
6500 return 0;
Ming Leiadbe5522018-03-13 17:42:40 +08006501 fail:
6502 kfree(instance->reply_map);
6503 instance->reply_map = NULL;
6504 return -ENOMEM;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006505}
6506
6507/*
6508 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6509 * producer, consumer buffers for MFI adapters
6510 *
6511 * @instance - Adapter soft instance
6512 *
6513 */
6514static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6515{
Ming Leiadbe5522018-03-13 17:42:40 +08006516 kfree(instance->reply_map);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006517 if (instance->adapter_type == MFI_SERIES) {
6518 if (instance->producer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006519 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006520 instance->producer,
6521 instance->producer_h);
6522 if (instance->consumer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006523 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006524 instance->consumer,
6525 instance->consumer_h);
6526 } else {
6527 megasas_free_fusion_context(instance);
6528 }
6529}
6530
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006531/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006532 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6533 * driver load time
6534 *
6535 * @instance- Adapter soft instance
6536 * @return- O for SUCCESS
6537 */
6538static inline
6539int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6540{
6541 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006542 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006543
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006544 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6545 sizeof(struct megasas_evt_detail),
6546 &instance->evt_detail_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006547
6548 if (!instance->evt_detail) {
6549 dev_err(&instance->pdev->dev,
6550 "Failed to allocate event detail buffer\n");
6551 return -ENOMEM;
6552 }
6553
Shivasharan S9b3d0282017-10-19 02:48:56 -07006554 if (fusion) {
6555 fusion->ioc_init_request =
6556 dma_alloc_coherent(&pdev->dev,
6557 sizeof(struct MPI2_IOC_INIT_REQUEST),
6558 &fusion->ioc_init_request_phys,
6559 GFP_KERNEL);
6560
6561 if (!fusion->ioc_init_request) {
6562 dev_err(&pdev->dev,
6563 "Failed to allocate PD list buffer\n");
6564 return -ENOMEM;
6565 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006566
6567 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6568 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6569 &instance->snapdump_prop_h, GFP_KERNEL);
6570
6571 if (!instance->snapdump_prop)
6572 dev_err(&pdev->dev,
6573 "Failed to allocate snapdump properties buffer\n");
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006574
6575 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6576 HOST_DEVICE_LIST_SZ,
6577 &instance->host_device_list_buf_h,
6578 GFP_KERNEL);
6579
6580 if (!instance->host_device_list_buf) {
6581 dev_err(&pdev->dev,
6582 "Failed to allocate targetid list buffer\n");
6583 return -ENOMEM;
6584 }
6585
Shivasharan S9b3d0282017-10-19 02:48:56 -07006586 }
6587
6588 instance->pd_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006589 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006590 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006591 &instance->pd_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006592
6593 if (!instance->pd_list_buf) {
6594 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6595 return -ENOMEM;
6596 }
6597
6598 instance->ctrl_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006599 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006600 sizeof(struct megasas_ctrl_info),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006601 &instance->ctrl_info_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006602
6603 if (!instance->ctrl_info_buf) {
6604 dev_err(&pdev->dev,
6605 "Failed to allocate controller info buffer\n");
6606 return -ENOMEM;
6607 }
6608
6609 instance->ld_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006610 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006611 sizeof(struct MR_LD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006612 &instance->ld_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006613
6614 if (!instance->ld_list_buf) {
6615 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6616 return -ENOMEM;
6617 }
6618
6619 instance->ld_targetid_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006620 dma_alloc_coherent(&pdev->dev,
6621 sizeof(struct MR_LD_TARGETID_LIST),
6622 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006623
6624 if (!instance->ld_targetid_list_buf) {
6625 dev_err(&pdev->dev,
6626 "Failed to allocate LD targetid list buffer\n");
6627 return -ENOMEM;
6628 }
6629
Shivasharan S1b4bed22017-10-19 02:48:55 -07006630 if (!reset_devices) {
6631 instance->system_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006632 dma_alloc_coherent(&pdev->dev,
6633 sizeof(struct MR_DRV_SYSTEM_INFO),
6634 &instance->system_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006635 instance->pd_info =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006636 dma_alloc_coherent(&pdev->dev,
6637 sizeof(struct MR_PD_INFO),
6638 &instance->pd_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006639 instance->tgt_prop =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006640 dma_alloc_coherent(&pdev->dev,
6641 sizeof(struct MR_TARGET_PROPERTIES),
6642 &instance->tgt_prop_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006643 instance->crash_dump_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006644 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6645 &instance->crash_dump_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006646
6647 if (!instance->system_info_buf)
6648 dev_err(&instance->pdev->dev,
6649 "Failed to allocate system info buffer\n");
6650
6651 if (!instance->pd_info)
6652 dev_err(&instance->pdev->dev,
6653 "Failed to allocate pd_info buffer\n");
6654
6655 if (!instance->tgt_prop)
6656 dev_err(&instance->pdev->dev,
6657 "Failed to allocate tgt_prop buffer\n");
6658
6659 if (!instance->crash_dump_buf)
6660 dev_err(&instance->pdev->dev,
6661 "Failed to allocate crash dump buffer\n");
6662 }
6663
6664 return 0;
6665}
6666
6667/*
6668 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6669 * during driver load time
6670 *
6671 * @instance- Adapter soft instance
6672 *
6673 */
6674static inline
6675void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6676{
6677 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006678 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006679
6680 if (instance->evt_detail)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006681 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006682 instance->evt_detail,
6683 instance->evt_detail_h);
6684
Shivasharan S9b3d0282017-10-19 02:48:56 -07006685 if (fusion && fusion->ioc_init_request)
6686 dma_free_coherent(&pdev->dev,
6687 sizeof(struct MPI2_IOC_INIT_REQUEST),
6688 fusion->ioc_init_request,
6689 fusion->ioc_init_request_phys);
6690
6691 if (instance->pd_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006692 dma_free_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006693 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6694 instance->pd_list_buf,
6695 instance->pd_list_buf_h);
6696
6697 if (instance->ld_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006698 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006699 instance->ld_list_buf,
6700 instance->ld_list_buf_h);
6701
6702 if (instance->ld_targetid_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006703 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006704 instance->ld_targetid_list_buf,
6705 instance->ld_targetid_list_buf_h);
6706
6707 if (instance->ctrl_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006708 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006709 instance->ctrl_info_buf,
6710 instance->ctrl_info_buf_h);
6711
Shivasharan S1b4bed22017-10-19 02:48:55 -07006712 if (instance->system_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006713 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006714 instance->system_info_buf,
6715 instance->system_info_h);
6716
6717 if (instance->pd_info)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006718 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006719 instance->pd_info, instance->pd_info_h);
6720
6721 if (instance->tgt_prop)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006722 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006723 instance->tgt_prop, instance->tgt_prop_h);
6724
6725 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006726 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
Shivasharan S1b4bed22017-10-19 02:48:55 -07006727 instance->crash_dump_buf,
6728 instance->crash_dump_h);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006729
6730 if (instance->snapdump_prop)
6731 dma_free_coherent(&pdev->dev,
6732 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6733 instance->snapdump_prop,
6734 instance->snapdump_prop_h);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006735
6736 if (instance->host_device_list_buf)
6737 dma_free_coherent(&pdev->dev,
6738 HOST_DEVICE_LIST_SZ,
6739 instance->host_device_list_buf,
6740 instance->host_device_list_buf_h);
6741
Shivasharan S1b4bed22017-10-19 02:48:55 -07006742}
6743
Shivasharan S7535f272017-10-19 02:48:58 -07006744/*
6745 * megasas_init_ctrl_params - Initialize controller's instance
6746 * parameters before FW init
6747 * @instance - Adapter soft instance
6748 * @return - void
6749 */
6750static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6751{
6752 instance->fw_crash_state = UNAVAILABLE;
6753
6754 megasas_poll_wait_aen = 0;
6755 instance->issuepend_done = 1;
6756 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6757
6758 /*
6759 * Initialize locks and queues
6760 */
6761 INIT_LIST_HEAD(&instance->cmd_pool);
6762 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6763
6764 atomic_set(&instance->fw_outstanding, 0);
6765
6766 init_waitqueue_head(&instance->int_cmd_wait_q);
6767 init_waitqueue_head(&instance->abort_cmd_wait_q);
6768
6769 spin_lock_init(&instance->crashdump_lock);
6770 spin_lock_init(&instance->mfi_pool_lock);
6771 spin_lock_init(&instance->hba_lock);
6772 spin_lock_init(&instance->stream_lock);
6773 spin_lock_init(&instance->completion_lock);
6774
Shivasharan S7535f272017-10-19 02:48:58 -07006775 mutex_init(&instance->reset_mutex);
6776
6777 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6778 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6779 instance->flag_ieee = 1;
6780
6781 megasas_dbg_lvl = 0;
6782 instance->flag = 0;
6783 instance->unload = 1;
6784 instance->last_time = 0;
6785 instance->disableOnlineCtrlReset = 1;
6786 instance->UnevenSpanSupport = 0;
6787
Shivasharan S3f6194a2018-10-16 23:37:39 -07006788 if (instance->adapter_type != MFI_SERIES)
Shivasharan S7535f272017-10-19 02:48:58 -07006789 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Shivasharan S3f6194a2018-10-16 23:37:39 -07006790 else
Shivasharan S7535f272017-10-19 02:48:58 -07006791 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006792}
6793
6794/**
6795 * megasas_probe_one - PCI hotplug entry point
6796 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006797 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006798 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006799static int megasas_probe_one(struct pci_dev *pdev,
6800 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006801{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306802 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006803 struct Scsi_Host *host;
6804 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006805 u16 control = 0;
6806
Shivasharan S469f72d2018-11-09 09:47:20 -08006807 switch (pdev->device) {
6808 case PCI_DEVICE_ID_LSI_AERO_10E1:
6809 case PCI_DEVICE_ID_LSI_AERO_10E5:
6810 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
6811 break;
6812 }
6813
adam radford66192dfe2011-02-24 20:56:28 -08006814 /* Reset MSI-X in the kdump kernel */
6815 if (reset_devices) {
6816 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6817 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006818 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006819 &control);
6820 if (control & PCI_MSIX_FLAGS_ENABLE) {
6821 dev_info(&pdev->dev, "resetting MSI-X\n");
6822 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006823 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006824 control &
6825 ~PCI_MSIX_FLAGS_ENABLE);
6826 }
6827 }
6828 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006829
6830 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006831 * PCI prepping: enable device set bus mastering and dma mask
6832 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006833 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006834
6835 if (rval) {
6836 return rval;
6837 }
6838
6839 pci_set_master(pdev);
6840
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006841 host = scsi_host_alloc(&megasas_template,
6842 sizeof(struct megasas_instance));
6843
6844 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006845 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006846 goto fail_alloc_instance;
6847 }
6848
6849 instance = (struct megasas_instance *)host->hostdata;
6850 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006851 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006852
6853 /*
6854 * Initialize PCI related and misc parameters
6855 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006856 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006857 instance->host = host;
6858 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6859 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6860
Shivasharan Sc3651782017-10-19 02:48:48 -07006861 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07006862
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006863 /*
adam radford0a770662011-02-24 20:56:12 -08006864 * Initialize MFI Firmware
6865 */
6866 if (megasas_init_fw(instance))
6867 goto fail_init_mfi;
6868
adam radford229fe472014-03-10 02:51:56 -07006869 if (instance->requestorId) {
6870 if (instance->PlasmaFW111) {
6871 instance->vf_affiliation_111 =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006872 dma_alloc_coherent(&pdev->dev,
6873 sizeof(struct MR_LD_VF_AFFILIATION_111),
6874 &instance->vf_affiliation_111_h,
6875 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07006876 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006877 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006878 "memory for VF affiliation buffer\n");
6879 } else {
6880 instance->vf_affiliation =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006881 dma_alloc_coherent(&pdev->dev,
6882 (MAX_LOGICAL_DRIVES + 1) *
6883 sizeof(struct MR_LD_VF_AFFILIATION),
6884 &instance->vf_affiliation_h,
6885 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07006886 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006887 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006888 "memory for VF affiliation buffer\n");
6889 }
6890 }
6891
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006892 /*
6893 * Store instance in PCI softstate
6894 */
6895 pci_set_drvdata(pdev, instance);
6896
6897 /*
6898 * Add this controller to megasas_mgmt_info structure so that it
6899 * can be exported to management applications
6900 */
6901 megasas_mgmt_info.count++;
6902 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6903 megasas_mgmt_info.max_index++;
6904
6905 /*
adam radford541f90b2011-05-11 18:34:29 -07006906 * Register with SCSI mid-layer
6907 */
6908 if (megasas_io_attach(instance))
6909 goto fail_io_attach;
6910
6911 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306912 /*
6913 * Trigger SCSI to scan our drives
6914 */
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006915 if (!instance->enable_fw_dev_list ||
6916 (instance->host_device_list_buf->count > 0))
6917 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006918
6919 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006920 * Initiate AEN (Asynchronous Event Notification)
6921 */
6922 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006923 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006924 goto fail_start_aen;
6925 }
6926
Adam Radford9ea81f82014-07-09 15:17:57 -07006927 /* Get current SR-IOV LD/VF affiliation */
6928 if (instance->requestorId)
6929 megasas_get_ld_vf_affiliation(instance, 1);
6930
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006931 return 0;
6932
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006933fail_start_aen:
6934fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006935 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006936 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08006937 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006938
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306939 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306940 megasas_destroy_irqs(instance);
6941
Shivasharan Se7d36b82017-10-19 02:48:50 -07006942 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08006943 megasas_release_fusion(instance);
6944 else
6945 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006946 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006947 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306948fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006949 scsi_host_put(host);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006950fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006951 pci_disable_device(pdev);
6952
6953 return -ENODEV;
6954}
6955
6956/**
6957 * megasas_flush_cache - Requests FW to flush all its caches
6958 * @instance: Adapter soft state
6959 */
6960static void megasas_flush_cache(struct megasas_instance *instance)
6961{
6962 struct megasas_cmd *cmd;
6963 struct megasas_dcmd_frame *dcmd;
6964
Sumit Saxena8a01a412016-01-28 21:04:32 +05306965 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006966 return;
6967
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006968 cmd = megasas_get_cmd(instance);
6969
6970 if (!cmd)
6971 return;
6972
6973 dcmd = &cmd->frame->dcmd;
6974
6975 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6976
6977 dcmd->cmd = MFI_CMD_DCMD;
6978 dcmd->cmd_status = 0x0;
6979 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306980 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006981 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006982 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006983 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306984 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006985 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6986
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306987 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6988 != DCMD_SUCCESS) {
6989 dev_err(&instance->pdev->dev,
6990 "return from %s %d\n", __func__, __LINE__);
6991 return;
6992 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006993
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306994 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006995}
6996
6997/**
6998 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6999 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05007000 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007001 */
bo yang31ea7082007-11-07 12:09:50 -05007002static void megasas_shutdown_controller(struct megasas_instance *instance,
7003 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007004{
7005 struct megasas_cmd *cmd;
7006 struct megasas_dcmd_frame *dcmd;
7007
Sumit Saxena8a01a412016-01-28 21:04:32 +05307008 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007009 return;
7010
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007011 cmd = megasas_get_cmd(instance);
7012
7013 if (!cmd)
7014 return;
7015
7016 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307017 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307018 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08007019 if (instance->map_update_cmd)
7020 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307021 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307022 if (instance->jbod_seq_cmd)
7023 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307024 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307025
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007026 dcmd = &cmd->frame->dcmd;
7027
7028 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7029
7030 dcmd->cmd = MFI_CMD_DCMD;
7031 dcmd->cmd_status = 0x0;
7032 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307033 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007034 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007035 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007036 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307037 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007038
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307039 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7040 != DCMD_SUCCESS) {
7041 dev_err(&instance->pdev->dev,
7042 "return from %s %d\n", __func__, __LINE__);
7043 return;
7044 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007045
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307046 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007047}
7048
Jiri Slaby33139b22008-05-01 17:56:02 +02007049#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007050/**
bo yangad84db22007-11-09 04:40:16 -05007051 * megasas_suspend - driver suspend entry point
7052 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05007053 * @state: PCI power state to suspend routine
7054 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007055static int
bo yang31ea7082007-11-07 12:09:50 -05007056megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7057{
7058 struct Scsi_Host *host;
7059 struct megasas_instance *instance;
7060
7061 instance = pci_get_drvdata(pdev);
7062 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06007063 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05007064
adam radford229fe472014-03-10 02:51:56 -07007065 /* Shutdown SR-IOV heartbeat timer */
7066 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7067 del_timer_sync(&instance->sriov_heartbeat_timer);
7068
Shivasharan S3f6194a2018-10-16 23:37:39 -07007069 /* Stop the FW fault detection watchdog */
7070 if (instance->adapter_type != MFI_SERIES)
7071 megasas_fusion_stop_watchdog(instance);
7072
bo yang31ea7082007-11-07 12:09:50 -05007073 megasas_flush_cache(instance);
7074 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007075
7076 /* cancel the delayed work if this work still in queue */
7077 if (instance->ev != NULL) {
7078 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007079 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007080 instance->ev = NULL;
7081 }
7082
bo yang31ea7082007-11-07 12:09:50 -05007083 tasklet_kill(&instance->isr_tasklet);
7084
7085 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307086 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007087
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307088 megasas_destroy_irqs(instance);
7089
adam radfordc8e858f2011-10-08 18:15:13 -07007090 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007091 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05007092
7093 pci_save_state(pdev);
7094 pci_disable_device(pdev);
7095
7096 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7097
7098 return 0;
7099}
7100
7101/**
7102 * megasas_resume- driver resume entry point
7103 * @pdev: PCI device structure
7104 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007105static int
bo yang31ea7082007-11-07 12:09:50 -05007106megasas_resume(struct pci_dev *pdev)
7107{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307108 int rval;
bo yang31ea7082007-11-07 12:09:50 -05007109 struct Scsi_Host *host;
7110 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01007111 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05007112
7113 instance = pci_get_drvdata(pdev);
7114 host = instance->host;
7115 pci_set_power_state(pdev, PCI_D0);
7116 pci_enable_wake(pdev, PCI_D0, 0);
7117 pci_restore_state(pdev);
7118
7119 /*
7120 * PCI prepping: enable device set bus mastering and dma mask
7121 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007122 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05007123
7124 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007125 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05007126 return rval;
7127 }
7128
7129 pci_set_master(pdev);
7130
Shivasharan S107a60d2017-10-19 02:49:05 -07007131 /*
7132 * We expect the FW state to be READY
7133 */
7134 if (megasas_transition_to_ready(instance, 0))
7135 goto fail_ready_state;
7136
7137 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05007138 goto fail_set_dma_mask;
7139
7140 /*
7141 * Initialize MFI Firmware
7142 */
7143
bo yang31ea7082007-11-07 12:09:50 -05007144 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08007145 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05007146
adam radford3f1abce2011-05-11 18:33:47 -07007147 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01007148 if (instance->msix_vectors) {
7149 irq_flags = PCI_IRQ_MSIX;
7150 if (smp_affinity_enable)
7151 irq_flags |= PCI_IRQ_AFFINITY;
7152 }
7153 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7154 instance->msix_vectors ?
7155 instance->msix_vectors : 1, irq_flags);
7156 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02007157 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07007158
Ming Leiadbe5522018-03-13 17:42:40 +08007159 megasas_setup_reply_map(instance);
7160
Shivasharan Se7d36b82017-10-19 02:48:50 -07007161 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007162 megasas_reset_reply_desc(instance);
7163 if (megasas_ioc_init_fusion(instance)) {
7164 megasas_free_cmds(instance);
7165 megasas_free_cmds_fusion(instance);
7166 goto fail_init_mfi;
7167 }
7168 if (!megasas_get_map_info(instance))
7169 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307170 } else {
adam radford9c915a82010-12-21 13:34:31 -08007171 *instance->producer = 0;
7172 *instance->consumer = 0;
7173 if (megasas_issue_init_mfi(instance))
7174 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08007175 }
bo yang31ea7082007-11-07 12:09:50 -05007176
Shivasharan Sc3b10a52018-06-04 03:45:10 -07007177 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7178 goto fail_init_mfi;
7179
adam radford9c915a82010-12-21 13:34:31 -08007180 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7181 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05007182
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307183 if (instance->msix_vectors ?
7184 megasas_setup_irqs_msix(instance, 0) :
7185 megasas_setup_irqs_ioapic(instance))
7186 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05007187
adam radford229fe472014-03-10 02:51:56 -07007188 /* Re-launch SR-IOV heartbeat timer */
7189 if (instance->requestorId) {
7190 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07007191 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307192 else {
adam radford229fe472014-03-10 02:51:56 -07007193 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307194 goto fail_init_mfi;
7195 }
adam radford229fe472014-03-10 02:51:56 -07007196 }
7197
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307198 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307199 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06007200 instance->unload = 0;
7201
adam radford541f90b2011-05-11 18:34:29 -07007202 /*
7203 * Initiate AEN (Asynchronous Event Notification)
7204 */
7205 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007206 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07007207
Shivasharan S3f6194a2018-10-16 23:37:39 -07007208 /* Re-launch FW fault watchdog */
7209 if (instance->adapter_type != MFI_SERIES)
7210 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7211 goto fail_start_watchdog;
7212
bo yang31ea7082007-11-07 12:09:50 -05007213 return 0;
7214
Shivasharan S3f6194a2018-10-16 23:37:39 -07007215fail_start_watchdog:
7216 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7217 del_timer_sync(&instance->sriov_heartbeat_timer);
bo yang31ea7082007-11-07 12:09:50 -05007218fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07007219 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007220 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05007221 scsi_host_put(host);
7222
Shivasharan S107a60d2017-10-19 02:49:05 -07007223fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05007224fail_set_dma_mask:
7225fail_ready_state:
7226
7227 pci_disable_device(pdev);
7228
7229 return -ENODEV;
7230}
Jiri Slaby33139b22008-05-01 17:56:02 +02007231#else
7232#define megasas_suspend NULL
7233#define megasas_resume NULL
7234#endif
bo yang31ea7082007-11-07 12:09:50 -05007235
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007236static inline int
7237megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7238{
7239 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7240 int i;
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007241 u8 adp_state;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007242
7243 for (i = 0; i < wait_time; i++) {
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007244 adp_state = atomic_read(&instance->adprecovery);
7245 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7246 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007247 break;
7248
7249 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7250 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7251
7252 msleep(1000);
7253 }
7254
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007255 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7256 dev_info(&instance->pdev->dev,
7257 "%s HBA failed to become operational, adp_state %d\n",
7258 __func__, adp_state);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007259 return 1;
7260 }
7261
7262 return 0;
7263}
7264
bo yang31ea7082007-11-07 12:09:50 -05007265/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007266 * megasas_detach_one - PCI hot"un"plug entry point
7267 * @pdev: PCI device structure
7268 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007269static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007270{
7271 int i;
7272 struct Scsi_Host *host;
7273 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08007274 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307275 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007276
7277 instance = pci_get_drvdata(pdev);
7278 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08007279 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007280
adam radford229fe472014-03-10 02:51:56 -07007281 /* Shutdown SR-IOV heartbeat timer */
7282 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7283 del_timer_sync(&instance->sriov_heartbeat_timer);
7284
Shivasharan S3f6194a2018-10-16 23:37:39 -07007285 /* Stop the FW fault detection watchdog */
7286 if (instance->adapter_type != MFI_SERIES)
7287 megasas_fusion_stop_watchdog(instance);
7288
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307289 if (instance->fw_crash_state != UNAVAILABLE)
7290 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007291 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08007292 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007293
7294 if (megasas_wait_for_adapter_operational(instance))
7295 goto skip_firing_dcmds;
7296
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007297 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05007298 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007299
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007300skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007301 /* cancel the delayed work if this work still in queue*/
7302 if (instance->ev != NULL) {
7303 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007304 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007305 instance->ev = NULL;
7306 }
7307
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307308 /* cancel all wait events */
7309 wake_up_all(&instance->int_cmd_wait_q);
7310
Sumant Patro5d018ad2006-10-03 13:13:18 -07007311 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007312
7313 /*
7314 * Take the instance off the instance array. Note that we will not
7315 * decrement the max_index. We let this array be sparse array
7316 */
7317 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7318 if (megasas_mgmt_info.instance[i] == instance) {
7319 megasas_mgmt_info.count--;
7320 megasas_mgmt_info.instance[i] = NULL;
7321
7322 break;
7323 }
7324 }
7325
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307326 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007327
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307328 megasas_destroy_irqs(instance);
7329
adam radfordc8e858f2011-10-08 18:15:13 -07007330 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007331 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007332
Shivasharan S630d42b2018-12-17 00:47:37 -08007333 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05007334 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7335 kfree(fusion->stream_detect_by_ld[i]);
7336 kfree(fusion->stream_detect_by_ld);
7337 fusion->stream_detect_by_ld = NULL;
7338 }
7339
7340
Shivasharan Se7d36b82017-10-19 02:48:50 -07007341 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007342 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307343 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7344 (sizeof(struct MR_PD_CFG_SEQ) *
7345 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307346 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08007347 if (fusion->ld_map[i])
7348 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307349 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08007350 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307351 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07007352 if (fusion->ld_drv_map[i]) {
7353 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7354 vfree(fusion->ld_drv_map[i]);
7355 else
7356 free_pages((ulong)fusion->ld_drv_map[i],
7357 fusion->drv_map_pages);
7358 }
7359
Maurizio Lombardi546e5592016-01-22 13:41:42 +01007360 if (fusion->pd_seq_sync[i])
7361 dma_free_coherent(&instance->pdev->dev,
7362 pd_seq_map_sz,
7363 fusion->pd_seq_sync[i],
7364 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307365 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307366 } else {
adam radford9c915a82010-12-21 13:34:31 -08007367 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08007368 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007369
adam radford229fe472014-03-10 02:51:56 -07007370 if (instance->vf_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007371 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
adam radford229fe472014-03-10 02:51:56 -07007372 sizeof(struct MR_LD_VF_AFFILIATION),
7373 instance->vf_affiliation,
7374 instance->vf_affiliation_h);
7375
7376 if (instance->vf_affiliation_111)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007377 dma_free_coherent(&pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07007378 sizeof(struct MR_LD_VF_AFFILIATION_111),
7379 instance->vf_affiliation_111,
7380 instance->vf_affiliation_111_h);
7381
7382 if (instance->hb_host_mem)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007383 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
adam radford229fe472014-03-10 02:51:56 -07007384 instance->hb_host_mem,
7385 instance->hb_host_mem_h);
7386
Shivasharan S1b4bed22017-10-19 02:48:55 -07007387 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307388
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007389 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307390
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007391 scsi_host_put(host);
7392
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007393 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007394}
7395
7396/**
7397 * megasas_shutdown - Shutdown entry point
7398 * @device: Generic device structure
7399 */
7400static void megasas_shutdown(struct pci_dev *pdev)
7401{
7402 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07007403
Yang, Bo0c79e682009-10-06 14:47:35 -06007404 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007405
7406 if (megasas_wait_for_adapter_operational(instance))
7407 goto skip_firing_dcmds;
7408
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007409 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07007410 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007411
7412skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307413 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307414 megasas_destroy_irqs(instance);
7415
adam radfordc8e858f2011-10-08 18:15:13 -07007416 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007417 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007418}
7419
7420/**
7421 * megasas_mgmt_open - char node "open" entry point
7422 */
7423static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7424{
7425 /*
7426 * Allow only those users with admin rights
7427 */
7428 if (!capable(CAP_SYS_ADMIN))
7429 return -EACCES;
7430
7431 return 0;
7432}
7433
7434/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007435 * megasas_mgmt_fasync - Async notifier registration from applications
7436 *
7437 * This function adds the calling process to a driver global queue. When an
7438 * event occurs, SIGIO will be sent to all processes in this queue.
7439 */
7440static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7441{
7442 int rc;
7443
Arjan van de Ven0b950672006-01-11 13:16:10 +01007444 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007445
7446 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7447
Arjan van de Ven0b950672006-01-11 13:16:10 +01007448 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007449
7450 if (rc >= 0) {
7451 /* For sanity check when we get ioctl */
7452 filep->private_data = filep;
7453 return 0;
7454 }
7455
7456 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7457
7458 return rc;
7459}
7460
7461/**
Yang, Boc3518832009-10-06 14:18:02 -06007462 * megasas_mgmt_poll - char node "poll" entry point
7463 * */
Al Viroafc9a422017-07-03 06:39:46 -04007464static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
Yang, Boc3518832009-10-06 14:18:02 -06007465{
Al Viroafc9a422017-07-03 06:39:46 -04007466 __poll_t mask;
Yang, Boc3518832009-10-06 14:18:02 -06007467 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007468
Yang, Boc3518832009-10-06 14:18:02 -06007469 poll_wait(file, &megasas_poll_wait, wait);
7470 spin_lock_irqsave(&poll_aen_lock, flags);
7471 if (megasas_poll_wait_aen)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007472 mask = (EPOLLIN | EPOLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007473 else
7474 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307475 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007476 spin_unlock_irqrestore(&poll_aen_lock, flags);
7477 return mask;
7478}
7479
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307480/*
7481 * megasas_set_crash_dump_params_ioctl:
7482 * Send CRASH_DUMP_MODE DCMD to all controllers
7483 * @cmd: MFI command frame
7484 */
7485
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007486static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307487{
7488 struct megasas_instance *local_instance;
7489 int i, error = 0;
7490 int crash_support;
7491
7492 crash_support = cmd->frame->dcmd.mbox.w[0];
7493
7494 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7495 local_instance = megasas_mgmt_info.instance[i];
7496 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307497 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307498 MEGASAS_HBA_OPERATIONAL) &&
7499 !megasas_set_crash_dump_params(local_instance,
7500 crash_support)) {
7501 local_instance->crash_dump_app_support =
7502 crash_support;
7503 dev_info(&local_instance->pdev->dev,
7504 "Application firmware crash "
7505 "dump mode set success\n");
7506 error = 0;
7507 } else {
7508 dev_info(&local_instance->pdev->dev,
7509 "Application firmware crash "
7510 "dump mode set failed\n");
7511 error = -1;
7512 }
7513 }
7514 }
7515 return error;
7516}
7517
Yang, Boc3518832009-10-06 14:18:02 -06007518/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007519 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7520 * @instance: Adapter soft state
7521 * @argp: User's ioctl packet
7522 */
7523static int
7524megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7525 struct megasas_iocpacket __user * user_ioc,
7526 struct megasas_iocpacket *ioc)
7527{
Shivasharan S107a60d2017-10-19 02:49:05 -07007528 struct megasas_sge64 *kern_sge64 = NULL;
7529 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007530 struct megasas_cmd *cmd;
7531 void *kbuff_arr[MAX_IOCTL_SGE];
7532 dma_addr_t buf_handle = 0;
7533 int error = 0, i;
7534 void *sense = NULL;
7535 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007536 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007537 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007538
7539 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7540
7541 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007542 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007543 ioc->sge_count, MAX_IOCTL_SGE);
7544 return -EINVAL;
7545 }
7546
Shivasharan Sf870bcb2018-01-05 05:33:04 -08007547 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7548 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7549 !instance->support_nvme_passthru)) {
Shivasharan S82add4e2017-10-19 02:49:02 -07007550 dev_err(&instance->pdev->dev,
7551 "Received invalid ioctl command 0x%x\n",
7552 ioc->frame.hdr.cmd);
7553 return -ENOTSUPP;
7554 }
7555
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007556 cmd = megasas_get_cmd(instance);
7557 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007558 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007559 return -ENOMEM;
7560 }
7561
7562 /*
7563 * User's IOCTL packet has 2 frames (maximum). Copy those two
7564 * frames into our cmd's frames. cmd->frame's context will get
7565 * overwritten when we copy from user's frames. So set that value
7566 * alone separately
7567 */
7568 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307569 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007570 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07007571
7572 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7573
7574 if (instance->consistent_mask_64bit)
7575 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7576 MFI_FRAME_SENSE64));
7577 else
7578 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307579 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07007580
7581 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7582 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007583
Shivasharan S8823abe2017-08-23 04:47:00 -07007584 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08007585 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7586 megasas_return_cmd(instance, cmd);
7587 return -1;
7588 }
7589 }
7590
Shivasharan S8823abe2017-08-23 04:47:00 -07007591 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307592 error = megasas_set_crash_dump_params_ioctl(cmd);
7593 megasas_return_cmd(instance, cmd);
7594 return error;
7595 }
7596
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007597 /*
7598 * The management interface between applications and the fw uses
7599 * MFI frames. E.g, RAID configuration changes, LD property changes
7600 * etc are accomplishes through different kinds of MFI frames. The
7601 * driver needs to care only about substituting user buffers with
7602 * kernel buffers in SGLs. The location of SGL is embedded in the
7603 * struct iocpacket itself.
7604 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007605 if (instance->consistent_mask_64bit)
7606 kern_sge64 = (struct megasas_sge64 *)
7607 ((unsigned long)cmd->frame + ioc->sgl_off);
7608 else
7609 kern_sge32 = (struct megasas_sge32 *)
7610 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007611
7612 /*
7613 * For each user buffer, create a mirror buffer and copy in
7614 */
7615 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01007616 if (!ioc->sgl[i].iov_len)
7617 continue;
7618
Sumant Patro9f35fa82007-02-14 12:55:45 -08007619 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007620 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08007621 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007622 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007623 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7624 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007625 error = -ENOMEM;
7626 goto out;
7627 }
7628
7629 /*
7630 * We don't change the dma_coherent_mask, so
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007631 * dma_alloc_coherent only returns 32bit addresses
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007632 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007633 if (instance->consistent_mask_64bit) {
7634 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7635 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7636 } else {
7637 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7638 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7639 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007640
7641 /*
7642 * We created a kernel buffer corresponding to the
7643 * user buffer. Now copy in from the user buffer
7644 */
7645 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7646 (u32) (ioc->sgl[i].iov_len))) {
7647 error = -EFAULT;
7648 goto out;
7649 }
7650 }
7651
7652 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007653 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7654 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007655 if (!sense) {
7656 error = -ENOMEM;
7657 goto out;
7658 }
7659
7660 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007661 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07007662 if (instance->consistent_mask_64bit)
7663 *sense_ptr = cpu_to_le64(sense_handle);
7664 else
7665 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007666 }
7667
7668 /*
7669 * Set the sync_cmd flag so that the ISR knows not to complete this
7670 * cmd to the SCSI mid-layer
7671 */
7672 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307673 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7674 cmd->sync_cmd = 0;
7675 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07007676 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7677 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7678 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307679 return -EBUSY;
7680 }
7681
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007682 cmd->sync_cmd = 0;
7683
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307684 if (instance->unload == 1) {
7685 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7686 "don't submit data to application\n");
7687 goto out;
7688 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007689 /*
7690 * copy out the kernel buffers to user buffers
7691 */
7692 for (i = 0; i < ioc->sge_count; i++) {
7693 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7694 ioc->sgl[i].iov_len)) {
7695 error = -EFAULT;
7696 goto out;
7697 }
7698 }
7699
7700 /*
7701 * copy out the sense
7702 */
7703 if (ioc->sense_len) {
7704 /*
bo yangb70a41e2008-03-18 03:13:06 -04007705 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007706 * sense buffer address
7707 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007708 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7709 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007710
Shivasharan S318aaef2017-02-10 00:59:22 -08007711 if (copy_to_user((void __user *)((unsigned long)
7712 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007713 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007714 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007715 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007716 error = -EFAULT;
7717 goto out;
7718 }
7719 }
7720
7721 /*
7722 * copy the status codes returned by the fw
7723 */
7724 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7725 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007726 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007727 error = -EFAULT;
7728 }
7729
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007730out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007731 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007732 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007733 sense, sense_handle);
7734 }
7735
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007736 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007737 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07007738 if (instance->consistent_mask_64bit)
7739 dma_free_coherent(&instance->pdev->dev,
7740 le32_to_cpu(kern_sge64[i].length),
7741 kbuff_arr[i],
7742 le64_to_cpu(kern_sge64[i].phys_addr));
7743 else
7744 dma_free_coherent(&instance->pdev->dev,
7745 le32_to_cpu(kern_sge32[i].length),
7746 kbuff_arr[i],
7747 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307748 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007749 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007750 }
7751
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307752 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007753 return error;
7754}
7755
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007756static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7757{
7758 struct megasas_iocpacket __user *user_ioc =
7759 (struct megasas_iocpacket __user *)arg;
7760 struct megasas_iocpacket *ioc;
7761 struct megasas_instance *instance;
7762 int error;
7763
Markus Elfring709ab232016-08-21 10:39:04 +02007764 ioc = memdup_user(user_ioc, sizeof(*ioc));
7765 if (IS_ERR(ioc))
7766 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007767
7768 instance = megasas_lookup_instance(ioc->host_no);
7769 if (!instance) {
7770 error = -ENODEV;
7771 goto out_kfree_ioc;
7772 }
7773
adam radford229fe472014-03-10 02:51:56 -07007774 /* Block ioctls in VF mode */
7775 if (instance->requestorId && !allow_vf_ioctls) {
7776 error = -ENODEV;
7777 goto out_kfree_ioc;
7778 }
7779
Sumit Saxena8a01a412016-01-28 21:04:32 +05307780 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007781 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007782 error = -ENODEV;
7783 goto out_kfree_ioc;
7784 }
7785
7786 if (instance->unload == 1) {
7787 error = -ENODEV;
7788 goto out_kfree_ioc;
7789 }
7790
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007791 if (down_interruptible(&instance->ioctl_sem)) {
7792 error = -ERESTARTSYS;
7793 goto out_kfree_ioc;
7794 }
bo yang39a98552010-09-22 22:36:29 -04007795
Shivasharan S619831f2018-01-05 05:27:43 -08007796 if (megasas_wait_for_adapter_operational(instance)) {
bo yang39a98552010-09-22 22:36:29 -04007797 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007798 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007799 }
bo yang39a98552010-09-22 22:36:29 -04007800
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007801 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007802out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007803 up(&instance->ioctl_sem);
7804
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007805out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007806 kfree(ioc);
7807 return error;
7808}
7809
7810static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7811{
7812 struct megasas_instance *instance;
7813 struct megasas_aen aen;
7814 int error;
7815
7816 if (file->private_data != file) {
7817 printk(KERN_DEBUG "megasas: fasync_helper was not "
7818 "called first\n");
7819 return -EINVAL;
7820 }
7821
7822 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7823 return -EFAULT;
7824
7825 instance = megasas_lookup_instance(aen.host_no);
7826
7827 if (!instance)
7828 return -ENODEV;
7829
Sumit Saxena8a01a412016-01-28 21:04:32 +05307830 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007831 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007832 }
7833
7834 if (instance->unload == 1) {
7835 return -ENODEV;
7836 }
7837
Shivasharan S619831f2018-01-05 05:27:43 -08007838 if (megasas_wait_for_adapter_operational(instance))
bo yang39a98552010-09-22 22:36:29 -04007839 return -ENODEV;
bo yang39a98552010-09-22 22:36:29 -04007840
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307841 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007842 error = megasas_register_aen(instance, aen.seq_num,
7843 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307844 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007845 return error;
7846}
7847
7848/**
7849 * megasas_mgmt_ioctl - char node ioctl entry point
7850 */
7851static long
7852megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7853{
7854 switch (cmd) {
7855 case MEGASAS_IOC_FIRMWARE:
7856 return megasas_mgmt_ioctl_fw(file, arg);
7857
7858 case MEGASAS_IOC_GET_AEN:
7859 return megasas_mgmt_ioctl_aen(file, arg);
7860 }
7861
7862 return -ENOTTY;
7863}
7864
7865#ifdef CONFIG_COMPAT
7866static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7867{
7868 struct compat_megasas_iocpacket __user *cioc =
7869 (struct compat_megasas_iocpacket __user *)arg;
7870 struct megasas_iocpacket __user *ioc =
7871 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7872 int i;
7873 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007874 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307875 u32 local_sense_off;
7876 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307877 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007878
Jeff Garzik83aabc12006-10-04 06:34:03 -04007879 if (clear_user(ioc, sizeof(*ioc)))
7880 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007881
7882 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7883 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7884 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7885 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7886 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7887 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7888 return -EFAULT;
7889
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007890 /*
7891 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7892 * sense_len is not null, so prepare the 64bit value under
7893 * the same condition.
7894 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307895 if (get_user(local_sense_off, &ioc->sense_off) ||
7896 get_user(local_sense_len, &ioc->sense_len) ||
7897 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307898 return -EFAULT;
7899
Wenwen Wang47db7872018-10-06 13:34:21 -05007900 if (local_sense_off != user_sense_off)
7901 return -EINVAL;
7902
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307903 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007904 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307905 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007906 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307907 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007908 if (get_user(ptr, sense_cioc_ptr) ||
7909 put_user(compat_ptr(ptr), sense_ioc_ptr))
7910 return -EFAULT;
7911 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007912
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007913 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007914 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7915 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7916 copy_in_user(&ioc->sgl[i].iov_len,
7917 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7918 return -EFAULT;
7919 }
7920
7921 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7922
7923 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7924 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7925 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7926 return -EFAULT;
7927 }
7928 return error;
7929}
7930
7931static long
7932megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7933 unsigned long arg)
7934{
7935 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007936 case MEGASAS_IOC_FIRMWARE32:
7937 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007938 case MEGASAS_IOC_GET_AEN:
7939 return megasas_mgmt_ioctl_aen(file, arg);
7940 }
7941
7942 return -ENOTTY;
7943}
7944#endif
7945
7946/*
7947 * File operations structure for management interface
7948 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007949static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007950 .owner = THIS_MODULE,
7951 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007952 .fasync = megasas_mgmt_fasync,
7953 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007954 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007955#ifdef CONFIG_COMPAT
7956 .compat_ioctl = megasas_mgmt_compat_ioctl,
7957#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007958 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007959};
7960
7961/*
7962 * PCI hotplug support registration structure
7963 */
7964static struct pci_driver megasas_pci_driver = {
7965
7966 .name = "megaraid_sas",
7967 .id_table = megasas_pci_table,
7968 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007969 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007970 .suspend = megasas_suspend,
7971 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007972 .shutdown = megasas_shutdown,
7973};
7974
7975/*
7976 * Sysfs driver attributes
7977 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007978static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007979{
7980 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7981 MEGASAS_VERSION);
7982}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007983static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007984
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007985static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307986{
7987 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7988 MEGASAS_RELDATE);
7989}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007990static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307991
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007992static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06007993{
7994 return sprintf(buf, "%u\n", support_poll_for_event);
7995}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007996static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007997
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007998static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06007999{
8000 return sprintf(buf, "%u\n", support_device_change);
8001}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008002static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06008003
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008004static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07008005{
bo yangad84db22007-11-09 04:40:16 -05008006 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07008007}
8008
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008009static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8010 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07008011{
8012 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008013
8014 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07008015 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8016 retval = -EINVAL;
8017 }
8018 return retval;
8019}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008020static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05008021
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008022static ssize_t
8023support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8024{
8025 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8026}
8027
8028static DRIVER_ATTR_RO(support_nvme_encapsulation);
8029
Shivasharan Sb4a42212017-02-10 00:59:16 -08008030static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8031{
8032 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8033 scsi_remove_device(sdev);
8034 scsi_device_put(sdev);
8035}
8036
Shivasharan S44abbaf2019-01-29 01:38:13 -08008037/**
8038 * megasas_update_device_list - Update the PD and LD device list from FW
8039 * after an AEN event notification
8040 * @instance: Adapter soft state
8041 * @event_type: Indicates type of event (PD or LD event)
8042 *
8043 * @return: Success or failure
8044 *
8045 * Issue DCMDs to Firmware to update the internal device list in driver.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008046 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8047 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan S44abbaf2019-01-29 01:38:13 -08008048 */
8049static
8050int megasas_update_device_list(struct megasas_instance *instance,
8051 int event_type)
8052{
8053 int dcmd_ret = DCMD_SUCCESS;
8054
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008055 if (instance->enable_fw_dev_list) {
8056 dcmd_ret = megasas_host_device_list_query(instance, false);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008057 if (dcmd_ret != DCMD_SUCCESS)
8058 goto out;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008059 } else {
8060 if (event_type & SCAN_PD_CHANNEL) {
8061 dcmd_ret = megasas_get_pd_list(instance);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008062
Shivasharan S44abbaf2019-01-29 01:38:13 -08008063 if (dcmd_ret != DCMD_SUCCESS)
8064 goto out;
8065 }
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008066
8067 if (event_type & SCAN_VD_CHANNEL) {
8068 if (!instance->requestorId ||
8069 (instance->requestorId &&
8070 megasas_get_ld_vf_affiliation(instance, 0))) {
8071 dcmd_ret = megasas_ld_list_query(instance,
8072 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8073 if (dcmd_ret != DCMD_SUCCESS)
8074 goto out;
8075 }
8076 }
Shivasharan S44abbaf2019-01-29 01:38:13 -08008077 }
8078
8079out:
8080 return dcmd_ret;
8081}
8082
8083/**
8084 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8085 * after an AEN event notification
8086 * @instance: Adapter soft state
8087 * @scan_type: Indicates type of devices (PD/LD) to add
8088 * @return void
8089 */
8090static
8091void megasas_add_remove_devices(struct megasas_instance *instance,
8092 int scan_type)
8093{
8094 int i, j;
8095 u16 pd_index = 0;
8096 u16 ld_index = 0;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008097 u16 channel = 0, id = 0;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008098 struct Scsi_Host *host;
8099 struct scsi_device *sdev1;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008100 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8101 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008102
8103 host = instance->host;
8104
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008105 if (instance->enable_fw_dev_list) {
8106 targetid_list = instance->host_device_list_buf;
8107 for (i = 0; i < targetid_list->count; i++) {
8108 targetid_entry = &targetid_list->host_device_list[i];
8109 if (targetid_entry->flags.u.bits.is_sys_pd) {
8110 channel = le16_to_cpu(targetid_entry->target_id) /
8111 MEGASAS_MAX_DEV_PER_CHANNEL;
8112 id = le16_to_cpu(targetid_entry->target_id) %
8113 MEGASAS_MAX_DEV_PER_CHANNEL;
8114 } else {
8115 channel = MEGASAS_MAX_PD_CHANNELS +
8116 (le16_to_cpu(targetid_entry->target_id) /
8117 MEGASAS_MAX_DEV_PER_CHANNEL);
8118 id = le16_to_cpu(targetid_entry->target_id) %
8119 MEGASAS_MAX_DEV_PER_CHANNEL;
8120 }
8121 sdev1 = scsi_device_lookup(host, channel, id, 0);
8122 if (!sdev1) {
8123 scsi_add_device(host, channel, id, 0);
8124 } else {
8125 scsi_device_put(sdev1);
8126 }
8127 }
8128 }
8129
Shivasharan S44abbaf2019-01-29 01:38:13 -08008130 if (scan_type & SCAN_PD_CHANNEL) {
8131 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8132 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8133 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8134 sdev1 = scsi_device_lookup(host, i, j, 0);
8135 if (instance->pd_list[pd_index].driveState ==
8136 MR_PD_STATE_SYSTEM) {
8137 if (!sdev1)
8138 scsi_add_device(host, i, j, 0);
8139 else
8140 scsi_device_put(sdev1);
8141 } else {
8142 if (sdev1)
8143 megasas_remove_scsi_device(sdev1);
8144 }
8145 }
8146 }
8147 }
8148
8149 if (scan_type & SCAN_VD_CHANNEL) {
8150 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8151 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8152 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8153 sdev1 = scsi_device_lookup(host,
8154 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8155 if (instance->ld_ids[ld_index] != 0xff) {
8156 if (!sdev1)
8157 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8158 else
8159 scsi_device_put(sdev1);
8160 } else {
8161 if (sdev1)
8162 megasas_remove_scsi_device(sdev1);
8163 }
8164 }
8165 }
8166 }
8167
8168}
8169
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008170static void
8171megasas_aen_polling(struct work_struct *work)
8172{
8173 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08008174 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008175 struct megasas_instance *instance = ev->instance;
8176 union megasas_evt_class_locale class_locale;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008177 int event_type = 0;
adam radford229fe472014-03-10 02:51:56 -07008178 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008179 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308180 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008181
8182 if (!instance) {
8183 printk(KERN_ERR "invalid instance!\n");
8184 kfree(ev);
8185 return;
8186 }
adam radford229fe472014-03-10 02:51:56 -07008187
8188 /* Adjust event workqueue thread wait time for VF mode */
8189 if (instance->requestorId)
8190 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8191
8192 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308193 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07008194
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008195 instance->ev = NULL;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008196 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05308197 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008198
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308199 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308200
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008201 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308202 case MR_EVT_PD_REMOVED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008203 event_type = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07008204 break;
8205
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308206 case MR_EVT_LD_OFFLINE:
8207 case MR_EVT_CFG_CLEARED:
8208 case MR_EVT_LD_DELETED:
8209 case MR_EVT_LD_CREATED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008210 event_type = SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308211 break;
8212
8213 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8214 case MR_EVT_FOREIGN_CFG_IMPORTED:
8215 case MR_EVT_LD_STATE_CHANGE:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008216 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308217 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8218 instance->host->host_no);
8219 break;
8220
8221 case MR_EVT_CTRL_PROP_CHANGED:
Shivasharan Sf0c21df2018-10-16 23:37:40 -07008222 dcmd_ret = megasas_get_ctrl_info(instance);
8223 if (dcmd_ret == DCMD_SUCCESS &&
8224 instance->snapdump_wait_time) {
8225 megasas_get_snapdump_properties(instance);
8226 dev_info(&instance->pdev->dev,
8227 "Snap dump wait time\t: %d\n",
8228 instance->snapdump_wait_time);
8229 }
8230 break;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308231 default:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008232 event_type = 0;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308233 break;
8234 }
8235 } else {
8236 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8237 mutex_unlock(&instance->reset_mutex);
8238 kfree(ev);
8239 return;
8240 }
8241
Shivasharan S44abbaf2019-01-29 01:38:13 -08008242 if (event_type)
8243 dcmd_ret = megasas_update_device_list(instance, event_type);
8244
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308245 mutex_unlock(&instance->reset_mutex);
8246
Shivasharan S44abbaf2019-01-29 01:38:13 -08008247 if (event_type && dcmd_ret == DCMD_SUCCESS)
8248 megasas_add_remove_devices(instance, event_type);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008249
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308250 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308251 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8252 else
8253 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008254
8255 /* Register AEN with FW for latest sequence number plus 1 */
8256 class_locale.members.reserved = 0;
8257 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8258 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308259
8260 if (instance->aen_cmd != NULL) {
8261 kfree(ev);
8262 return;
8263 }
8264
8265 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008266 error = megasas_register_aen(instance, seq_num,
8267 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008268 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308269 dev_err(&instance->pdev->dev,
8270 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008271
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308272 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008273 kfree(ev);
8274}
8275
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008276/**
8277 * megasas_init - Driver load entry point
8278 */
8279static int __init megasas_init(void)
8280{
8281 int rval;
8282
8283 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07008284 * Booted in kdump kernel, minimize memory footprints by
8285 * disabling few features
8286 */
8287 if (reset_devices) {
8288 msix_vectors = 1;
8289 rdpq_enable = 0;
8290 dual_qdepth_disable = 1;
8291 }
8292
8293 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008294 * Announce driver version and other information
8295 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05308296 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008297
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07008298 spin_lock_init(&poll_aen_lock);
8299
Yang, Bo72c4fd32009-10-06 14:20:59 -06008300 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06008301 support_device_change = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008302 support_nvme_encapsulation = true;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008303
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008304 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8305
8306 /*
8307 * Register character device node
8308 */
8309 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8310
8311 if (rval < 0) {
8312 printk(KERN_DEBUG "megasas: failed to open device node\n");
8313 return rval;
8314 }
8315
8316 megasas_mgmt_majorno = rval;
8317
8318 /*
8319 * Register ourselves as PCI hotplug module
8320 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00008321 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008322
8323 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09008324 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04008325 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008326 }
8327
Jeff Garzik83aabc12006-10-04 06:34:03 -04008328 rval = driver_create_file(&megasas_pci_driver.driver,
8329 &driver_attr_version);
8330 if (rval)
8331 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008332
8333 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308334 &driver_attr_release_date);
8335 if (rval)
8336 goto err_dcf_rel_date;
8337
8338 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008339 &driver_attr_support_poll_for_event);
8340 if (rval)
8341 goto err_dcf_support_poll_for_event;
8342
Jeff Garzik83aabc12006-10-04 06:34:03 -04008343 rval = driver_create_file(&megasas_pci_driver.driver,
8344 &driver_attr_dbg_lvl);
8345 if (rval)
8346 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05008347 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008348 &driver_attr_support_device_change);
8349 if (rval)
8350 goto err_dcf_support_device_change;
8351
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008352 rval = driver_create_file(&megasas_pci_driver.driver,
8353 &driver_attr_support_nvme_encapsulation);
8354 if (rval)
8355 goto err_dcf_support_nvme_encapsulation;
8356
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008357 return rval;
bo yangad84db22007-11-09 04:40:16 -05008358
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008359err_dcf_support_nvme_encapsulation:
8360 driver_remove_file(&megasas_pci_driver.driver,
8361 &driver_attr_support_device_change);
8362
Yang, Bo837f5fe2010-10-11 06:59:20 -06008363err_dcf_support_device_change:
8364 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05008365 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008366err_dcf_dbg_lvl:
8367 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008368 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008369err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308370 driver_remove_file(&megasas_pci_driver.driver,
8371 &driver_attr_release_date);
8372err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04008373 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8374err_dcf_attr_ver:
8375 pci_unregister_driver(&megasas_pci_driver);
8376err_pcidrv:
8377 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08008378 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008379}
8380
8381/**
8382 * megasas_exit - Driver unload entry point
8383 */
8384static void __exit megasas_exit(void)
8385{
Sumant Patro658dced2006-10-03 13:09:14 -07008386 driver_remove_file(&megasas_pci_driver.driver,
8387 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008388 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008389 &driver_attr_support_poll_for_event);
8390 driver_remove_file(&megasas_pci_driver.driver,
8391 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308392 driver_remove_file(&megasas_pci_driver.driver,
8393 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008394 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008395 driver_remove_file(&megasas_pci_driver.driver,
8396 &driver_attr_support_nvme_encapsulation);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008397
8398 pci_unregister_driver(&megasas_pci_driver);
8399 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8400}
8401
8402module_init(megasas_init);
8403module_exit(megasas_exit);