blob: 6d2390bf7271dba1439bcb8d65b3e2d1311bc53a [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
adam radford3f1530c2010-12-14 18:51:48 -08002 * Linux MegaRAID driver for SAS based RAID controllers
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2003-2013 LSI Corporation
Shivasharan S365597c2018-10-16 23:37:43 -07005 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007 *
adam radford3f1530c2010-12-14 18:51:48 -08008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040012 *
adam radford3f1530c2010-12-14 18:51:48 -080013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040017 *
adam radford3f1530c2010-12-14 18:51:48 -080018 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040020 *
Shivasharan S365597c2018-10-16 23:37:43 -070021 * Authors: Broadcom Inc.
adam radford3f1530c2010-12-14 18:51:48 -080022 * Sreenivas Bagalkote
23 * Sumant Patro
24 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053025 * Adam Radford
Shivasharan S365597c2018-10-16 23:37:43 -070026 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040028 *
Shivasharan S365597c2018-10-16 23:37:43 -070029 * Send feedback to: megaraidlinux.pdl@broadcom.com
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040030 */
31
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/pci.h>
35#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040036#include <linux/moduleparam.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
39#include <linux/interrupt.h>
40#include <linux/delay.h>
41#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Shivasharan S318aaef2017-02-10 00:59:22 -080044#include <asm/unaligned.h>
Al Viro43399232005-10-04 17:36:04 +010045#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040046#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080047#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010048#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060049#include <linux/poll.h>
Shivasharan Sdef3e8d2017-08-23 04:47:03 -070050#include <linux/vmalloc.h>
Shivasharan S62a04f82019-05-07 10:05:35 -070051#include <linux/irq_poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040052
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070057#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080058#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040059#include "megaraid_sas.h"
60
bo yangad84db22007-11-09 04:40:16 -050061/*
Yang, Bo1fd10682010-10-12 07:18:50 -060062 * Number of sectors per IO command
63 * Will be set in megasas_init_mfi if user does not provide
64 */
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
adam radford80d9da92010-12-21 10:17:40 -080070static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
adam radford079eadd2012-10-01 19:26:59 -070074static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
adam radford229fe472014-03-10 02:51:56 -070078static int allow_vf_ioctls;
79module_param(allow_vf_ioctls, int, S_IRUGO);
80MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053082static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070083module_param(throttlequeuedepth, int, S_IRUGO);
84MODULE_PARM_DESC(throttlequeuedepth,
85 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86
Sumit Saxenae3d178c2016-01-28 21:04:34 +053087unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070088module_param(resetwaittime, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070089MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
adam radfordc007b8b2012-07-17 18:20:24 -070090
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053091int smp_affinity_enable = 1;
92module_param(smp_affinity_enable, int, S_IRUGO);
Colin Ian King55d9a1d2018-04-29 13:25:32 +010093MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053094
Sumit Saxena179ac142016-01-28 21:04:28 +053095int rdpq_enable = 1;
96module_param(rdpq_enable, int, S_IRUGO);
Shivasharan S14013712018-10-16 23:37:45 -070097MODULE_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 +053098
Sumit Saxena308ec452016-01-28 21:04:30 +053099unsigned int dual_qdepth_disable;
100module_param(dual_qdepth_disable, int, S_IRUGO);
101MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
102
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530103unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104module_param(scmd_timeout, int, S_IRUGO);
105MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
106
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400107MODULE_LICENSE("GPL");
108MODULE_VERSION(MEGASAS_VERSION);
Shivasharan S365597c2018-10-16 23:37:43 -0700109MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
110MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400111
adam radford058a8fa2011-10-08 18:14:27 -0700112int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400113static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700114static int megasas_ld_list_query(struct megasas_instance *instance,
115 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400116static int megasas_issue_init_mfi(struct megasas_instance *instance);
117static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800119static void megasas_get_pd_info(struct megasas_instance *instance,
120 struct scsi_device *sdev);
Shivasharan Se9495e22018-06-04 03:45:12 -0700121
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400122/*
123 * PCI ID table for all supported controllers
124 */
125static struct pci_device_id megasas_pci_table[] = {
126
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200127 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
128 /* xscale IOP */
129 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
130 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400131 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
132 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700133 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
134 /* gen2*/
135 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
136 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600137 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
138 /* skinny*/
139 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
140 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200141 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
142 /* xscale IOP, vega */
143 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
144 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800145 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
146 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700147 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
148 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700149 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
150 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530151 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
152 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530153 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
154 /* Intruder */
155 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
156 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500159 /* VENTURA */
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Shivasharan S469f72d2018-11-09 09:47:20 -0800166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200170 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400171};
172
173MODULE_DEVICE_TABLE(pci, megasas_pci_table);
174
175static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700176struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400177static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100178static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400179
Yang, Boc3518832009-10-06 14:18:02 -0600180static int megasas_poll_wait_aen;
181static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600182static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800183u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600184static u32 support_device_change;
Shivasharan Sf870bcb2018-01-05 05:33:04 -0800185static bool support_nvme_encapsulation;
Sumant Patro658dced2006-10-03 13:09:14 -0700186
Yang, Boc3518832009-10-06 14:18:02 -0600187/* define lock for aen poll */
188spinlock_t poll_aen_lock;
189
adam radford9c915a82010-12-21 13:34:31 -0800190void
bo yang7343eb62007-11-09 04:35:44 -0500191megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
192 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800193static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800194megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
adam radfordebf054b2011-02-24 20:57:15 -0800195static int
196megasas_adp_reset_gen2(struct megasas_instance *instance,
197 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800198static irqreturn_t megasas_isr(int irq, void *devp);
199static u32
200megasas_init_adapter_mfi(struct megasas_instance *instance);
201u32
202megasas_build_and_issue_cmd(struct megasas_instance *instance,
203 struct scsi_cmnd *scmd);
204static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800205int
adam radford229fe472014-03-10 02:51:56 -0700206wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
207 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800208void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700209static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
210 int initial);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700211static int
Shivasharan S107a60d2017-10-19 02:49:05 -0700212megasas_set_dma_mask(struct megasas_instance *instance);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700213static int
214megasas_alloc_ctrl_mem(struct megasas_instance *instance);
215static inline void
216megasas_free_ctrl_mem(struct megasas_instance *instance);
217static inline int
218megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
219static inline void
220megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
221static inline void
222megasas_init_ctrl_params(struct megasas_instance *instance);
adam radfordcd50ba82010-12-21 10:23:23 -0800223
Shivasharan S272652f2018-12-17 00:47:40 -0800224u32 megasas_readl(struct megasas_instance *instance,
225 const volatile void __iomem *addr)
226{
227 u32 i = 0, ret_val;
228 /*
229 * Due to a HW errata in Aero controllers, reads to certain
230 * Fusion registers could intermittently return all zeroes.
231 * This behavior is transient in nature and subsequent reads will
232 * return valid value. As a workaround in driver, retry readl for
233 * upto three times until a non-zero value is read.
234 */
235 if (instance->adapter_type == AERO_SERIES) {
236 do {
237 ret_val = readl(addr);
238 i++;
239 } while (ret_val == 0 && i < 3);
240 return ret_val;
241 } else {
242 return readl(addr);
243 }
244}
245
Shivasharan S107a60d2017-10-19 02:49:05 -0700246/**
247 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
248 * @instance: Adapter soft state
249 * @dcmd: DCMD frame inside MFI command
250 * @dma_addr: DMA address of buffer to be passed to FW
251 * @dma_len: Length of DMA buffer to be passed to FW
252 * @return: void
253 */
254void megasas_set_dma_settings(struct megasas_instance *instance,
255 struct megasas_dcmd_frame *dcmd,
256 dma_addr_t dma_addr, u32 dma_len)
257{
258 if (instance->consistent_mask_64bit) {
259 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
260 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
261 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
262
263 } else {
264 dcmd->sgl.sge32[0].phys_addr =
265 cpu_to_le32(lower_32_bits(dma_addr));
266 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
267 dcmd->flags = cpu_to_le16(dcmd->flags);
268 }
269}
adam radfordcd50ba82010-12-21 10:23:23 -0800270
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800271void
adam radfordcd50ba82010-12-21 10:23:23 -0800272megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
273{
274 instance->instancet->fire_cmd(instance,
275 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800276 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800277}
bo yang7343eb62007-11-09 04:35:44 -0500278
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400279/**
280 * megasas_get_cmd - Get a command from the free pool
281 * @instance: Adapter soft state
282 *
283 * Returns a free command from the pool
284 */
adam radford9c915a82010-12-21 13:34:31 -0800285struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400286 *instance)
287{
288 unsigned long flags;
289 struct megasas_cmd *cmd = NULL;
290
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530291 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400292
293 if (!list_empty(&instance->cmd_pool)) {
294 cmd = list_entry((&instance->cmd_pool)->next,
295 struct megasas_cmd, list);
296 list_del_init(&cmd->list);
297 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500298 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400299 }
300
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530301 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400302 return cmd;
303}
304
305/**
306 * megasas_return_cmd - Return a cmd to free command pool
307 * @instance: Adapter soft state
308 * @cmd: Command packet to be returned to free command pool
309 */
Andi Kleen68b43742017-05-08 15:58:53 -0700310void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400311megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
312{
313 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530314 u32 blk_tags;
315 struct megasas_cmd_fusion *cmd_fusion;
316 struct fusion_context *fusion = instance->ctrl_context;
317
318 /* This flag is used only for fusion adapter.
319 * Wait for Interrupt for Polled mode DCMD
320 */
321 if (cmd->flags & DRV_DCMD_POLLED_MODE)
322 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400323
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530324 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400325
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530326 if (fusion) {
327 blk_tags = instance->max_scsi_cmds + cmd->index;
328 cmd_fusion = fusion->cmd_list[blk_tags];
329 megasas_return_cmd_fusion(instance, cmd_fusion);
330 }
331 cmd->scmd = NULL;
332 cmd->frame_count = 0;
333 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800334 memset(cmd->frame, 0, instance->mfi_frame_size);
335 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530336 if (!fusion && reset_devices)
337 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
338 list_add(&cmd->list, (&instance->cmd_pool)->next);
339
340 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
341
342}
Sumant Patro1341c932006-01-25 12:02:40 -0800343
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530344static const char *
345format_timestamp(uint32_t timestamp)
346{
347 static char buffer[32];
348
349 if ((timestamp & 0xff000000) == 0xff000000)
350 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
351 0x00ffffff);
352 else
353 snprintf(buffer, sizeof(buffer), "%us", timestamp);
354 return buffer;
355}
356
357static const char *
358format_class(int8_t class)
359{
360 static char buffer[6];
361
362 switch (class) {
363 case MFI_EVT_CLASS_DEBUG:
364 return "debug";
365 case MFI_EVT_CLASS_PROGRESS:
366 return "progress";
367 case MFI_EVT_CLASS_INFO:
368 return "info";
369 case MFI_EVT_CLASS_WARNING:
370 return "WARN";
371 case MFI_EVT_CLASS_CRITICAL:
372 return "CRIT";
373 case MFI_EVT_CLASS_FATAL:
374 return "FATAL";
375 case MFI_EVT_CLASS_DEAD:
376 return "DEAD";
377 default:
378 snprintf(buffer, sizeof(buffer), "%d", class);
379 return buffer;
380 }
381}
382
383/**
384 * megasas_decode_evt: Decode FW AEN event and print critical event
385 * for information.
386 * @instance: Adapter soft state
387 */
388static void
389megasas_decode_evt(struct megasas_instance *instance)
390{
391 struct megasas_evt_detail *evt_detail = instance->evt_detail;
392 union megasas_evt_class_locale class_locale;
393 class_locale.word = le32_to_cpu(evt_detail->cl.word);
394
395 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
396 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
397 le32_to_cpu(evt_detail->seq_num),
398 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
399 (class_locale.members.locale),
400 format_class(class_locale.members.class),
401 evt_detail->description);
402}
403
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400404/**
adam radford0d490162010-12-14 19:17:17 -0800405* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800406* (deviceid : 1064R, PERC5) controllers
407*/
408
409/**
410 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400411 * @regs: MFI register set
412 */
413static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530414megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400415{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530416 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500417
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530418 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400419 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400420
421 /* Dummy readl to force pci flush */
422 readl(&regs->outbound_intr_mask);
423}
424
425/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700426 * megasas_disable_intr_xscale -Disables interrupt
427 * @regs: MFI register set
428 */
429static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530430megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700431{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530432 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700433 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500434
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530435 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700436 writel(mask, &regs->outbound_intr_mask);
437 /* Dummy readl to force pci flush */
438 readl(&regs->outbound_intr_mask);
439}
440
441/**
Sumant Patro1341c932006-01-25 12:02:40 -0800442 * megasas_read_fw_status_reg_xscale - returns the current FW status value
443 * @regs: MFI register set
444 */
445static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800446megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800447{
Shivasharan Sde516372018-12-17 00:47:39 -0800448 return readl(&instance->reg_set->outbound_msg_0);
Sumant Patro1341c932006-01-25 12:02:40 -0800449}
450/**
451 * megasas_clear_interrupt_xscale - Check & clear interrupt
452 * @regs: MFI register set
453 */
adam radford0d490162010-12-14 19:17:17 -0800454static int
Shivasharan Sde516372018-12-17 00:47:39 -0800455megasas_clear_intr_xscale(struct megasas_instance *instance)
Sumant Patro1341c932006-01-25 12:02:40 -0800456{
457 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400458 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800459 struct megasas_register_set __iomem *regs;
460 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500461
Sumant Patro1341c932006-01-25 12:02:40 -0800462 /*
463 * Check if it is our interrupt
464 */
465 status = readl(&regs->outbound_intr_status);
466
bo yang39a98552010-09-22 22:36:29 -0400467 if (status & MFI_OB_INTR_STATUS_MASK)
468 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
469 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
470 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800471
472 /*
473 * Clear the interrupt by writing back the same value
474 */
bo yang39a98552010-09-22 22:36:29 -0400475 if (mfiStatus)
476 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800477
Yang, Bo06f579d2008-08-10 12:42:37 -0700478 /* Dummy readl to force pci flush */
479 readl(&regs->outbound_intr_status);
480
bo yang39a98552010-09-22 22:36:29 -0400481 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800482}
483
484/**
485 * megasas_fire_cmd_xscale - Sends command to the FW
486 * @frame_phys_addr : Physical address of cmd
487 * @frame_count : Number of frames for the command
488 * @regs : MFI register set
489 */
adam radford0d490162010-12-14 19:17:17 -0800490static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600491megasas_fire_cmd_xscale(struct megasas_instance *instance,
492 dma_addr_t frame_phys_addr,
493 u32 frame_count,
494 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800495{
bo yang39a98552010-09-22 22:36:29 -0400496 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500497
bo yang39a98552010-09-22 22:36:29 -0400498 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800499 writel((frame_phys_addr >> 3)|(frame_count),
500 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400501 spin_unlock_irqrestore(&instance->hba_lock, flags);
502}
503
504/**
505 * megasas_adp_reset_xscale - For controller reset
506 * @regs: MFI register set
507 */
508static int
509megasas_adp_reset_xscale(struct megasas_instance *instance,
510 struct megasas_register_set __iomem *regs)
511{
512 u32 i;
513 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500514
bo yang39a98552010-09-22 22:36:29 -0400515 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
516
517 for (i = 0; i < 3; i++)
518 msleep(1000); /* sleep for 3 secs */
519 pcidata = 0;
520 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500521 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400522 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500523 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400524 pcidata &= ~0x2;
525 pci_write_config_dword(instance->pdev,
526 MFI_1068_PCSR_OFFSET, pcidata);
527
528 for (i = 0; i < 2; i++)
529 msleep(1000); /* need to wait 2 secs again */
530
531 pcidata = 0;
532 pci_read_config_dword(instance->pdev,
533 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500534 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400535 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500536 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400537 pcidata = 0;
538 pci_write_config_dword(instance->pdev,
539 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
540 }
541 }
542 return 0;
543}
544
545/**
546 * megasas_check_reset_xscale - For controller reset check
547 * @regs: MFI register set
548 */
549static int
550megasas_check_reset_xscale(struct megasas_instance *instance,
551 struct megasas_register_set __iomem *regs)
552{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530553 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530554 (le32_to_cpu(*instance->consumer) ==
555 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400556 return 1;
bo yang39a98552010-09-22 22:36:29 -0400557 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800558}
559
560static struct megasas_instance_template megasas_instance_template_xscale = {
561
562 .fire_cmd = megasas_fire_cmd_xscale,
563 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700564 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800565 .clear_intr = megasas_clear_intr_xscale,
566 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400567 .adp_reset = megasas_adp_reset_xscale,
568 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800569 .service_isr = megasas_isr,
570 .tasklet = megasas_complete_cmd_dpc,
571 .init_adapter = megasas_init_adapter_mfi,
572 .build_and_issue_cmd = megasas_build_and_issue_cmd,
573 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800574};
575
576/**
adam radford0d490162010-12-14 19:17:17 -0800577* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800578* to xscale (deviceid : 1064R, PERC5) controllers
579*/
580
581/**
adam radford0d490162010-12-14 19:17:17 -0800582* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500583* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800584*/
585
586/**
587 * megasas_enable_intr_ppc - Enables interrupts
588 * @regs: MFI register set
589 */
590static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530591megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800592{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530593 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500594
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530595 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800596 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800597
bo yang39a98552010-09-22 22:36:29 -0400598 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800599
600 /* Dummy readl to force pci flush */
601 readl(&regs->outbound_intr_mask);
602}
603
604/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700605 * megasas_disable_intr_ppc - Disable interrupt
606 * @regs: MFI register set
607 */
608static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530609megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700610{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530611 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700612 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500613
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530614 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700615 writel(mask, &regs->outbound_intr_mask);
616 /* Dummy readl to force pci flush */
617 readl(&regs->outbound_intr_mask);
618}
619
620/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800621 * megasas_read_fw_status_reg_ppc - returns the current FW status value
622 * @regs: MFI register set
623 */
624static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800625megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800626{
Shivasharan Sde516372018-12-17 00:47:39 -0800627 return readl(&instance->reg_set->outbound_scratch_pad_0);
Sumant Patrof9876f02006-02-03 15:34:35 -0800628}
629
630/**
631 * megasas_clear_interrupt_ppc - Check & clear interrupt
632 * @regs: MFI register set
633 */
adam radford0d490162010-12-14 19:17:17 -0800634static int
Shivasharan Sde516372018-12-17 00:47:39 -0800635megasas_clear_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800636{
adam radford3cc6851f92011-05-11 18:34:52 -0700637 u32 status, mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800638 struct megasas_register_set __iomem *regs;
639 regs = instance->reg_set;
adam radford3cc6851f92011-05-11 18:34:52 -0700640
Sumant Patrof9876f02006-02-03 15:34:35 -0800641 /*
642 * Check if it is our interrupt
643 */
644 status = readl(&regs->outbound_intr_status);
645
adam radford3cc6851f92011-05-11 18:34:52 -0700646 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
647 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
648
649 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
650 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800651
652 /*
653 * Clear the interrupt by writing back the same value
654 */
655 writel(status, &regs->outbound_doorbell_clear);
656
Yang, Bo06f579d2008-08-10 12:42:37 -0700657 /* Dummy readl to force pci flush */
658 readl(&regs->outbound_doorbell_clear);
659
adam radford3cc6851f92011-05-11 18:34:52 -0700660 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800661}
adam radford3cc6851f92011-05-11 18:34:52 -0700662
Sumant Patrof9876f02006-02-03 15:34:35 -0800663/**
664 * megasas_fire_cmd_ppc - Sends command to the FW
665 * @frame_phys_addr : Physical address of cmd
666 * @frame_count : Number of frames for the command
667 * @regs : MFI register set
668 */
adam radford0d490162010-12-14 19:17:17 -0800669static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600670megasas_fire_cmd_ppc(struct megasas_instance *instance,
671 dma_addr_t frame_phys_addr,
672 u32 frame_count,
673 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800674{
bo yang39a98552010-09-22 22:36:29 -0400675 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500676
bo yang39a98552010-09-22 22:36:29 -0400677 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800678 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800679 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400680 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800681}
682
bo yang39a98552010-09-22 22:36:29 -0400683/**
bo yang39a98552010-09-22 22:36:29 -0400684 * megasas_check_reset_ppc - For controller reset check
685 * @regs: MFI register set
686 */
687static int
688megasas_check_reset_ppc(struct megasas_instance *instance,
689 struct megasas_register_set __iomem *regs)
690{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530691 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700692 return 1;
693
bo yang39a98552010-09-22 22:36:29 -0400694 return 0;
695}
adam radford3cc6851f92011-05-11 18:34:52 -0700696
Sumant Patrof9876f02006-02-03 15:34:35 -0800697static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800698
Sumant Patrof9876f02006-02-03 15:34:35 -0800699 .fire_cmd = megasas_fire_cmd_ppc,
700 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700701 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800702 .clear_intr = megasas_clear_intr_ppc,
703 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700704 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400705 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800706 .service_isr = megasas_isr,
707 .tasklet = megasas_complete_cmd_dpc,
708 .init_adapter = megasas_init_adapter_mfi,
709 .build_and_issue_cmd = megasas_build_and_issue_cmd,
710 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800711};
712
713/**
Yang, Bo87911122009-10-06 14:31:54 -0600714 * megasas_enable_intr_skinny - Enables interrupts
715 * @regs: MFI register set
716 */
717static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530718megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600719{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530720 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500721
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530722 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600723 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
724
725 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
726
727 /* Dummy readl to force pci flush */
728 readl(&regs->outbound_intr_mask);
729}
730
731/**
732 * megasas_disable_intr_skinny - Disables interrupt
733 * @regs: MFI register set
734 */
735static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530736megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600737{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530738 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600739 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500740
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530741 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600742 writel(mask, &regs->outbound_intr_mask);
743 /* Dummy readl to force pci flush */
744 readl(&regs->outbound_intr_mask);
745}
746
747/**
748 * megasas_read_fw_status_reg_skinny - returns the current FW status value
749 * @regs: MFI register set
750 */
751static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800752megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600753{
Shivasharan Sde516372018-12-17 00:47:39 -0800754 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo87911122009-10-06 14:31:54 -0600755}
756
757/**
758 * megasas_clear_interrupt_skinny - Check & clear interrupt
759 * @regs: MFI register set
760 */
761static int
Shivasharan Sde516372018-12-17 00:47:39 -0800762megasas_clear_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600763{
764 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800765 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800766 struct megasas_register_set __iomem *regs;
767 regs = instance->reg_set;
adam radfordebf054b2011-02-24 20:57:15 -0800768
Yang, Bo87911122009-10-06 14:31:54 -0600769 /*
770 * Check if it is our interrupt
771 */
772 status = readl(&regs->outbound_intr_status);
773
774 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400775 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600776 }
777
778 /*
adam radfordebf054b2011-02-24 20:57:15 -0800779 * Check if it is our interrupt
780 */
Shivasharan Sde516372018-12-17 00:47:39 -0800781 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800782 MFI_STATE_FAULT) {
783 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
784 } else
785 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
786
787 /*
Yang, Bo87911122009-10-06 14:31:54 -0600788 * Clear the interrupt by writing back the same value
789 */
790 writel(status, &regs->outbound_intr_status);
791
792 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500793 * dummy read to flush PCI
794 */
Yang, Bo87911122009-10-06 14:31:54 -0600795 readl(&regs->outbound_intr_status);
796
adam radfordebf054b2011-02-24 20:57:15 -0800797 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600798}
799
800/**
801 * megasas_fire_cmd_skinny - Sends command to the FW
802 * @frame_phys_addr : Physical address of cmd
803 * @frame_count : Number of frames for the command
804 * @regs : MFI register set
805 */
806static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600807megasas_fire_cmd_skinny(struct megasas_instance *instance,
808 dma_addr_t frame_phys_addr,
809 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600810 struct megasas_register_set __iomem *regs)
811{
Yang, Bo0c79e682009-10-06 14:47:35 -0600812 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500813
bo yang39a98552010-09-22 22:36:29 -0400814 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530815 writel(upper_32_bits(frame_phys_addr),
816 &(regs)->inbound_high_queue_port);
817 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
818 &(regs)->inbound_low_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400819 spin_unlock_irqrestore(&instance->hba_lock, flags);
820}
821
822/**
bo yang39a98552010-09-22 22:36:29 -0400823 * megasas_check_reset_skinny - For controller reset check
824 * @regs: MFI register set
825 */
826static int
827megasas_check_reset_skinny(struct megasas_instance *instance,
828 struct megasas_register_set __iomem *regs)
829{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530830 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700831 return 1;
832
bo yang39a98552010-09-22 22:36:29 -0400833 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600834}
835
836static struct megasas_instance_template megasas_instance_template_skinny = {
837
838 .fire_cmd = megasas_fire_cmd_skinny,
839 .enable_intr = megasas_enable_intr_skinny,
840 .disable_intr = megasas_disable_intr_skinny,
841 .clear_intr = megasas_clear_intr_skinny,
842 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800843 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400844 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800845 .service_isr = megasas_isr,
846 .tasklet = megasas_complete_cmd_dpc,
847 .init_adapter = megasas_init_adapter_mfi,
848 .build_and_issue_cmd = megasas_build_and_issue_cmd,
849 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600850};
851
852
853/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700854* The following functions are defined for gen2 (deviceid : 0x78 0x79)
855* controllers
856*/
857
858/**
859 * megasas_enable_intr_gen2 - Enables interrupts
860 * @regs: MFI register set
861 */
862static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530863megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700864{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530865 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500866
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530867 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700868 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
869
870 /* write ~0x00000005 (4 & 1) to the intr mask*/
871 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
872
873 /* Dummy readl to force pci flush */
874 readl(&regs->outbound_intr_mask);
875}
876
877/**
878 * megasas_disable_intr_gen2 - Disables interrupt
879 * @regs: MFI register set
880 */
881static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530882megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700883{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530884 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700885 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500886
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530887 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700888 writel(mask, &regs->outbound_intr_mask);
889 /* Dummy readl to force pci flush */
890 readl(&regs->outbound_intr_mask);
891}
892
893/**
894 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
895 * @regs: MFI register set
896 */
897static u32
Shivasharan Sde516372018-12-17 00:47:39 -0800898megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700899{
Shivasharan Sde516372018-12-17 00:47:39 -0800900 return readl(&instance->reg_set->outbound_scratch_pad_0);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700901}
902
903/**
904 * megasas_clear_interrupt_gen2 - Check & clear interrupt
905 * @regs: MFI register set
906 */
907static int
Shivasharan Sde516372018-12-17 00:47:39 -0800908megasas_clear_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700909{
910 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400911 u32 mfiStatus = 0;
Shivasharan Sde516372018-12-17 00:47:39 -0800912 struct megasas_register_set __iomem *regs;
913 regs = instance->reg_set;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500914
Yang, Bo6610a6b2008-08-10 12:42:38 -0700915 /*
916 * Check if it is our interrupt
917 */
918 status = readl(&regs->outbound_intr_status);
919
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530920 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400921 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
922 }
923 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
924 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
925 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700926
927 /*
928 * Clear the interrupt by writing back the same value
929 */
bo yang39a98552010-09-22 22:36:29 -0400930 if (mfiStatus)
931 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700932
933 /* Dummy readl to force pci flush */
934 readl(&regs->outbound_intr_status);
935
bo yang39a98552010-09-22 22:36:29 -0400936 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700937}
938/**
939 * megasas_fire_cmd_gen2 - Sends command to the FW
940 * @frame_phys_addr : Physical address of cmd
941 * @frame_count : Number of frames for the command
942 * @regs : MFI register set
943 */
944static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600945megasas_fire_cmd_gen2(struct megasas_instance *instance,
946 dma_addr_t frame_phys_addr,
947 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700948 struct megasas_register_set __iomem *regs)
949{
bo yang39a98552010-09-22 22:36:29 -0400950 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500951
bo yang39a98552010-09-22 22:36:29 -0400952 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700953 writel((frame_phys_addr | (frame_count<<1))|1,
954 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400955 spin_unlock_irqrestore(&instance->hba_lock, flags);
956}
957
958/**
959 * megasas_adp_reset_gen2 - For controller reset
960 * @regs: MFI register set
961 */
962static int
963megasas_adp_reset_gen2(struct megasas_instance *instance,
964 struct megasas_register_set __iomem *reg_set)
965{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500966 u32 retry = 0 ;
967 u32 HostDiag;
968 u32 __iomem *seq_offset = &reg_set->seq_offset;
969 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400970
adam radfordebf054b2011-02-24 20:57:15 -0800971 if (instance->instancet == &megasas_instance_template_skinny) {
972 seq_offset = &reg_set->fusion_seq_offset;
973 hostdiag_offset = &reg_set->fusion_host_diag;
974 }
975
976 writel(0, seq_offset);
977 writel(4, seq_offset);
978 writel(0xb, seq_offset);
979 writel(2, seq_offset);
980 writel(7, seq_offset);
981 writel(0xd, seq_offset);
982
bo yang39a98552010-09-22 22:36:29 -0400983 msleep(1000);
984
adam radfordebf054b2011-02-24 20:57:15 -0800985 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400986
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500987 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400988 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800989 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500990 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400991 retry, HostDiag);
992
993 if (retry++ >= 100)
994 return 1;
995
996 }
997
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500998 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400999
adam radfordebf054b2011-02-24 20:57:15 -08001000 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -04001001
1002 ssleep(10);
1003
adam radfordebf054b2011-02-24 20:57:15 -08001004 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001005 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -04001006 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -08001007 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001008 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -04001009 retry, HostDiag);
1010
1011 if (retry++ >= 1000)
1012 return 1;
1013
1014 }
1015 return 0;
1016}
1017
1018/**
1019 * megasas_check_reset_gen2 - For controller reset check
1020 * @regs: MFI register set
1021 */
1022static int
1023megasas_check_reset_gen2(struct megasas_instance *instance,
1024 struct megasas_register_set __iomem *regs)
1025{
Sumit Saxena8a01a412016-01-28 21:04:32 +05301026 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -06001027 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -06001028
bo yang39a98552010-09-22 22:36:29 -04001029 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -07001030}
1031
1032static struct megasas_instance_template megasas_instance_template_gen2 = {
1033
1034 .fire_cmd = megasas_fire_cmd_gen2,
1035 .enable_intr = megasas_enable_intr_gen2,
1036 .disable_intr = megasas_disable_intr_gen2,
1037 .clear_intr = megasas_clear_intr_gen2,
1038 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -04001039 .adp_reset = megasas_adp_reset_gen2,
1040 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -08001041 .service_isr = megasas_isr,
1042 .tasklet = megasas_complete_cmd_dpc,
1043 .init_adapter = megasas_init_adapter_mfi,
1044 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1045 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -07001046};
1047
1048/**
Sumant Patrof9876f02006-02-03 15:34:35 -08001049* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -04001050* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -08001051*/
1052
adam radford9c915a82010-12-21 13:34:31 -08001053/*
1054 * Template added for TB (Fusion)
1055 */
1056extern struct megasas_instance_template megasas_instance_template_fusion;
1057
Sumant Patrof9876f02006-02-03 15:34:35 -08001058/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001059 * megasas_issue_polled - Issues a polling command
1060 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -08001061 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001062 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301063 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001064 */
adam radford9c915a82010-12-21 13:34:31 -08001065int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001066megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1067{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001068 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1069
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301070 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301071 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001072
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001073 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301074 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1075 __func__, __LINE__);
1076 return DCMD_NOT_FIRED;
1077 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001078
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001079 instance->instancet->issue_dcmd(instance, cmd);
1080
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301081 return wait_and_poll(instance, cmd, instance->requestorId ?
1082 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001083}
1084
1085/**
1086 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1087 * @instance: Adapter soft state
1088 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301089 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090 *
1091 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001092 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093 * Used to issue ioctl commands.
1094 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301095int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001096megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301097 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001098{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301099 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301100 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001101
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001102 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301103 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1104 __func__, __LINE__);
1105 return DCMD_NOT_FIRED;
1106 }
1107
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001108 instance->instancet->issue_dcmd(instance, cmd);
1109
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301110 if (timeout) {
1111 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301112 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301113 if (!ret) {
1114 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1115 __func__, __LINE__);
1116 return DCMD_TIMEOUT;
1117 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301118 } else
1119 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301120 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001121
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301122 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301123 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001124}
1125
1126/**
1127 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1128 * @instance: Adapter soft state
1129 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301130 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001131 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301132 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001133 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001134 * cmd and waits for return status.
1135 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001136 */
1137static int
1138megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301139 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001140{
1141 struct megasas_cmd *cmd;
1142 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301143 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001144
1145 cmd = megasas_get_cmd(instance);
1146
1147 if (!cmd)
1148 return -1;
1149
1150 abort_fr = &cmd->frame->abort;
1151
1152 /*
1153 * Prepare and issue the abort frame
1154 */
1155 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301156 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301157 abort_fr->flags = cpu_to_le16(0);
1158 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1159 abort_fr->abort_mfi_phys_addr_lo =
1160 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1161 abort_fr->abort_mfi_phys_addr_hi =
1162 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001163
1164 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301165 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001166
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001167 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301168 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1169 __func__, __LINE__);
1170 return DCMD_NOT_FIRED;
1171 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001172
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001173 instance->instancet->issue_dcmd(instance, cmd);
1174
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301175 if (timeout) {
1176 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301177 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301178 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301179 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1180 __func__, __LINE__);
1181 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301182 }
1183 } else
1184 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301185 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301186
bo yang39a98552010-09-22 22:36:29 -04001187 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001188
1189 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301190 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1191 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001192}
1193
1194/**
1195 * megasas_make_sgl32 - Prepares 32-bit SGL
1196 * @instance: Adapter soft state
1197 * @scp: SCSI command from the mid-layer
1198 * @mfi_sgl: SGL to be filled in
1199 *
1200 * If successful, this function returns the number of SG elements. Otherwise,
1201 * it returnes -1.
1202 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001203static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001204megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1205 union megasas_sgl *mfi_sgl)
1206{
1207 int i;
1208 int sge_count;
1209 struct scatterlist *os_sgl;
1210
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001211 sge_count = scsi_dma_map(scp);
1212 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001213
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001214 if (sge_count) {
1215 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301216 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1217 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001218 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001219 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001220 return sge_count;
1221}
1222
1223/**
1224 * megasas_make_sgl64 - Prepares 64-bit SGL
1225 * @instance: Adapter soft state
1226 * @scp: SCSI command from the mid-layer
1227 * @mfi_sgl: SGL to be filled in
1228 *
1229 * If successful, this function returns the number of SG elements. Otherwise,
1230 * it returnes -1.
1231 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001232static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001233megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1234 union megasas_sgl *mfi_sgl)
1235{
1236 int i;
1237 int sge_count;
1238 struct scatterlist *os_sgl;
1239
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001240 sge_count = scsi_dma_map(scp);
1241 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001242
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001243 if (sge_count) {
1244 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301245 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1246 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001247 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001248 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001249 return sge_count;
1250}
1251
Yang, Bof4c9a132009-10-06 14:43:28 -06001252/**
1253 * megasas_make_sgl_skinny - Prepares IEEE SGL
1254 * @instance: Adapter soft state
1255 * @scp: SCSI command from the mid-layer
1256 * @mfi_sgl: SGL to be filled in
1257 *
1258 * If successful, this function returns the number of SG elements. Otherwise,
1259 * it returnes -1.
1260 */
1261static int
1262megasas_make_sgl_skinny(struct megasas_instance *instance,
1263 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1264{
1265 int i;
1266 int sge_count;
1267 struct scatterlist *os_sgl;
1268
1269 sge_count = scsi_dma_map(scp);
1270
1271 if (sge_count) {
1272 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301273 mfi_sgl->sge_skinny[i].length =
1274 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001275 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301276 cpu_to_le64(sg_dma_address(os_sgl));
1277 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001278 }
1279 }
1280 return sge_count;
1281}
1282
Sumant Patrob1df99d2006-10-03 12:40:47 -07001283 /**
1284 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001285 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001286 * @sge_count : number of sg elements
1287 *
1288 * Returns the number of frames required for numnber of sge's (sge_count)
1289 */
1290
Yang, Bof4c9a132009-10-06 14:43:28 -06001291static u32 megasas_get_frame_count(struct megasas_instance *instance,
1292 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001293{
1294 int num_cnt;
1295 int sge_bytes;
1296 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001297 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001298
1299 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1300 sizeof(struct megasas_sge32);
1301
Yang, Bof4c9a132009-10-06 14:43:28 -06001302 if (instance->flag_ieee) {
1303 sge_sz = sizeof(struct megasas_sge_skinny);
1304 }
1305
Sumant Patrob1df99d2006-10-03 12:40:47 -07001306 /*
bo yangd532dbe2008-03-17 03:36:43 -04001307 * Main frame can contain 2 SGEs for 64-bit SGLs and
1308 * 3 SGEs for 32-bit SGLs for ldio &
1309 * 1 SGEs for 64-bit SGLs and
1310 * 2 SGEs for 32-bit SGLs for pthru frame
1311 */
1312 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001313 if (instance->flag_ieee == 1) {
1314 num_cnt = sge_count - 1;
1315 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001316 num_cnt = sge_count - 1;
1317 else
1318 num_cnt = sge_count - 2;
1319 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001320 if (instance->flag_ieee == 1) {
1321 num_cnt = sge_count - 1;
1322 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001323 num_cnt = sge_count - 2;
1324 else
1325 num_cnt = sge_count - 3;
1326 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001327
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001328 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001329 sge_bytes = sge_sz * num_cnt;
1330
1331 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1332 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1333 }
1334 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001335 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001336
1337 if (frame_count > 7)
1338 frame_count = 8;
1339 return frame_count;
1340}
1341
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001342/**
1343 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1344 * @instance: Adapter soft state
1345 * @scp: SCSI command
1346 * @cmd: Command to be prepared in
1347 *
1348 * This function prepares CDB commands. These are typcially pass-through
1349 * commands to the devices.
1350 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001351static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001352megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1353 struct megasas_cmd *cmd)
1354{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001355 u32 is_logical;
1356 u32 device_id;
1357 u16 flags = 0;
1358 struct megasas_pthru_frame *pthru;
1359
Shivasharan S3cabd162017-02-10 00:59:05 -08001360 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301361 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001362 pthru = (struct megasas_pthru_frame *)cmd->frame;
1363
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001364 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001365 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001366 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001367 flags = MFI_FRAME_DIR_READ;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001368 else if (scp->sc_data_direction == DMA_NONE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001369 flags = MFI_FRAME_DIR_NONE;
1370
Yang, Bof4c9a132009-10-06 14:43:28 -06001371 if (instance->flag_ieee == 1) {
1372 flags |= MFI_FRAME_IEEE;
1373 }
1374
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001375 /*
1376 * Prepare the DCDB frame
1377 */
1378 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1379 pthru->cmd_status = 0x0;
1380 pthru->scsi_status = 0x0;
1381 pthru->target_id = device_id;
1382 pthru->lun = scp->device->lun;
1383 pthru->cdb_len = scp->cmd_len;
1384 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001385 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301386 pthru->flags = cpu_to_le16(flags);
1387 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001388
1389 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1390
1391 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001392 * If the command is for the tape device, set the
1393 * pthru timeout to the os layer timeout value.
1394 */
Yang, Bo8d568252009-10-06 14:12:21 -06001395 if (scp->device->type == TYPE_TAPE) {
1396 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301397 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001398 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301399 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001400 }
1401
1402 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001403 * Construct SGL
1404 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001405 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301406 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001407 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1408 &pthru->sgl);
1409 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301410 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001411 pthru->sge_count = megasas_make_sgl64(instance, scp,
1412 &pthru->sgl);
1413 } else
1414 pthru->sge_count = megasas_make_sgl32(instance, scp,
1415 &pthru->sgl);
1416
Yang, Bobdc6fb82009-12-06 08:30:19 -07001417 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001418 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001419 pthru->sge_count);
1420 return 0;
1421 }
1422
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423 /*
1424 * Sense info specific
1425 */
1426 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301427 pthru->sense_buf_phys_addr_hi =
1428 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1429 pthru->sense_buf_phys_addr_lo =
1430 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001431
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001432 /*
1433 * Compute the total number of frames this command consumes. FW uses
1434 * this number to pull sufficient number of frames from host memory.
1435 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001436 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001437 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001438
1439 return cmd->frame_count;
1440}
1441
1442/**
1443 * megasas_build_ldio - Prepares IOs to logical devices
1444 * @instance: Adapter soft state
1445 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001446 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001447 *
1448 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1449 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001450static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001451megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1452 struct megasas_cmd *cmd)
1453{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001454 u32 device_id;
1455 u8 sc = scp->cmnd[0];
1456 u16 flags = 0;
1457 struct megasas_io_frame *ldio;
1458
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301459 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001460 ldio = (struct megasas_io_frame *)cmd->frame;
1461
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001462 if (scp->sc_data_direction == DMA_TO_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001463 flags = MFI_FRAME_DIR_WRITE;
Christoph Hellwig60ee6522018-10-10 19:31:25 +02001464 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001465 flags = MFI_FRAME_DIR_READ;
1466
Yang, Bof4c9a132009-10-06 14:43:28 -06001467 if (instance->flag_ieee == 1) {
1468 flags |= MFI_FRAME_IEEE;
1469 }
1470
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001471 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001472 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001473 */
1474 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1475 ldio->cmd_status = 0x0;
1476 ldio->scsi_status = 0x0;
1477 ldio->target_id = device_id;
1478 ldio->timeout = 0;
1479 ldio->reserved_0 = 0;
1480 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301481 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001482 ldio->start_lba_hi = 0;
1483 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1484
1485 /*
1486 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1487 */
1488 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301489 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1490 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1491 ((u32) scp->cmnd[2] << 8) |
1492 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001493
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301494 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001495 }
1496
1497 /*
1498 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1499 */
1500 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301501 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1502 ((u32) scp->cmnd[7] << 8));
1503 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1504 ((u32) scp->cmnd[3] << 16) |
1505 ((u32) scp->cmnd[4] << 8) |
1506 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001507 }
1508
1509 /*
1510 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1511 */
1512 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301513 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1514 ((u32) scp->cmnd[7] << 16) |
1515 ((u32) scp->cmnd[8] << 8) |
1516 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001517
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301518 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1519 ((u32) scp->cmnd[3] << 16) |
1520 ((u32) scp->cmnd[4] << 8) |
1521 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001522 }
1523
1524 /*
1525 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1526 */
1527 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301528 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1529 ((u32) scp->cmnd[11] << 16) |
1530 ((u32) scp->cmnd[12] << 8) |
1531 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001532
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301533 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1534 ((u32) scp->cmnd[7] << 16) |
1535 ((u32) scp->cmnd[8] << 8) |
1536 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001537
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301538 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1539 ((u32) scp->cmnd[3] << 16) |
1540 ((u32) scp->cmnd[4] << 8) |
1541 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001542
1543 }
1544
1545 /*
1546 * Construct SGL
1547 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001548 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301549 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001550 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1551 &ldio->sgl);
1552 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301553 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001554 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1555 } else
1556 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1557
Yang, Bobdc6fb82009-12-06 08:30:19 -07001558 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001559 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001560 ldio->sge_count);
1561 return 0;
1562 }
1563
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001564 /*
1565 * Sense info specific
1566 */
1567 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1568 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301569 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001570
Sumant Patrob1df99d2006-10-03 12:40:47 -07001571 /*
1572 * Compute the total number of frames this command consumes. FW uses
1573 * this number to pull sufficient number of frames from host memory.
1574 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001575 cmd->frame_count = megasas_get_frame_count(instance,
1576 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001577
1578 return cmd->frame_count;
1579}
1580
1581/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301582 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1583 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001584 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001585 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001586 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301587inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001588{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301589 int ret;
1590
Sumant Patrocb59aa62006-01-25 11:53:25 -08001591 switch (cmd->cmnd[0]) {
1592 case READ_10:
1593 case WRITE_10:
1594 case READ_12:
1595 case WRITE_12:
1596 case READ_6:
1597 case WRITE_6:
1598 case READ_16:
1599 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001600 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301601 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1602 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001603 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001604 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301605 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001606 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301607 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001608}
1609
Sumant Patro658dced2006-10-03 13:09:14 -07001610 /**
1611 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001612 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001613 * @instance: Adapter soft state
1614 */
1615static inline void
1616megasas_dump_pending_frames(struct megasas_instance *instance)
1617{
1618 struct megasas_cmd *cmd;
1619 int i,n;
1620 union megasas_sgl *mfi_sgl;
1621 struct megasas_io_frame *ldio;
1622 struct megasas_pthru_frame *pthru;
1623 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001624 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001625
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001626 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1627 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 -07001628 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001629 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 -07001630 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001631 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 -07001632
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001633 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001634 for (i = 0; i < max_cmd; i++) {
1635 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001636 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001637 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001638 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 +05301639 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001640 ldio = (struct megasas_io_frame *)cmd->frame;
1641 mfi_sgl = &ldio->sgl;
1642 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001643 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 +05301644 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1645 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1646 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1647 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001648 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001649 pthru = (struct megasas_pthru_frame *) cmd->frame;
1650 mfi_sgl = &pthru->sgl;
1651 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001652 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 +05301653 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1654 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1655 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1656 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001657 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001658 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1659 for (n = 0; n < sgcount; n++) {
1660 if (IS_DMA64)
1661 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1662 le32_to_cpu(mfi_sgl->sge64[n].length),
1663 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1664 else
1665 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1666 le32_to_cpu(mfi_sgl->sge32[n].length),
1667 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001668 }
1669 }
Sumant Patro658dced2006-10-03 13:09:14 -07001670 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001671 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001672 for (i = 0; i < max_cmd; i++) {
1673
1674 cmd = instance->cmd_list[i];
1675
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001676 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001677 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001678 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001679 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001680}
1681
adam radfordcd50ba82010-12-21 10:23:23 -08001682u32
1683megasas_build_and_issue_cmd(struct megasas_instance *instance,
1684 struct scsi_cmnd *scmd)
1685{
1686 struct megasas_cmd *cmd;
1687 u32 frame_count;
1688
1689 cmd = megasas_get_cmd(instance);
1690 if (!cmd)
1691 return SCSI_MLQUEUE_HOST_BUSY;
1692
1693 /*
1694 * Logical drive command
1695 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301696 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001697 frame_count = megasas_build_ldio(instance, scmd, cmd);
1698 else
1699 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1700
1701 if (!frame_count)
1702 goto out_return_cmd;
1703
1704 cmd->scmd = scmd;
1705 scmd->SCp.ptr = (char *)cmd;
1706
1707 /*
1708 * Issue the command to the FW
1709 */
1710 atomic_inc(&instance->fw_outstanding);
1711
1712 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1713 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001714
1715 return 0;
1716out_return_cmd:
1717 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301718 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001719}
1720
1721
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001722/**
1723 * megasas_queue_command - Queue entry point
1724 * @scmd: SCSI command to be queued
1725 * @done: Callback entry point
1726 */
1727static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301728megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001729{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001730 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301731 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001732
1733 instance = (struct megasas_instance *)
1734 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001735
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301736 if (instance->unload == 1) {
1737 scmd->result = DID_NO_CONNECT << 16;
1738 scmd->scsi_done(scmd);
1739 return 0;
1740 }
1741
bo yang39a98552010-09-22 22:36:29 -04001742 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001743 return SCSI_MLQUEUE_HOST_BUSY;
1744
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301745
adam radford229fe472014-03-10 02:51:56 -07001746 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301747 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001748 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001749 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001750 return SCSI_MLQUEUE_HOST_BUSY;
1751 } else {
adam radford229fe472014-03-10 02:51:56 -07001752 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301753 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001754 return 0;
1755 }
1756 }
1757
Sumit Saxena8a01a412016-01-28 21:04:32 +05301758 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001759 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301760 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301761 return 0;
1762 }
1763
Sumit Saxena18365b12016-01-28 21:04:25 +05301764 mr_device_priv_data = scmd->device->hostdata;
1765 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301766 scmd->result = DID_NO_CONNECT << 16;
1767 scmd->scsi_done(scmd);
1768 return 0;
1769 }
1770
Sumit Saxena8a01a412016-01-28 21:04:32 +05301771 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001772 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001773
Sumit Saxena8a01a412016-01-28 21:04:32 +05301774 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301775 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301776
bo yang39a98552010-09-22 22:36:29 -04001777
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001778 scmd->result = 0;
1779
Shivasharan S3cabd162017-02-10 00:59:05 -08001780 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301781 (scmd->device->id >= instance->fw_supported_vd_count ||
1782 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001783 scmd->result = DID_BAD_TARGET << 16;
1784 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001785 }
1786
Shivasharan S3cabd162017-02-10 00:59:05 -08001787 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1788 MEGASAS_IS_LOGICAL(scmd->device) &&
1789 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001790 scmd->result = DID_OK << 16;
1791 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001792 }
1793
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301794 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001795
Sumant Patrocb59aa62006-01-25 11:53:25 -08001796 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301797 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001798 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001799}
1800
Yang, Bo044833b2009-10-06 14:33:06 -06001801static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1802{
1803 int i;
1804
1805 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1806
1807 if ((megasas_mgmt_info.instance[i]) &&
1808 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1809 return megasas_mgmt_info.instance[i];
1810 }
1811
1812 return NULL;
1813}
1814
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301815/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001816* megasas_set_dynamic_target_properties -
1817* Device property set by driver may not be static and it is required to be
1818* updated after OCR
1819*
1820* set tm_capable.
1821* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301822*
1823* @sdev: OS provided scsi device
1824*
1825* Returns void
1826*/
Shivasharan Se9495e22018-06-04 03:45:12 -07001827void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1828 bool is_target_prop)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301829{
Shivasharan S15dd0382017-02-10 00:59:10 -08001830 u16 pd_index = 0, ld;
1831 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301832 struct megasas_instance *instance;
1833 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301834 struct MR_PRIV_DEVICE *mr_device_priv_data;
1835 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301836 struct MR_LD_RAID *raid;
1837 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1838
1839 instance = megasas_lookup_instance(sdev->host->host_no);
1840 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301841 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301842
Shivasharan Sed981b82017-02-10 00:59:06 -08001843 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301844 return;
1845
Shivasharan Sed981b82017-02-10 00:59:06 -08001846 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301847 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1848 + sdev->id;
1849 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1850 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001851 if (ld >= instance->fw_supported_vd_count)
1852 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301853 raid = MR_LdRaidGet(ld, local_map_ptr);
1854
1855 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001856 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001857
Sumit Saxena18365b12016-01-28 21:04:25 +05301858 mr_device_priv_data->is_tm_capable =
1859 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001860 } else if (instance->use_seqnum_jbod_fp) {
1861 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001862 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001863 pd_sync = (void *)fusion->pd_seq_sync
1864 [(instance->pd_seq_map_id - 1) & 1];
1865 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001866 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301867 }
Shivasharan Se9495e22018-06-04 03:45:12 -07001868
1869 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1870 /*
1871 * If FW provides a target reset timeout value, driver will use
1872 * it. If not set, fallback to default values.
1873 */
1874 mr_device_priv_data->target_reset_tmo =
1875 min_t(u8, instance->max_reset_tmo,
1876 instance->tgt_prop->reset_tmo);
1877 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1878 } else {
1879 mr_device_priv_data->target_reset_tmo =
1880 MEGASAS_DEFAULT_TM_TIMEOUT;
1881 mr_device_priv_data->task_abort_tmo =
1882 MEGASAS_DEFAULT_TM_TIMEOUT;
1883 }
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301884}
1885
Shivasharan S15dd0382017-02-10 00:59:10 -08001886/*
1887 * megasas_set_nvme_device_properties -
1888 * set nomerges=2
1889 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1890 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1891 *
1892 * MR firmware provides value in KB. Caller of this function converts
1893 * kb into bytes.
1894 *
1895 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1896 * MR firmware provides value 128 as (32 * 4K) = 128K.
1897 *
1898 * @sdev: scsi device
1899 * @max_io_size: maximum io transfer size
1900 *
1901 */
1902static inline void
1903megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301904{
Sumit Saxena2216c302016-01-28 21:04:26 +05301905 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001906 u32 mr_nvme_pg_size;
1907
1908 instance = (struct megasas_instance *)sdev->host->hostdata;
1909 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1910 MR_DEFAULT_NVME_PAGE_SIZE);
1911
1912 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1913
Bart Van Assche8b904b52018-03-07 17:10:10 -08001914 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
Shivasharan S15dd0382017-02-10 00:59:10 -08001915 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1916}
1917
1918
1919/*
1920 * megasas_set_static_target_properties -
1921 * Device property set by driver are static and it is not required to be
1922 * updated after OCR.
1923 *
1924 * set io timeout
1925 * set device queue depth
1926 * set nvme device properties. see - megasas_set_nvme_device_properties
1927 *
1928 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001929 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001930 */
Shivasharan S96188a82017-02-10 00:59:11 -08001931static void megasas_set_static_target_properties(struct scsi_device *sdev,
1932 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001933{
Shivasharan S15dd0382017-02-10 00:59:10 -08001934 u8 interface_type;
1935 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1936 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001937 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001938 struct megasas_instance *instance;
1939 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301940
1941 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001942 mr_device_priv_data = sdev->hostdata;
1943 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301944
Shivasharan S15dd0382017-02-10 00:59:10 -08001945 /*
1946 * The RAID firmware may require extended timeouts.
1947 */
1948 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301949
Shivasharan S15dd0382017-02-10 00:59:10 -08001950 switch (interface_type) {
1951 case SAS_PD:
1952 device_qd = MEGASAS_SAS_QD;
1953 break;
1954 case SATA_PD:
1955 device_qd = MEGASAS_SATA_QD;
1956 break;
1957 case NVME_PD:
1958 device_qd = MEGASAS_NVME_QD;
1959 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301960 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001961
Shivasharan S96188a82017-02-10 00:59:11 -08001962 if (is_target_prop) {
1963 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1964 if (tgt_device_qd &&
1965 (tgt_device_qd <= instance->host->can_queue))
1966 device_qd = tgt_device_qd;
1967
1968 /* max_io_size_kb will be set to non zero for
1969 * nvme based vd and syspd.
1970 */
1971 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1972 }
1973
Shivasharan S15dd0382017-02-10 00:59:10 -08001974 if (instance->nvme_page_size && max_io_size_kb)
1975 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1976
1977 scsi_change_queue_depth(sdev, device_qd);
1978
Sumit Saxena2216c302016-01-28 21:04:26 +05301979}
1980
Sumit Saxena18365b12016-01-28 21:04:25 +05301981
Christoph Hellwig147aab62006-02-17 12:13:48 +01001982static int megasas_slave_configure(struct scsi_device *sdev)
1983{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301984 u16 pd_index = 0;
1985 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001986 int ret_target_prop = DCMD_FAILED;
1987 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301988
1989 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001990 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001991 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301992 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1993 sdev->id;
1994 if (instance->pd_list[pd_index].driveState !=
1995 MR_PD_STATE_SYSTEM)
1996 return -ENXIO;
1997 }
1998 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301999
Shivasharan S149c5752018-01-05 05:27:42 -08002000 mutex_lock(&instance->reset_mutex);
Shivasharan S15dd0382017-02-10 00:59:10 -08002001 /* Send DCMD to Firmware and cache the information */
2002 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2003 megasas_get_pd_info(instance, sdev);
2004
Shivasharan S96188a82017-02-10 00:59:11 -08002005 /* Some ventura firmware may not have instance->nvme_page_size set.
2006 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2007 */
2008 if ((instance->tgt_prop) && (instance->nvme_page_size))
2009 ret_target_prop = megasas_get_target_prop(instance, sdev);
2010
2011 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2012 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08002013
Shivasharan S15dd0382017-02-10 00:59:10 -08002014 /* This sdev property may change post OCR */
Shivasharan Se9495e22018-06-04 03:45:12 -07002015 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2016
2017 mutex_unlock(&instance->reset_mutex);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05302018
Yang, Bo044833b2009-10-06 14:33:06 -06002019 return 0;
2020}
2021
2022static int megasas_slave_alloc(struct scsi_device *sdev)
2023{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002024 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06002025 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05302026 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002027
Yang, Bo044833b2009-10-06 14:33:06 -06002028 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08002029 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06002030 /*
2031 * Open the OS scan to the SYSTEM PD
2032 */
2033 pd_index =
2034 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2035 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08002036 if ((instance->pd_list_not_supported ||
2037 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05302038 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05302039 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06002040 }
2041 return -ENXIO;
2042 }
Sumit Saxena18365b12016-01-28 21:04:25 +05302043
2044scan_target:
2045 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2046 GFP_KERNEL);
2047 if (!mr_device_priv_data)
2048 return -ENOMEM;
2049 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08002050
2051 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2052 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01002053 return 0;
2054}
2055
Sumit Saxena18365b12016-01-28 21:04:25 +05302056static void megasas_slave_destroy(struct scsi_device *sdev)
2057{
2058 kfree(sdev->hostdata);
2059 sdev->hostdata = NULL;
2060}
2061
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302062/*
2063* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2064* kill adapter
2065* @instance: Adapter soft state
2066*
2067*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05302068static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302069{
2070 int i;
2071 struct megasas_cmd *cmd_mfi;
2072 struct megasas_cmd_fusion *cmd_fusion;
2073 struct fusion_context *fusion = instance->ctrl_context;
2074
2075 /* Find all outstanding ioctls */
2076 if (fusion) {
2077 for (i = 0; i < instance->max_fw_cmds; i++) {
2078 cmd_fusion = fusion->cmd_list[i];
2079 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2080 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2081 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002082 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2083 cmd_mfi->frame->hdr.cmd_status =
2084 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302085 megasas_complete_cmd(instance,
2086 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002087 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302088 }
2089 }
2090 } else {
2091 for (i = 0; i < instance->max_fw_cmds; i++) {
2092 cmd_mfi = instance->cmd_list[i];
2093 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2094 MFI_CMD_ABORT)
2095 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2096 }
2097 }
2098}
2099
2100
adam radford9c915a82010-12-21 13:34:31 -08002101void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002102{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302103 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302104 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302105 /* Wait 1 second to ensure IO or ioctls in build have posted */
2106 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002107 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302108 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002109 (instance->adapter_type != MFI_SERIES)) {
Shivasharan S5acad9b2018-10-16 23:37:47 -07002110 if (!instance->requestorId) {
2111 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2112 /* Flush */
2113 readl(&instance->reg_set->doorbell);
2114 }
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302115 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002116 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002117 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302118 writel(MFI_STOP_ADP,
2119 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002120 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302121 /* Complete outstanding ioctls when adapter is killed */
2122 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002123}
2124
2125 /**
2126 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2127 * restored to max value
2128 * @instance: Adapter soft state
2129 *
2130 */
2131void
2132megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2133{
2134 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302135
adam radford9c915a82010-12-21 13:34:31 -08002136 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002137 && time_after(jiffies, instance->last_time + 5 * HZ)
2138 && atomic_read(&instance->fw_outstanding) <
2139 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002140
2141 spin_lock_irqsave(instance->host->host_lock, flags);
2142 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002143
Sumit Saxena308ec452016-01-28 21:04:30 +05302144 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002145 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002146 }
2147}
2148
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002149/**
bo yang7343eb62007-11-09 04:35:44 -05002150 * megasas_complete_cmd_dpc - Returns FW's controller structure
2151 * @instance_addr: Address of adapter soft state
2152 *
2153 * Tasklet to complete cmds
2154 */
2155static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2156{
2157 u32 producer;
2158 u32 consumer;
2159 u32 context;
2160 struct megasas_cmd *cmd;
2161 struct megasas_instance *instance =
2162 (struct megasas_instance *)instance_addr;
2163 unsigned long flags;
2164
2165 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302166 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002167 return;
2168
2169 spin_lock_irqsave(&instance->completion_lock, flags);
2170
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302171 producer = le32_to_cpu(*instance->producer);
2172 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002173
2174 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302175 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002176 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002177 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002178 context);
2179 BUG();
2180 }
bo yang7343eb62007-11-09 04:35:44 -05002181
2182 cmd = instance->cmd_list[context];
2183
2184 megasas_complete_cmd(instance, cmd, DID_OK);
2185
2186 consumer++;
2187 if (consumer == (instance->max_fw_cmds + 1)) {
2188 consumer = 0;
2189 }
2190 }
2191
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302192 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002193
2194 spin_unlock_irqrestore(&instance->completion_lock, flags);
2195
2196 /*
2197 * Check if we can restore can_queue
2198 */
adam radford9c915a82010-12-21 13:34:31 -08002199 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002200}
2201
Kees Cookc251a7b2017-10-22 15:30:04 -07002202static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2203
adam radford229fe472014-03-10 02:51:56 -07002204/**
Kees Cookc251a7b2017-10-22 15:30:04 -07002205 * megasas_start_timer - Initializes sriov heartbeat timer object
adam radford229fe472014-03-10 02:51:56 -07002206 * @instance: Adapter soft state
adam radford229fe472014-03-10 02:51:56 -07002207 *
2208 */
Kees Cookc251a7b2017-10-22 15:30:04 -07002209void megasas_start_timer(struct megasas_instance *instance)
adam radford229fe472014-03-10 02:51:56 -07002210{
Kees Cookc251a7b2017-10-22 15:30:04 -07002211 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2212
2213 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2214 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
adam radford229fe472014-03-10 02:51:56 -07002215 add_timer(timer);
2216}
2217
Yang, Bo707e09b2010-10-12 07:20:27 -06002218static void
2219megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2220
2221static void
2222process_fw_state_change_wq(struct work_struct *work);
2223
2224void megasas_do_ocr(struct megasas_instance *instance)
2225{
2226 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2227 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2228 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302229 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002230 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302231 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302232 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002233 instance->issuepend_done = 0;
2234
2235 atomic_set(&instance->fw_outstanding, 0);
2236 megasas_internal_reset_defer_cmds(instance);
2237 process_fw_state_change_wq(&instance->work_init);
2238}
2239
Adam Radford4cbfea82014-07-09 15:17:56 -07002240static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2241 int initial)
adam radford229fe472014-03-10 02:51:56 -07002242{
2243 struct megasas_cmd *cmd;
2244 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002245 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002246 dma_addr_t new_affiliation_111_h;
2247 int ld, retval = 0;
2248 u8 thisVf;
2249
2250 cmd = megasas_get_cmd(instance);
2251
2252 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002253 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2254 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002255 instance->host->host_no);
2256 return -ENOMEM;
2257 }
2258
2259 dcmd = &cmd->frame->dcmd;
2260
Adam Radford4cbfea82014-07-09 15:17:56 -07002261 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002262 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2263 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002264 megasas_return_cmd(instance, cmd);
2265 return -ENOMEM;
2266 }
2267
2268 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002269 memset(instance->vf_affiliation_111, 0,
2270 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002271 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002272 new_affiliation_111 =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002273 dma_alloc_coherent(&instance->pdev->dev,
2274 sizeof(struct MR_LD_VF_AFFILIATION_111),
2275 &new_affiliation_111_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002276 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002277 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2278 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002279 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002280 megasas_return_cmd(instance, cmd);
2281 return -ENOMEM;
2282 }
adam radford229fe472014-03-10 02:51:56 -07002283 }
2284
2285 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2286
2287 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302288 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002289 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302290 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002291 dcmd->timeout = 0;
2292 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302293 dcmd->data_xfer_len =
2294 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2295 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002296
Adam Radford4cbfea82014-07-09 15:17:56 -07002297 if (initial)
2298 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302299 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002300 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302301 dcmd->sgl.sge32[0].phys_addr =
2302 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002303
Christoph Hellwig2213a462015-04-23 16:33:54 +05302304 dcmd->sgl.sge32[0].length = cpu_to_le32(
2305 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002306
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002307 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002308 "scsi%d\n", instance->host->host_no);
2309
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302310 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002311 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2312 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002313 dcmd->cmd_status, instance->host->host_no);
2314 retval = 1; /* Do a scan if we couldn't get affiliation */
2315 goto out;
2316 }
2317
2318 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002319 thisVf = new_affiliation_111->thisVf;
2320 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2321 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2322 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002323 dev_warn(&instance->pdev->dev, "SR-IOV: "
2324 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002325 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002326 memcpy(instance->vf_affiliation_111,
2327 new_affiliation_111,
2328 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002329 retval = 1;
2330 goto out;
2331 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002332 }
2333out:
2334 if (new_affiliation_111) {
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002335 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002336 sizeof(struct MR_LD_VF_AFFILIATION_111),
2337 new_affiliation_111,
2338 new_affiliation_111_h);
2339 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302340
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302341 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002342
2343 return retval;
2344}
2345
2346static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2347 int initial)
2348{
2349 struct megasas_cmd *cmd;
2350 struct megasas_dcmd_frame *dcmd;
2351 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2352 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2353 dma_addr_t new_affiliation_h;
2354 int i, j, retval = 0, found = 0, doscan = 0;
2355 u8 thisVf;
2356
2357 cmd = megasas_get_cmd(instance);
2358
2359 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002360 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2361 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002362 instance->host->host_no);
2363 return -ENOMEM;
2364 }
2365
2366 dcmd = &cmd->frame->dcmd;
2367
2368 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002369 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2370 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002371 megasas_return_cmd(instance, cmd);
2372 return -ENOMEM;
2373 }
2374
2375 if (initial)
2376 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2377 sizeof(struct MR_LD_VF_AFFILIATION));
2378 else {
2379 new_affiliation =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002380 dma_alloc_coherent(&instance->pdev->dev,
2381 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2382 &new_affiliation_h, GFP_KERNEL);
Adam Radford4cbfea82014-07-09 15:17:56 -07002383 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002384 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2385 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002386 instance->host->host_no);
2387 megasas_return_cmd(instance, cmd);
2388 return -ENOMEM;
2389 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002390 }
2391
2392 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2393
2394 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302395 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002396 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302397 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002398 dcmd->timeout = 0;
2399 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302400 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2401 sizeof(struct MR_LD_VF_AFFILIATION));
2402 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002403
2404 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302405 dcmd->sgl.sge32[0].phys_addr =
2406 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002407 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302408 dcmd->sgl.sge32[0].phys_addr =
2409 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002410
Christoph Hellwig2213a462015-04-23 16:33:54 +05302411 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2412 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002413
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002414 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002415 "scsi%d\n", instance->host->host_no);
2416
Adam Radford4cbfea82014-07-09 15:17:56 -07002417
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302418 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002419 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2420 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002421 dcmd->cmd_status, instance->host->host_no);
2422 retval = 1; /* Do a scan if we couldn't get affiliation */
2423 goto out;
2424 }
2425
2426 if (!initial) {
2427 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002428 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2429 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002430 instance->host->host_no);
2431 retval = 1;
2432 goto out;
2433 }
2434 newmap = new_affiliation->map;
2435 savedmap = instance->vf_affiliation->map;
2436 thisVf = new_affiliation->thisVf;
2437 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2438 found = 0;
2439 for (j = 0; j < instance->vf_affiliation->ldCount;
2440 j++) {
2441 if (newmap->ref.targetId ==
2442 savedmap->ref.targetId) {
2443 found = 1;
2444 if (newmap->policy[thisVf] !=
2445 savedmap->policy[thisVf]) {
2446 doscan = 1;
2447 goto out;
2448 }
adam radford229fe472014-03-10 02:51:56 -07002449 }
2450 savedmap = (struct MR_LD_VF_MAP *)
2451 ((unsigned char *)savedmap +
2452 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002453 }
2454 if (!found && newmap->policy[thisVf] !=
2455 MR_LD_ACCESS_HIDDEN) {
2456 doscan = 1;
2457 goto out;
2458 }
2459 newmap = (struct MR_LD_VF_MAP *)
2460 ((unsigned char *)newmap + newmap->size);
2461 }
2462
2463 newmap = new_affiliation->map;
2464 savedmap = instance->vf_affiliation->map;
2465
2466 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2467 found = 0;
2468 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2469 if (savedmap->ref.targetId ==
2470 newmap->ref.targetId) {
2471 found = 1;
2472 if (savedmap->policy[thisVf] !=
2473 newmap->policy[thisVf]) {
2474 doscan = 1;
2475 goto out;
2476 }
2477 }
adam radford229fe472014-03-10 02:51:56 -07002478 newmap = (struct MR_LD_VF_MAP *)
2479 ((unsigned char *)newmap +
2480 newmap->size);
2481 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002482 if (!found && savedmap->policy[thisVf] !=
2483 MR_LD_ACCESS_HIDDEN) {
2484 doscan = 1;
2485 goto out;
2486 }
2487 savedmap = (struct MR_LD_VF_MAP *)
2488 ((unsigned char *)savedmap +
2489 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002490 }
2491 }
2492out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002493 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002494 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2495 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002496 memcpy(instance->vf_affiliation, new_affiliation,
2497 new_affiliation->size);
2498 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002499 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002500
2501 if (new_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002502 dma_free_coherent(&instance->pdev->dev,
Adam Radford4cbfea82014-07-09 15:17:56 -07002503 (MAX_LOGICAL_DRIVES + 1) *
2504 sizeof(struct MR_LD_VF_AFFILIATION),
2505 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302506 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002507
2508 return retval;
2509}
2510
Adam Radford4cbfea82014-07-09 15:17:56 -07002511/* This function will get the current SR-IOV LD/VF affiliation */
2512static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2513 int initial)
2514{
2515 int retval;
2516
2517 if (instance->PlasmaFW111)
2518 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2519 else
2520 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2521 return retval;
2522}
2523
adam radford229fe472014-03-10 02:51:56 -07002524/* This function will tell FW to start the SR-IOV heartbeat */
2525int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2526 int initial)
2527{
2528 struct megasas_cmd *cmd;
2529 struct megasas_dcmd_frame *dcmd;
2530 int retval = 0;
2531
2532 cmd = megasas_get_cmd(instance);
2533
2534 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002535 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2536 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002537 instance->host->host_no);
2538 return -ENOMEM;
2539 }
2540
2541 dcmd = &cmd->frame->dcmd;
2542
2543 if (initial) {
2544 instance->hb_host_mem =
Luis Chamberlain750afb02019-01-04 09:23:09 +01002545 dma_alloc_coherent(&instance->pdev->dev,
2546 sizeof(struct MR_CTRL_HB_HOST_MEM),
2547 &instance->hb_host_mem_h,
2548 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07002549 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002550 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2551 " memory for heartbeat host memory for scsi%d\n",
2552 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002553 retval = -ENOMEM;
2554 goto out;
2555 }
adam radford229fe472014-03-10 02:51:56 -07002556 }
2557
2558 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2559
Christoph Hellwig2213a462015-04-23 16:33:54 +05302560 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002561 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302562 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002563 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302564 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002565 dcmd->timeout = 0;
2566 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302567 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2568 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
Shivasharan S107a60d2017-10-19 02:49:05 -07002569
2570 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2571 sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002572
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002573 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002574 instance->host->host_no);
2575
Shivasharan Se7d36b82017-10-19 02:48:50 -07002576 if ((instance->adapter_type != MFI_SERIES) &&
2577 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302578 retval = megasas_issue_blocked_cmd(instance, cmd,
2579 MEGASAS_ROUTINE_WAIT_TIME_VF);
2580 else
2581 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002582
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302583 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302584 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2585 "_MEM_ALLOC DCMD %s for scsi%d\n",
2586 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2587 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002588 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002589 }
2590
2591out:
2592 megasas_return_cmd(instance, cmd);
2593
2594 return retval;
2595}
2596
2597/* Handler for SR-IOV heartbeat */
Kees Cookc251a7b2017-10-22 15:30:04 -07002598static void megasas_sriov_heartbeat_handler(struct timer_list *t)
adam radford229fe472014-03-10 02:51:56 -07002599{
2600 struct megasas_instance *instance =
Kees Cookc251a7b2017-10-22 15:30:04 -07002601 from_timer(instance, t, sriov_heartbeat_timer);
adam radford229fe472014-03-10 02:51:56 -07002602
2603 if (instance->hb_host_mem->HB.fwCounter !=
2604 instance->hb_host_mem->HB.driverCounter) {
2605 instance->hb_host_mem->HB.driverCounter =
2606 instance->hb_host_mem->HB.fwCounter;
2607 mod_timer(&instance->sriov_heartbeat_timer,
2608 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2609 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002610 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002611 "completed for scsi%d\n", instance->host->host_no);
2612 schedule_work(&instance->work_init);
2613 }
2614}
2615
bo yang7343eb62007-11-09 04:35:44 -05002616/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002617 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2618 * @instance: Adapter soft state
2619 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002620 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002621 * complete all its outstanding commands. Returns error if one or more IOs
2622 * are pending after this time period. It also marks the controller dead.
2623 */
2624static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2625{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302626 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002627 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002628 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002629 unsigned long flags;
2630 struct list_head clist_local;
2631 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002632 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002633
Sumit Saxenaccc75072016-01-28 21:04:33 +05302634 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2635 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2636 __func__, __LINE__);
2637 return FAILED;
2638 }
bo yang39a98552010-09-22 22:36:29 -04002639
Sumit Saxena8a01a412016-01-28 21:04:32 +05302640 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002641
2642 INIT_LIST_HEAD(&clist_local);
2643 spin_lock_irqsave(&instance->hba_lock, flags);
2644 list_splice_init(&instance->internal_reset_pending_q,
2645 &clist_local);
2646 spin_unlock_irqrestore(&instance->hba_lock, flags);
2647
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002648 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002649 for (i = 0; i < wait_time; i++) {
2650 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302651 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002652 break;
2653 }
2654
Sumit Saxena8a01a412016-01-28 21:04:32 +05302655 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002656 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302657 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002658 return FAILED;
2659 }
2660
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002661 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002662 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002663 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002664 struct megasas_cmd, list);
2665 list_del_init(&reset_cmd->list);
2666 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002667 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002668 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002669 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002670 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002671
2672 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2673 megasas_return_cmd(instance, reset_cmd);
2674 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002675 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002676 "reset queue\n",
2677 reset_cmd);
2678
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302679 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002680 instance->instancet->fire_cmd(instance,
2681 reset_cmd->frame_phys_addr,
2682 0, instance->reg_set);
2683 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002684 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002685 "cmds lst\n",
2686 reset_cmd);
2687 }
2688 reset_index++;
2689 }
2690
2691 return SUCCESS;
2692 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002693
adam radfordc007b8b2012-07-17 18:20:24 -07002694 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302695 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002696
2697 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002698 break;
2699
2700 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002701 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002702 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002703 /*
2704 * Call cmd completion routine. Cmd to be
2705 * be completed directly without depending on isr.
2706 */
2707 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002708 }
2709
2710 msleep(1000);
2711 }
2712
Yang, Bo707e09b2010-10-12 07:20:27 -06002713 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302714 outstanding = atomic_read(&instance->fw_outstanding);
Shivasharan Sde516372018-12-17 00:47:39 -08002715 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002716
Sumit Saxenaccc75072016-01-28 21:04:33 +05302717 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2718 goto no_outstanding;
2719
2720 if (instance->disableOnlineCtrlReset)
2721 goto kill_hba_and_failed;
2722 do {
2723 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2724 dev_info(&instance->pdev->dev,
Colin Ian Kingefc372c2019-04-17 14:51:09 +01002725 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
Sumit Saxenaccc75072016-01-28 21:04:33 +05302726 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2727 if (i == 3)
2728 goto kill_hba_and_failed;
2729 megasas_do_ocr(instance);
2730
2731 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2732 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2733 __func__, __LINE__);
2734 return FAILED;
2735 }
2736 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2737 __func__, __LINE__);
2738
2739 for (sl = 0; sl < 10; sl++)
2740 msleep(500);
2741
2742 outstanding = atomic_read(&instance->fw_outstanding);
2743
Shivasharan Sde516372018-12-17 00:47:39 -08002744 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302745 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2746 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002747 }
2748 i++;
2749 } while (i <= 3);
2750
Sumit Saxenaccc75072016-01-28 21:04:33 +05302751no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002752
Sumit Saxenaccc75072016-01-28 21:04:33 +05302753 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2754 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002755 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302756
2757kill_hba_and_failed:
2758
2759 /* Reset not supported, kill adapter */
2760 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2761 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2762 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2763 atomic_read(&instance->fw_outstanding));
2764 megasas_dump_pending_frames(instance);
2765 megaraid_sas_kill_hba(instance);
2766
2767 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002768}
2769
2770/**
2771 * megasas_generic_reset - Generic reset routine
2772 * @scmd: Mid-layer SCSI command
2773 *
2774 * This routine implements a generic reset handler for device, bus and host
2775 * reset requests. Device, bus and host specific reset handlers can use this
2776 * function after they do their specific tasks.
2777 */
2778static int megasas_generic_reset(struct scsi_cmnd *scmd)
2779{
2780 int ret_val;
2781 struct megasas_instance *instance;
2782
2783 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2784
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002785 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2786 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002787
Sumit Saxena8a01a412016-01-28 21:04:32 +05302788 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002789 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002790 return FAILED;
2791 }
2792
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002793 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002794 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002795 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002796 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002797 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002798
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002799 return ret_val;
2800}
2801
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002802/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002803 * megasas_reset_timer - quiesce the adapter if required
2804 * @scmd: scsi cmnd
2805 *
2806 * Sets the FW busy flag and reduces the host->can_queue if the
2807 * cmd has not been completed within the timeout period.
2808 */
2809static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002810blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002811{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002812 struct megasas_instance *instance;
2813 unsigned long flags;
2814
2815 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302816 (scmd_timeout * 2) * HZ)) {
Christoph Hellwig66005932018-05-29 15:52:29 +02002817 return BLK_EH_DONE;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002818 }
2819
adam radfordf575c5d2011-10-13 16:01:12 -07002820 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002821 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2822 /* FW is busy, throttle IO */
2823 spin_lock_irqsave(instance->host->host_lock, flags);
2824
adam radfordc5daa6a2012-07-17 18:20:03 -07002825 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002826 instance->last_time = jiffies;
2827 instance->flag |= MEGASAS_FW_BUSY;
2828
2829 spin_unlock_irqrestore(instance->host->host_lock, flags);
2830 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002831 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002832}
2833
2834/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002835 * megasas_dump_frame - This function will dump MPT/MFI frame
2836 */
2837static inline void
2838megasas_dump_frame(void *mpi_request, int sz)
2839{
2840 int i;
2841 __le32 *mfp = (__le32 *)mpi_request;
2842
2843 printk(KERN_INFO "IO request frame:\n\t");
Dan Carpenter40a4c2c2017-02-14 19:38:55 +03002844 for (i = 0; i < sz / sizeof(__le32); i++) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002845 if (i && ((i % 8) == 0))
2846 printk("\n\t");
2847 printk("%08x ", le32_to_cpu(mfp[i]));
2848 }
2849 printk("\n");
2850}
2851
2852/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002853 * megasas_reset_bus_host - Bus & host reset handler entry point
2854 */
2855static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2856{
2857 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002858 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002859
adam radford9c915a82010-12-21 13:34:31 -08002860 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002861
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002862 scmd_printk(KERN_INFO, scmd,
2863 "Controller reset is requested due to IO timeout\n"
2864 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2865 " SCSI host busy: %d\t FW outstanding: %d\n",
2866 scmd, scmd->device->host->shost_state,
Ming Leic84b0232018-06-24 22:03:26 +08002867 scsi_host_busy(scmd->device->host),
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002868 atomic_read(&instance->fw_outstanding));
2869
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002870 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002871 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002872 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002873 if (instance->adapter_type == MFI_SERIES) {
2874 ret = megasas_generic_reset(scmd);
2875 } else {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002876 struct megasas_cmd_fusion *cmd;
2877 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2878 if (cmd)
2879 megasas_dump_frame(cmd->io_request,
Shivasharan S5dd977e2017-08-23 04:47:07 -07002880 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002881 ret = megasas_reset_fusion(scmd->device->host,
2882 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002883 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002884
2885 return ret;
2886}
2887
2888/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002889 * megasas_task_abort - Issues task abort request to firmware
2890 * (supported only for fusion adapters)
2891 * @scmd: SCSI command pointer
2892 */
2893static int megasas_task_abort(struct scsi_cmnd *scmd)
2894{
2895 int ret;
2896 struct megasas_instance *instance;
2897
2898 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2899
Shivasharan Se7d36b82017-10-19 02:48:50 -07002900 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002901 ret = megasas_task_abort_fusion(scmd);
2902 else {
2903 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2904 ret = FAILED;
2905 }
2906
2907 return ret;
2908}
2909
2910/**
2911 * megasas_reset_target: Issues target reset request to firmware
2912 * (supported only for fusion adapters)
2913 * @scmd: SCSI command pointer
2914 */
2915static int megasas_reset_target(struct scsi_cmnd *scmd)
2916{
2917 int ret;
2918 struct megasas_instance *instance;
2919
2920 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2921
Shivasharan Se7d36b82017-10-19 02:48:50 -07002922 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002923 ret = megasas_reset_target_fusion(scmd);
2924 else {
2925 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2926 ret = FAILED;
2927 }
2928
2929 return ret;
2930}
2931
2932/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002933 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002934 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002935 * @bdev: block device
2936 * @capacity: drive capacity
2937 * @geom: geometry parameters
2938 */
2939static int
2940megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2941 sector_t capacity, int geom[])
2942{
2943 int heads;
2944 int sectors;
2945 sector_t cylinders;
2946 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002947
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002948 /* Default heads (64) & sectors (32) */
2949 heads = 64;
2950 sectors = 32;
2951
2952 tmp = heads * sectors;
2953 cylinders = capacity;
2954
2955 sector_div(cylinders, tmp);
2956
2957 /*
2958 * Handle extended translation size for logical drives > 1Gb
2959 */
2960
2961 if (capacity >= 0x200000) {
2962 heads = 255;
2963 sectors = 63;
2964 tmp = heads*sectors;
2965 cylinders = capacity;
2966 sector_div(cylinders, tmp);
2967 }
2968
2969 geom[0] = heads;
2970 geom[1] = sectors;
2971 geom[2] = cylinders;
2972
2973 return 0;
2974}
2975
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002976static void megasas_aen_polling(struct work_struct *work);
2977
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002978/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002979 * megasas_service_aen - Processes an event notification
2980 * @instance: Adapter soft state
2981 * @cmd: AEN command completed by the ISR
2982 *
2983 * For AEN, driver sends a command down to FW that is held by the FW till an
2984 * event occurs. When an event of interest occurs, FW completes the command
2985 * that it was previously holding.
2986 *
2987 * This routines sends SIGIO signal to processes that have registered with the
2988 * driver for AEN.
2989 */
2990static void
2991megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2992{
Yang, Boc3518832009-10-06 14:18:02 -06002993 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002994
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002995 /*
2996 * Don't signal app if it is just an aborted previously registered aen
2997 */
Yang, Boc3518832009-10-06 14:18:02 -06002998 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2999 spin_lock_irqsave(&poll_aen_lock, flags);
3000 megasas_poll_wait_aen = 1;
3001 spin_unlock_irqrestore(&poll_aen_lock, flags);
3002 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003003 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06003004 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003005 else
3006 cmd->abort_aen = 0;
3007
3008 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303009
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303010 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003011
bo yang39a98552010-09-22 22:36:29 -04003012 if ((instance->unload == 0) &&
3013 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003014 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003015
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003016 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3017 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003018 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003019 } else {
3020 ev->instance = instance;
3021 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08003022 INIT_DELAYED_WORK(&ev->hotplug_work,
3023 megasas_aen_polling);
3024 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003025 }
3026 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003027}
3028
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303029static ssize_t
3030megasas_fw_crash_buffer_store(struct device *cdev,
3031 struct device_attribute *attr, const char *buf, size_t count)
3032{
3033 struct Scsi_Host *shost = class_to_shost(cdev);
3034 struct megasas_instance *instance =
3035 (struct megasas_instance *) shost->hostdata;
3036 int val = 0;
3037 unsigned long flags;
3038
3039 if (kstrtoint(buf, 0, &val) != 0)
3040 return -EINVAL;
3041
3042 spin_lock_irqsave(&instance->crashdump_lock, flags);
3043 instance->fw_crash_buffer_offset = val;
3044 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3045 return strlen(buf);
3046}
3047
3048static ssize_t
3049megasas_fw_crash_buffer_show(struct device *cdev,
3050 struct device_attribute *attr, char *buf)
3051{
3052 struct Scsi_Host *shost = class_to_shost(cdev);
3053 struct megasas_instance *instance =
3054 (struct megasas_instance *) shost->hostdata;
3055 u32 size;
3056 unsigned long buff_addr;
3057 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3058 unsigned long src_addr;
3059 unsigned long flags;
3060 u32 buff_offset;
3061
3062 spin_lock_irqsave(&instance->crashdump_lock, flags);
3063 buff_offset = instance->fw_crash_buffer_offset;
3064 if (!instance->crash_dump_buf &&
3065 !((instance->fw_crash_state == AVAILABLE) ||
3066 (instance->fw_crash_state == COPYING))) {
3067 dev_err(&instance->pdev->dev,
3068 "Firmware crash dump is not available\n");
3069 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3070 return -EINVAL;
3071 }
3072
3073 buff_addr = (unsigned long) buf;
3074
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003075 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303076 dev_err(&instance->pdev->dev,
3077 "Firmware crash dump offset is out of range\n");
3078 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3079 return 0;
3080 }
3081
3082 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3083 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3084
3085 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3086 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003087 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303088 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3089
3090 return size;
3091}
3092
3093static ssize_t
3094megasas_fw_crash_buffer_size_show(struct device *cdev,
3095 struct device_attribute *attr, char *buf)
3096{
3097 struct Scsi_Host *shost = class_to_shost(cdev);
3098 struct megasas_instance *instance =
3099 (struct megasas_instance *) shost->hostdata;
3100
3101 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3102 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3103}
3104
3105static ssize_t
3106megasas_fw_crash_state_store(struct device *cdev,
3107 struct device_attribute *attr, const char *buf, size_t count)
3108{
3109 struct Scsi_Host *shost = class_to_shost(cdev);
3110 struct megasas_instance *instance =
3111 (struct megasas_instance *) shost->hostdata;
3112 int val = 0;
3113 unsigned long flags;
3114
3115 if (kstrtoint(buf, 0, &val) != 0)
3116 return -EINVAL;
3117
3118 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3119 dev_err(&instance->pdev->dev, "application updates invalid "
3120 "firmware crash state\n");
3121 return -EINVAL;
3122 }
3123
3124 instance->fw_crash_state = val;
3125
3126 if ((val == COPIED) || (val == COPY_ERROR)) {
3127 spin_lock_irqsave(&instance->crashdump_lock, flags);
3128 megasas_free_host_crash_buffer(instance);
3129 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3130 if (val == COPY_ERROR)
3131 dev_info(&instance->pdev->dev, "application failed to "
3132 "copy Firmware crash dump\n");
3133 else
3134 dev_info(&instance->pdev->dev, "Firmware crash dump "
3135 "copied successfully\n");
3136 }
3137 return strlen(buf);
3138}
3139
3140static ssize_t
3141megasas_fw_crash_state_show(struct device *cdev,
3142 struct device_attribute *attr, char *buf)
3143{
3144 struct Scsi_Host *shost = class_to_shost(cdev);
3145 struct megasas_instance *instance =
3146 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003147
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303148 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3149}
3150
3151static ssize_t
3152megasas_page_size_show(struct device *cdev,
3153 struct device_attribute *attr, char *buf)
3154{
3155 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3156}
3157
Sumit Saxena308ec452016-01-28 21:04:30 +05303158static ssize_t
3159megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3160 char *buf)
3161{
3162 struct Scsi_Host *shost = class_to_shost(cdev);
3163 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3164
3165 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3166}
3167
Shivasharan S88d155c2018-01-05 05:27:46 -08003168static ssize_t
3169megasas_fw_cmds_outstanding_show(struct device *cdev,
3170 struct device_attribute *attr, char *buf)
3171{
3172 struct Scsi_Host *shost = class_to_shost(cdev);
3173 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3174
3175 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3176}
3177
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303178static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3179 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3180static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3181 megasas_fw_crash_buffer_size_show, NULL);
3182static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3183 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3184static DEVICE_ATTR(page_size, S_IRUGO,
3185 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303186static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3187 megasas_ldio_outstanding_show, NULL);
Shivasharan S88d155c2018-01-05 05:27:46 -08003188static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3189 megasas_fw_cmds_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303190
3191struct device_attribute *megaraid_host_attrs[] = {
3192 &dev_attr_fw_crash_buffer_size,
3193 &dev_attr_fw_crash_buffer,
3194 &dev_attr_fw_crash_state,
3195 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303196 &dev_attr_ldio_outstanding,
Shivasharan S88d155c2018-01-05 05:27:46 -08003197 &dev_attr_fw_cmds_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303198 NULL,
3199};
3200
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003201/*
3202 * Scsi host template for megaraid_sas driver
3203 */
3204static struct scsi_host_template megasas_template = {
3205
3206 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303207 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003208 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003209 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003210 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303211 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003212 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003213 .eh_target_reset_handler = megasas_reset_target,
3214 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003215 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003216 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303217 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003218 .bios_param = megasas_bios_param,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003219 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003220 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003221};
3222
3223/**
3224 * megasas_complete_int_cmd - Completes an internal command
3225 * @instance: Adapter soft state
3226 * @cmd: Command to be completed
3227 *
3228 * The megasas_issue_blocked_cmd() function waits for a command to complete
3229 * after it issues a command. This function wakes up that waiting routine by
3230 * calling wake_up() on the wait queue.
3231 */
3232static void
3233megasas_complete_int_cmd(struct megasas_instance *instance,
3234 struct megasas_cmd *cmd)
3235{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303236 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003237 wake_up(&instance->int_cmd_wait_q);
3238}
3239
3240/**
3241 * megasas_complete_abort - Completes aborting a command
3242 * @instance: Adapter soft state
3243 * @cmd: Cmd that was issued to abort another cmd
3244 *
adam radford0d490162010-12-14 19:17:17 -08003245 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3246 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003247 * wakes up all functions waiting on the same wait queue.
3248 */
3249static void
3250megasas_complete_abort(struct megasas_instance *instance,
3251 struct megasas_cmd *cmd)
3252{
3253 if (cmd->sync_cmd) {
3254 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303255 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003256 wake_up(&instance->abort_cmd_wait_q);
3257 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003258}
3259
3260/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003261 * megasas_complete_cmd - Completes a command
3262 * @instance: Adapter soft state
3263 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003264 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003265 * SCSI mid-layer instead of the value returned
3266 * by the FW. This should be used if caller wants
3267 * an alternate status (as in the case of aborted
3268 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003269 */
adam radford9c915a82010-12-21 13:34:31 -08003270void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003271megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3272 u8 alt_status)
3273{
3274 int exception = 0;
3275 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003276 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003277 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303278 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003279
bo yang39a98552010-09-22 22:36:29 -04003280 /* flag for the retry reset */
3281 cmd->retry_for_fw_reset = 0;
3282
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003283 if (cmd->scmd)
3284 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003285
3286 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003287 case MFI_CMD_INVALID:
3288 /* Some older 1068 controller FW may keep a pended
3289 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3290 when booting the kdump kernel. Ignore this command to
3291 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003292 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3293 "completed\n");
3294 dev_warn(&instance->pdev->dev, "If you have a controller "
3295 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003296 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003297 case MFI_CMD_PD_SCSI_IO:
3298 case MFI_CMD_LD_SCSI_IO:
3299
3300 /*
3301 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3302 * issued either through an IO path or an IOCTL path. If it
3303 * was via IOCTL, we will send it to internal completion.
3304 */
3305 if (cmd->sync_cmd) {
3306 cmd->sync_cmd = 0;
3307 megasas_complete_int_cmd(instance, cmd);
3308 break;
3309 }
Gustavo A. R. Silva8d07f7d2018-11-27 22:32:27 -06003310 /* fall through */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003311
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003312 case MFI_CMD_LD_READ:
3313 case MFI_CMD_LD_WRITE:
3314
3315 if (alt_status) {
3316 cmd->scmd->result = alt_status << 16;
3317 exception = 1;
3318 }
3319
3320 if (exception) {
3321
Sumant Patroe4a082c2006-05-30 12:03:37 -07003322 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003323
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003324 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003325 cmd->scmd->scsi_done(cmd->scmd);
3326 megasas_return_cmd(instance, cmd);
3327
3328 break;
3329 }
3330
3331 switch (hdr->cmd_status) {
3332
3333 case MFI_STAT_OK:
3334 cmd->scmd->result = DID_OK << 16;
3335 break;
3336
3337 case MFI_STAT_SCSI_IO_FAILED:
3338 case MFI_STAT_LD_INIT_IN_PROGRESS:
3339 cmd->scmd->result =
3340 (DID_ERROR << 16) | hdr->scsi_status;
3341 break;
3342
3343 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3344
3345 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3346
3347 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3348 memset(cmd->scmd->sense_buffer, 0,
3349 SCSI_SENSE_BUFFERSIZE);
3350 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3351 hdr->sense_len);
3352
3353 cmd->scmd->result |= DRIVER_SENSE << 24;
3354 }
3355
3356 break;
3357
3358 case MFI_STAT_LD_OFFLINE:
3359 case MFI_STAT_DEVICE_NOT_FOUND:
3360 cmd->scmd->result = DID_BAD_TARGET << 16;
3361 break;
3362
3363 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003364 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003365 hdr->cmd_status);
3366 cmd->scmd->result = DID_ERROR << 16;
3367 break;
3368 }
3369
Sumant Patroe4a082c2006-05-30 12:03:37 -07003370 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003372 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003373 cmd->scmd->scsi_done(cmd->scmd);
3374 megasas_return_cmd(instance, cmd);
3375
3376 break;
3377
3378 case MFI_CMD_SMP:
3379 case MFI_CMD_STP:
Shivasharan Sf870bcb2018-01-05 05:33:04 -08003380 case MFI_CMD_NVME:
Shivasharan S82add4e2017-10-19 02:49:02 -07003381 megasas_complete_int_cmd(instance, cmd);
3382 break;
3383
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003384 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303385 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003386 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303387 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3388 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303389 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003390 spin_lock_irqsave(instance->host->host_lock, flags);
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003391 status = cmd->frame->hdr.cmd_status;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303392 instance->map_update_cmd = NULL;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003393 if (status != MFI_STAT_OK) {
3394 if (status != MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003395 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003396 cmd->frame->hdr.cmd_status);
3397 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303398 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003399 spin_unlock_irqrestore(
3400 instance->host->host_lock,
3401 flags);
3402 break;
3403 }
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003404 }
3405
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303406 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303407
3408 /*
3409 * Set fast path IO to ZERO.
3410 * Validate Map will set proper value.
3411 * Meanwhile all IOs will go as LD IO.
3412 */
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003413 if (status == MFI_STAT_OK &&
3414 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3415 instance->map_id++;
adam radford9c915a82010-12-21 13:34:31 -08003416 fusion->fast_path_io = 1;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003417 } else {
adam radford9c915a82010-12-21 13:34:31 -08003418 fusion->fast_path_io = 0;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08003419 }
3420
adam radford9c915a82010-12-21 13:34:31 -08003421 megasas_sync_map_info(instance);
3422 spin_unlock_irqrestore(instance->host->host_lock,
3423 flags);
3424 break;
3425 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303426 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3427 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003428 spin_lock_irqsave(&poll_aen_lock, flags);
3429 megasas_poll_wait_aen = 0;
3430 spin_unlock_irqrestore(&poll_aen_lock, flags);
3431 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003432
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303433 /* FW has an updated PD sequence */
3434 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3435 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3436
3437 spin_lock_irqsave(instance->host->host_lock, flags);
3438 status = cmd->frame->hdr.cmd_status;
3439 instance->jbod_seq_cmd = NULL;
3440 megasas_return_cmd(instance, cmd);
3441
3442 if (status == MFI_STAT_OK) {
3443 instance->pd_seq_map_id++;
3444 /* Re-register a pd sync seq num cmd */
3445 if (megasas_sync_pd_seq_num(instance, true))
3446 instance->use_seqnum_jbod_fp = false;
3447 } else
3448 instance->use_seqnum_jbod_fp = false;
3449
3450 spin_unlock_irqrestore(instance->host->host_lock, flags);
3451 break;
3452 }
3453
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003454 /*
3455 * See if got an event notification
3456 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303457 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003458 megasas_service_aen(instance, cmd);
3459 else
3460 megasas_complete_int_cmd(instance, cmd);
3461
3462 break;
3463
3464 case MFI_CMD_ABORT:
3465 /*
3466 * Cmd issued to abort another cmd returned
3467 */
3468 megasas_complete_abort(instance, cmd);
3469 break;
3470
3471 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003472 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003473 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003474 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003475 break;
3476 }
3477}
3478
3479/**
bo yang39a98552010-09-22 22:36:29 -04003480 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003481 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003482 * @instance: Adapter soft state
3483 */
3484static inline void
3485megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3486{
3487 struct megasas_cmd *cmd;
3488 struct list_head clist_local;
3489 union megasas_evt_class_locale class_locale;
3490 unsigned long flags;
3491 u32 seq_num;
3492
3493 INIT_LIST_HEAD(&clist_local);
3494 spin_lock_irqsave(&instance->hba_lock, flags);
3495 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3496 spin_unlock_irqrestore(&instance->hba_lock, flags);
3497
3498 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003499 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003500 struct megasas_cmd, list);
3501 list_del_init(&cmd->list);
3502
3503 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003504 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3505 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003506 cmd, cmd->scmd, cmd->sync_cmd);
3507
3508 cmd->retry_for_fw_reset++;
3509
3510 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003511 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003512 "was tried multiple times during reset."
3513 "Shutting down the HBA\n",
3514 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303515 instance->instancet->disable_intr(instance);
3516 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003517 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003518 return;
3519 }
3520 }
3521
3522 if (cmd->sync_cmd == 1) {
3523 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003524 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003525 "cmd attached to internal command!\n");
3526 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003527 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003528 "on the internal reset queue,"
3529 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303530 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003531 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003532 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003533 0, instance->reg_set);
3534 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003535 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003536 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003537 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003538
3539 atomic_inc(&instance->fw_outstanding);
3540 instance->instancet->fire_cmd(instance,
3541 cmd->frame_phys_addr,
3542 cmd->frame_count-1, instance->reg_set);
3543 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003544 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003545 "internal reset defer list while re-issue!!\n",
3546 cmd);
3547 }
3548 }
3549
3550 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003551 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003552 megasas_return_cmd(instance, instance->aen_cmd);
3553
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003554 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003555 }
3556
3557 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003558 * Initiate AEN (Asynchronous Event Notification)
3559 */
bo yang39a98552010-09-22 22:36:29 -04003560 seq_num = instance->last_seq_num;
3561 class_locale.members.reserved = 0;
3562 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3563 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3564
3565 megasas_register_aen(instance, seq_num, class_locale.word);
3566}
3567
3568/**
3569 * Move the internal reset pending commands to a deferred queue.
3570 *
3571 * We move the commands pending at internal reset time to a
3572 * pending queue. This queue would be flushed after successful
3573 * completion of the internal reset sequence. if the internal reset
3574 * did not complete in time, the kernel reset handler would flush
3575 * these commands.
3576 **/
3577static void
3578megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3579{
3580 struct megasas_cmd *cmd;
3581 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003582 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003583 u32 defer_index;
3584 unsigned long flags;
3585
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003586 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303587 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003588 for (i = 0; i < max_cmd; i++) {
3589 cmd = instance->cmd_list[i];
3590 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003591 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003592 "on the defer queue as internal\n",
3593 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3594
3595 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003596 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003597 " moving this cmd:%p, %d %p, it was"
3598 "discovered on some list?\n",
3599 cmd, cmd->sync_cmd, cmd->scmd);
3600
3601 list_del_init(&cmd->list);
3602 }
3603 defer_index++;
3604 list_add_tail(&cmd->list,
3605 &instance->internal_reset_pending_q);
3606 }
3607 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303608 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003609}
3610
3611
3612static void
3613process_fw_state_change_wq(struct work_struct *work)
3614{
3615 struct megasas_instance *instance =
3616 container_of(work, struct megasas_instance, work_init);
3617 u32 wait;
3618 unsigned long flags;
3619
Sumit Saxena8a01a412016-01-28 21:04:32 +05303620 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003621 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303622 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003623 return ;
3624 }
3625
Sumit Saxena8a01a412016-01-28 21:04:32 +05303626 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003627 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003628 "state, restarting it...\n");
3629
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303630 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003631 atomic_set(&instance->fw_outstanding, 0);
3632
3633 atomic_set(&instance->fw_reset_no_pci_access, 1);
3634 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003635 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003636
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003637 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003638 "initiating next stage...\n");
3639
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003640 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003641 "state 2 starting...\n");
3642
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003643 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003644 for (wait = 0; wait < 30; wait++) {
3645 msleep(1000);
3646 }
3647
adam radford058a8fa2011-10-08 18:14:27 -07003648 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003649 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003650
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303651 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003652 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003653 return ;
3654 }
3655
3656 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3657 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3658 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3659 ) {
3660 *instance->consumer = *instance->producer;
3661 } else {
3662 *instance->consumer = 0;
3663 *instance->producer = 0;
3664 }
3665
3666 megasas_issue_init_mfi(instance);
3667
3668 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303669 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003670 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303671 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003672
3673 megasas_issue_pending_cmds_again(instance);
3674 instance->issuepend_done = 1;
3675 }
bo yang39a98552010-09-22 22:36:29 -04003676}
3677
3678/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003679 * megasas_deplete_reply_queue - Processes all completed commands
3680 * @instance: Adapter soft state
3681 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003682 * SCSI mid-layer instead of the status
3683 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003684 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003685 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003686static int
bo yang39a98552010-09-22 22:36:29 -04003687megasas_deplete_reply_queue(struct megasas_instance *instance,
3688 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003689{
bo yang39a98552010-09-22 22:36:29 -04003690 u32 mfiStatus;
3691 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003692
bo yang39a98552010-09-22 22:36:29 -04003693 if ((mfiStatus = instance->instancet->check_reset(instance,
3694 instance->reg_set)) == 1) {
3695 return IRQ_HANDLED;
3696 }
3697
Shivasharan Sde516372018-12-17 00:47:39 -08003698 mfiStatus = instance->instancet->clear_intr(instance);
3699 if (mfiStatus == 0) {
adam radforde1419192011-02-24 20:56:21 -08003700 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003701 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003702 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003703 }
3704
3705 instance->mfiStatus = mfiStatus;
3706
3707 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3708 fw_state = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08003709 instance) & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003710
3711 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003712 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003713 fw_state);
3714 }
3715
3716 if ((fw_state == MFI_STATE_FAULT) &&
3717 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003718 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003719
3720 if ((instance->pdev->device ==
3721 PCI_DEVICE_ID_LSI_SAS1064R) ||
3722 (instance->pdev->device ==
3723 PCI_DEVICE_ID_DELL_PERC5) ||
3724 (instance->pdev->device ==
3725 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3726
3727 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303728 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003729 }
3730
3731
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303732 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303733 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003734 instance->issuepend_done = 0;
3735
3736 atomic_set(&instance->fw_outstanding, 0);
3737 megasas_internal_reset_defer_cmds(instance);
3738
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003739 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303740 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003741
3742 schedule_work(&instance->work_init);
3743 return IRQ_HANDLED;
3744
3745 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003746 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003747 fw_state, instance->disableOnlineCtrlReset);
3748 }
3749 }
3750
Sumant Patro5d018ad2006-10-03 13:13:18 -07003751 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003752 return IRQ_HANDLED;
3753}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003754/**
3755 * megasas_isr - isr entry point
3756 */
David Howells7d12e782006-10-05 14:55:46 +01003757static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003758{
adam radfordc8e858f2011-10-08 18:15:13 -07003759 struct megasas_irq_context *irq_context = devp;
3760 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003761 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003762 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003763
adam radfordc8e858f2011-10-08 18:15:13 -07003764 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003765 return IRQ_HANDLED;
3766
bo yang39a98552010-09-22 22:36:29 -04003767 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003768 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003769 spin_unlock_irqrestore(&instance->hba_lock, flags);
3770
3771 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003772}
3773
3774/**
3775 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003776 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003777 *
3778 * During the initialization, FW passes can potentially be in any one of
3779 * several possible states. If the FW in operational, waiting-for-handshake
3780 * states, driver must take steps to bring it to ready state. Otherwise, it
3781 * has to wait for the ready state.
3782 */
adam radford9c915a82010-12-21 13:34:31 -08003783int
adam radford058a8fa2011-10-08 18:14:27 -07003784megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003785{
3786 int i;
3787 u8 max_wait;
3788 u32 fw_state;
3789 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003790 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003791
Shivasharan Sde516372018-12-17 00:47:39 -08003792 abs_state = instance->instancet->read_fw_status_reg(instance);
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003793 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003794
Sumant Patroe3bbff92006-10-03 12:28:49 -07003795 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003796 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003797 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003798
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003799 while (fw_state != MFI_STATE_READY) {
3800
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003801 switch (fw_state) {
3802
3803 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003804 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003805 if (ocr) {
3806 max_wait = MEGASAS_RESET_WAIT_TIME;
3807 cur_state = MFI_STATE_FAULT;
3808 break;
3809 } else
3810 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003811
3812 case MFI_STATE_WAIT_HANDSHAKE:
3813 /*
3814 * Set the CLR bit in inbound doorbell
3815 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003816 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003817 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3818 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003819 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003820 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003821 writel(
3822 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003823 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303824 else
Yang, Bo87911122009-10-06 14:31:54 -06003825 writel(
3826 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3827 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003828
Yang, Bo7218df62009-10-06 14:52:20 -06003829 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003830 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3831 break;
3832
Sumant Patroe3bbff92006-10-03 12:28:49 -07003833 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003834 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003835 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3836 (instance->pdev->device ==
3837 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003838 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003839 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003840 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303841 else
Yang, Bo87911122009-10-06 14:31:54 -06003842 writel(MFI_INIT_HOTPLUG,
3843 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003844
Yang, Bo7218df62009-10-06 14:52:20 -06003845 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003846 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3847 break;
3848
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003849 case MFI_STATE_OPERATIONAL:
3850 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003851 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003852 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303853 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003854 if ((instance->pdev->device ==
3855 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3856 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003857 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003858 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003859 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003860 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303861
Shivasharan Se7d36b82017-10-19 02:48:50 -07003862 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003863 for (i = 0; i < (10 * 1000); i += 20) {
Shivasharan S272652f2018-12-17 00:47:40 -08003864 if (megasas_readl(
3865 instance,
adam radford9c915a82010-12-21 13:34:31 -08003866 &instance->
3867 reg_set->
3868 doorbell) & 1)
3869 msleep(20);
3870 else
3871 break;
3872 }
3873 }
Yang, Bo87911122009-10-06 14:31:54 -06003874 } else
3875 writel(MFI_RESET_FLAGS,
3876 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003877
Yang, Bo7218df62009-10-06 14:52:20 -06003878 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003879 cur_state = MFI_STATE_OPERATIONAL;
3880 break;
3881
3882 case MFI_STATE_UNDEFINED:
3883 /*
3884 * This state should not last for more than 2 seconds
3885 */
Yang, Bo7218df62009-10-06 14:52:20 -06003886 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003887 cur_state = MFI_STATE_UNDEFINED;
3888 break;
3889
3890 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003891 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003892 cur_state = MFI_STATE_BB_INIT;
3893 break;
3894
3895 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003896 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003897 cur_state = MFI_STATE_FW_INIT;
3898 break;
3899
3900 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003901 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003902 cur_state = MFI_STATE_FW_INIT_2;
3903 break;
3904
3905 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003906 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003907 cur_state = MFI_STATE_DEVICE_SCAN;
3908 break;
3909
3910 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003911 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912 cur_state = MFI_STATE_FLUSH_CACHE;
3913 break;
3914
3915 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003916 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003917 fw_state);
3918 return -ENODEV;
3919 }
3920
3921 /*
3922 * The cur_state should not last for more than max_wait secs
3923 */
Steve Sistare31b6a052019-03-01 06:46:28 -08003924 for (i = 0; i < max_wait * 50; i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003925 curr_abs_state = instance->instancet->
Shivasharan Sde516372018-12-17 00:47:39 -08003926 read_fw_status_reg(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003927
Yang, Bo7218df62009-10-06 14:52:20 -06003928 if (abs_state == curr_abs_state) {
Steve Sistare31b6a052019-03-01 06:46:28 -08003929 msleep(20);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003930 } else
3931 break;
3932 }
3933
3934 /*
3935 * Return error if fw_state hasn't changed after max_wait
3936 */
Yang, Bo7218df62009-10-06 14:52:20 -06003937 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003938 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003939 "in %d secs\n", fw_state, max_wait);
3940 return -ENODEV;
3941 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003942
3943 abs_state = curr_abs_state;
3944 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003945 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003946 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003947
3948 return 0;
3949}
3950
3951/**
3952 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3953 * @instance: Adapter soft state
3954 */
3955static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3956{
3957 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003958 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003959 struct megasas_cmd *cmd;
3960
3961 if (!instance->frame_dma_pool)
3962 return;
3963
3964 /*
3965 * Return all frames to pool
3966 */
3967 for (i = 0; i < max_cmd; i++) {
3968
3969 cmd = instance->cmd_list[i];
3970
3971 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02003972 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003973 cmd->frame_phys_addr);
3974
3975 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02003976 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003977 cmd->sense_phys_addr);
3978 }
3979
3980 /*
3981 * Now destroy the pool itself
3982 */
Romain Perierfc69d862017-07-06 10:13:06 +02003983 dma_pool_destroy(instance->frame_dma_pool);
3984 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003985
3986 instance->frame_dma_pool = NULL;
3987 instance->sense_dma_pool = NULL;
3988}
3989
3990/**
3991 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3992 * @instance: Adapter soft state
3993 *
3994 * Each command packet has an embedded DMA memory buffer that is used for
3995 * filling MFI frame and the SG list that immediately follows the frame. This
3996 * function creates those DMA memory buffers for each command packet by using
3997 * PCI pool facility.
3998 */
3999static int megasas_create_frame_pool(struct megasas_instance *instance)
4000{
4001 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004002 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004003 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004004 u32 frame_count;
4005 struct megasas_cmd *cmd;
4006
adam radford9c915a82010-12-21 13:34:31 -08004007 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004008
4009 /*
4010 * Size of our frame is 64 bytes for MFI frame, followed by max SG
4011 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4012 */
4013 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4014 sizeof(struct megasas_sge32);
4015
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004016 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06004017 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06004018
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004019 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05304020 * For MFI controllers.
4021 * max_num_sge = 60
4022 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4023 * Total 960 byte (15 MFI frame of 64 byte)
4024 *
4025 * Fusion adapter require only 3 extra frame.
4026 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4027 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4028 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004029 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07004030 frame_count = (instance->adapter_type == MFI_SERIES) ?
4031 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08004032 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004033 /*
4034 * Use DMA pool facility provided by PCI layer
4035 */
Romain Perierfc69d862017-07-06 10:13:06 +02004036 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4037 &instance->pdev->dev,
4038 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004039
4040 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004041 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004042 return -ENOMEM;
4043 }
4044
Romain Perierfc69d862017-07-06 10:13:06 +02004045 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4046 &instance->pdev->dev, 128,
4047 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004048
4049 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004050 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004051
Romain Perierfc69d862017-07-06 10:13:06 +02004052 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004053 instance->frame_dma_pool = NULL;
4054
4055 return -ENOMEM;
4056 }
4057
4058 /*
4059 * Allocate and attach a frame to each of the commands in cmd_list.
4060 * By making cmd->index as the context instead of the &cmd, we can
4061 * always use 32bit context regardless of the architecture
4062 */
4063 for (i = 0; i < max_cmd; i++) {
4064
4065 cmd = instance->cmd_list[i];
4066
Souptick Joarder61b142a2018-02-15 21:55:06 +05304067 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004068 GFP_KERNEL, &cmd->frame_phys_addr);
4069
Romain Perierfc69d862017-07-06 10:13:06 +02004070 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004071 GFP_KERNEL, &cmd->sense_phys_addr);
4072
4073 /*
4074 * megasas_teardown_frame_pool() takes care of freeing
4075 * whatever has been allocated
4076 */
4077 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004078 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004079 megasas_teardown_frame_pool(instance);
4080 return -ENOMEM;
4081 }
4082
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304083 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004084 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004085 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004086 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004087 }
4088
4089 return 0;
4090}
4091
4092/**
4093 * megasas_free_cmds - Free all the cmds in the free cmd pool
4094 * @instance: Adapter soft state
4095 */
adam radford9c915a82010-12-21 13:34:31 -08004096void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004097{
4098 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004099
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004100 /* First free the MFI frame pool */
4101 megasas_teardown_frame_pool(instance);
4102
4103 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004104 for (i = 0; i < instance->max_mfi_cmds; i++)
4105
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004106 kfree(instance->cmd_list[i]);
4107
4108 /* Free the cmd_list buffer itself */
4109 kfree(instance->cmd_list);
4110 instance->cmd_list = NULL;
4111
4112 INIT_LIST_HEAD(&instance->cmd_pool);
4113}
4114
4115/**
4116 * megasas_alloc_cmds - Allocates the command packets
4117 * @instance: Adapter soft state
4118 *
4119 * Each command that is issued to the FW, whether IO commands from the OS or
4120 * internal commands like IOCTLs, are wrapped in local data structure called
4121 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4122 * the FW.
4123 *
4124 * Each frame has a 32-bit field called context (tag). This context is used
4125 * to get back the megasas_cmd from the frame when a frame gets completed in
4126 * the ISR. Typically the address of the megasas_cmd itself would be used as
4127 * the context. But we wanted to keep the differences between 32 and 64 bit
4128 * systems to the mininum. We always use 32 bit integers for the context. In
4129 * this driver, the 32 bit values are the indices into an array cmd_list.
4130 * This array is used only to look up the megasas_cmd given the context. The
4131 * free commands themselves are maintained in a linked list called cmd_pool.
4132 */
adam radford9c915a82010-12-21 13:34:31 -08004133int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004134{
4135 int i;
4136 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004137 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004138 struct megasas_cmd *cmd;
4139
adam radford9c915a82010-12-21 13:34:31 -08004140 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004141
4142 /*
4143 * instance->cmd_list is an array of struct megasas_cmd pointers.
4144 * Allocate the dynamic array first and then allocate individual
4145 * commands.
4146 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004147 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004148
4149 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004150 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004151 return -ENOMEM;
4152 }
4153
adam radford9c915a82010-12-21 13:34:31 -08004154 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004155
4156 for (i = 0; i < max_cmd; i++) {
4157 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4158 GFP_KERNEL);
4159
4160 if (!instance->cmd_list[i]) {
4161
4162 for (j = 0; j < i; j++)
4163 kfree(instance->cmd_list[j]);
4164
4165 kfree(instance->cmd_list);
4166 instance->cmd_list = NULL;
4167
4168 return -ENOMEM;
4169 }
4170 }
4171
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004172 for (i = 0; i < max_cmd; i++) {
4173 cmd = instance->cmd_list[i];
4174 memset(cmd, 0, sizeof(struct megasas_cmd));
4175 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004176 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004177 cmd->instance = instance;
4178
4179 list_add_tail(&cmd->list, &instance->cmd_pool);
4180 }
4181
4182 /*
4183 * Create a frame pool and assign one frame to each cmd
4184 */
4185 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004186 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004187 megasas_free_cmds(instance);
Jason Yanbcf3b672019-02-15 19:50:27 +08004188 return -ENOMEM;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004189 }
4190
4191 return 0;
4192}
4193
Yang, Bo81e403c2009-10-06 14:27:54 -06004194/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304195 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4196 * @instance: Adapter soft state
4197 *
4198 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4199 * or FW is not under OCR.
4200 */
4201inline int
4202dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4203
Shivasharan Se7d36b82017-10-19 02:48:50 -07004204 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304205 return KILL_ADAPTER;
4206 else if (instance->unload ||
4207 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4208 return IGNORE_TIMEOUT;
4209 else
4210 return INITIATE_OCR;
4211}
4212
Shivasharan S15dd0382017-02-10 00:59:10 -08004213static void
4214megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304215{
4216 int ret;
4217 struct megasas_cmd *cmd;
4218 struct megasas_dcmd_frame *dcmd;
4219
Shivasharan S15dd0382017-02-10 00:59:10 -08004220 struct MR_PRIV_DEVICE *mr_device_priv_data;
4221 u16 device_id = 0;
4222
4223 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304224 cmd = megasas_get_cmd(instance);
4225
4226 if (!cmd) {
4227 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004228 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304229 }
4230
4231 dcmd = &cmd->frame->dcmd;
4232
4233 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4234 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4235
4236 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4237 dcmd->cmd = MFI_CMD_DCMD;
4238 dcmd->cmd_status = 0xFF;
4239 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004240 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304241 dcmd->timeout = 0;
4242 dcmd->pad_0 = 0;
4243 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4244 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304245
Shivasharan S107a60d2017-10-19 02:49:05 -07004246 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4247 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304248
Shivasharan Se7d36b82017-10-19 02:48:50 -07004249 if ((instance->adapter_type != MFI_SERIES) &&
4250 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304251 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4252 else
4253 ret = megasas_issue_polled(instance, cmd);
4254
4255 switch (ret) {
4256 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004257 mr_device_priv_data = sdev->hostdata;
4258 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4259 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304260 instance->pd_info->state.ddf.pdType.intf;
4261 break;
4262
4263 case DCMD_TIMEOUT:
4264
4265 switch (dcmd_timeout_ocr_possible(instance)) {
4266 case INITIATE_OCR:
4267 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4268 megasas_reset_fusion(instance->host,
4269 MFI_IO_TIMEOUT_OCR);
4270 break;
4271 case KILL_ADAPTER:
4272 megaraid_sas_kill_hba(instance);
4273 break;
4274 case IGNORE_TIMEOUT:
4275 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4276 __func__, __LINE__);
4277 break;
4278 }
4279
4280 break;
4281 }
4282
4283 if (ret != DCMD_TIMEOUT)
4284 megasas_return_cmd(instance, cmd);
4285
Shivasharan S15dd0382017-02-10 00:59:10 -08004286 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304287}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304288/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004289 * megasas_get_pd_list_info - Returns FW's pd_list structure
4290 * @instance: Adapter soft state
4291 * @pd_list: pd_list structure
4292 *
4293 * Issues an internal command (DCMD) to get the FW's controller PD
4294 * list structure. This information is mainly used to find out SYSTEM
4295 * supported by the FW.
4296 */
4297static int
4298megasas_get_pd_list(struct megasas_instance *instance)
4299{
4300 int ret = 0, pd_index = 0;
4301 struct megasas_cmd *cmd;
4302 struct megasas_dcmd_frame *dcmd;
4303 struct MR_PD_LIST *ci;
4304 struct MR_PD_ADDRESS *pd_addr;
4305 dma_addr_t ci_h = 0;
4306
Sumit Saxenad9083162016-07-08 03:30:16 -07004307 if (instance->pd_list_not_supported) {
4308 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4309 "not supported by firmware\n");
4310 return ret;
4311 }
4312
Shivasharan S9b3d0282017-10-19 02:48:56 -07004313 ci = instance->pd_list_buf;
4314 ci_h = instance->pd_list_buf_h;
4315
Yang, Bo81e403c2009-10-06 14:27:54 -06004316 cmd = megasas_get_cmd(instance);
4317
4318 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004319 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004320 return -ENOMEM;
4321 }
4322
4323 dcmd = &cmd->frame->dcmd;
4324
Yang, Bo81e403c2009-10-06 14:27:54 -06004325 memset(ci, 0, sizeof(*ci));
4326 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4327
4328 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4329 dcmd->mbox.b[1] = 0;
4330 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304331 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004332 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004333 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004334 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004335 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304336 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4337 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004338
Shivasharan S107a60d2017-10-19 02:49:05 -07004339 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4340 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004341
Shivasharan Se7d36b82017-10-19 02:48:50 -07004342 if ((instance->adapter_type != MFI_SERIES) &&
4343 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304344 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304345 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304346 else
4347 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004348
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304349 switch (ret) {
4350 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004351 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4352 "failed/not supported by firmware\n");
4353
Shivasharan Se7d36b82017-10-19 02:48:50 -07004354 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004355 megaraid_sas_kill_hba(instance);
4356 else
4357 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304358 break;
4359 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004360
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304361 switch (dcmd_timeout_ocr_possible(instance)) {
4362 case INITIATE_OCR:
4363 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4364 /*
4365 * DCMD failed from AEN path.
4366 * AEN path already hold reset_mutex to avoid PCI access
4367 * while OCR is in progress.
4368 */
4369 mutex_unlock(&instance->reset_mutex);
4370 megasas_reset_fusion(instance->host,
4371 MFI_IO_TIMEOUT_OCR);
4372 mutex_lock(&instance->reset_mutex);
4373 break;
4374 case KILL_ADAPTER:
4375 megaraid_sas_kill_hba(instance);
4376 break;
4377 case IGNORE_TIMEOUT:
4378 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4379 __func__, __LINE__);
4380 break;
4381 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004382
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304383 break;
4384
4385 case DCMD_SUCCESS:
4386 pd_addr = ci->addr;
4387
4388 if ((le32_to_cpu(ci->count) >
4389 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4390 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004391
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304392 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304393 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004394
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304395 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304396 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304397 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304398 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304399 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304400 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304401 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004402 pd_addr++;
4403 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304404
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304405 memcpy(instance->pd_list, instance->local_pd_list,
4406 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304407 break;
4408
Yang, Bo81e403c2009-10-06 14:27:54 -06004409 }
4410
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304411 if (ret != DCMD_TIMEOUT)
4412 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004413
4414 return ret;
4415}
4416
Yang, Bobdc6fb82009-12-06 08:30:19 -07004417/*
4418 * megasas_get_ld_list_info - Returns FW's ld_list structure
4419 * @instance: Adapter soft state
4420 * @ld_list: ld_list structure
4421 *
4422 * Issues an internal command (DCMD) to get the FW's controller PD
4423 * list structure. This information is mainly used to find out SYSTEM
4424 * supported by the FW.
4425 */
4426static int
4427megasas_get_ld_list(struct megasas_instance *instance)
4428{
4429 int ret = 0, ld_index = 0, ids = 0;
4430 struct megasas_cmd *cmd;
4431 struct megasas_dcmd_frame *dcmd;
4432 struct MR_LD_LIST *ci;
4433 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304434 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004435
Shivasharan S9b3d0282017-10-19 02:48:56 -07004436 ci = instance->ld_list_buf;
4437 ci_h = instance->ld_list_buf_h;
4438
Yang, Bobdc6fb82009-12-06 08:30:19 -07004439 cmd = megasas_get_cmd(instance);
4440
4441 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004442 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004443 return -ENOMEM;
4444 }
4445
4446 dcmd = &cmd->frame->dcmd;
4447
Yang, Bobdc6fb82009-12-06 08:30:19 -07004448 memset(ci, 0, sizeof(*ci));
4449 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4450
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304451 if (instance->supportmax256vd)
4452 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004453 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304454 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004455 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004456 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004457 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304458 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4459 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004460 dcmd->pad_0 = 0;
4461
Shivasharan S107a60d2017-10-19 02:49:05 -07004462 megasas_set_dma_settings(instance, dcmd, ci_h,
4463 sizeof(struct MR_LD_LIST));
4464
Shivasharan Se7d36b82017-10-19 02:48:50 -07004465 if ((instance->adapter_type != MFI_SERIES) &&
4466 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304467 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304468 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304469 else
4470 ret = megasas_issue_polled(instance, cmd);
4471
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304472 ld_count = le32_to_cpu(ci->ldCount);
4473
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304474 switch (ret) {
4475 case DCMD_FAILED:
4476 megaraid_sas_kill_hba(instance);
4477 break;
4478 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004479
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304480 switch (dcmd_timeout_ocr_possible(instance)) {
4481 case INITIATE_OCR:
4482 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4483 /*
4484 * DCMD failed from AEN path.
4485 * AEN path already hold reset_mutex to avoid PCI access
4486 * while OCR is in progress.
4487 */
4488 mutex_unlock(&instance->reset_mutex);
4489 megasas_reset_fusion(instance->host,
4490 MFI_IO_TIMEOUT_OCR);
4491 mutex_lock(&instance->reset_mutex);
4492 break;
4493 case KILL_ADAPTER:
4494 megaraid_sas_kill_hba(instance);
4495 break;
4496 case IGNORE_TIMEOUT:
4497 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4498 __func__, __LINE__);
4499 break;
4500 }
4501
4502 break;
4503
4504 case DCMD_SUCCESS:
4505 if (ld_count > instance->fw_supported_vd_count)
4506 break;
4507
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304508 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004509
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304510 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004511 if (ci->ldList[ld_index].state != 0) {
4512 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304513 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004514 }
4515 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304516
4517 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004518 }
4519
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304520 if (ret != DCMD_TIMEOUT)
4521 megasas_return_cmd(instance, cmd);
4522
Yang, Bobdc6fb82009-12-06 08:30:19 -07004523 return ret;
4524}
4525
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004526/**
adam radford21c9e162013-09-06 15:27:14 -07004527 * megasas_ld_list_query - Returns FW's ld_list structure
4528 * @instance: Adapter soft state
4529 * @ld_list: ld_list structure
4530 *
4531 * Issues an internal command (DCMD) to get the FW's controller PD
4532 * list structure. This information is mainly used to find out SYSTEM
4533 * supported by the FW.
4534 */
4535static int
4536megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4537{
4538 int ret = 0, ld_index = 0, ids = 0;
4539 struct megasas_cmd *cmd;
4540 struct megasas_dcmd_frame *dcmd;
4541 struct MR_LD_TARGETID_LIST *ci;
4542 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304543 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004544
Shivasharan S9b3d0282017-10-19 02:48:56 -07004545 ci = instance->ld_targetid_list_buf;
4546 ci_h = instance->ld_targetid_list_buf_h;
4547
adam radford21c9e162013-09-06 15:27:14 -07004548 cmd = megasas_get_cmd(instance);
4549
4550 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004551 dev_warn(&instance->pdev->dev,
4552 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004553 return -ENOMEM;
4554 }
4555
4556 dcmd = &cmd->frame->dcmd;
4557
adam radford21c9e162013-09-06 15:27:14 -07004558 memset(ci, 0, sizeof(*ci));
4559 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4560
4561 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304562 if (instance->supportmax256vd)
4563 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004564
4565 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304566 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004567 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004568 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004569 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304570 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4571 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004572 dcmd->pad_0 = 0;
4573
Shivasharan S107a60d2017-10-19 02:49:05 -07004574 megasas_set_dma_settings(instance, dcmd, ci_h,
4575 sizeof(struct MR_LD_TARGETID_LIST));
4576
Shivasharan Se7d36b82017-10-19 02:48:50 -07004577 if ((instance->adapter_type != MFI_SERIES) &&
4578 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304579 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304580 else
4581 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004582
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304583 switch (ret) {
4584 case DCMD_FAILED:
4585 dev_info(&instance->pdev->dev,
4586 "DCMD not supported by firmware - %s %d\n",
4587 __func__, __LINE__);
4588 ret = megasas_get_ld_list(instance);
4589 break;
4590 case DCMD_TIMEOUT:
4591 switch (dcmd_timeout_ocr_possible(instance)) {
4592 case INITIATE_OCR:
4593 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4594 /*
4595 * DCMD failed from AEN path.
4596 * AEN path already hold reset_mutex to avoid PCI access
4597 * while OCR is in progress.
4598 */
4599 mutex_unlock(&instance->reset_mutex);
4600 megasas_reset_fusion(instance->host,
4601 MFI_IO_TIMEOUT_OCR);
4602 mutex_lock(&instance->reset_mutex);
4603 break;
4604 case KILL_ADAPTER:
4605 megaraid_sas_kill_hba(instance);
4606 break;
4607 case IGNORE_TIMEOUT:
4608 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4609 __func__, __LINE__);
4610 break;
4611 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304612
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304613 break;
4614 case DCMD_SUCCESS:
4615 tgtid_count = le32_to_cpu(ci->count);
4616
4617 if ((tgtid_count > (instance->fw_supported_vd_count)))
4618 break;
4619
adam radford21c9e162013-09-06 15:27:14 -07004620 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304621 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004622 ids = ci->targetId[ld_index];
4623 instance->ld_ids[ids] = ci->targetId[ld_index];
4624 }
4625
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304626 break;
adam radford21c9e162013-09-06 15:27:14 -07004627 }
4628
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304629 if (ret != DCMD_TIMEOUT)
4630 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004631
4632 return ret;
4633}
4634
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004635/**
4636 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4637 * dcmd.mbox - reserved
4638 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4639 * Desc: This DCMD will return the combined device list
4640 * Status: MFI_STAT_OK - List returned successfully
4641 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4642 * disabled
4643 * @instance: Adapter soft state
4644 * @is_probe: Driver probe check
4645 * Return: 0 if DCMD succeeded
4646 * non-zero if failed
4647 */
YueHaibing7c3f8ca2019-04-13 22:21:38 +08004648static int
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004649megasas_host_device_list_query(struct megasas_instance *instance,
4650 bool is_probe)
4651{
4652 int ret, i, target_id;
4653 struct megasas_cmd *cmd;
4654 struct megasas_dcmd_frame *dcmd;
4655 struct MR_HOST_DEVICE_LIST *ci;
4656 u32 count;
4657 dma_addr_t ci_h;
4658
4659 ci = instance->host_device_list_buf;
4660 ci_h = instance->host_device_list_buf_h;
4661
4662 cmd = megasas_get_cmd(instance);
4663
4664 if (!cmd) {
4665 dev_warn(&instance->pdev->dev,
4666 "%s: failed to get cmd\n",
4667 __func__);
4668 return -ENOMEM;
4669 }
4670
4671 dcmd = &cmd->frame->dcmd;
4672
4673 memset(ci, 0, sizeof(*ci));
4674 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4675
4676 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4677 dcmd->cmd = MFI_CMD_DCMD;
4678 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4679 dcmd->sge_count = 1;
4680 dcmd->flags = MFI_FRAME_DIR_READ;
4681 dcmd->timeout = 0;
4682 dcmd->pad_0 = 0;
4683 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4684 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4685
4686 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4687
4688 if (!instance->mask_interrupts) {
4689 ret = megasas_issue_blocked_cmd(instance, cmd,
4690 MFI_IO_TIMEOUT_SECS);
4691 } else {
4692 ret = megasas_issue_polled(instance, cmd);
4693 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4694 }
4695
4696 switch (ret) {
4697 case DCMD_SUCCESS:
4698 /* Fill the internal pd_list and ld_ids array based on
4699 * targetIds returned by FW
4700 */
4701 count = le32_to_cpu(ci->count);
4702
4703 memset(instance->local_pd_list, 0,
4704 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4705 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4706 for (i = 0; i < count; i++) {
4707 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4708 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4709 instance->local_pd_list[target_id].tid = target_id;
4710 instance->local_pd_list[target_id].driveType =
4711 ci->host_device_list[i].scsi_type;
4712 instance->local_pd_list[target_id].driveState =
4713 MR_PD_STATE_SYSTEM;
4714 } else {
4715 instance->ld_ids[target_id] = target_id;
4716 }
4717 }
4718
4719 memcpy(instance->pd_list, instance->local_pd_list,
4720 sizeof(instance->pd_list));
4721 break;
4722
4723 case DCMD_TIMEOUT:
4724 switch (dcmd_timeout_ocr_possible(instance)) {
4725 case INITIATE_OCR:
4726 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4727 megasas_reset_fusion(instance->host,
4728 MFI_IO_TIMEOUT_OCR);
4729 break;
4730 case KILL_ADAPTER:
4731 megaraid_sas_kill_hba(instance);
4732 break;
4733 case IGNORE_TIMEOUT:
4734 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4735 __func__, __LINE__);
4736 break;
4737 }
4738 break;
4739 case DCMD_FAILED:
4740 dev_err(&instance->pdev->dev,
4741 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4742 __func__);
4743 break;
4744 }
4745
4746 if (ret != DCMD_TIMEOUT)
4747 megasas_return_cmd(instance, cmd);
4748
4749 return ret;
4750}
4751
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304752/*
4753 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4754 * instance : Controller's instance
4755*/
4756static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4757{
4758 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004759 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304760
4761 fusion = instance->ctrl_context;
4762 /* For MFI based controllers return dummy success */
4763 if (!fusion)
4764 return;
4765
4766 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004767 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304768 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004769 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304770 instance->supportmax256vd = 1;
4771
4772 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4773 * MEGASAS_MAX_DEV_PER_CHANNEL;
4774 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4775 * MEGASAS_MAX_DEV_PER_CHANNEL;
4776 if (instance->supportmax256vd) {
4777 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4778 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4779 } else {
4780 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4781 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4782 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304783
4784 dev_info(&instance->pdev->dev,
Shivasharan Scba67d92018-10-16 23:37:56 -07004785 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4786 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4787 instance->ctrl_info_buf->max_lds);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304788
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004789 if (instance->max_raid_mapsize) {
4790 ventura_map_sz = instance->max_raid_mapsize *
4791 MR_MIN_MAP_SIZE; /* 64k */
4792 fusion->current_map_sz = ventura_map_sz;
4793 fusion->max_map_sz = ventura_map_sz;
4794 } else {
4795 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4796 (sizeof(struct MR_LD_SPAN_MAP) *
4797 (instance->fw_supported_vd_count - 1));
4798 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304799
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004800 fusion->max_map_sz =
4801 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304802
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004803 if (instance->supportmax256vd)
4804 fusion->current_map_sz = fusion->new_map_sz;
4805 else
4806 fusion->current_map_sz = fusion->old_map_sz;
4807 }
4808 /* irrespective of FW raid maps, driver raid map is constant */
4809 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304810}
4811
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004812/*
4813 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4814 * dcmd.hdr.length - number of bytes to read
4815 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4816 * Desc: Fill in snapdump properties
4817 * Status: MFI_STAT_OK- Command successful
4818 */
4819void megasas_get_snapdump_properties(struct megasas_instance *instance)
4820{
4821 int ret = 0;
4822 struct megasas_cmd *cmd;
4823 struct megasas_dcmd_frame *dcmd;
4824 struct MR_SNAPDUMP_PROPERTIES *ci;
4825 dma_addr_t ci_h = 0;
4826
4827 ci = instance->snapdump_prop;
4828 ci_h = instance->snapdump_prop_h;
4829
4830 if (!ci)
4831 return;
4832
4833 cmd = megasas_get_cmd(instance);
4834
4835 if (!cmd) {
4836 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4837 return;
4838 }
4839
4840 dcmd = &cmd->frame->dcmd;
4841
4842 memset(ci, 0, sizeof(*ci));
4843 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4844
4845 dcmd->cmd = MFI_CMD_DCMD;
4846 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4847 dcmd->sge_count = 1;
4848 dcmd->flags = MFI_FRAME_DIR_READ;
4849 dcmd->timeout = 0;
4850 dcmd->pad_0 = 0;
4851 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
4852 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
4853
4854 megasas_set_dma_settings(instance, dcmd, ci_h,
4855 sizeof(struct MR_SNAPDUMP_PROPERTIES));
4856
4857 if (!instance->mask_interrupts) {
4858 ret = megasas_issue_blocked_cmd(instance, cmd,
4859 MFI_IO_TIMEOUT_SECS);
4860 } else {
4861 ret = megasas_issue_polled(instance, cmd);
4862 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4863 }
4864
4865 switch (ret) {
4866 case DCMD_SUCCESS:
4867 instance->snapdump_wait_time =
4868 min_t(u8, ci->trigger_min_num_sec_before_ocr,
4869 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
4870 break;
4871
4872 case DCMD_TIMEOUT:
4873 switch (dcmd_timeout_ocr_possible(instance)) {
4874 case INITIATE_OCR:
4875 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4876 megasas_reset_fusion(instance->host,
4877 MFI_IO_TIMEOUT_OCR);
4878 break;
4879 case KILL_ADAPTER:
4880 megaraid_sas_kill_hba(instance);
4881 break;
4882 case IGNORE_TIMEOUT:
4883 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4884 __func__, __LINE__);
4885 break;
4886 }
4887 }
4888
4889 if (ret != DCMD_TIMEOUT)
4890 megasas_return_cmd(instance, cmd);
4891}
4892
adam radford21c9e162013-09-06 15:27:14 -07004893/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004894 * megasas_get_controller_info - Returns FW's controller structure
4895 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004896 *
4897 * Issues an internal command (DCMD) to get the FW's controller structure.
4898 * This information is mainly used to find out the maximum IO transfer per
4899 * command supported by the FW.
4900 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304901int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304902megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004903{
4904 int ret = 0;
4905 struct megasas_cmd *cmd;
4906 struct megasas_dcmd_frame *dcmd;
4907 struct megasas_ctrl_info *ci;
4908 dma_addr_t ci_h = 0;
4909
Shivasharan S9b3d0282017-10-19 02:48:56 -07004910 ci = instance->ctrl_info_buf;
4911 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304912
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004913 cmd = megasas_get_cmd(instance);
4914
4915 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004916 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004917 return -ENOMEM;
4918 }
4919
4920 dcmd = &cmd->frame->dcmd;
4921
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004922 memset(ci, 0, sizeof(*ci));
4923 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4924
4925 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304926 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004927 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004928 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004929 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004930 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304931 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4932 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304933 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004934
Shivasharan S107a60d2017-10-19 02:49:05 -07004935 megasas_set_dma_settings(instance, dcmd, ci_h,
4936 sizeof(struct megasas_ctrl_info));
4937
Shivasharan Se7d36b82017-10-19 02:48:50 -07004938 if ((instance->adapter_type != MFI_SERIES) &&
Shivasharan S54b28042018-01-05 05:27:47 -08004939 !instance->mask_interrupts) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304940 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Shivasharan S54b28042018-01-05 05:27:47 -08004941 } else {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304942 ret = megasas_issue_polled(instance, cmd);
Shivasharan S54b28042018-01-05 05:27:47 -08004943 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4944 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304945
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304946 switch (ret) {
4947 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304948 /* Save required controller information in
4949 * CPU endianness format.
4950 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004951 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004952 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
Shivasharan S9ad18a92017-10-19 02:48:57 -07004953 le32_to_cpus((u32 *)&ci->adapterOperations2);
4954 le32_to_cpus((u32 *)&ci->adapterOperations3);
4955 le16_to_cpus((u16 *)&ci->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304956
4957 /* Update the latest Ext VD info.
4958 * From Init path, store current firmware details.
4959 * From OCR path, detect any firmware properties changes.
4960 * in case of Firmware upgrade without system reboot.
4961 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304962 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304963 instance->use_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004964 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004965 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004966 ci->adapter_operations4.support_pd_map_target_id;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004967 instance->support_nvme_passthru =
4968 ci->adapter_operations4.support_nvme_passthru;
Shivasharan Se9495e22018-06-04 03:45:12 -07004969 instance->task_abort_tmo = ci->TaskAbortTO;
4970 instance->max_reset_tmo = ci->MaxResetTO;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304971
4972 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004973 instance->is_imr = (ci->memory_size ? 0 : 1);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004974
4975 instance->snapdump_wait_time =
4976 (ci->properties.on_off_properties2.enable_snap_dump ?
4977 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
4978
Shivasharan Sf6fe5732019-01-29 01:38:14 -08004979 instance->enable_fw_dev_list =
4980 ci->properties.on_off_properties2.enable_fw_dev_list;
4981
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304982 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304983 "controller type\t: %s(%dMB)\n",
4984 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07004985 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304986
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304987 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004988 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304989 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004990 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304991 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4992 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304993 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4994 instance->secure_jbod_support ? "Yes" : "No");
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004995 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
4996 instance->support_nvme_passthru ? "Yes" : "No");
Shivasharan Se9495e22018-06-04 03:45:12 -07004997 dev_info(&instance->pdev->dev,
4998 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
4999 instance->task_abort_tmo, instance->max_reset_tmo);
5000
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305001 break;
5002
5003 case DCMD_TIMEOUT:
5004 switch (dcmd_timeout_ocr_possible(instance)) {
5005 case INITIATE_OCR:
5006 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5007 megasas_reset_fusion(instance->host,
5008 MFI_IO_TIMEOUT_OCR);
5009 break;
5010 case KILL_ADAPTER:
5011 megaraid_sas_kill_hba(instance);
5012 break;
5013 case IGNORE_TIMEOUT:
5014 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5015 __func__, __LINE__);
5016 break;
5017 }
Shivasharan S2747e6b2018-06-04 03:45:09 -07005018 break;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305019 case DCMD_FAILED:
5020 megaraid_sas_kill_hba(instance);
5021 break;
5022
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305023 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005024
Shivasharan S2747e6b2018-06-04 03:45:09 -07005025 if (ret != DCMD_TIMEOUT)
5026 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305027
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005028 return ret;
5029}
5030
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305031/*
5032 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5033 * to firmware
5034 *
5035 * @instance: Adapter soft state
5036 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5037 MR_CRASH_BUF_TURN_OFF = 0
5038 MR_CRASH_BUF_TURN_ON = 1
5039 * @return 0 on success non-zero on failure.
5040 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5041 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5042 * that driver supports crash dump feature. This DCMD will be sent only if
5043 * crash dump feature is supported by the FW.
5044 *
5045 */
5046int megasas_set_crash_dump_params(struct megasas_instance *instance,
5047 u8 crash_buf_state)
5048{
5049 int ret = 0;
5050 struct megasas_cmd *cmd;
5051 struct megasas_dcmd_frame *dcmd;
5052
5053 cmd = megasas_get_cmd(instance);
5054
5055 if (!cmd) {
5056 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5057 return -ENOMEM;
5058 }
5059
5060
5061 dcmd = &cmd->frame->dcmd;
5062
5063 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5064 dcmd->mbox.b[0] = crash_buf_state;
5065 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305066 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305067 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005068 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305069 dcmd->timeout = 0;
5070 dcmd->pad_0 = 0;
5071 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5072 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305073
Shivasharan S107a60d2017-10-19 02:49:05 -07005074 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5075 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305076
Shivasharan Se7d36b82017-10-19 02:48:50 -07005077 if ((instance->adapter_type != MFI_SERIES) &&
5078 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305079 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305080 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305081 ret = megasas_issue_polled(instance, cmd);
5082
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305083 if (ret == DCMD_TIMEOUT) {
5084 switch (dcmd_timeout_ocr_possible(instance)) {
5085 case INITIATE_OCR:
5086 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5087 megasas_reset_fusion(instance->host,
5088 MFI_IO_TIMEOUT_OCR);
5089 break;
5090 case KILL_ADAPTER:
5091 megaraid_sas_kill_hba(instance);
5092 break;
5093 case IGNORE_TIMEOUT:
5094 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5095 __func__, __LINE__);
5096 break;
5097 }
5098 } else
5099 megasas_return_cmd(instance, cmd);
5100
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305101 return ret;
5102}
5103
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005104/**
bo yang31ea7082007-11-07 12:09:50 -05005105 * megasas_issue_init_mfi - Initializes the FW
5106 * @instance: Adapter soft state
5107 *
5108 * Issues the INIT MFI cmd
5109 */
5110static int
5111megasas_issue_init_mfi(struct megasas_instance *instance)
5112{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05305113 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05005114 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05005115 struct megasas_init_frame *init_frame;
5116 struct megasas_init_queue_info *initq_info;
5117 dma_addr_t init_frame_h;
5118 dma_addr_t initq_info_h;
5119
5120 /*
5121 * Prepare a init frame. Note the init frame points to queue info
5122 * structure. Each frame has SGL allocated after first 64 bytes. For
5123 * this frame - since we don't need any SGL - we use SGL's space as
5124 * queue info structure
5125 *
5126 * We will not get a NULL command below. We just created the pool.
5127 */
5128 cmd = megasas_get_cmd(instance);
5129
5130 init_frame = (struct megasas_init_frame *)cmd->frame;
5131 initq_info = (struct megasas_init_queue_info *)
5132 ((unsigned long)init_frame + 64);
5133
5134 init_frame_h = cmd->frame_phys_addr;
5135 initq_info_h = init_frame_h + 64;
5136
5137 context = init_frame->context;
5138 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5139 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5140 init_frame->context = context;
5141
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305142 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5143 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05005144
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305145 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5146 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05005147
5148 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05305149 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305150 init_frame->queue_info_new_phys_addr_lo =
5151 cpu_to_le32(lower_32_bits(initq_info_h));
5152 init_frame->queue_info_new_phys_addr_hi =
5153 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05005154
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305155 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05005156
5157 /*
5158 * disable the intr before firing the init frame to FW
5159 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305160 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05005161
5162 /*
5163 * Issue the init frame in polled mode
5164 */
5165
5166 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005167 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05005168 megasas_return_cmd(instance, cmd);
5169 goto fail_fw_init;
5170 }
5171
5172 megasas_return_cmd(instance, cmd);
5173
5174 return 0;
5175
5176fail_fw_init:
5177 return -EINVAL;
5178}
5179
adam radfordcd50ba82010-12-21 10:23:23 -08005180static u32
5181megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005182{
5183 u32 context_sz;
5184 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005185
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005186 /*
5187 * Get various operational parameters from status register
5188 */
Shivasharan Sde516372018-12-17 00:47:39 -08005189 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07005190 /*
5191 * Reduce the max supported cmds by 1. This is to ensure that the
5192 * reply_q_sz (1 more than the max cmd that driver may send)
5193 * does not exceed max cmds that the FW can support
5194 */
5195 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08005196 instance->max_mfi_cmds = instance->max_fw_cmds;
Shivasharan Sde516372018-12-17 00:47:39 -08005197 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08005198 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005199 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305200 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5201 * are reserved for IOCTL + driver's internal DCMDs.
5202 */
5203 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5204 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5205 instance->max_scsi_cmds = (instance->max_fw_cmds -
5206 MEGASAS_SKINNY_INT_CMDS);
5207 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5208 } else {
5209 instance->max_scsi_cmds = (instance->max_fw_cmds -
5210 MEGASAS_INT_CMDS);
5211 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5212 }
5213
Sumit Saxena308ec452016-01-28 21:04:30 +05305214 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05305215 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005216 * Create a pool of commands
5217 */
5218 if (megasas_alloc_cmds(instance))
5219 goto fail_alloc_cmds;
5220
5221 /*
5222 * Allocate memory for reply queue. Length of reply queue should
5223 * be _one_ more than the maximum commands handled by the firmware.
5224 *
5225 * Note: When FW completes commands, it places corresponding contex
5226 * values in this circular reply queue. This circular queue is a fairly
5227 * typical producer-consumer queue. FW is the producer (of completed
5228 * commands) and the driver is the consumer.
5229 */
5230 context_sz = sizeof(u32);
5231 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5232
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005233 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5234 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005235
5236 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005237 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005238 goto fail_reply_queue;
5239 }
5240
bo yang31ea7082007-11-07 12:09:50 -05005241 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005242 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005243
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305244 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305245 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5246 "Fail from %s %d\n", instance->unique_id,
5247 __func__, __LINE__);
5248 goto fail_fw_init;
5249 }
5250
bo yang39a98552010-09-22 22:36:29 -04005251 instance->fw_support_ieee = 0;
5252 instance->fw_support_ieee =
Shivasharan Sde516372018-12-17 00:47:39 -08005253 (instance->instancet->read_fw_status_reg(instance) &
bo yang39a98552010-09-22 22:36:29 -04005254 0x04000000);
5255
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005256 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04005257 instance->fw_support_ieee);
5258
5259 if (instance->fw_support_ieee)
5260 instance->flag_ieee = 1;
5261
adam radfordcd50ba82010-12-21 10:23:23 -08005262 return 0;
5263
5264fail_fw_init:
5265
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005266 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
adam radfordcd50ba82010-12-21 10:23:23 -08005267 instance->reply_queue, instance->reply_queue_h);
5268fail_reply_queue:
5269 megasas_free_cmds(instance);
5270
5271fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08005272 return 1;
5273}
5274
Shivasharan S62a04f82019-05-07 10:05:35 -07005275static
5276void megasas_setup_irq_poll(struct megasas_instance *instance)
5277{
5278 struct megasas_irq_context *irq_ctx;
5279 u32 count, i;
5280
5281 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5282
5283 /* Initialize IRQ poll */
5284 for (i = 0; i < count; i++) {
5285 irq_ctx = &instance->irq_context[i];
5286 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5287 irq_ctx->irq_poll_scheduled = false;
5288 irq_poll_init(&irq_ctx->irqpoll,
5289 instance->threshold_reply_count,
5290 megasas_irqpoll);
5291 }
5292}
5293
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305294/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01005295 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305296 * @instance: Adapter soft state
5297 *
5298 * Do not enable interrupt, only setup ISRs.
5299 *
5300 * Return 0 on success.
5301 */
5302static int
5303megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5304{
5305 struct pci_dev *pdev;
5306
5307 pdev = instance->pdev;
5308 instance->irq_context[0].instance = instance;
5309 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005310 if (request_irq(pci_irq_vector(pdev, 0),
5311 instance->instancet->service_isr, IRQF_SHARED,
5312 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305313 dev_err(&instance->pdev->dev,
5314 "Failed to register IRQ from %s %d\n",
5315 __func__, __LINE__);
5316 return -1;
5317 }
5318 return 0;
5319}
5320
5321/**
5322 * megasas_setup_irqs_msix - register MSI-x interrupts.
5323 * @instance: Adapter soft state
5324 * @is_probe: Driver probe check
5325 *
5326 * Do not enable interrupt, only setup ISRs.
5327 *
5328 * Return 0 on success.
5329 */
5330static int
5331megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5332{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005333 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305334 struct pci_dev *pdev;
5335
5336 pdev = instance->pdev;
5337
5338 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305339 for (i = 0; i < instance->msix_vectors; i++) {
5340 instance->irq_context[i].instance = instance;
5341 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005342 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305343 instance->instancet->service_isr, 0, "megasas",
5344 &instance->irq_context[i])) {
5345 dev_err(&instance->pdev->dev,
5346 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005347 for (j = 0; j < i; j++)
5348 free_irq(pci_irq_vector(pdev, j),
5349 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305350 /* Retry irq register for IO_APIC*/
5351 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005352 instance->msix_load_balance = false;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005353 if (is_probe) {
5354 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305355 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005356 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305357 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005358 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305359 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305360 }
Shivasharan S1d15d902019-05-07 10:05:36 -07005361
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305362 return 0;
5363}
5364
5365/*
5366 * megasas_destroy_irqs- unregister interrupts.
5367 * @instance: Adapter soft state
5368 * return: void
5369 */
5370static void
5371megasas_destroy_irqs(struct megasas_instance *instance) {
5372
5373 int i;
Shivasharan S62a04f82019-05-07 10:05:35 -07005374 int count;
5375 struct megasas_irq_context *irq_ctx;
5376
5377 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5378 if (instance->adapter_type != MFI_SERIES) {
5379 for (i = 0; i < count; i++) {
5380 irq_ctx = &instance->irq_context[i];
5381 irq_poll_disable(&irq_ctx->irqpoll);
5382 }
5383 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305384
5385 if (instance->msix_vectors)
5386 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005387 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305388 &instance->irq_context[i]);
5389 }
5390 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005391 free_irq(pci_irq_vector(instance->pdev, 0),
5392 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305393}
5394
adam radfordcd50ba82010-12-21 10:23:23 -08005395/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305396 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5397 * @instance: Adapter soft state
5398 * @is_probe: Driver probe check
5399 *
5400 * Return 0 on success.
5401 */
5402void
5403megasas_setup_jbod_map(struct megasas_instance *instance)
5404{
5405 int i;
5406 struct fusion_context *fusion = instance->ctrl_context;
5407 u32 pd_seq_map_sz;
5408
5409 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5410 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5411
5412 if (reset_devices || !fusion ||
Shivasharan S9ad18a92017-10-19 02:48:57 -07005413 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305414 dev_info(&instance->pdev->dev,
5415 "Jbod map is not supported %s %d\n",
5416 __func__, __LINE__);
5417 instance->use_seqnum_jbod_fp = false;
5418 return;
5419 }
5420
5421 if (fusion->pd_seq_sync[0])
5422 goto skip_alloc;
5423
5424 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5425 fusion->pd_seq_sync[i] = dma_alloc_coherent
5426 (&instance->pdev->dev, pd_seq_map_sz,
5427 &fusion->pd_seq_phys[i], GFP_KERNEL);
5428 if (!fusion->pd_seq_sync[i]) {
5429 dev_err(&instance->pdev->dev,
5430 "Failed to allocate memory from %s %d\n",
5431 __func__, __LINE__);
5432 if (i == 1) {
5433 dma_free_coherent(&instance->pdev->dev,
5434 pd_seq_map_sz, fusion->pd_seq_sync[0],
5435 fusion->pd_seq_phys[0]);
5436 fusion->pd_seq_sync[0] = NULL;
5437 }
5438 instance->use_seqnum_jbod_fp = false;
5439 return;
5440 }
5441 }
5442
5443skip_alloc:
5444 if (!megasas_sync_pd_seq_num(instance, false) &&
5445 !megasas_sync_pd_seq_num(instance, true))
5446 instance->use_seqnum_jbod_fp = true;
5447 else
5448 instance->use_seqnum_jbod_fp = false;
5449}
5450
Ming Leiadbe5522018-03-13 17:42:40 +08005451static void megasas_setup_reply_map(struct megasas_instance *instance)
5452{
5453 const struct cpumask *mask;
5454 unsigned int queue, cpu;
5455
5456 for (queue = 0; queue < instance->msix_vectors; queue++) {
5457 mask = pci_irq_get_affinity(instance->pdev, queue);
5458 if (!mask)
5459 goto fallback;
5460
5461 for_each_cpu(cpu, mask)
5462 instance->reply_map[cpu] = queue;
5463 }
5464 return;
5465
5466fallback:
5467 for_each_possible_cpu(cpu)
5468 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5469}
5470
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305471/**
Shivasharan Sdaa06812019-01-29 01:38:12 -08005472 * megasas_get_device_list - Get the PD and LD device list from FW.
5473 * @instance: Adapter soft state
5474 * @return: Success or failure
5475 *
5476 * Issue DCMDs to Firmware to get the PD and LD list.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005477 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5478 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan Sdaa06812019-01-29 01:38:12 -08005479 */
5480static
5481int megasas_get_device_list(struct megasas_instance *instance)
5482{
5483 memset(instance->pd_list, 0,
5484 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5485 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5486
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005487 if (instance->enable_fw_dev_list) {
5488 if (megasas_host_device_list_query(instance, true))
5489 return FAILED;
5490 } else {
5491 if (megasas_get_pd_list(instance) < 0) {
5492 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5493 return FAILED;
5494 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005495
Shivasharan Sf6fe5732019-01-29 01:38:14 -08005496 if (megasas_ld_list_query(instance,
5497 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5498 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5499 return FAILED;
5500 }
Shivasharan Sdaa06812019-01-29 01:38:12 -08005501 }
5502
5503 return SUCCESS;
5504}
5505/**
adam radfordcd50ba82010-12-21 10:23:23 -08005506 * megasas_init_fw - Initializes the FW
5507 * @instance: Adapter soft state
5508 *
5509 * This is the main function for initializing firmware
5510 */
5511
5512static int megasas_init_fw(struct megasas_instance *instance)
5513{
5514 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005515 u32 max_sectors_2, tmp_sectors, msix_enable;
Shivasharan S81b76452018-10-16 23:37:51 -07005516 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005517 resource_size_t base_addr;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305518 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005519 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005520 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005521 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305522 struct fusion_context *fusion;
5523
5524 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005525
5526 /* Find first memory bar */
5527 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005528 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005529 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005530 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005531 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005532 return -EBUSY;
5533 }
5534
Ben Collins11f8a7b2013-09-13 12:46:44 -04005535 base_addr = pci_resource_start(instance->pdev, instance->bar);
5536 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005537
5538 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005539 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005540 goto fail_ioremap;
5541 }
5542
Shivasharan Se7d36b82017-10-19 02:48:50 -07005543 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005544 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005545 else {
5546 switch (instance->pdev->device) {
5547 case PCI_DEVICE_ID_LSI_SAS1078R:
5548 case PCI_DEVICE_ID_LSI_SAS1078DE:
5549 instance->instancet = &megasas_instance_template_ppc;
5550 break;
5551 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5552 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5553 instance->instancet = &megasas_instance_template_gen2;
5554 break;
5555 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5556 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5557 instance->instancet = &megasas_instance_template_skinny;
5558 break;
5559 case PCI_DEVICE_ID_LSI_SAS1064R:
5560 case PCI_DEVICE_ID_DELL_PERC5:
5561 default:
5562 instance->instancet = &megasas_instance_template_xscale;
5563 instance->pd_list_not_supported = 1;
5564 break;
5565 }
adam radfordcd50ba82010-12-21 10:23:23 -08005566 }
5567
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305568 if (megasas_transition_to_ready(instance, 0)) {
Shivasharan S78409d42019-05-07 10:05:34 -07005569 dev_info(&instance->pdev->dev,
5570 "Failed to transition controller to ready from %s!\n",
5571 __func__);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005572 if (instance->adapter_type != MFI_SERIES) {
Shivasharan Sde93b402018-10-16 23:37:42 -07005573 status_reg = instance->instancet->read_fw_status_reg(
Shivasharan Sde516372018-12-17 00:47:39 -08005574 instance);
Shivasharan Sf10fb852019-05-07 10:05:32 -07005575 if (status_reg & MFI_RESET_ADAPTER) {
Shivasharan S78409d42019-05-07 10:05:34 -07005576 if (megasas_adp_reset_wait_for_ready
5577 (instance, true, 0) == FAILED)
Shivasharan Sf10fb852019-05-07 10:05:32 -07005578 goto fail_ready_state;
5579 } else {
5580 goto fail_ready_state;
5581 }
5582 } else {
Shivasharan Sde93b402018-10-16 23:37:42 -07005583 atomic_set(&instance->fw_reset_no_pci_access, 1);
5584 instance->instancet->adp_reset
5585 (instance, instance->reg_set);
5586 atomic_set(&instance->fw_reset_no_pci_access, 0);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305587
Shivasharan Sde93b402018-10-16 23:37:42 -07005588 /*waiting for about 30 second before retry*/
5589 ssleep(30);
5590
5591 if (megasas_transition_to_ready(instance, 0))
5592 goto fail_ready_state;
Shivasharan Sde93b402018-10-16 23:37:42 -07005593 }
Shivasharan S78409d42019-05-07 10:05:34 -07005594
5595 dev_info(&instance->pdev->dev,
5596 "FW restarted successfully from %s!\n",
5597 __func__);
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305598 }
adam radfordcd50ba82010-12-21 10:23:23 -08005599
Shivasharan Se5d65b42017-10-19 02:48:59 -07005600 megasas_init_ctrl_params(instance);
5601
Shivasharan S107a60d2017-10-19 02:49:05 -07005602 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005603 goto fail_ready_state;
5604
5605 if (megasas_alloc_ctrl_mem(instance))
5606 goto fail_alloc_dma_buf;
5607
5608 if (megasas_alloc_ctrl_dma_buffers(instance))
5609 goto fail_alloc_dma_buf;
5610
5611 fusion = instance->ctrl_context;
5612
Shivasharan S630d42b2018-12-17 00:47:37 -08005613 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005614 scratch_pad_2 =
Shivasharan S272652f2018-12-17 00:47:40 -08005615 megasas_readl(instance,
5616 &instance->reg_set->outbound_scratch_pad_2);
Shivasharan S81b76452018-10-16 23:37:51 -07005617 instance->max_raid_mapsize = ((scratch_pad_2 >>
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005618 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5619 MR_MAX_RAID_MAP_SIZE_MASK);
5620 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305621
adam radford3f1abce2011-05-11 18:33:47 -07005622 /* Check if MSI-X is supported while in ready state */
Shivasharan Sde516372018-12-17 00:47:39 -08005623 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
adam radford3f1abce2011-05-11 18:33:47 -07005624 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005625 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005626 int irq_flags = PCI_IRQ_MSIX;
5627
Shivasharan S272652f2018-12-17 00:47:40 -08005628 scratch_pad_1 = megasas_readl
5629 (instance, &instance->reg_set->outbound_scratch_pad_1);
adam radfordc8e858f2011-10-08 18:15:13 -07005630 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305631 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005632 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5633 /* Thunderbolt Series*/
Shivasharan S81b76452018-10-16 23:37:51 -07005634 instance->msix_vectors = (scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305635 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5636 fw_msix_count = instance->msix_vectors;
Shivasharan Se29c3222018-10-16 23:37:46 -07005637 } else {
Shivasharan S81b76452018-10-16 23:37:51 -07005638 instance->msix_vectors = ((scratch_pad_1
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305639 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5640 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Shivasharan Se29c3222018-10-16 23:37:46 -07005641
5642 /*
5643 * For Invader series, > 8 MSI-x vectors
5644 * supported by FW/HW implies combined
5645 * reply queue mode is enabled.
5646 * For Ventura series, > 16 MSI-x vectors
5647 * supported by FW/HW implies combined
5648 * reply queue mode is enabled.
5649 */
5650 switch (instance->adapter_type) {
5651 case INVADER_SERIES:
5652 if (instance->msix_vectors > 8)
5653 instance->msix_combined = true;
5654 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08005655 case AERO_SERIES:
Shivasharan Se29c3222018-10-16 23:37:46 -07005656 case VENTURA_SERIES:
5657 if (instance->msix_vectors > 16)
5658 instance->msix_combined = true;
5659 break;
5660 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005661
Sumit Saxena179ac142016-01-28 21:04:28 +05305662 if (rdpq_enable)
Shivasharan S81b76452018-10-16 23:37:51 -07005663 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
Sumit Saxena179ac142016-01-28 21:04:28 +05305664 1 : 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005665
5666 if (!instance->msix_combined) {
5667 instance->msix_load_balance = true;
5668 instance->smp_affinity_enable = false;
5669 }
5670
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305671 fw_msix_count = instance->msix_vectors;
5672 /* Save 1-15 reply post index address to local memory
5673 * Index 0 is already saved from reg offset
5674 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5675 */
5676 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5677 instance->reply_post_host_index_addr[loop] =
5678 (u32 __iomem *)
5679 ((u8 __iomem *)instance->reg_set +
5680 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5681 + (loop * 0x10));
5682 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305683 }
5684 if (msix_vectors)
5685 instance->msix_vectors = min(msix_vectors,
5686 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305687 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005688 instance->msix_vectors = 1;
Shivasharan S1d15d902019-05-07 10:05:36 -07005689
adam radfordc8e858f2011-10-08 18:15:13 -07005690 /* Don't bother allocating more MSI-X vectors than cpus */
5691 instance->msix_vectors = min(instance->msix_vectors,
5692 (unsigned int)num_online_cpus());
Shivasharan S1d15d902019-05-07 10:05:36 -07005693 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005694 irq_flags |= PCI_IRQ_AFFINITY;
5695 i = pci_alloc_irq_vectors(instance->pdev, 1,
5696 instance->msix_vectors, irq_flags);
Shivasharan S1d15d902019-05-07 10:05:36 -07005697 if (i > 0) {
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005698 instance->msix_vectors = i;
Shivasharan S1d15d902019-05-07 10:05:36 -07005699 } else {
adam radfordc8e858f2011-10-08 18:15:13 -07005700 instance->msix_vectors = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07005701 instance->msix_load_balance = false;
5702 }
adam radfordc8e858f2011-10-08 18:15:13 -07005703 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005704 /*
5705 * MSI-X host index 0 is common for all adapter.
5706 * It is used for all MPT based Adapters.
5707 */
5708 if (instance->msix_combined) {
5709 instance->reply_post_host_index_addr[0] =
5710 (u32 *)((u8 *)instance->reg_set +
5711 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5712 } else {
5713 instance->reply_post_host_index_addr[0] =
5714 (u32 *)((u8 *)instance->reg_set +
5715 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5716 }
5717
Shivasharan S64ff64b2017-03-10 03:22:12 -08005718 if (!instance->msix_vectors) {
5719 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5720 if (i < 0)
Shivasharan S8a25fa12018-10-16 23:37:44 -07005721 goto fail_init_adapter;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005722 }
adam radford3f1abce2011-05-11 18:33:47 -07005723
Ming Leiadbe5522018-03-13 17:42:40 +08005724 megasas_setup_reply_map(instance);
5725
Tomas Henzl258c3af2015-06-02 16:09:46 +05305726 dev_info(&instance->pdev->dev,
5727 "firmware supports msix\t: (%d)", fw_msix_count);
5728 dev_info(&instance->pdev->dev,
5729 "current msix/online cpus\t: (%d/%d)\n",
5730 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305731 dev_info(&instance->pdev->dev,
5732 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305733
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305734 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5735 (unsigned long)instance);
5736
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305737 /*
5738 * Below are default value for legacy Firmware.
5739 * non-fusion based controllers
5740 */
5741 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5742 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005743 /* Get operational params, sge flags, send init cmd to controller */
5744 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005745 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005746
Shivasharan S630d42b2018-12-17 00:47:37 -08005747 if (instance->adapter_type >= VENTURA_SERIES) {
Shivasharan S81b76452018-10-16 23:37:51 -07005748 scratch_pad_3 =
Shivasharan S272652f2018-12-17 00:47:40 -08005749 megasas_readl(instance,
5750 &instance->reg_set->outbound_scratch_pad_3);
Shivasharan S81b76452018-10-16 23:37:51 -07005751 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
Shivasharan S15dd0382017-02-10 00:59:10 -08005752 MR_DEFAULT_NVME_PAGE_SHIFT)
5753 instance->nvme_page_size =
Shivasharan S81b76452018-10-16 23:37:51 -07005754 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
Shivasharan S15dd0382017-02-10 00:59:10 -08005755
5756 dev_info(&instance->pdev->dev,
5757 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5758 }
5759
Tomas Henzl18103ef2016-11-01 17:32:02 +01005760 if (instance->msix_vectors ?
5761 megasas_setup_irqs_msix(instance, 1) :
5762 megasas_setup_irqs_ioapic(instance))
5763 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305764
Shivasharan S62a04f82019-05-07 10:05:35 -07005765 if (instance->adapter_type != MFI_SERIES)
5766 megasas_setup_irq_poll(instance);
5767
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305768 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005769
Andy Lutomirski13f30772016-05-03 10:24:31 -07005770 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005771
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305772 megasas_setup_jbod_map(instance);
5773
Shivasharan Sdaa06812019-01-29 01:38:12 -08005774 if (megasas_get_device_list(instance) != SUCCESS) {
5775 dev_err(&instance->pdev->dev,
5776 "%s: megasas_get_device_list failed\n",
5777 __func__);
Shivasharan S72bff2d2017-02-10 00:59:33 -08005778 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005779 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005780
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005781 /* stream detection initialization */
Shivasharan S630d42b2018-12-17 00:47:37 -08005782 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005783 fusion->stream_detect_by_ld =
Kees Cook6396bb22018-06-12 14:03:40 -07005784 kcalloc(MAX_LOGICAL_DRIVES_EXT,
5785 sizeof(struct LD_STREAM_DETECT *),
5786 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005787 if (!fusion->stream_detect_by_ld) {
5788 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005789 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005790 goto fail_get_ld_pd_list;
5791 }
5792 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5793 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08005794 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005795 GFP_KERNEL);
5796 if (!fusion->stream_detect_by_ld[i]) {
5797 dev_err(&instance->pdev->dev,
5798 "unable to allocate stream detect by LD\n ");
5799 for (j = 0; j < i; ++j)
5800 kfree(fusion->stream_detect_by_ld[j]);
5801 kfree(fusion->stream_detect_by_ld);
5802 fusion->stream_detect_by_ld = NULL;
5803 goto fail_get_ld_pd_list;
5804 }
5805 fusion->stream_detect_by_ld[i]->mru_bit_map
5806 = MR_STREAM_BITMAP;
5807 }
5808 }
5809
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005810 /*
5811 * Compute the max allowed sectors per IO: The controller info has two
5812 * limits on max sectors. Driver should use the minimum of these two.
5813 *
5814 * 1 << stripe_sz_ops.min = max sectors per strip
5815 *
5816 * Note that older firmwares ( < FW ver 30) didn't report information
5817 * to calculate max_sectors_1. So the number ended up as zero always.
5818 */
bo yang14faea92007-11-09 04:14:00 -05005819 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07005820 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005821
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305822 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5823 le16_to_cpu(ctrl_info->max_strips_per_io);
5824 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005825
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005826 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305827
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305828 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5829 instance->passive = ctrl_info->cluster.passive;
5830 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305831 instance->UnevenSpanSupport =
5832 ctrl_info->adapterOperations2.supportUnevenSpans;
5833 if (instance->UnevenSpanSupport) {
5834 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S5f19f7c2018-01-05 05:27:44 -08005835 if (MR_ValidateMapInfo(instance, instance->map_id))
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305836 fusion->fast_path_io = 1;
5837 else
5838 fusion->fast_path_io = 0;
5839
5840 }
5841 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305842 instance->requestorId = ctrl_info->iov.requestorId;
5843 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5844 if (!ctrl_info->adapterOperations2.activePassive)
5845 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305846
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305847 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5848 instance->PlasmaFW111 ? "1.11" : "new");
5849
5850 if (instance->PlasmaFW111) {
5851 iovPtr = (struct IOV_111 *)
5852 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5853 instance->requestorId = iovPtr->requestorId;
5854 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305855 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305856 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5857 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305858 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305859
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305860 instance->crash_dump_fw_support =
5861 ctrl_info->adapterOperations3.supportCrashDump;
5862 instance->crash_dump_drv_support =
5863 (instance->crash_dump_fw_support &&
5864 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305865 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305866 megasas_set_crash_dump_params(instance,
5867 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005868
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305869 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305870 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005871 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305872 CRASH_DMA_BUF_SIZE,
5873 instance->crash_dump_buf,
5874 instance->crash_dump_h);
5875 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005876 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305877
Shivasharan Sf0c21df2018-10-16 23:37:40 -07005878 if (instance->snapdump_wait_time) {
5879 megasas_get_snapdump_properties(instance);
5880 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
5881 instance->snapdump_wait_time);
5882 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305883
5884 dev_info(&instance->pdev->dev,
5885 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5886 le16_to_cpu(ctrl_info->pci.vendor_id),
5887 le16_to_cpu(ctrl_info->pci.device_id),
5888 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5889 le16_to_cpu(ctrl_info->pci.sub_device_id));
5890 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5891 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305892 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5893 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305894 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5895 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305896
bo yang14faea92007-11-09 04:14:00 -05005897 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305898 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005899 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5900 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005901
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305902 /* Check for valid throttlequeuedepth module parameter */
5903 if (throttlequeuedepth &&
5904 throttlequeuedepth <= instance->max_scsi_cmds)
5905 instance->throttlequeuedepth = throttlequeuedepth;
5906 else
5907 instance->throttlequeuedepth =
5908 MEGASAS_THROTTLE_QUEUE_DEPTH;
5909
Shivasharan Se636a7a2017-08-23 04:46:56 -07005910 if ((resetwaittime < 1) ||
5911 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305912 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5913
5914 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5915 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005916
adam radford229fe472014-03-10 02:51:56 -07005917 /* Launch SR-IOV heartbeat timer */
5918 if (instance->requestorId) {
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005919 if (!megasas_sriov_start_heartbeat(instance, 1)) {
Kees Cookc251a7b2017-10-22 15:30:04 -07005920 megasas_start_timer(instance);
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005921 } else {
adam radford229fe472014-03-10 02:51:56 -07005922 instance->skip_heartbeat_timer_del = 1;
Shivasharan S2e47e4e2018-10-16 23:37:48 -07005923 goto fail_get_ld_pd_list;
5924 }
adam radford229fe472014-03-10 02:51:56 -07005925 }
5926
Shivasharan S3f6194a2018-10-16 23:37:39 -07005927 /*
5928 * Create and start watchdog thread which will monitor
5929 * controller state every 1 sec and trigger OCR when
5930 * it enters fault state
5931 */
5932 if (instance->adapter_type != MFI_SERIES)
5933 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
5934 goto fail_start_watchdog;
5935
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005936 return 0;
5937
Shivasharan S3f6194a2018-10-16 23:37:39 -07005938fail_start_watchdog:
5939 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5940 del_timer_sync(&instance->sriov_heartbeat_timer);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005941fail_get_ld_pd_list:
5942 instance->instancet->disable_intr(instance);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005943 megasas_destroy_irqs(instance);
Shivasharan S8a25fa12018-10-16 23:37:44 -07005944fail_init_adapter:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305945 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005946 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305947 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07005948fail_alloc_dma_buf:
5949 megasas_free_ctrl_dma_buffers(instance);
5950 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005951fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005952 iounmap(instance->reg_set);
5953
Shivasharan S72bff2d2017-02-10 00:59:33 -08005954fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005955 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005956
Shivasharan S72bff2d2017-02-10 00:59:33 -08005957 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5958 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005959 return -EINVAL;
5960}
5961
5962/**
5963 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005964 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005965 */
5966static void megasas_release_mfi(struct megasas_instance *instance)
5967{
adam radford9c915a82010-12-21 13:34:31 -08005968 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005969
adam radford9c915a82010-12-21 13:34:31 -08005970 if (instance->reply_queue)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02005971 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005972 instance->reply_queue, instance->reply_queue_h);
5973
5974 megasas_free_cmds(instance);
5975
5976 iounmap(instance->reg_set);
5977
Yinghai Lue7f85162016-08-05 23:37:34 -07005978 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005979}
5980
5981/**
5982 * megasas_get_seq_num - Gets latest event sequence numbers
5983 * @instance: Adapter soft state
5984 * @eli: FW event log sequence numbers information
5985 *
5986 * FW maintains a log of all events in a non-volatile area. Upper layers would
5987 * usually find out the latest sequence number of the events, the seq number at
5988 * the boot etc. They would "read" all the events below the latest seq number
5989 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5990 * number), they would subsribe to AEN (asynchronous event notification) and
5991 * wait for the events to happen.
5992 */
5993static int
5994megasas_get_seq_num(struct megasas_instance *instance,
5995 struct megasas_evt_log_info *eli)
5996{
5997 struct megasas_cmd *cmd;
5998 struct megasas_dcmd_frame *dcmd;
5999 struct megasas_evt_log_info *el_info;
6000 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08006001 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006002
6003 cmd = megasas_get_cmd(instance);
6004
6005 if (!cmd) {
6006 return -ENOMEM;
6007 }
6008
6009 dcmd = &cmd->frame->dcmd;
Luis Chamberlain750afb02019-01-04 09:23:09 +01006010 el_info = dma_alloc_coherent(&instance->pdev->dev,
6011 sizeof(struct megasas_evt_log_info),
6012 &el_info_h, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006013 if (!el_info) {
6014 megasas_return_cmd(instance, cmd);
6015 return -ENOMEM;
6016 }
6017
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006018 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6019
6020 dcmd->cmd = MFI_CMD_DCMD;
6021 dcmd->cmd_status = 0x0;
6022 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006023 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006024 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006025 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306026 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6027 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07006028
6029 megasas_set_dma_settings(instance, dcmd, el_info_h,
6030 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006031
Shivasharan Sb051cc62018-01-05 05:27:38 -08006032 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6033 if (ret != DCMD_SUCCESS) {
6034 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6035 __func__, __LINE__);
6036 goto dcmd_failed;
6037 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006038
Shivasharan Sb051cc62018-01-05 05:27:38 -08006039 /*
6040 * Copy the data back into callers buffer
6041 */
6042 eli->newest_seq_num = el_info->newest_seq_num;
6043 eli->oldest_seq_num = el_info->oldest_seq_num;
6044 eli->clear_seq_num = el_info->clear_seq_num;
6045 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6046 eli->boot_seq_num = el_info->boot_seq_num;
6047
6048dcmd_failed:
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006049 dma_free_coherent(&instance->pdev->dev,
6050 sizeof(struct megasas_evt_log_info),
6051 el_info, el_info_h);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006052
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306053 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006054
Shivasharan Sb051cc62018-01-05 05:27:38 -08006055 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006056}
6057
6058/**
6059 * megasas_register_aen - Registers for asynchronous event notification
6060 * @instance: Adapter soft state
6061 * @seq_num: The starting sequence number
6062 * @class_locale: Class of the event
6063 *
6064 * This function subscribes for AEN for events beyond the @seq_num. It requests
6065 * to be notified if and only if the event is of type @class_locale
6066 */
6067static int
6068megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6069 u32 class_locale_word)
6070{
6071 int ret_val;
6072 struct megasas_cmd *cmd;
6073 struct megasas_dcmd_frame *dcmd;
6074 union megasas_evt_class_locale curr_aen;
6075 union megasas_evt_class_locale prev_aen;
6076
6077 /*
6078 * If there an AEN pending already (aen_cmd), check if the
6079 * class_locale of that pending AEN is inclusive of the new
6080 * AEN request we currently have. If it is, then we don't have
6081 * to do anything. In other words, whichever events the current
6082 * AEN request is subscribing to, have already been subscribed
6083 * to.
6084 *
6085 * If the old_cmd is _not_ inclusive, then we have to abort
6086 * that command, form a class_locale that is superset of both
6087 * old and current and re-issue to the FW
6088 */
6089
6090 curr_aen.word = class_locale_word;
6091
6092 if (instance->aen_cmd) {
6093
Christoph Hellwiga9555532015-04-23 16:34:24 +05306094 prev_aen.word =
6095 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006096
Shivasharan S91b3d9f2017-08-23 04:47:01 -07006097 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6098 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6099 dev_info(&instance->pdev->dev,
6100 "%s %d out of range class %d send by application\n",
6101 __func__, __LINE__, curr_aen.members.class);
6102 return 0;
6103 }
6104
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006105 /*
6106 * A class whose enum value is smaller is inclusive of all
6107 * higher values. If a PROGRESS (= -1) was previously
6108 * registered, then a new registration requests for higher
6109 * classes need not be sent to FW. They are automatically
6110 * included.
6111 *
6112 * Locale numbers don't have such hierarchy. They are bitmap
6113 * values
6114 */
6115 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306116 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006117 curr_aen.members.locale)) {
6118 /*
6119 * Previously issued event registration includes
6120 * current request. Nothing to do.
6121 */
6122 return 0;
6123 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05306124 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006125
6126 if (prev_aen.members.class < curr_aen.members.class)
6127 curr_aen.members.class = prev_aen.members.class;
6128
6129 instance->aen_cmd->abort_aen = 1;
6130 ret_val = megasas_issue_blocked_abort_cmd(instance,
6131 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306132 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006133
6134 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006135 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006136 "previous AEN command\n");
6137 return ret_val;
6138 }
6139 }
6140 }
6141
6142 cmd = megasas_get_cmd(instance);
6143
6144 if (!cmd)
6145 return -ENOMEM;
6146
6147 dcmd = &cmd->frame->dcmd;
6148
6149 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6150
6151 /*
6152 * Prepare DCMD for aen registration
6153 */
6154 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6155
6156 dcmd->cmd = MFI_CMD_DCMD;
6157 dcmd->cmd_status = 0x0;
6158 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006159 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006160 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006161 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306162 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6163 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6164 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04006165 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306166 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07006167
6168 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6169 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006170
Yang, Bof4c9a132009-10-06 14:43:28 -06006171 if (instance->aen_cmd != NULL) {
6172 megasas_return_cmd(instance, cmd);
6173 return 0;
6174 }
6175
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006176 /*
6177 * Store reference to the cmd used to register for AEN. When an
6178 * application wants us to register for AEN, we have to abort this
6179 * cmd and re-register with a new EVENT LOCALE supplied by that app
6180 */
6181 instance->aen_cmd = cmd;
6182
6183 /*
6184 * Issue the aen registration frame
6185 */
adam radford9c915a82010-12-21 13:34:31 -08006186 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006187
6188 return 0;
6189}
6190
Shivasharan S96188a82017-02-10 00:59:11 -08006191/* megasas_get_target_prop - Send DCMD with below details to firmware.
6192 *
6193 * This DCMD will fetch few properties of LD/system PD defined
6194 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6195 *
6196 * DCMD send by drivers whenever new target is added to the OS.
6197 *
6198 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6199 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6200 * 0 = system PD, 1 = LD.
6201 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6202 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6203 *
6204 * @instance: Adapter soft state
6205 * @sdev: OS provided scsi device
6206 *
6207 * Returns 0 on success non-zero on failure.
6208 */
Shivasharan Se9495e22018-06-04 03:45:12 -07006209int
Shivasharan S96188a82017-02-10 00:59:11 -08006210megasas_get_target_prop(struct megasas_instance *instance,
6211 struct scsi_device *sdev)
6212{
6213 int ret;
6214 struct megasas_cmd *cmd;
6215 struct megasas_dcmd_frame *dcmd;
6216 u16 targetId = (sdev->channel % 2) + sdev->id;
6217
6218 cmd = megasas_get_cmd(instance);
6219
6220 if (!cmd) {
6221 dev_err(&instance->pdev->dev,
6222 "Failed to get cmd %s\n", __func__);
6223 return -ENOMEM;
6224 }
6225
6226 dcmd = &cmd->frame->dcmd;
6227
6228 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6229 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6230 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6231
6232 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6233 dcmd->cmd = MFI_CMD_DCMD;
6234 dcmd->cmd_status = 0xFF;
6235 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07006236 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08006237 dcmd->timeout = 0;
6238 dcmd->pad_0 = 0;
6239 dcmd->data_xfer_len =
6240 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6241 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08006242
Shivasharan S107a60d2017-10-19 02:49:05 -07006243 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6244 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08006245
Shivasharan Se7d36b82017-10-19 02:48:50 -07006246 if ((instance->adapter_type != MFI_SERIES) &&
6247 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08006248 ret = megasas_issue_blocked_cmd(instance,
6249 cmd, MFI_IO_TIMEOUT_SECS);
6250 else
6251 ret = megasas_issue_polled(instance, cmd);
6252
6253 switch (ret) {
6254 case DCMD_TIMEOUT:
6255 switch (dcmd_timeout_ocr_possible(instance)) {
6256 case INITIATE_OCR:
6257 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6258 megasas_reset_fusion(instance->host,
6259 MFI_IO_TIMEOUT_OCR);
6260 break;
6261 case KILL_ADAPTER:
6262 megaraid_sas_kill_hba(instance);
6263 break;
6264 case IGNORE_TIMEOUT:
6265 dev_info(&instance->pdev->dev,
6266 "Ignore DCMD timeout: %s %d\n",
6267 __func__, __LINE__);
6268 break;
6269 }
6270 break;
6271
6272 default:
6273 megasas_return_cmd(instance, cmd);
6274 }
6275 if (ret != DCMD_SUCCESS)
6276 dev_err(&instance->pdev->dev,
6277 "return from %s %d return value %d\n",
6278 __func__, __LINE__, ret);
6279
6280 return ret;
6281}
6282
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006283/**
6284 * megasas_start_aen - Subscribes to AEN during driver load time
6285 * @instance: Adapter soft state
6286 */
6287static int megasas_start_aen(struct megasas_instance *instance)
6288{
6289 struct megasas_evt_log_info eli;
6290 union megasas_evt_class_locale class_locale;
6291
6292 /*
6293 * Get the latest sequence number from FW
6294 */
6295 memset(&eli, 0, sizeof(eli));
6296
6297 if (megasas_get_seq_num(instance, &eli))
6298 return -1;
6299
6300 /*
6301 * Register AEN with FW for latest sequence number plus 1
6302 */
6303 class_locale.members.reserved = 0;
6304 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6305 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6306
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306307 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05306308 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306309 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006310}
6311
6312/**
6313 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6314 * @instance: Adapter soft state
6315 */
6316static int megasas_io_attach(struct megasas_instance *instance)
6317{
6318 struct Scsi_Host *host = instance->host;
6319
6320 /*
6321 * Export parameters required by SCSI mid-layer
6322 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006323 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05306324 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006325 host->this_id = instance->init_id;
6326 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08006327
6328 if (instance->fw_support_ieee)
6329 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6330
Yang, Bo1fd10682010-10-12 07:18:50 -06006331 /*
6332 * Check if the module parameter value for max_sectors can be used
6333 */
6334 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6335 instance->max_sectors_per_req = max_sectors;
6336 else {
6337 if (max_sectors) {
6338 if (((instance->pdev->device ==
6339 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6340 (instance->pdev->device ==
6341 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6342 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6343 instance->max_sectors_per_req = max_sectors;
6344 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006345 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06006346 "and <= %d (or < 1MB for GEN2 controller)\n",
6347 instance->max_sectors_per_req);
6348 }
6349 }
6350 }
6351
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006352 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08006353 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006354 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6355 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6356 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08006357 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006358
6359 /*
6360 * Notify the mid-layer about the new controller
6361 */
6362 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306363 dev_err(&instance->pdev->dev,
6364 "Failed to add host from %s %d\n",
6365 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006366 return -ENODEV;
6367 }
6368
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006369 return 0;
6370}
6371
Shivasharan S107a60d2017-10-19 02:49:05 -07006372/**
6373 * megasas_set_dma_mask - Set DMA mask for supported controllers
6374 *
6375 * @instance: Adapter soft state
6376 * Description:
6377 *
Shivasharan S894169d2018-12-18 05:59:54 -08006378 * For Ventura, driver/FW will operate in 63bit DMA addresses.
Shivasharan S107a60d2017-10-19 02:49:05 -07006379 *
6380 * For invader-
6381 * By default, driver/FW will operate in 32bit DMA addresses
6382 * for consistent DMA mapping but if 32 bit consistent
Shivasharan S894169d2018-12-18 05:59:54 -08006383 * DMA mask fails, driver will try with 63 bit consistent
6384 * mask provided FW is true 63bit DMA capable
Shivasharan S107a60d2017-10-19 02:49:05 -07006385 *
6386 * For older controllers(Thunderbolt and MFI based adapters)-
6387 * driver/FW will operate in 32 bit consistent DMA addresses.
6388 */
bo yang31ea7082007-11-07 12:09:50 -05006389static int
Shivasharan S107a60d2017-10-19 02:49:05 -07006390megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05006391{
Shivasharan S107a60d2017-10-19 02:49:05 -07006392 u64 consistent_mask;
6393 struct pci_dev *pdev;
Shivasharan S81b76452018-10-16 23:37:51 -07006394 u32 scratch_pad_1;
bo yang31ea7082007-11-07 12:09:50 -05006395
Shivasharan S107a60d2017-10-19 02:49:05 -07006396 pdev = instance->pdev;
Shivasharan S630d42b2018-12-17 00:47:37 -08006397 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
Shivasharan S894169d2018-12-18 05:59:54 -08006398 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
Shivasharan S107a60d2017-10-19 02:49:05 -07006399
6400 if (IS_DMA64) {
Shivasharan S894169d2018-12-18 05:59:54 -08006401 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006402 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6403 goto fail_set_dma_mask;
6404
Shivasharan S894169d2018-12-18 05:59:54 -08006405 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
Shivasharan S107a60d2017-10-19 02:49:05 -07006406 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6407 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6408 /*
6409 * If 32 bit DMA mask fails, then try for 64 bit mask
6410 * for FW capable of handling 64 bit DMA.
6411 */
Shivasharan S272652f2018-12-17 00:47:40 -08006412 scratch_pad_1 = megasas_readl
6413 (instance, &instance->reg_set->outbound_scratch_pad_1);
Shivasharan S107a60d2017-10-19 02:49:05 -07006414
Shivasharan S81b76452018-10-16 23:37:51 -07006415 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
Shivasharan S107a60d2017-10-19 02:49:05 -07006416 goto fail_set_dma_mask;
6417 else if (dma_set_mask_and_coherent(&pdev->dev,
Shivasharan S894169d2018-12-18 05:59:54 -08006418 DMA_BIT_MASK(63)))
bo yang31ea7082007-11-07 12:09:50 -05006419 goto fail_set_dma_mask;
6420 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006421 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6422 goto fail_set_dma_mask;
6423
6424 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6425 instance->consistent_mask_64bit = false;
6426 else
6427 instance->consistent_mask_64bit = true;
6428
6429 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
Tomas Henzld1f38d92019-01-02 16:07:25 +01006430 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
Shivasharan S894169d2018-12-18 05:59:54 -08006431 (instance->consistent_mask_64bit ? "63" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306432
bo yang31ea7082007-11-07 12:09:50 -05006433 return 0;
6434
6435fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006436 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6437 return -1;
6438
bo yang31ea7082007-11-07 12:09:50 -05006439}
6440
Shivasharan Sc3651782017-10-19 02:48:48 -07006441/*
6442 * megasas_set_adapter_type - Set adapter type.
6443 * Supported controllers can be divided in
Shivasharan S154a7cd2018-12-17 00:47:38 -08006444 * different categories-
6445 * enum MR_ADAPTER_TYPE {
6446 * MFI_SERIES = 1,
6447 * THUNDERBOLT_SERIES = 2,
6448 * INVADER_SERIES = 3,
6449 * VENTURA_SERIES = 4,
6450 * AERO_SERIES = 5,
6451 * };
Shivasharan Sc3651782017-10-19 02:48:48 -07006452 * @instance: Adapter soft state
6453 * return: void
6454 */
6455static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6456{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006457 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6458 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006459 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006460 } else {
6461 switch (instance->pdev->device) {
Shivasharan S469f72d2018-11-09 09:47:20 -08006462 case PCI_DEVICE_ID_LSI_AERO_10E1:
6463 case PCI_DEVICE_ID_LSI_AERO_10E2:
6464 case PCI_DEVICE_ID_LSI_AERO_10E5:
6465 case PCI_DEVICE_ID_LSI_AERO_10E6:
Shivasharan S154a7cd2018-12-17 00:47:38 -08006466 instance->adapter_type = AERO_SERIES;
6467 break;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006468 case PCI_DEVICE_ID_LSI_VENTURA:
6469 case PCI_DEVICE_ID_LSI_CRUSADER:
6470 case PCI_DEVICE_ID_LSI_HARPOON:
6471 case PCI_DEVICE_ID_LSI_TOMCAT:
6472 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6473 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6474 instance->adapter_type = VENTURA_SERIES;
6475 break;
6476 case PCI_DEVICE_ID_LSI_FUSION:
6477 case PCI_DEVICE_ID_LSI_PLASMA:
6478 instance->adapter_type = THUNDERBOLT_SERIES;
6479 break;
6480 case PCI_DEVICE_ID_LSI_INVADER:
6481 case PCI_DEVICE_ID_LSI_INTRUDER:
6482 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6483 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6484 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6485 case PCI_DEVICE_ID_LSI_FURY:
6486 instance->adapter_type = INVADER_SERIES;
6487 break;
6488 default: /* For all other supported controllers */
6489 instance->adapter_type = MFI_SERIES;
6490 break;
6491 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006492 }
6493}
6494
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006495static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6496{
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006497 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6498 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6499 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6500 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006501
6502 if (!instance->producer || !instance->consumer) {
6503 dev_err(&instance->pdev->dev,
6504 "Failed to allocate memory for producer, consumer\n");
6505 return -1;
6506 }
6507
6508 *instance->producer = 0;
6509 *instance->consumer = 0;
6510 return 0;
6511}
6512
6513/**
6514 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6515 * structures which are not common across MFI
6516 * adapters and fusion adapters.
6517 * For MFI based adapters, allocate producer and
6518 * consumer buffers. For fusion adapters, allocate
6519 * memory for fusion context.
6520 * @instance: Adapter soft state
6521 * return: 0 for SUCCESS
6522 */
6523static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6524{
Kees Cook6396bb22018-06-12 14:03:40 -07006525 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
Ming Leiadbe5522018-03-13 17:42:40 +08006526 GFP_KERNEL);
6527 if (!instance->reply_map)
6528 return -ENOMEM;
6529
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006530 switch (instance->adapter_type) {
6531 case MFI_SERIES:
6532 if (megasas_alloc_mfi_ctrl_mem(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006533 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006534 break;
Shivasharan S154a7cd2018-12-17 00:47:38 -08006535 case AERO_SERIES:
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006536 case VENTURA_SERIES:
6537 case THUNDERBOLT_SERIES:
6538 case INVADER_SERIES:
6539 if (megasas_alloc_fusion_context(instance))
Ming Leiadbe5522018-03-13 17:42:40 +08006540 goto fail;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006541 break;
6542 }
6543
6544 return 0;
Ming Leiadbe5522018-03-13 17:42:40 +08006545 fail:
6546 kfree(instance->reply_map);
6547 instance->reply_map = NULL;
6548 return -ENOMEM;
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006549}
6550
6551/*
6552 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6553 * producer, consumer buffers for MFI adapters
6554 *
6555 * @instance - Adapter soft instance
6556 *
6557 */
6558static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6559{
Ming Leiadbe5522018-03-13 17:42:40 +08006560 kfree(instance->reply_map);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006561 if (instance->adapter_type == MFI_SERIES) {
6562 if (instance->producer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006563 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006564 instance->producer,
6565 instance->producer_h);
6566 if (instance->consumer)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006567 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006568 instance->consumer,
6569 instance->consumer_h);
6570 } else {
6571 megasas_free_fusion_context(instance);
6572 }
6573}
6574
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006575/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006576 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6577 * driver load time
6578 *
6579 * @instance- Adapter soft instance
6580 * @return- O for SUCCESS
6581 */
6582static inline
6583int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6584{
6585 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006586 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006587
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006588 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6589 sizeof(struct megasas_evt_detail),
6590 &instance->evt_detail_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006591
6592 if (!instance->evt_detail) {
6593 dev_err(&instance->pdev->dev,
6594 "Failed to allocate event detail buffer\n");
6595 return -ENOMEM;
6596 }
6597
Shivasharan S9b3d0282017-10-19 02:48:56 -07006598 if (fusion) {
6599 fusion->ioc_init_request =
6600 dma_alloc_coherent(&pdev->dev,
6601 sizeof(struct MPI2_IOC_INIT_REQUEST),
6602 &fusion->ioc_init_request_phys,
6603 GFP_KERNEL);
6604
6605 if (!fusion->ioc_init_request) {
6606 dev_err(&pdev->dev,
6607 "Failed to allocate PD list buffer\n");
6608 return -ENOMEM;
6609 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006610
6611 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6612 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6613 &instance->snapdump_prop_h, GFP_KERNEL);
6614
6615 if (!instance->snapdump_prop)
6616 dev_err(&pdev->dev,
6617 "Failed to allocate snapdump properties buffer\n");
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006618
6619 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6620 HOST_DEVICE_LIST_SZ,
6621 &instance->host_device_list_buf_h,
6622 GFP_KERNEL);
6623
6624 if (!instance->host_device_list_buf) {
6625 dev_err(&pdev->dev,
6626 "Failed to allocate targetid list buffer\n");
6627 return -ENOMEM;
6628 }
6629
Shivasharan S9b3d0282017-10-19 02:48:56 -07006630 }
6631
6632 instance->pd_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006633 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006634 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006635 &instance->pd_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006636
6637 if (!instance->pd_list_buf) {
6638 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6639 return -ENOMEM;
6640 }
6641
6642 instance->ctrl_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006643 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006644 sizeof(struct megasas_ctrl_info),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006645 &instance->ctrl_info_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006646
6647 if (!instance->ctrl_info_buf) {
6648 dev_err(&pdev->dev,
6649 "Failed to allocate controller info buffer\n");
6650 return -ENOMEM;
6651 }
6652
6653 instance->ld_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006654 dma_alloc_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006655 sizeof(struct MR_LD_LIST),
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006656 &instance->ld_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006657
6658 if (!instance->ld_list_buf) {
6659 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6660 return -ENOMEM;
6661 }
6662
6663 instance->ld_targetid_list_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006664 dma_alloc_coherent(&pdev->dev,
6665 sizeof(struct MR_LD_TARGETID_LIST),
6666 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
Shivasharan S9b3d0282017-10-19 02:48:56 -07006667
6668 if (!instance->ld_targetid_list_buf) {
6669 dev_err(&pdev->dev,
6670 "Failed to allocate LD targetid list buffer\n");
6671 return -ENOMEM;
6672 }
6673
Shivasharan S1b4bed22017-10-19 02:48:55 -07006674 if (!reset_devices) {
6675 instance->system_info_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006676 dma_alloc_coherent(&pdev->dev,
6677 sizeof(struct MR_DRV_SYSTEM_INFO),
6678 &instance->system_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006679 instance->pd_info =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006680 dma_alloc_coherent(&pdev->dev,
6681 sizeof(struct MR_PD_INFO),
6682 &instance->pd_info_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006683 instance->tgt_prop =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006684 dma_alloc_coherent(&pdev->dev,
6685 sizeof(struct MR_TARGET_PROPERTIES),
6686 &instance->tgt_prop_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006687 instance->crash_dump_buf =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006688 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6689 &instance->crash_dump_h, GFP_KERNEL);
Shivasharan S1b4bed22017-10-19 02:48:55 -07006690
6691 if (!instance->system_info_buf)
6692 dev_err(&instance->pdev->dev,
6693 "Failed to allocate system info buffer\n");
6694
6695 if (!instance->pd_info)
6696 dev_err(&instance->pdev->dev,
6697 "Failed to allocate pd_info buffer\n");
6698
6699 if (!instance->tgt_prop)
6700 dev_err(&instance->pdev->dev,
6701 "Failed to allocate tgt_prop buffer\n");
6702
6703 if (!instance->crash_dump_buf)
6704 dev_err(&instance->pdev->dev,
6705 "Failed to allocate crash dump buffer\n");
6706 }
6707
6708 return 0;
6709}
6710
6711/*
6712 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6713 * during driver load time
6714 *
6715 * @instance- Adapter soft instance
6716 *
6717 */
6718static inline
6719void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6720{
6721 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006722 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006723
6724 if (instance->evt_detail)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006725 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006726 instance->evt_detail,
6727 instance->evt_detail_h);
6728
Shivasharan S9b3d0282017-10-19 02:48:56 -07006729 if (fusion && fusion->ioc_init_request)
6730 dma_free_coherent(&pdev->dev,
6731 sizeof(struct MPI2_IOC_INIT_REQUEST),
6732 fusion->ioc_init_request,
6733 fusion->ioc_init_request_phys);
6734
6735 if (instance->pd_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006736 dma_free_coherent(&pdev->dev,
Shivasharan S9b3d0282017-10-19 02:48:56 -07006737 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6738 instance->pd_list_buf,
6739 instance->pd_list_buf_h);
6740
6741 if (instance->ld_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006742 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006743 instance->ld_list_buf,
6744 instance->ld_list_buf_h);
6745
6746 if (instance->ld_targetid_list_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006747 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006748 instance->ld_targetid_list_buf,
6749 instance->ld_targetid_list_buf_h);
6750
6751 if (instance->ctrl_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006752 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
Shivasharan S9b3d0282017-10-19 02:48:56 -07006753 instance->ctrl_info_buf,
6754 instance->ctrl_info_buf_h);
6755
Shivasharan S1b4bed22017-10-19 02:48:55 -07006756 if (instance->system_info_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006757 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006758 instance->system_info_buf,
6759 instance->system_info_h);
6760
6761 if (instance->pd_info)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006762 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006763 instance->pd_info, instance->pd_info_h);
6764
6765 if (instance->tgt_prop)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006766 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
Shivasharan S1b4bed22017-10-19 02:48:55 -07006767 instance->tgt_prop, instance->tgt_prop_h);
6768
6769 if (instance->crash_dump_buf)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006770 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
Shivasharan S1b4bed22017-10-19 02:48:55 -07006771 instance->crash_dump_buf,
6772 instance->crash_dump_h);
Shivasharan Sf0c21df2018-10-16 23:37:40 -07006773
6774 if (instance->snapdump_prop)
6775 dma_free_coherent(&pdev->dev,
6776 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6777 instance->snapdump_prop,
6778 instance->snapdump_prop_h);
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006779
6780 if (instance->host_device_list_buf)
6781 dma_free_coherent(&pdev->dev,
6782 HOST_DEVICE_LIST_SZ,
6783 instance->host_device_list_buf,
6784 instance->host_device_list_buf_h);
6785
Shivasharan S1b4bed22017-10-19 02:48:55 -07006786}
6787
Shivasharan S7535f272017-10-19 02:48:58 -07006788/*
6789 * megasas_init_ctrl_params - Initialize controller's instance
6790 * parameters before FW init
6791 * @instance - Adapter soft instance
6792 * @return - void
6793 */
6794static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6795{
6796 instance->fw_crash_state = UNAVAILABLE;
6797
6798 megasas_poll_wait_aen = 0;
6799 instance->issuepend_done = 1;
6800 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6801
6802 /*
6803 * Initialize locks and queues
6804 */
6805 INIT_LIST_HEAD(&instance->cmd_pool);
6806 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6807
6808 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S1d15d902019-05-07 10:05:36 -07006809 atomic64_set(&instance->total_io_count, 0);
Shivasharan S7535f272017-10-19 02:48:58 -07006810
6811 init_waitqueue_head(&instance->int_cmd_wait_q);
6812 init_waitqueue_head(&instance->abort_cmd_wait_q);
6813
6814 spin_lock_init(&instance->crashdump_lock);
6815 spin_lock_init(&instance->mfi_pool_lock);
6816 spin_lock_init(&instance->hba_lock);
6817 spin_lock_init(&instance->stream_lock);
6818 spin_lock_init(&instance->completion_lock);
6819
Shivasharan S7535f272017-10-19 02:48:58 -07006820 mutex_init(&instance->reset_mutex);
6821
6822 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6823 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6824 instance->flag_ieee = 1;
6825
6826 megasas_dbg_lvl = 0;
6827 instance->flag = 0;
6828 instance->unload = 1;
6829 instance->last_time = 0;
6830 instance->disableOnlineCtrlReset = 1;
6831 instance->UnevenSpanSupport = 0;
Shivasharan S1d15d902019-05-07 10:05:36 -07006832 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
6833 instance->msix_load_balance = false;
Shivasharan S7535f272017-10-19 02:48:58 -07006834
Shivasharan S3f6194a2018-10-16 23:37:39 -07006835 if (instance->adapter_type != MFI_SERIES)
Shivasharan S7535f272017-10-19 02:48:58 -07006836 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Shivasharan S3f6194a2018-10-16 23:37:39 -07006837 else
Shivasharan S7535f272017-10-19 02:48:58 -07006838 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006839}
6840
6841/**
6842 * megasas_probe_one - PCI hotplug entry point
6843 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006844 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006845 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006846static int megasas_probe_one(struct pci_dev *pdev,
6847 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006848{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306849 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006850 struct Scsi_Host *host;
6851 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006852 u16 control = 0;
6853
Shivasharan S469f72d2018-11-09 09:47:20 -08006854 switch (pdev->device) {
6855 case PCI_DEVICE_ID_LSI_AERO_10E1:
6856 case PCI_DEVICE_ID_LSI_AERO_10E5:
6857 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
6858 break;
6859 }
6860
adam radford66192dfe2011-02-24 20:56:28 -08006861 /* Reset MSI-X in the kdump kernel */
6862 if (reset_devices) {
6863 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6864 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006865 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006866 &control);
6867 if (control & PCI_MSIX_FLAGS_ENABLE) {
6868 dev_info(&pdev->dev, "resetting MSI-X\n");
6869 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006870 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006871 control &
6872 ~PCI_MSIX_FLAGS_ENABLE);
6873 }
6874 }
6875 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006876
6877 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006878 * PCI prepping: enable device set bus mastering and dma mask
6879 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006880 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006881
6882 if (rval) {
6883 return rval;
6884 }
6885
6886 pci_set_master(pdev);
6887
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006888 host = scsi_host_alloc(&megasas_template,
6889 sizeof(struct megasas_instance));
6890
6891 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006892 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006893 goto fail_alloc_instance;
6894 }
6895
6896 instance = (struct megasas_instance *)host->hostdata;
6897 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006898 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006899
6900 /*
6901 * Initialize PCI related and misc parameters
6902 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006903 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006904 instance->host = host;
6905 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6906 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6907
Shivasharan Sc3651782017-10-19 02:48:48 -07006908 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07006909
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006910 /*
adam radford0a770662011-02-24 20:56:12 -08006911 * Initialize MFI Firmware
6912 */
6913 if (megasas_init_fw(instance))
6914 goto fail_init_mfi;
6915
adam radford229fe472014-03-10 02:51:56 -07006916 if (instance->requestorId) {
6917 if (instance->PlasmaFW111) {
6918 instance->vf_affiliation_111 =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006919 dma_alloc_coherent(&pdev->dev,
6920 sizeof(struct MR_LD_VF_AFFILIATION_111),
6921 &instance->vf_affiliation_111_h,
6922 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07006923 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006924 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006925 "memory for VF affiliation buffer\n");
6926 } else {
6927 instance->vf_affiliation =
Christoph Hellwig60ee6522018-10-10 19:31:25 +02006928 dma_alloc_coherent(&pdev->dev,
6929 (MAX_LOGICAL_DRIVES + 1) *
6930 sizeof(struct MR_LD_VF_AFFILIATION),
6931 &instance->vf_affiliation_h,
6932 GFP_KERNEL);
adam radford229fe472014-03-10 02:51:56 -07006933 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006934 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006935 "memory for VF affiliation buffer\n");
6936 }
6937 }
6938
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006939 /*
6940 * Store instance in PCI softstate
6941 */
6942 pci_set_drvdata(pdev, instance);
6943
6944 /*
6945 * Add this controller to megasas_mgmt_info structure so that it
6946 * can be exported to management applications
6947 */
6948 megasas_mgmt_info.count++;
6949 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6950 megasas_mgmt_info.max_index++;
6951
6952 /*
adam radford541f90b2011-05-11 18:34:29 -07006953 * Register with SCSI mid-layer
6954 */
6955 if (megasas_io_attach(instance))
6956 goto fail_io_attach;
6957
6958 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306959 /*
6960 * Trigger SCSI to scan our drives
6961 */
Shivasharan Sf6fe5732019-01-29 01:38:14 -08006962 if (!instance->enable_fw_dev_list ||
6963 (instance->host_device_list_buf->count > 0))
6964 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006965
6966 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006967 * Initiate AEN (Asynchronous Event Notification)
6968 */
6969 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006970 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006971 goto fail_start_aen;
6972 }
6973
Adam Radford9ea81f82014-07-09 15:17:57 -07006974 /* Get current SR-IOV LD/VF affiliation */
6975 if (instance->requestorId)
6976 megasas_get_ld_vf_affiliation(instance, 1);
6977
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006978 return 0;
6979
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006980fail_start_aen:
6981fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006982 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006983 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08006984 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006985
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306986 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306987 megasas_destroy_irqs(instance);
6988
Shivasharan Se7d36b82017-10-19 02:48:50 -07006989 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08006990 megasas_release_fusion(instance);
6991 else
6992 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006993 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006994 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306995fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006996 scsi_host_put(host);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006997fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006998 pci_disable_device(pdev);
6999
7000 return -ENODEV;
7001}
7002
7003/**
7004 * megasas_flush_cache - Requests FW to flush all its caches
7005 * @instance: Adapter soft state
7006 */
7007static void megasas_flush_cache(struct megasas_instance *instance)
7008{
7009 struct megasas_cmd *cmd;
7010 struct megasas_dcmd_frame *dcmd;
7011
Sumit Saxena8a01a412016-01-28 21:04:32 +05307012 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007013 return;
7014
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007015 cmd = megasas_get_cmd(instance);
7016
7017 if (!cmd)
7018 return;
7019
7020 dcmd = &cmd->frame->dcmd;
7021
7022 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7023
7024 dcmd->cmd = MFI_CMD_DCMD;
7025 dcmd->cmd_status = 0x0;
7026 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307027 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007028 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007029 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007030 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307031 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007032 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7033
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307034 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7035 != DCMD_SUCCESS) {
7036 dev_err(&instance->pdev->dev,
7037 "return from %s %d\n", __func__, __LINE__);
7038 return;
7039 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007040
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307041 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007042}
7043
7044/**
7045 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7046 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05007047 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007048 */
bo yang31ea7082007-11-07 12:09:50 -05007049static void megasas_shutdown_controller(struct megasas_instance *instance,
7050 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007051{
7052 struct megasas_cmd *cmd;
7053 struct megasas_dcmd_frame *dcmd;
7054
Sumit Saxena8a01a412016-01-28 21:04:32 +05307055 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04007056 return;
7057
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007058 cmd = megasas_get_cmd(instance);
7059
7060 if (!cmd)
7061 return;
7062
7063 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307064 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307065 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08007066 if (instance->map_update_cmd)
7067 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307068 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307069 if (instance->jbod_seq_cmd)
7070 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307071 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307072
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007073 dcmd = &cmd->frame->dcmd;
7074
7075 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7076
7077 dcmd->cmd = MFI_CMD_DCMD;
7078 dcmd->cmd_status = 0x0;
7079 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307080 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007081 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07007082 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007083 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307084 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007085
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307086 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7087 != DCMD_SUCCESS) {
7088 dev_err(&instance->pdev->dev,
7089 "return from %s %d\n", __func__, __LINE__);
7090 return;
7091 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007092
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307093 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007094}
7095
Jiri Slaby33139b22008-05-01 17:56:02 +02007096#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007097/**
bo yangad84db22007-11-09 04:40:16 -05007098 * megasas_suspend - driver suspend entry point
7099 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05007100 * @state: PCI power state to suspend routine
7101 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007102static int
bo yang31ea7082007-11-07 12:09:50 -05007103megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7104{
7105 struct Scsi_Host *host;
7106 struct megasas_instance *instance;
7107
7108 instance = pci_get_drvdata(pdev);
7109 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06007110 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05007111
adam radford229fe472014-03-10 02:51:56 -07007112 /* Shutdown SR-IOV heartbeat timer */
7113 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7114 del_timer_sync(&instance->sriov_heartbeat_timer);
7115
Shivasharan S3f6194a2018-10-16 23:37:39 -07007116 /* Stop the FW fault detection watchdog */
7117 if (instance->adapter_type != MFI_SERIES)
7118 megasas_fusion_stop_watchdog(instance);
7119
bo yang31ea7082007-11-07 12:09:50 -05007120 megasas_flush_cache(instance);
7121 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007122
7123 /* cancel the delayed work if this work still in queue */
7124 if (instance->ev != NULL) {
7125 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007126 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007127 instance->ev = NULL;
7128 }
7129
bo yang31ea7082007-11-07 12:09:50 -05007130 tasklet_kill(&instance->isr_tasklet);
7131
7132 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307133 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07007134
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307135 megasas_destroy_irqs(instance);
7136
adam radfordc8e858f2011-10-08 18:15:13 -07007137 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007138 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05007139
7140 pci_save_state(pdev);
7141 pci_disable_device(pdev);
7142
7143 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7144
7145 return 0;
7146}
7147
7148/**
7149 * megasas_resume- driver resume entry point
7150 * @pdev: PCI device structure
7151 */
Jiri Slaby33139b22008-05-01 17:56:02 +02007152static int
bo yang31ea7082007-11-07 12:09:50 -05007153megasas_resume(struct pci_dev *pdev)
7154{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307155 int rval;
bo yang31ea7082007-11-07 12:09:50 -05007156 struct Scsi_Host *host;
7157 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01007158 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05007159
7160 instance = pci_get_drvdata(pdev);
7161 host = instance->host;
7162 pci_set_power_state(pdev, PCI_D0);
7163 pci_enable_wake(pdev, PCI_D0, 0);
7164 pci_restore_state(pdev);
7165
7166 /*
7167 * PCI prepping: enable device set bus mastering and dma mask
7168 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09007169 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05007170
7171 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007172 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05007173 return rval;
7174 }
7175
7176 pci_set_master(pdev);
7177
Shivasharan S107a60d2017-10-19 02:49:05 -07007178 /*
7179 * We expect the FW state to be READY
7180 */
7181 if (megasas_transition_to_ready(instance, 0))
7182 goto fail_ready_state;
7183
7184 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05007185 goto fail_set_dma_mask;
7186
7187 /*
7188 * Initialize MFI Firmware
7189 */
7190
bo yang31ea7082007-11-07 12:09:50 -05007191 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08007192 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05007193
adam radford3f1abce2011-05-11 18:33:47 -07007194 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01007195 if (instance->msix_vectors) {
7196 irq_flags = PCI_IRQ_MSIX;
Shivasharan S1d15d902019-05-07 10:05:36 -07007197 if (instance->smp_affinity_enable)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007198 irq_flags |= PCI_IRQ_AFFINITY;
7199 }
7200 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7201 instance->msix_vectors ?
7202 instance->msix_vectors : 1, irq_flags);
7203 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02007204 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07007205
Ming Leiadbe5522018-03-13 17:42:40 +08007206 megasas_setup_reply_map(instance);
7207
Shivasharan Se7d36b82017-10-19 02:48:50 -07007208 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007209 megasas_reset_reply_desc(instance);
7210 if (megasas_ioc_init_fusion(instance)) {
7211 megasas_free_cmds(instance);
7212 megasas_free_cmds_fusion(instance);
7213 goto fail_init_mfi;
7214 }
7215 if (!megasas_get_map_info(instance))
7216 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307217 } else {
adam radford9c915a82010-12-21 13:34:31 -08007218 *instance->producer = 0;
7219 *instance->consumer = 0;
7220 if (megasas_issue_init_mfi(instance))
7221 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08007222 }
bo yang31ea7082007-11-07 12:09:50 -05007223
Shivasharan Sc3b10a52018-06-04 03:45:10 -07007224 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7225 goto fail_init_mfi;
7226
adam radford9c915a82010-12-21 13:34:31 -08007227 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7228 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05007229
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307230 if (instance->msix_vectors ?
7231 megasas_setup_irqs_msix(instance, 0) :
7232 megasas_setup_irqs_ioapic(instance))
7233 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05007234
Shivasharan S62a04f82019-05-07 10:05:35 -07007235 if (instance->adapter_type != MFI_SERIES)
7236 megasas_setup_irq_poll(instance);
7237
adam radford229fe472014-03-10 02:51:56 -07007238 /* Re-launch SR-IOV heartbeat timer */
7239 if (instance->requestorId) {
7240 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07007241 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307242 else {
adam radford229fe472014-03-10 02:51:56 -07007243 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307244 goto fail_init_mfi;
7245 }
adam radford229fe472014-03-10 02:51:56 -07007246 }
7247
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307248 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307249 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06007250 instance->unload = 0;
7251
adam radford541f90b2011-05-11 18:34:29 -07007252 /*
7253 * Initiate AEN (Asynchronous Event Notification)
7254 */
7255 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007256 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07007257
Shivasharan S3f6194a2018-10-16 23:37:39 -07007258 /* Re-launch FW fault watchdog */
7259 if (instance->adapter_type != MFI_SERIES)
7260 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7261 goto fail_start_watchdog;
7262
bo yang31ea7082007-11-07 12:09:50 -05007263 return 0;
7264
Shivasharan S3f6194a2018-10-16 23:37:39 -07007265fail_start_watchdog:
7266 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7267 del_timer_sync(&instance->sriov_heartbeat_timer);
bo yang31ea7082007-11-07 12:09:50 -05007268fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07007269 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007270 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05007271 scsi_host_put(host);
7272
Shivasharan S107a60d2017-10-19 02:49:05 -07007273fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05007274fail_set_dma_mask:
7275fail_ready_state:
7276
7277 pci_disable_device(pdev);
7278
7279 return -ENODEV;
7280}
Jiri Slaby33139b22008-05-01 17:56:02 +02007281#else
7282#define megasas_suspend NULL
7283#define megasas_resume NULL
7284#endif
bo yang31ea7082007-11-07 12:09:50 -05007285
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007286static inline int
7287megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7288{
7289 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7290 int i;
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007291 u8 adp_state;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007292
7293 for (i = 0; i < wait_time; i++) {
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007294 adp_state = atomic_read(&instance->adprecovery);
7295 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7296 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007297 break;
7298
7299 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7300 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7301
7302 msleep(1000);
7303 }
7304
Shivasharan S9c9db8b2018-06-04 03:45:11 -07007305 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7306 dev_info(&instance->pdev->dev,
7307 "%s HBA failed to become operational, adp_state %d\n",
7308 __func__, adp_state);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007309 return 1;
7310 }
7311
7312 return 0;
7313}
7314
bo yang31ea7082007-11-07 12:09:50 -05007315/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007316 * megasas_detach_one - PCI hot"un"plug entry point
7317 * @pdev: PCI device structure
7318 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007319static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007320{
7321 int i;
7322 struct Scsi_Host *host;
7323 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08007324 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307325 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007326
7327 instance = pci_get_drvdata(pdev);
7328 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08007329 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007330
adam radford229fe472014-03-10 02:51:56 -07007331 /* Shutdown SR-IOV heartbeat timer */
7332 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7333 del_timer_sync(&instance->sriov_heartbeat_timer);
7334
Shivasharan S3f6194a2018-10-16 23:37:39 -07007335 /* Stop the FW fault detection watchdog */
7336 if (instance->adapter_type != MFI_SERIES)
7337 megasas_fusion_stop_watchdog(instance);
7338
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307339 if (instance->fw_crash_state != UNAVAILABLE)
7340 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007341 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08007342 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007343
7344 if (megasas_wait_for_adapter_operational(instance))
7345 goto skip_firing_dcmds;
7346
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007347 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05007348 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007349
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007350skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007351 /* cancel the delayed work if this work still in queue*/
7352 if (instance->ev != NULL) {
7353 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007354 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007355 instance->ev = NULL;
7356 }
7357
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05307358 /* cancel all wait events */
7359 wake_up_all(&instance->int_cmd_wait_q);
7360
Sumant Patro5d018ad2006-10-03 13:13:18 -07007361 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007362
7363 /*
7364 * Take the instance off the instance array. Note that we will not
7365 * decrement the max_index. We let this array be sparse array
7366 */
7367 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7368 if (megasas_mgmt_info.instance[i] == instance) {
7369 megasas_mgmt_info.count--;
7370 megasas_mgmt_info.instance[i] = NULL;
7371
7372 break;
7373 }
7374 }
7375
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307376 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007377
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307378 megasas_destroy_irqs(instance);
7379
adam radfordc8e858f2011-10-08 18:15:13 -07007380 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007381 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007382
Shivasharan S630d42b2018-12-17 00:47:37 -08007383 if (instance->adapter_type >= VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05007384 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7385 kfree(fusion->stream_detect_by_ld[i]);
7386 kfree(fusion->stream_detect_by_ld);
7387 fusion->stream_detect_by_ld = NULL;
7388 }
7389
7390
Shivasharan Se7d36b82017-10-19 02:48:50 -07007391 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08007392 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05307393 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7394 (sizeof(struct MR_PD_CFG_SEQ) *
7395 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307396 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08007397 if (fusion->ld_map[i])
7398 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307399 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08007400 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307401 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07007402 if (fusion->ld_drv_map[i]) {
7403 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7404 vfree(fusion->ld_drv_map[i]);
7405 else
7406 free_pages((ulong)fusion->ld_drv_map[i],
7407 fusion->drv_map_pages);
7408 }
7409
Maurizio Lombardi546e5592016-01-22 13:41:42 +01007410 if (fusion->pd_seq_sync[i])
7411 dma_free_coherent(&instance->pdev->dev,
7412 pd_seq_map_sz,
7413 fusion->pd_seq_sync[i],
7414 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307415 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05307416 } else {
adam radford9c915a82010-12-21 13:34:31 -08007417 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08007418 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007419
adam radford229fe472014-03-10 02:51:56 -07007420 if (instance->vf_affiliation)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007421 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
adam radford229fe472014-03-10 02:51:56 -07007422 sizeof(struct MR_LD_VF_AFFILIATION),
7423 instance->vf_affiliation,
7424 instance->vf_affiliation_h);
7425
7426 if (instance->vf_affiliation_111)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007427 dma_free_coherent(&pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07007428 sizeof(struct MR_LD_VF_AFFILIATION_111),
7429 instance->vf_affiliation_111,
7430 instance->vf_affiliation_111_h);
7431
7432 if (instance->hb_host_mem)
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007433 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
adam radford229fe472014-03-10 02:51:56 -07007434 instance->hb_host_mem,
7435 instance->hb_host_mem_h);
7436
Shivasharan S1b4bed22017-10-19 02:48:55 -07007437 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307438
Shivasharan S49a7a4a2017-10-19 02:48:54 -07007439 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05307440
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007441 scsi_host_put(host);
7442
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007443 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007444}
7445
7446/**
7447 * megasas_shutdown - Shutdown entry point
7448 * @device: Generic device structure
7449 */
7450static void megasas_shutdown(struct pci_dev *pdev)
7451{
7452 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07007453
Yang, Bo0c79e682009-10-06 14:47:35 -06007454 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007455
7456 if (megasas_wait_for_adapter_operational(instance))
7457 goto skip_firing_dcmds;
7458
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007459 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07007460 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07007461
7462skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05307463 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05307464 megasas_destroy_irqs(instance);
7465
adam radfordc8e858f2011-10-08 18:15:13 -07007466 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01007467 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007468}
7469
7470/**
7471 * megasas_mgmt_open - char node "open" entry point
7472 */
7473static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7474{
7475 /*
7476 * Allow only those users with admin rights
7477 */
7478 if (!capable(CAP_SYS_ADMIN))
7479 return -EACCES;
7480
7481 return 0;
7482}
7483
7484/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007485 * megasas_mgmt_fasync - Async notifier registration from applications
7486 *
7487 * This function adds the calling process to a driver global queue. When an
7488 * event occurs, SIGIO will be sent to all processes in this queue.
7489 */
7490static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7491{
7492 int rc;
7493
Arjan van de Ven0b950672006-01-11 13:16:10 +01007494 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007495
7496 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7497
Arjan van de Ven0b950672006-01-11 13:16:10 +01007498 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007499
7500 if (rc >= 0) {
7501 /* For sanity check when we get ioctl */
7502 filep->private_data = filep;
7503 return 0;
7504 }
7505
7506 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7507
7508 return rc;
7509}
7510
7511/**
Yang, Boc3518832009-10-06 14:18:02 -06007512 * megasas_mgmt_poll - char node "poll" entry point
7513 * */
Al Viroafc9a422017-07-03 06:39:46 -04007514static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
Yang, Boc3518832009-10-06 14:18:02 -06007515{
Al Viroafc9a422017-07-03 06:39:46 -04007516 __poll_t mask;
Yang, Boc3518832009-10-06 14:18:02 -06007517 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007518
Yang, Boc3518832009-10-06 14:18:02 -06007519 poll_wait(file, &megasas_poll_wait, wait);
7520 spin_lock_irqsave(&poll_aen_lock, flags);
7521 if (megasas_poll_wait_aen)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08007522 mask = (EPOLLIN | EPOLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007523 else
7524 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307525 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007526 spin_unlock_irqrestore(&poll_aen_lock, flags);
7527 return mask;
7528}
7529
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307530/*
7531 * megasas_set_crash_dump_params_ioctl:
7532 * Send CRASH_DUMP_MODE DCMD to all controllers
7533 * @cmd: MFI command frame
7534 */
7535
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007536static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307537{
7538 struct megasas_instance *local_instance;
7539 int i, error = 0;
7540 int crash_support;
7541
7542 crash_support = cmd->frame->dcmd.mbox.w[0];
7543
7544 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7545 local_instance = megasas_mgmt_info.instance[i];
7546 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307547 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307548 MEGASAS_HBA_OPERATIONAL) &&
7549 !megasas_set_crash_dump_params(local_instance,
7550 crash_support)) {
7551 local_instance->crash_dump_app_support =
7552 crash_support;
7553 dev_info(&local_instance->pdev->dev,
7554 "Application firmware crash "
7555 "dump mode set success\n");
7556 error = 0;
7557 } else {
7558 dev_info(&local_instance->pdev->dev,
7559 "Application firmware crash "
7560 "dump mode set failed\n");
7561 error = -1;
7562 }
7563 }
7564 }
7565 return error;
7566}
7567
Yang, Boc3518832009-10-06 14:18:02 -06007568/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007569 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7570 * @instance: Adapter soft state
7571 * @argp: User's ioctl packet
7572 */
7573static int
7574megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7575 struct megasas_iocpacket __user * user_ioc,
7576 struct megasas_iocpacket *ioc)
7577{
Shivasharan S107a60d2017-10-19 02:49:05 -07007578 struct megasas_sge64 *kern_sge64 = NULL;
7579 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007580 struct megasas_cmd *cmd;
7581 void *kbuff_arr[MAX_IOCTL_SGE];
7582 dma_addr_t buf_handle = 0;
7583 int error = 0, i;
7584 void *sense = NULL;
7585 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007586 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007587 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007588
7589 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7590
7591 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007592 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007593 ioc->sge_count, MAX_IOCTL_SGE);
7594 return -EINVAL;
7595 }
7596
Shivasharan Sf870bcb2018-01-05 05:33:04 -08007597 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7598 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7599 !instance->support_nvme_passthru)) {
Shivasharan S82add4e2017-10-19 02:49:02 -07007600 dev_err(&instance->pdev->dev,
7601 "Received invalid ioctl command 0x%x\n",
7602 ioc->frame.hdr.cmd);
7603 return -ENOTSUPP;
7604 }
7605
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007606 cmd = megasas_get_cmd(instance);
7607 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007608 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007609 return -ENOMEM;
7610 }
7611
7612 /*
7613 * User's IOCTL packet has 2 frames (maximum). Copy those two
7614 * frames into our cmd's frames. cmd->frame's context will get
7615 * overwritten when we copy from user's frames. So set that value
7616 * alone separately
7617 */
7618 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307619 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007620 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07007621
7622 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7623
7624 if (instance->consistent_mask_64bit)
7625 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7626 MFI_FRAME_SENSE64));
7627 else
7628 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307629 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07007630
7631 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7632 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007633
Shivasharan S8823abe2017-08-23 04:47:00 -07007634 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08007635 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7636 megasas_return_cmd(instance, cmd);
7637 return -1;
7638 }
7639 }
7640
Shivasharan S8823abe2017-08-23 04:47:00 -07007641 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307642 error = megasas_set_crash_dump_params_ioctl(cmd);
7643 megasas_return_cmd(instance, cmd);
7644 return error;
7645 }
7646
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007647 /*
7648 * The management interface between applications and the fw uses
7649 * MFI frames. E.g, RAID configuration changes, LD property changes
7650 * etc are accomplishes through different kinds of MFI frames. The
7651 * driver needs to care only about substituting user buffers with
7652 * kernel buffers in SGLs. The location of SGL is embedded in the
7653 * struct iocpacket itself.
7654 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007655 if (instance->consistent_mask_64bit)
7656 kern_sge64 = (struct megasas_sge64 *)
7657 ((unsigned long)cmd->frame + ioc->sgl_off);
7658 else
7659 kern_sge32 = (struct megasas_sge32 *)
7660 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007661
7662 /*
7663 * For each user buffer, create a mirror buffer and copy in
7664 */
7665 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01007666 if (!ioc->sgl[i].iov_len)
7667 continue;
7668
Sumant Patro9f35fa82007-02-14 12:55:45 -08007669 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007670 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08007671 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007672 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007673 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7674 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007675 error = -ENOMEM;
7676 goto out;
7677 }
7678
7679 /*
7680 * We don't change the dma_coherent_mask, so
Christoph Hellwig60ee6522018-10-10 19:31:25 +02007681 * dma_alloc_coherent only returns 32bit addresses
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007682 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007683 if (instance->consistent_mask_64bit) {
7684 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7685 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7686 } else {
7687 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7688 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7689 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007690
7691 /*
7692 * We created a kernel buffer corresponding to the
7693 * user buffer. Now copy in from the user buffer
7694 */
7695 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7696 (u32) (ioc->sgl[i].iov_len))) {
7697 error = -EFAULT;
7698 goto out;
7699 }
7700 }
7701
7702 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007703 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7704 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007705 if (!sense) {
7706 error = -ENOMEM;
7707 goto out;
7708 }
7709
7710 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007711 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07007712 if (instance->consistent_mask_64bit)
7713 *sense_ptr = cpu_to_le64(sense_handle);
7714 else
7715 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007716 }
7717
7718 /*
7719 * Set the sync_cmd flag so that the ISR knows not to complete this
7720 * cmd to the SCSI mid-layer
7721 */
7722 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307723 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7724 cmd->sync_cmd = 0;
7725 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07007726 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7727 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7728 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307729 return -EBUSY;
7730 }
7731
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007732 cmd->sync_cmd = 0;
7733
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307734 if (instance->unload == 1) {
7735 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7736 "don't submit data to application\n");
7737 goto out;
7738 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007739 /*
7740 * copy out the kernel buffers to user buffers
7741 */
7742 for (i = 0; i < ioc->sge_count; i++) {
7743 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7744 ioc->sgl[i].iov_len)) {
7745 error = -EFAULT;
7746 goto out;
7747 }
7748 }
7749
7750 /*
7751 * copy out the sense
7752 */
7753 if (ioc->sense_len) {
7754 /*
bo yangb70a41e2008-03-18 03:13:06 -04007755 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007756 * sense buffer address
7757 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007758 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7759 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007760
Shivasharan S318aaef2017-02-10 00:59:22 -08007761 if (copy_to_user((void __user *)((unsigned long)
7762 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007763 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007764 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007765 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007766 error = -EFAULT;
7767 goto out;
7768 }
7769 }
7770
7771 /*
7772 * copy the status codes returned by the fw
7773 */
7774 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7775 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007776 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007777 error = -EFAULT;
7778 }
7779
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007780out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007781 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007782 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007783 sense, sense_handle);
7784 }
7785
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007786 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007787 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07007788 if (instance->consistent_mask_64bit)
7789 dma_free_coherent(&instance->pdev->dev,
7790 le32_to_cpu(kern_sge64[i].length),
7791 kbuff_arr[i],
7792 le64_to_cpu(kern_sge64[i].phys_addr));
7793 else
7794 dma_free_coherent(&instance->pdev->dev,
7795 le32_to_cpu(kern_sge32[i].length),
7796 kbuff_arr[i],
7797 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307798 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007799 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007800 }
7801
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307802 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007803 return error;
7804}
7805
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007806static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7807{
7808 struct megasas_iocpacket __user *user_ioc =
7809 (struct megasas_iocpacket __user *)arg;
7810 struct megasas_iocpacket *ioc;
7811 struct megasas_instance *instance;
7812 int error;
7813
Markus Elfring709ab232016-08-21 10:39:04 +02007814 ioc = memdup_user(user_ioc, sizeof(*ioc));
7815 if (IS_ERR(ioc))
7816 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007817
7818 instance = megasas_lookup_instance(ioc->host_no);
7819 if (!instance) {
7820 error = -ENODEV;
7821 goto out_kfree_ioc;
7822 }
7823
adam radford229fe472014-03-10 02:51:56 -07007824 /* Block ioctls in VF mode */
7825 if (instance->requestorId && !allow_vf_ioctls) {
7826 error = -ENODEV;
7827 goto out_kfree_ioc;
7828 }
7829
Sumit Saxena8a01a412016-01-28 21:04:32 +05307830 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007831 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007832 error = -ENODEV;
7833 goto out_kfree_ioc;
7834 }
7835
7836 if (instance->unload == 1) {
7837 error = -ENODEV;
7838 goto out_kfree_ioc;
7839 }
7840
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007841 if (down_interruptible(&instance->ioctl_sem)) {
7842 error = -ERESTARTSYS;
7843 goto out_kfree_ioc;
7844 }
bo yang39a98552010-09-22 22:36:29 -04007845
Shivasharan S619831f2018-01-05 05:27:43 -08007846 if (megasas_wait_for_adapter_operational(instance)) {
bo yang39a98552010-09-22 22:36:29 -04007847 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007848 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007849 }
bo yang39a98552010-09-22 22:36:29 -04007850
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007851 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007852out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007853 up(&instance->ioctl_sem);
7854
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007855out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007856 kfree(ioc);
7857 return error;
7858}
7859
7860static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7861{
7862 struct megasas_instance *instance;
7863 struct megasas_aen aen;
7864 int error;
7865
7866 if (file->private_data != file) {
7867 printk(KERN_DEBUG "megasas: fasync_helper was not "
7868 "called first\n");
7869 return -EINVAL;
7870 }
7871
7872 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7873 return -EFAULT;
7874
7875 instance = megasas_lookup_instance(aen.host_no);
7876
7877 if (!instance)
7878 return -ENODEV;
7879
Sumit Saxena8a01a412016-01-28 21:04:32 +05307880 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007881 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007882 }
7883
7884 if (instance->unload == 1) {
7885 return -ENODEV;
7886 }
7887
Shivasharan S619831f2018-01-05 05:27:43 -08007888 if (megasas_wait_for_adapter_operational(instance))
bo yang39a98552010-09-22 22:36:29 -04007889 return -ENODEV;
bo yang39a98552010-09-22 22:36:29 -04007890
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307891 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007892 error = megasas_register_aen(instance, aen.seq_num,
7893 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307894 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007895 return error;
7896}
7897
7898/**
7899 * megasas_mgmt_ioctl - char node ioctl entry point
7900 */
7901static long
7902megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7903{
7904 switch (cmd) {
7905 case MEGASAS_IOC_FIRMWARE:
7906 return megasas_mgmt_ioctl_fw(file, arg);
7907
7908 case MEGASAS_IOC_GET_AEN:
7909 return megasas_mgmt_ioctl_aen(file, arg);
7910 }
7911
7912 return -ENOTTY;
7913}
7914
7915#ifdef CONFIG_COMPAT
7916static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7917{
7918 struct compat_megasas_iocpacket __user *cioc =
7919 (struct compat_megasas_iocpacket __user *)arg;
7920 struct megasas_iocpacket __user *ioc =
7921 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7922 int i;
7923 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007924 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307925 u32 local_sense_off;
7926 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307927 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007928
Jeff Garzik83aabc12006-10-04 06:34:03 -04007929 if (clear_user(ioc, sizeof(*ioc)))
7930 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007931
7932 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7933 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7934 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7935 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7936 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7937 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7938 return -EFAULT;
7939
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007940 /*
7941 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7942 * sense_len is not null, so prepare the 64bit value under
7943 * the same condition.
7944 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307945 if (get_user(local_sense_off, &ioc->sense_off) ||
7946 get_user(local_sense_len, &ioc->sense_len) ||
7947 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307948 return -EFAULT;
7949
Wenwen Wang47db7872018-10-06 13:34:21 -05007950 if (local_sense_off != user_sense_off)
7951 return -EINVAL;
7952
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307953 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007954 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307955 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007956 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307957 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007958 if (get_user(ptr, sense_cioc_ptr) ||
7959 put_user(compat_ptr(ptr), sense_ioc_ptr))
7960 return -EFAULT;
7961 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007962
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007963 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007964 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7965 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7966 copy_in_user(&ioc->sgl[i].iov_len,
7967 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7968 return -EFAULT;
7969 }
7970
7971 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7972
7973 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7974 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7975 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7976 return -EFAULT;
7977 }
7978 return error;
7979}
7980
7981static long
7982megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7983 unsigned long arg)
7984{
7985 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007986 case MEGASAS_IOC_FIRMWARE32:
7987 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007988 case MEGASAS_IOC_GET_AEN:
7989 return megasas_mgmt_ioctl_aen(file, arg);
7990 }
7991
7992 return -ENOTTY;
7993}
7994#endif
7995
7996/*
7997 * File operations structure for management interface
7998 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007999static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008000 .owner = THIS_MODULE,
8001 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008002 .fasync = megasas_mgmt_fasync,
8003 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06008004 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008005#ifdef CONFIG_COMPAT
8006 .compat_ioctl = megasas_mgmt_compat_ioctl,
8007#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02008008 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008009};
8010
8011/*
8012 * PCI hotplug support registration structure
8013 */
8014static struct pci_driver megasas_pci_driver = {
8015
8016 .name = "megaraid_sas",
8017 .id_table = megasas_pci_table,
8018 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008019 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05008020 .suspend = megasas_suspend,
8021 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008022 .shutdown = megasas_shutdown,
8023};
8024
8025/*
8026 * Sysfs driver attributes
8027 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008028static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008029{
8030 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8031 MEGASAS_VERSION);
8032}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008033static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008034
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008035static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308036{
8037 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8038 MEGASAS_RELDATE);
8039}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008040static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308041
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008042static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06008043{
8044 return sprintf(buf, "%u\n", support_poll_for_event);
8045}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008046static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008047
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008048static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06008049{
8050 return sprintf(buf, "%u\n", support_device_change);
8051}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008052static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06008053
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008054static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07008055{
bo yangad84db22007-11-09 04:40:16 -05008056 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07008057}
8058
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008059static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8060 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07008061{
8062 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05008063
8064 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07008065 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8066 retval = -EINVAL;
8067 }
8068 return retval;
8069}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02008070static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05008071
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008072static ssize_t
8073support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8074{
8075 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8076}
8077
8078static DRIVER_ATTR_RO(support_nvme_encapsulation);
8079
Shivasharan Sb4a42212017-02-10 00:59:16 -08008080static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8081{
8082 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8083 scsi_remove_device(sdev);
8084 scsi_device_put(sdev);
8085}
8086
Shivasharan S44abbaf2019-01-29 01:38:13 -08008087/**
8088 * megasas_update_device_list - Update the PD and LD device list from FW
8089 * after an AEN event notification
8090 * @instance: Adapter soft state
8091 * @event_type: Indicates type of event (PD or LD event)
8092 *
8093 * @return: Success or failure
8094 *
8095 * Issue DCMDs to Firmware to update the internal device list in driver.
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008096 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8097 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
Shivasharan S44abbaf2019-01-29 01:38:13 -08008098 */
8099static
8100int megasas_update_device_list(struct megasas_instance *instance,
8101 int event_type)
8102{
8103 int dcmd_ret = DCMD_SUCCESS;
8104
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008105 if (instance->enable_fw_dev_list) {
8106 dcmd_ret = megasas_host_device_list_query(instance, false);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008107 if (dcmd_ret != DCMD_SUCCESS)
8108 goto out;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008109 } else {
8110 if (event_type & SCAN_PD_CHANNEL) {
8111 dcmd_ret = megasas_get_pd_list(instance);
Shivasharan S44abbaf2019-01-29 01:38:13 -08008112
Shivasharan S44abbaf2019-01-29 01:38:13 -08008113 if (dcmd_ret != DCMD_SUCCESS)
8114 goto out;
8115 }
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008116
8117 if (event_type & SCAN_VD_CHANNEL) {
8118 if (!instance->requestorId ||
8119 (instance->requestorId &&
8120 megasas_get_ld_vf_affiliation(instance, 0))) {
8121 dcmd_ret = megasas_ld_list_query(instance,
8122 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8123 if (dcmd_ret != DCMD_SUCCESS)
8124 goto out;
8125 }
8126 }
Shivasharan S44abbaf2019-01-29 01:38:13 -08008127 }
8128
8129out:
8130 return dcmd_ret;
8131}
8132
8133/**
8134 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8135 * after an AEN event notification
8136 * @instance: Adapter soft state
8137 * @scan_type: Indicates type of devices (PD/LD) to add
8138 * @return void
8139 */
8140static
8141void megasas_add_remove_devices(struct megasas_instance *instance,
8142 int scan_type)
8143{
8144 int i, j;
8145 u16 pd_index = 0;
8146 u16 ld_index = 0;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008147 u16 channel = 0, id = 0;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008148 struct Scsi_Host *host;
8149 struct scsi_device *sdev1;
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008150 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8151 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008152
8153 host = instance->host;
8154
Shivasharan Sf6fe5732019-01-29 01:38:14 -08008155 if (instance->enable_fw_dev_list) {
8156 targetid_list = instance->host_device_list_buf;
8157 for (i = 0; i < targetid_list->count; i++) {
8158 targetid_entry = &targetid_list->host_device_list[i];
8159 if (targetid_entry->flags.u.bits.is_sys_pd) {
8160 channel = le16_to_cpu(targetid_entry->target_id) /
8161 MEGASAS_MAX_DEV_PER_CHANNEL;
8162 id = le16_to_cpu(targetid_entry->target_id) %
8163 MEGASAS_MAX_DEV_PER_CHANNEL;
8164 } else {
8165 channel = MEGASAS_MAX_PD_CHANNELS +
8166 (le16_to_cpu(targetid_entry->target_id) /
8167 MEGASAS_MAX_DEV_PER_CHANNEL);
8168 id = le16_to_cpu(targetid_entry->target_id) %
8169 MEGASAS_MAX_DEV_PER_CHANNEL;
8170 }
8171 sdev1 = scsi_device_lookup(host, channel, id, 0);
8172 if (!sdev1) {
8173 scsi_add_device(host, channel, id, 0);
8174 } else {
8175 scsi_device_put(sdev1);
8176 }
8177 }
8178 }
8179
Shivasharan S44abbaf2019-01-29 01:38:13 -08008180 if (scan_type & SCAN_PD_CHANNEL) {
8181 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8182 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8183 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8184 sdev1 = scsi_device_lookup(host, i, j, 0);
8185 if (instance->pd_list[pd_index].driveState ==
8186 MR_PD_STATE_SYSTEM) {
8187 if (!sdev1)
8188 scsi_add_device(host, i, j, 0);
8189 else
8190 scsi_device_put(sdev1);
8191 } else {
8192 if (sdev1)
8193 megasas_remove_scsi_device(sdev1);
8194 }
8195 }
8196 }
8197 }
8198
8199 if (scan_type & SCAN_VD_CHANNEL) {
8200 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8201 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8202 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8203 sdev1 = scsi_device_lookup(host,
8204 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8205 if (instance->ld_ids[ld_index] != 0xff) {
8206 if (!sdev1)
8207 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8208 else
8209 scsi_device_put(sdev1);
8210 } else {
8211 if (sdev1)
8212 megasas_remove_scsi_device(sdev1);
8213 }
8214 }
8215 }
8216 }
8217
8218}
8219
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008220static void
8221megasas_aen_polling(struct work_struct *work)
8222{
8223 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08008224 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008225 struct megasas_instance *instance = ev->instance;
8226 union megasas_evt_class_locale class_locale;
Shivasharan S44abbaf2019-01-29 01:38:13 -08008227 int event_type = 0;
adam radford229fe472014-03-10 02:51:56 -07008228 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008229 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308230 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008231
8232 if (!instance) {
8233 printk(KERN_ERR "invalid instance!\n");
8234 kfree(ev);
8235 return;
8236 }
adam radford229fe472014-03-10 02:51:56 -07008237
8238 /* Adjust event workqueue thread wait time for VF mode */
8239 if (instance->requestorId)
8240 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8241
8242 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308243 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07008244
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008245 instance->ev = NULL;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008246 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05308247 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008248
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05308249 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308250
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008251 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308252 case MR_EVT_PD_REMOVED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008253 event_type = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07008254 break;
8255
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308256 case MR_EVT_LD_OFFLINE:
8257 case MR_EVT_CFG_CLEARED:
8258 case MR_EVT_LD_DELETED:
8259 case MR_EVT_LD_CREATED:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008260 event_type = SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308261 break;
8262
8263 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8264 case MR_EVT_FOREIGN_CFG_IMPORTED:
8265 case MR_EVT_LD_STATE_CHANGE:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008266 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308267 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8268 instance->host->host_no);
8269 break;
8270
8271 case MR_EVT_CTRL_PROP_CHANGED:
Shivasharan Sf0c21df2018-10-16 23:37:40 -07008272 dcmd_ret = megasas_get_ctrl_info(instance);
8273 if (dcmd_ret == DCMD_SUCCESS &&
8274 instance->snapdump_wait_time) {
8275 megasas_get_snapdump_properties(instance);
8276 dev_info(&instance->pdev->dev,
8277 "Snap dump wait time\t: %d\n",
8278 instance->snapdump_wait_time);
8279 }
8280 break;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308281 default:
Shivasharan S44abbaf2019-01-29 01:38:13 -08008282 event_type = 0;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308283 break;
8284 }
8285 } else {
8286 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8287 mutex_unlock(&instance->reset_mutex);
8288 kfree(ev);
8289 return;
8290 }
8291
Shivasharan S44abbaf2019-01-29 01:38:13 -08008292 if (event_type)
8293 dcmd_ret = megasas_update_device_list(instance, event_type);
8294
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308295 mutex_unlock(&instance->reset_mutex);
8296
Shivasharan S44abbaf2019-01-29 01:38:13 -08008297 if (event_type && dcmd_ret == DCMD_SUCCESS)
8298 megasas_add_remove_devices(instance, event_type);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008299
Sumit Saxena6d40afb2016-01-28 21:04:23 +05308300 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308301 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8302 else
8303 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008304
8305 /* Register AEN with FW for latest sequence number plus 1 */
8306 class_locale.members.reserved = 0;
8307 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8308 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308309
8310 if (instance->aen_cmd != NULL) {
8311 kfree(ev);
8312 return;
8313 }
8314
8315 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008316 error = megasas_register_aen(instance, seq_num,
8317 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008318 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308319 dev_err(&instance->pdev->dev,
8320 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008321
Sumit Saxena11c71cb2016-01-28 21:04:22 +05308322 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06008323 kfree(ev);
8324}
8325
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008326/**
8327 * megasas_init - Driver load entry point
8328 */
8329static int __init megasas_init(void)
8330{
8331 int rval;
8332
8333 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07008334 * Booted in kdump kernel, minimize memory footprints by
8335 * disabling few features
8336 */
8337 if (reset_devices) {
8338 msix_vectors = 1;
8339 rdpq_enable = 0;
8340 dual_qdepth_disable = 1;
8341 }
8342
8343 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008344 * Announce driver version and other information
8345 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05308346 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008347
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07008348 spin_lock_init(&poll_aen_lock);
8349
Yang, Bo72c4fd32009-10-06 14:20:59 -06008350 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06008351 support_device_change = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008352 support_nvme_encapsulation = true;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008354 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8355
8356 /*
8357 * Register character device node
8358 */
8359 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8360
8361 if (rval < 0) {
8362 printk(KERN_DEBUG "megasas: failed to open device node\n");
8363 return rval;
8364 }
8365
8366 megasas_mgmt_majorno = rval;
8367
8368 /*
8369 * Register ourselves as PCI hotplug module
8370 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00008371 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008372
8373 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09008374 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04008375 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008376 }
8377
Jeff Garzik83aabc12006-10-04 06:34:03 -04008378 rval = driver_create_file(&megasas_pci_driver.driver,
8379 &driver_attr_version);
8380 if (rval)
8381 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06008382
8383 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308384 &driver_attr_release_date);
8385 if (rval)
8386 goto err_dcf_rel_date;
8387
8388 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008389 &driver_attr_support_poll_for_event);
8390 if (rval)
8391 goto err_dcf_support_poll_for_event;
8392
Jeff Garzik83aabc12006-10-04 06:34:03 -04008393 rval = driver_create_file(&megasas_pci_driver.driver,
8394 &driver_attr_dbg_lvl);
8395 if (rval)
8396 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05008397 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008398 &driver_attr_support_device_change);
8399 if (rval)
8400 goto err_dcf_support_device_change;
8401
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008402 rval = driver_create_file(&megasas_pci_driver.driver,
8403 &driver_attr_support_nvme_encapsulation);
8404 if (rval)
8405 goto err_dcf_support_nvme_encapsulation;
8406
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008407 return rval;
bo yangad84db22007-11-09 04:40:16 -05008408
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008409err_dcf_support_nvme_encapsulation:
8410 driver_remove_file(&megasas_pci_driver.driver,
8411 &driver_attr_support_device_change);
8412
Yang, Bo837f5fe2010-10-11 06:59:20 -06008413err_dcf_support_device_change:
8414 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05008415 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008416err_dcf_dbg_lvl:
8417 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06008418 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06008419err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308420 driver_remove_file(&megasas_pci_driver.driver,
8421 &driver_attr_release_date);
8422err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04008423 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8424err_dcf_attr_ver:
8425 pci_unregister_driver(&megasas_pci_driver);
8426err_pcidrv:
8427 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08008428 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008429}
8430
8431/**
8432 * megasas_exit - Driver unload entry point
8433 */
8434static void __exit megasas_exit(void)
8435{
Sumant Patro658dced2006-10-03 13:09:14 -07008436 driver_remove_file(&megasas_pci_driver.driver,
8437 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008438 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06008439 &driver_attr_support_poll_for_event);
8440 driver_remove_file(&megasas_pci_driver.driver,
8441 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05308442 driver_remove_file(&megasas_pci_driver.driver,
8443 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04008444 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Shivasharan Sf870bcb2018-01-05 05:33:04 -08008445 driver_remove_file(&megasas_pci_driver.driver,
8446 &driver_attr_support_nvme_encapsulation);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04008447
8448 pci_unregister_driver(&megasas_pci_driver);
8449 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8450}
8451
8452module_init(megasas_init);
8453module_exit(megasas_exit);