blob: 8a0776d6c6faeb5f7a511bb9209128d6c4efc3da [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
5 * Copyright (c) 2013-2014 Avago Technologies
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006 *
adam radford3f1530c2010-12-14 18:51:48 -08007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040011 *
adam radford3f1530c2010-12-14 18:51:48 -080012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040016 *
adam radford3f1530c2010-12-14 18:51:48 -080017 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040019 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053020 * Authors: Avago Technologies
adam radford3f1530c2010-12-14 18:51:48 -080021 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford3f1530c2010-12-14 18:51:48 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040038#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
43#include <linux/uio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Shivasharan S318aaef2017-02-10 00:59:22 -080046#include <asm/unaligned.h>
Al Viro43399232005-10-04 17:36:04 +010047#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040048#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080049#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010050#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060051#include <linux/poll.h>
Shivasharan Sdef3e8d2017-08-23 04:47:03 -070052#include <linux/vmalloc.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040053
54#include <scsi/scsi.h>
55#include <scsi/scsi_cmnd.h>
56#include <scsi/scsi_device.h>
57#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070058#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080059#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040060#include "megaraid_sas.h"
61
bo yangad84db22007-11-09 04:40:16 -050062/*
Yang, Bo1fd10682010-10-12 07:18:50 -060063 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
65 */
66static unsigned int max_sectors;
67module_param_named(max_sectors, max_sectors, int, 0);
68MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
70
adam radford80d9da92010-12-21 10:17:40 -080071static int msix_disable;
72module_param(msix_disable, int, S_IRUGO);
73MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74
adam radford079eadd2012-10-01 19:26:59 -070075static unsigned int msix_vectors;
76module_param(msix_vectors, int, S_IRUGO);
77MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
78
adam radford229fe472014-03-10 02:51:56 -070079static int allow_vf_ioctls;
80module_param(allow_vf_ioctls, int, S_IRUGO);
81MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
82
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053083static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070084module_param(throttlequeuedepth, int, S_IRUGO);
85MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87
Sumit Saxenae3d178c2016-01-28 21:04:34 +053088unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070089module_param(resetwaittime, int, S_IRUGO);
90MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
91 "before resetting adapter. Default: 180");
92
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053093int smp_affinity_enable = 1;
94module_param(smp_affinity_enable, int, S_IRUGO);
95MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
96
Sumit Saxena179ac142016-01-28 21:04:28 +053097int rdpq_enable = 1;
98module_param(rdpq_enable, int, S_IRUGO);
99MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
100
Sumit Saxena308ec452016-01-28 21:04:30 +0530101unsigned int dual_qdepth_disable;
102module_param(dual_qdepth_disable, int, S_IRUGO);
103MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
104
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530105unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
106module_param(scmd_timeout, int, S_IRUGO);
107MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
108
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400109MODULE_LICENSE("GPL");
110MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530111MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
112MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400113
adam radford058a8fa2011-10-08 18:14:27 -0700114int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400115static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700116static int megasas_ld_list_query(struct megasas_instance *instance,
117 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400118static int megasas_issue_init_mfi(struct megasas_instance *instance);
119static int megasas_register_aen(struct megasas_instance *instance,
120 u32 seq_num, u32 class_locale_word);
Shivasharan S15dd0382017-02-10 00:59:10 -0800121static void megasas_get_pd_info(struct megasas_instance *instance,
122 struct scsi_device *sdev);
Shivasharan S96188a82017-02-10 00:59:11 -0800123static int megasas_get_target_prop(struct megasas_instance *instance,
124 struct scsi_device *sdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400125/*
126 * PCI ID table for all supported controllers
127 */
128static struct pci_device_id megasas_pci_table[] = {
129
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
131 /* xscale IOP */
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
133 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
135 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
137 /* gen2*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
139 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
141 /* skinny*/
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
143 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
145 /* xscale IOP, vega */
146 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
147 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
149 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
151 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
153 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
155 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
157 /* Intruder */
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
159 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500162 /* VENTURA */
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
Shivasharan S754f1ba2017-10-19 02:48:49 -0700164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200169 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400170};
171
172MODULE_DEVICE_TABLE(pci, megasas_pci_table);
173
174static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700175struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400176static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100177static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400178
Yang, Boc3518832009-10-06 14:18:02 -0600179static int megasas_poll_wait_aen;
180static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600181static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800182u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600183static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700184
Yang, Boc3518832009-10-06 14:18:02 -0600185/* define lock for aen poll */
186spinlock_t poll_aen_lock;
187
adam radford9c915a82010-12-21 13:34:31 -0800188void
bo yang7343eb62007-11-09 04:35:44 -0500189megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
190 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800191static u32
192megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
193static int
194megasas_adp_reset_gen2(struct megasas_instance *instance,
195 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800196static irqreturn_t megasas_isr(int irq, void *devp);
197static u32
198megasas_init_adapter_mfi(struct megasas_instance *instance);
199u32
200megasas_build_and_issue_cmd(struct megasas_instance *instance,
201 struct scsi_cmnd *scmd);
202static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800203int
adam radford229fe472014-03-10 02:51:56 -0700204wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
205 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800206void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700207static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
208 int initial);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700209static int
Shivasharan S107a60d2017-10-19 02:49:05 -0700210megasas_set_dma_mask(struct megasas_instance *instance);
Shivasharan Se5d65b42017-10-19 02:48:59 -0700211static int
212megasas_alloc_ctrl_mem(struct megasas_instance *instance);
213static inline void
214megasas_free_ctrl_mem(struct megasas_instance *instance);
215static inline int
216megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
217static inline void
218megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
219static inline void
220megasas_init_ctrl_params(struct megasas_instance *instance);
adam radfordcd50ba82010-12-21 10:23:23 -0800221
Shivasharan S107a60d2017-10-19 02:49:05 -0700222/**
223 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
224 * @instance: Adapter soft state
225 * @dcmd: DCMD frame inside MFI command
226 * @dma_addr: DMA address of buffer to be passed to FW
227 * @dma_len: Length of DMA buffer to be passed to FW
228 * @return: void
229 */
230void megasas_set_dma_settings(struct megasas_instance *instance,
231 struct megasas_dcmd_frame *dcmd,
232 dma_addr_t dma_addr, u32 dma_len)
233{
234 if (instance->consistent_mask_64bit) {
235 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
236 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
237 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
238
239 } else {
240 dcmd->sgl.sge32[0].phys_addr =
241 cpu_to_le32(lower_32_bits(dma_addr));
242 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
243 dcmd->flags = cpu_to_le16(dcmd->flags);
244 }
245}
adam radfordcd50ba82010-12-21 10:23:23 -0800246
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800247void
adam radfordcd50ba82010-12-21 10:23:23 -0800248megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
249{
250 instance->instancet->fire_cmd(instance,
251 cmd->frame_phys_addr, 0, instance->reg_set);
Shivasharan Sf4fc2092017-02-10 00:59:09 -0800252 return;
adam radfordcd50ba82010-12-21 10:23:23 -0800253}
bo yang7343eb62007-11-09 04:35:44 -0500254
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400255/**
256 * megasas_get_cmd - Get a command from the free pool
257 * @instance: Adapter soft state
258 *
259 * Returns a free command from the pool
260 */
adam radford9c915a82010-12-21 13:34:31 -0800261struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400262 *instance)
263{
264 unsigned long flags;
265 struct megasas_cmd *cmd = NULL;
266
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530267 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400268
269 if (!list_empty(&instance->cmd_pool)) {
270 cmd = list_entry((&instance->cmd_pool)->next,
271 struct megasas_cmd, list);
272 list_del_init(&cmd->list);
273 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500274 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400275 }
276
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530277 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400278 return cmd;
279}
280
281/**
282 * megasas_return_cmd - Return a cmd to free command pool
283 * @instance: Adapter soft state
284 * @cmd: Command packet to be returned to free command pool
285 */
Andi Kleen68b43742017-05-08 15:58:53 -0700286void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400287megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
288{
289 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530290 u32 blk_tags;
291 struct megasas_cmd_fusion *cmd_fusion;
292 struct fusion_context *fusion = instance->ctrl_context;
293
294 /* This flag is used only for fusion adapter.
295 * Wait for Interrupt for Polled mode DCMD
296 */
297 if (cmd->flags & DRV_DCMD_POLLED_MODE)
298 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400299
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530300 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400301
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530302 if (fusion) {
303 blk_tags = instance->max_scsi_cmds + cmd->index;
304 cmd_fusion = fusion->cmd_list[blk_tags];
305 megasas_return_cmd_fusion(instance, cmd_fusion);
306 }
307 cmd->scmd = NULL;
308 cmd->frame_count = 0;
309 cmd->flags = 0;
Shivasharan S21c34002017-02-10 00:59:28 -0800310 memset(cmd->frame, 0, instance->mfi_frame_size);
311 cmd->frame->io.context = cpu_to_le32(cmd->index);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530312 if (!fusion && reset_devices)
313 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
314 list_add(&cmd->list, (&instance->cmd_pool)->next);
315
316 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
317
318}
Sumant Patro1341c932006-01-25 12:02:40 -0800319
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530320static const char *
321format_timestamp(uint32_t timestamp)
322{
323 static char buffer[32];
324
325 if ((timestamp & 0xff000000) == 0xff000000)
326 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
327 0x00ffffff);
328 else
329 snprintf(buffer, sizeof(buffer), "%us", timestamp);
330 return buffer;
331}
332
333static const char *
334format_class(int8_t class)
335{
336 static char buffer[6];
337
338 switch (class) {
339 case MFI_EVT_CLASS_DEBUG:
340 return "debug";
341 case MFI_EVT_CLASS_PROGRESS:
342 return "progress";
343 case MFI_EVT_CLASS_INFO:
344 return "info";
345 case MFI_EVT_CLASS_WARNING:
346 return "WARN";
347 case MFI_EVT_CLASS_CRITICAL:
348 return "CRIT";
349 case MFI_EVT_CLASS_FATAL:
350 return "FATAL";
351 case MFI_EVT_CLASS_DEAD:
352 return "DEAD";
353 default:
354 snprintf(buffer, sizeof(buffer), "%d", class);
355 return buffer;
356 }
357}
358
359/**
360 * megasas_decode_evt: Decode FW AEN event and print critical event
361 * for information.
362 * @instance: Adapter soft state
363 */
364static void
365megasas_decode_evt(struct megasas_instance *instance)
366{
367 struct megasas_evt_detail *evt_detail = instance->evt_detail;
368 union megasas_evt_class_locale class_locale;
369 class_locale.word = le32_to_cpu(evt_detail->cl.word);
370
371 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
372 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
373 le32_to_cpu(evt_detail->seq_num),
374 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
375 (class_locale.members.locale),
376 format_class(class_locale.members.class),
377 evt_detail->description);
378}
379
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400380/**
adam radford0d490162010-12-14 19:17:17 -0800381* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800382* (deviceid : 1064R, PERC5) controllers
383*/
384
385/**
386 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400387 * @regs: MFI register set
388 */
389static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530390megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400391{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530392 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500393
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530394 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400395 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400396
397 /* Dummy readl to force pci flush */
398 readl(&regs->outbound_intr_mask);
399}
400
401/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700402 * megasas_disable_intr_xscale -Disables interrupt
403 * @regs: MFI register set
404 */
405static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530406megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700407{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530408 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700409 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500410
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530411 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700412 writel(mask, &regs->outbound_intr_mask);
413 /* Dummy readl to force pci flush */
414 readl(&regs->outbound_intr_mask);
415}
416
417/**
Sumant Patro1341c932006-01-25 12:02:40 -0800418 * megasas_read_fw_status_reg_xscale - returns the current FW status value
419 * @regs: MFI register set
420 */
421static u32
422megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
423{
424 return readl(&(regs)->outbound_msg_0);
425}
426/**
427 * megasas_clear_interrupt_xscale - Check & clear interrupt
428 * @regs: MFI register set
429 */
adam radford0d490162010-12-14 19:17:17 -0800430static int
Sumant Patro1341c932006-01-25 12:02:40 -0800431megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
432{
433 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400434 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500435
Sumant Patro1341c932006-01-25 12:02:40 -0800436 /*
437 * Check if it is our interrupt
438 */
439 status = readl(&regs->outbound_intr_status);
440
bo yang39a98552010-09-22 22:36:29 -0400441 if (status & MFI_OB_INTR_STATUS_MASK)
442 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
443 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
444 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800445
446 /*
447 * Clear the interrupt by writing back the same value
448 */
bo yang39a98552010-09-22 22:36:29 -0400449 if (mfiStatus)
450 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800451
Yang, Bo06f579d2008-08-10 12:42:37 -0700452 /* Dummy readl to force pci flush */
453 readl(&regs->outbound_intr_status);
454
bo yang39a98552010-09-22 22:36:29 -0400455 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800456}
457
458/**
459 * megasas_fire_cmd_xscale - Sends command to the FW
460 * @frame_phys_addr : Physical address of cmd
461 * @frame_count : Number of frames for the command
462 * @regs : MFI register set
463 */
adam radford0d490162010-12-14 19:17:17 -0800464static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600465megasas_fire_cmd_xscale(struct megasas_instance *instance,
466 dma_addr_t frame_phys_addr,
467 u32 frame_count,
468 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800469{
bo yang39a98552010-09-22 22:36:29 -0400470 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500471
bo yang39a98552010-09-22 22:36:29 -0400472 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800473 writel((frame_phys_addr >> 3)|(frame_count),
474 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400475 spin_unlock_irqrestore(&instance->hba_lock, flags);
476}
477
478/**
479 * megasas_adp_reset_xscale - For controller reset
480 * @regs: MFI register set
481 */
482static int
483megasas_adp_reset_xscale(struct megasas_instance *instance,
484 struct megasas_register_set __iomem *regs)
485{
486 u32 i;
487 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500488
bo yang39a98552010-09-22 22:36:29 -0400489 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
490
491 for (i = 0; i < 3; i++)
492 msleep(1000); /* sleep for 3 secs */
493 pcidata = 0;
494 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500495 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400496 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500497 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400498 pcidata &= ~0x2;
499 pci_write_config_dword(instance->pdev,
500 MFI_1068_PCSR_OFFSET, pcidata);
501
502 for (i = 0; i < 2; i++)
503 msleep(1000); /* need to wait 2 secs again */
504
505 pcidata = 0;
506 pci_read_config_dword(instance->pdev,
507 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500508 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400509 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500510 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400511 pcidata = 0;
512 pci_write_config_dword(instance->pdev,
513 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
514 }
515 }
516 return 0;
517}
518
519/**
520 * megasas_check_reset_xscale - For controller reset check
521 * @regs: MFI register set
522 */
523static int
524megasas_check_reset_xscale(struct megasas_instance *instance,
525 struct megasas_register_set __iomem *regs)
526{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530527 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530528 (le32_to_cpu(*instance->consumer) ==
529 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400530 return 1;
bo yang39a98552010-09-22 22:36:29 -0400531 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800532}
533
534static struct megasas_instance_template megasas_instance_template_xscale = {
535
536 .fire_cmd = megasas_fire_cmd_xscale,
537 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700538 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800539 .clear_intr = megasas_clear_intr_xscale,
540 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400541 .adp_reset = megasas_adp_reset_xscale,
542 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800543 .service_isr = megasas_isr,
544 .tasklet = megasas_complete_cmd_dpc,
545 .init_adapter = megasas_init_adapter_mfi,
546 .build_and_issue_cmd = megasas_build_and_issue_cmd,
547 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800548};
549
550/**
adam radford0d490162010-12-14 19:17:17 -0800551* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800552* to xscale (deviceid : 1064R, PERC5) controllers
553*/
554
555/**
adam radford0d490162010-12-14 19:17:17 -0800556* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500557* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800558*/
559
560/**
561 * megasas_enable_intr_ppc - Enables interrupts
562 * @regs: MFI register set
563 */
564static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530565megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800566{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530567 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500568
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530569 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800570 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800571
bo yang39a98552010-09-22 22:36:29 -0400572 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800573
574 /* Dummy readl to force pci flush */
575 readl(&regs->outbound_intr_mask);
576}
577
578/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700579 * megasas_disable_intr_ppc - Disable interrupt
580 * @regs: MFI register set
581 */
582static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530583megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700584{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530585 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700586 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500587
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530588 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700589 writel(mask, &regs->outbound_intr_mask);
590 /* Dummy readl to force pci flush */
591 readl(&regs->outbound_intr_mask);
592}
593
594/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800595 * megasas_read_fw_status_reg_ppc - returns the current FW status value
596 * @regs: MFI register set
597 */
598static u32
599megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
600{
601 return readl(&(regs)->outbound_scratch_pad);
602}
603
604/**
605 * megasas_clear_interrupt_ppc - Check & clear interrupt
606 * @regs: MFI register set
607 */
adam radford0d490162010-12-14 19:17:17 -0800608static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800609megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
610{
adam radford3cc6851f92011-05-11 18:34:52 -0700611 u32 status, mfiStatus = 0;
612
Sumant Patrof9876f02006-02-03 15:34:35 -0800613 /*
614 * Check if it is our interrupt
615 */
616 status = readl(&regs->outbound_intr_status);
617
adam radford3cc6851f92011-05-11 18:34:52 -0700618 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
619 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
620
621 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
622 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800623
624 /*
625 * Clear the interrupt by writing back the same value
626 */
627 writel(status, &regs->outbound_doorbell_clear);
628
Yang, Bo06f579d2008-08-10 12:42:37 -0700629 /* Dummy readl to force pci flush */
630 readl(&regs->outbound_doorbell_clear);
631
adam radford3cc6851f92011-05-11 18:34:52 -0700632 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800633}
adam radford3cc6851f92011-05-11 18:34:52 -0700634
Sumant Patrof9876f02006-02-03 15:34:35 -0800635/**
636 * megasas_fire_cmd_ppc - Sends command to the FW
637 * @frame_phys_addr : Physical address of cmd
638 * @frame_count : Number of frames for the command
639 * @regs : MFI register set
640 */
adam radford0d490162010-12-14 19:17:17 -0800641static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600642megasas_fire_cmd_ppc(struct megasas_instance *instance,
643 dma_addr_t frame_phys_addr,
644 u32 frame_count,
645 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800646{
bo yang39a98552010-09-22 22:36:29 -0400647 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500648
bo yang39a98552010-09-22 22:36:29 -0400649 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800650 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800651 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400652 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800653}
654
bo yang39a98552010-09-22 22:36:29 -0400655/**
bo yang39a98552010-09-22 22:36:29 -0400656 * megasas_check_reset_ppc - For controller reset check
657 * @regs: MFI register set
658 */
659static int
660megasas_check_reset_ppc(struct megasas_instance *instance,
661 struct megasas_register_set __iomem *regs)
662{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530663 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700664 return 1;
665
bo yang39a98552010-09-22 22:36:29 -0400666 return 0;
667}
adam radford3cc6851f92011-05-11 18:34:52 -0700668
Sumant Patrof9876f02006-02-03 15:34:35 -0800669static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800670
Sumant Patrof9876f02006-02-03 15:34:35 -0800671 .fire_cmd = megasas_fire_cmd_ppc,
672 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700673 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800674 .clear_intr = megasas_clear_intr_ppc,
675 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700676 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400677 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800678 .service_isr = megasas_isr,
679 .tasklet = megasas_complete_cmd_dpc,
680 .init_adapter = megasas_init_adapter_mfi,
681 .build_and_issue_cmd = megasas_build_and_issue_cmd,
682 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800683};
684
685/**
Yang, Bo87911122009-10-06 14:31:54 -0600686 * megasas_enable_intr_skinny - Enables interrupts
687 * @regs: MFI register set
688 */
689static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530690megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600691{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530692 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500693
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530694 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600695 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
696
697 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
698
699 /* Dummy readl to force pci flush */
700 readl(&regs->outbound_intr_mask);
701}
702
703/**
704 * megasas_disable_intr_skinny - Disables interrupt
705 * @regs: MFI register set
706 */
707static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530708megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600709{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530710 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600711 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500712
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530713 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600714 writel(mask, &regs->outbound_intr_mask);
715 /* Dummy readl to force pci flush */
716 readl(&regs->outbound_intr_mask);
717}
718
719/**
720 * megasas_read_fw_status_reg_skinny - returns the current FW status value
721 * @regs: MFI register set
722 */
723static u32
724megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
725{
726 return readl(&(regs)->outbound_scratch_pad);
727}
728
729/**
730 * megasas_clear_interrupt_skinny - Check & clear interrupt
731 * @regs: MFI register set
732 */
733static int
734megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
735{
736 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800737 u32 mfiStatus = 0;
738
Yang, Bo87911122009-10-06 14:31:54 -0600739 /*
740 * Check if it is our interrupt
741 */
742 status = readl(&regs->outbound_intr_status);
743
744 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400745 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600746 }
747
748 /*
adam radfordebf054b2011-02-24 20:57:15 -0800749 * Check if it is our interrupt
750 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600751 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800752 MFI_STATE_FAULT) {
753 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
754 } else
755 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
756
757 /*
Yang, Bo87911122009-10-06 14:31:54 -0600758 * Clear the interrupt by writing back the same value
759 */
760 writel(status, &regs->outbound_intr_status);
761
762 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500763 * dummy read to flush PCI
764 */
Yang, Bo87911122009-10-06 14:31:54 -0600765 readl(&regs->outbound_intr_status);
766
adam radfordebf054b2011-02-24 20:57:15 -0800767 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600768}
769
770/**
771 * megasas_fire_cmd_skinny - Sends command to the FW
772 * @frame_phys_addr : Physical address of cmd
773 * @frame_count : Number of frames for the command
774 * @regs : MFI register set
775 */
776static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600777megasas_fire_cmd_skinny(struct megasas_instance *instance,
778 dma_addr_t frame_phys_addr,
779 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600780 struct megasas_register_set __iomem *regs)
781{
Yang, Bo0c79e682009-10-06 14:47:35 -0600782 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500783
bo yang39a98552010-09-22 22:36:29 -0400784 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530785 writel(upper_32_bits(frame_phys_addr),
786 &(regs)->inbound_high_queue_port);
787 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
788 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100789 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400790 spin_unlock_irqrestore(&instance->hba_lock, flags);
791}
792
793/**
bo yang39a98552010-09-22 22:36:29 -0400794 * megasas_check_reset_skinny - For controller reset check
795 * @regs: MFI register set
796 */
797static int
798megasas_check_reset_skinny(struct megasas_instance *instance,
799 struct megasas_register_set __iomem *regs)
800{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530801 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700802 return 1;
803
bo yang39a98552010-09-22 22:36:29 -0400804 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600805}
806
807static struct megasas_instance_template megasas_instance_template_skinny = {
808
809 .fire_cmd = megasas_fire_cmd_skinny,
810 .enable_intr = megasas_enable_intr_skinny,
811 .disable_intr = megasas_disable_intr_skinny,
812 .clear_intr = megasas_clear_intr_skinny,
813 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800814 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400815 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800816 .service_isr = megasas_isr,
817 .tasklet = megasas_complete_cmd_dpc,
818 .init_adapter = megasas_init_adapter_mfi,
819 .build_and_issue_cmd = megasas_build_and_issue_cmd,
820 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600821};
822
823
824/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700825* The following functions are defined for gen2 (deviceid : 0x78 0x79)
826* controllers
827*/
828
829/**
830 * megasas_enable_intr_gen2 - Enables interrupts
831 * @regs: MFI register set
832 */
833static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530834megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700835{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530836 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500837
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530838 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700839 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
840
841 /* write ~0x00000005 (4 & 1) to the intr mask*/
842 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
843
844 /* Dummy readl to force pci flush */
845 readl(&regs->outbound_intr_mask);
846}
847
848/**
849 * megasas_disable_intr_gen2 - Disables interrupt
850 * @regs: MFI register set
851 */
852static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530853megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700854{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530855 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700856 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500857
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530858 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700859 writel(mask, &regs->outbound_intr_mask);
860 /* Dummy readl to force pci flush */
861 readl(&regs->outbound_intr_mask);
862}
863
864/**
865 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
866 * @regs: MFI register set
867 */
868static u32
869megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
870{
871 return readl(&(regs)->outbound_scratch_pad);
872}
873
874/**
875 * megasas_clear_interrupt_gen2 - Check & clear interrupt
876 * @regs: MFI register set
877 */
878static int
879megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
880{
881 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400882 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500883
Yang, Bo6610a6b2008-08-10 12:42:38 -0700884 /*
885 * Check if it is our interrupt
886 */
887 status = readl(&regs->outbound_intr_status);
888
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530889 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400890 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
891 }
892 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
893 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
894 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700895
896 /*
897 * Clear the interrupt by writing back the same value
898 */
bo yang39a98552010-09-22 22:36:29 -0400899 if (mfiStatus)
900 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700901
902 /* Dummy readl to force pci flush */
903 readl(&regs->outbound_intr_status);
904
bo yang39a98552010-09-22 22:36:29 -0400905 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700906}
907/**
908 * megasas_fire_cmd_gen2 - Sends command to the FW
909 * @frame_phys_addr : Physical address of cmd
910 * @frame_count : Number of frames for the command
911 * @regs : MFI register set
912 */
913static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600914megasas_fire_cmd_gen2(struct megasas_instance *instance,
915 dma_addr_t frame_phys_addr,
916 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700917 struct megasas_register_set __iomem *regs)
918{
bo yang39a98552010-09-22 22:36:29 -0400919 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500920
bo yang39a98552010-09-22 22:36:29 -0400921 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700922 writel((frame_phys_addr | (frame_count<<1))|1,
923 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400924 spin_unlock_irqrestore(&instance->hba_lock, flags);
925}
926
927/**
928 * megasas_adp_reset_gen2 - For controller reset
929 * @regs: MFI register set
930 */
931static int
932megasas_adp_reset_gen2(struct megasas_instance *instance,
933 struct megasas_register_set __iomem *reg_set)
934{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500935 u32 retry = 0 ;
936 u32 HostDiag;
937 u32 __iomem *seq_offset = &reg_set->seq_offset;
938 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400939
adam radfordebf054b2011-02-24 20:57:15 -0800940 if (instance->instancet == &megasas_instance_template_skinny) {
941 seq_offset = &reg_set->fusion_seq_offset;
942 hostdiag_offset = &reg_set->fusion_host_diag;
943 }
944
945 writel(0, seq_offset);
946 writel(4, seq_offset);
947 writel(0xb, seq_offset);
948 writel(2, seq_offset);
949 writel(7, seq_offset);
950 writel(0xd, seq_offset);
951
bo yang39a98552010-09-22 22:36:29 -0400952 msleep(1000);
953
adam radfordebf054b2011-02-24 20:57:15 -0800954 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400955
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500956 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400957 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800958 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500959 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400960 retry, HostDiag);
961
962 if (retry++ >= 100)
963 return 1;
964
965 }
966
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500967 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400968
adam radfordebf054b2011-02-24 20:57:15 -0800969 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400970
971 ssleep(10);
972
adam radfordebf054b2011-02-24 20:57:15 -0800973 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500974 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400975 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800976 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500977 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400978 retry, HostDiag);
979
980 if (retry++ >= 1000)
981 return 1;
982
983 }
984 return 0;
985}
986
987/**
988 * megasas_check_reset_gen2 - For controller reset check
989 * @regs: MFI register set
990 */
991static int
992megasas_check_reset_gen2(struct megasas_instance *instance,
993 struct megasas_register_set __iomem *regs)
994{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530995 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600996 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600997
bo yang39a98552010-09-22 22:36:29 -0400998 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700999}
1000
1001static struct megasas_instance_template megasas_instance_template_gen2 = {
1002
1003 .fire_cmd = megasas_fire_cmd_gen2,
1004 .enable_intr = megasas_enable_intr_gen2,
1005 .disable_intr = megasas_disable_intr_gen2,
1006 .clear_intr = megasas_clear_intr_gen2,
1007 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -04001008 .adp_reset = megasas_adp_reset_gen2,
1009 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -08001010 .service_isr = megasas_isr,
1011 .tasklet = megasas_complete_cmd_dpc,
1012 .init_adapter = megasas_init_adapter_mfi,
1013 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1014 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -07001015};
1016
1017/**
Sumant Patrof9876f02006-02-03 15:34:35 -08001018* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -04001019* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -08001020*/
1021
adam radford9c915a82010-12-21 13:34:31 -08001022/*
1023 * Template added for TB (Fusion)
1024 */
1025extern struct megasas_instance_template megasas_instance_template_fusion;
1026
Sumant Patrof9876f02006-02-03 15:34:35 -08001027/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001028 * megasas_issue_polled - Issues a polling command
1029 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -08001030 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001031 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301032 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001033 */
adam radford9c915a82010-12-21 13:34:31 -08001034int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001035megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1036{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001037 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1038
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301039 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301040 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001041
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001042 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301043 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1044 __func__, __LINE__);
1045 return DCMD_NOT_FIRED;
1046 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001048 instance->instancet->issue_dcmd(instance, cmd);
1049
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301050 return wait_and_poll(instance, cmd, instance->requestorId ?
1051 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001052}
1053
1054/**
1055 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1056 * @instance: Adapter soft state
1057 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301058 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001059 *
1060 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001061 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001062 * Used to issue ioctl commands.
1063 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301064int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001065megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301066 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001067{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301068 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301069 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001070
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001071 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301072 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1073 __func__, __LINE__);
1074 return DCMD_NOT_FIRED;
1075 }
1076
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001077 instance->instancet->issue_dcmd(instance, cmd);
1078
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301079 if (timeout) {
1080 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301081 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301082 if (!ret) {
1083 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1084 __func__, __LINE__);
1085 return DCMD_TIMEOUT;
1086 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301087 } else
1088 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301089 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001090
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301091 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301092 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001093}
1094
1095/**
1096 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1097 * @instance: Adapter soft state
1098 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301099 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001100 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301101 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001102 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001103 * cmd and waits for return status.
1104 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001105 */
1106static int
1107megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301108 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001109{
1110 struct megasas_cmd *cmd;
1111 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301112 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001113
1114 cmd = megasas_get_cmd(instance);
1115
1116 if (!cmd)
1117 return -1;
1118
1119 abort_fr = &cmd->frame->abort;
1120
1121 /*
1122 * Prepare and issue the abort frame
1123 */
1124 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301125 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301126 abort_fr->flags = cpu_to_le16(0);
1127 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1128 abort_fr->abort_mfi_phys_addr_lo =
1129 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1130 abort_fr->abort_mfi_phys_addr_hi =
1131 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001132
1133 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301134 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001136 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301137 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1138 __func__, __LINE__);
1139 return DCMD_NOT_FIRED;
1140 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001141
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001142 instance->instancet->issue_dcmd(instance, cmd);
1143
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301144 if (timeout) {
1145 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301146 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301147 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301148 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1149 __func__, __LINE__);
1150 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301151 }
1152 } else
1153 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301154 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301155
bo yang39a98552010-09-22 22:36:29 -04001156 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001157
1158 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301159 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1160 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001161}
1162
1163/**
1164 * megasas_make_sgl32 - Prepares 32-bit SGL
1165 * @instance: Adapter soft state
1166 * @scp: SCSI command from the mid-layer
1167 * @mfi_sgl: SGL to be filled in
1168 *
1169 * If successful, this function returns the number of SG elements. Otherwise,
1170 * it returnes -1.
1171 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001172static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001173megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1174 union megasas_sgl *mfi_sgl)
1175{
1176 int i;
1177 int sge_count;
1178 struct scatterlist *os_sgl;
1179
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001180 sge_count = scsi_dma_map(scp);
1181 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001182
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001183 if (sge_count) {
1184 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301185 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1186 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001187 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001188 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001189 return sge_count;
1190}
1191
1192/**
1193 * megasas_make_sgl64 - Prepares 64-bit SGL
1194 * @instance: Adapter soft state
1195 * @scp: SCSI command from the mid-layer
1196 * @mfi_sgl: SGL to be filled in
1197 *
1198 * If successful, this function returns the number of SG elements. Otherwise,
1199 * it returnes -1.
1200 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001201static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001202megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1203 union megasas_sgl *mfi_sgl)
1204{
1205 int i;
1206 int sge_count;
1207 struct scatterlist *os_sgl;
1208
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001209 sge_count = scsi_dma_map(scp);
1210 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001211
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001212 if (sge_count) {
1213 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301214 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1215 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001216 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001217 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001218 return sge_count;
1219}
1220
Yang, Bof4c9a132009-10-06 14:43:28 -06001221/**
1222 * megasas_make_sgl_skinny - Prepares IEEE SGL
1223 * @instance: Adapter soft state
1224 * @scp: SCSI command from the mid-layer
1225 * @mfi_sgl: SGL to be filled in
1226 *
1227 * If successful, this function returns the number of SG elements. Otherwise,
1228 * it returnes -1.
1229 */
1230static int
1231megasas_make_sgl_skinny(struct megasas_instance *instance,
1232 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1233{
1234 int i;
1235 int sge_count;
1236 struct scatterlist *os_sgl;
1237
1238 sge_count = scsi_dma_map(scp);
1239
1240 if (sge_count) {
1241 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301242 mfi_sgl->sge_skinny[i].length =
1243 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001244 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301245 cpu_to_le64(sg_dma_address(os_sgl));
1246 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001247 }
1248 }
1249 return sge_count;
1250}
1251
Sumant Patrob1df99d2006-10-03 12:40:47 -07001252 /**
1253 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001254 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001255 * @sge_count : number of sg elements
1256 *
1257 * Returns the number of frames required for numnber of sge's (sge_count)
1258 */
1259
Yang, Bof4c9a132009-10-06 14:43:28 -06001260static u32 megasas_get_frame_count(struct megasas_instance *instance,
1261 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001262{
1263 int num_cnt;
1264 int sge_bytes;
1265 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001266 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001267
1268 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1269 sizeof(struct megasas_sge32);
1270
Yang, Bof4c9a132009-10-06 14:43:28 -06001271 if (instance->flag_ieee) {
1272 sge_sz = sizeof(struct megasas_sge_skinny);
1273 }
1274
Sumant Patrob1df99d2006-10-03 12:40:47 -07001275 /*
bo yangd532dbe2008-03-17 03:36:43 -04001276 * Main frame can contain 2 SGEs for 64-bit SGLs and
1277 * 3 SGEs for 32-bit SGLs for ldio &
1278 * 1 SGEs for 64-bit SGLs and
1279 * 2 SGEs for 32-bit SGLs for pthru frame
1280 */
1281 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001282 if (instance->flag_ieee == 1) {
1283 num_cnt = sge_count - 1;
1284 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001285 num_cnt = sge_count - 1;
1286 else
1287 num_cnt = sge_count - 2;
1288 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001289 if (instance->flag_ieee == 1) {
1290 num_cnt = sge_count - 1;
1291 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001292 num_cnt = sge_count - 2;
1293 else
1294 num_cnt = sge_count - 3;
1295 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001296
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001297 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001298 sge_bytes = sge_sz * num_cnt;
1299
1300 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1301 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1302 }
1303 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001304 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001305
1306 if (frame_count > 7)
1307 frame_count = 8;
1308 return frame_count;
1309}
1310
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001311/**
1312 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1313 * @instance: Adapter soft state
1314 * @scp: SCSI command
1315 * @cmd: Command to be prepared in
1316 *
1317 * This function prepares CDB commands. These are typcially pass-through
1318 * commands to the devices.
1319 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001320static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001321megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1322 struct megasas_cmd *cmd)
1323{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001324 u32 is_logical;
1325 u32 device_id;
1326 u16 flags = 0;
1327 struct megasas_pthru_frame *pthru;
1328
Shivasharan S3cabd162017-02-10 00:59:05 -08001329 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301330 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001331 pthru = (struct megasas_pthru_frame *)cmd->frame;
1332
1333 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1334 flags = MFI_FRAME_DIR_WRITE;
1335 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1336 flags = MFI_FRAME_DIR_READ;
1337 else if (scp->sc_data_direction == PCI_DMA_NONE)
1338 flags = MFI_FRAME_DIR_NONE;
1339
Yang, Bof4c9a132009-10-06 14:43:28 -06001340 if (instance->flag_ieee == 1) {
1341 flags |= MFI_FRAME_IEEE;
1342 }
1343
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001344 /*
1345 * Prepare the DCDB frame
1346 */
1347 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1348 pthru->cmd_status = 0x0;
1349 pthru->scsi_status = 0x0;
1350 pthru->target_id = device_id;
1351 pthru->lun = scp->device->lun;
1352 pthru->cdb_len = scp->cmd_len;
1353 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001354 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301355 pthru->flags = cpu_to_le16(flags);
1356 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001357
1358 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1359
1360 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001361 * If the command is for the tape device, set the
1362 * pthru timeout to the os layer timeout value.
1363 */
Yang, Bo8d568252009-10-06 14:12:21 -06001364 if (scp->device->type == TYPE_TAPE) {
1365 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301366 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001367 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301368 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001369 }
1370
1371 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001372 * Construct SGL
1373 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001374 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301375 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001376 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1377 &pthru->sgl);
1378 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301379 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001380 pthru->sge_count = megasas_make_sgl64(instance, scp,
1381 &pthru->sgl);
1382 } else
1383 pthru->sge_count = megasas_make_sgl32(instance, scp,
1384 &pthru->sgl);
1385
Yang, Bobdc6fb82009-12-06 08:30:19 -07001386 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001387 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001388 pthru->sge_count);
1389 return 0;
1390 }
1391
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001392 /*
1393 * Sense info specific
1394 */
1395 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301396 pthru->sense_buf_phys_addr_hi =
1397 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1398 pthru->sense_buf_phys_addr_lo =
1399 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001400
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001401 /*
1402 * Compute the total number of frames this command consumes. FW uses
1403 * this number to pull sufficient number of frames from host memory.
1404 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001405 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001406 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001407
1408 return cmd->frame_count;
1409}
1410
1411/**
1412 * megasas_build_ldio - Prepares IOs to logical devices
1413 * @instance: Adapter soft state
1414 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001415 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001416 *
1417 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1418 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001419static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001420megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1421 struct megasas_cmd *cmd)
1422{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001423 u32 device_id;
1424 u8 sc = scp->cmnd[0];
1425 u16 flags = 0;
1426 struct megasas_io_frame *ldio;
1427
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301428 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001429 ldio = (struct megasas_io_frame *)cmd->frame;
1430
1431 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1432 flags = MFI_FRAME_DIR_WRITE;
1433 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1434 flags = MFI_FRAME_DIR_READ;
1435
Yang, Bof4c9a132009-10-06 14:43:28 -06001436 if (instance->flag_ieee == 1) {
1437 flags |= MFI_FRAME_IEEE;
1438 }
1439
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001440 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001441 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001442 */
1443 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1444 ldio->cmd_status = 0x0;
1445 ldio->scsi_status = 0x0;
1446 ldio->target_id = device_id;
1447 ldio->timeout = 0;
1448 ldio->reserved_0 = 0;
1449 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301450 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001451 ldio->start_lba_hi = 0;
1452 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1453
1454 /*
1455 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1456 */
1457 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301458 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1459 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1460 ((u32) scp->cmnd[2] << 8) |
1461 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001462
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301463 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464 }
1465
1466 /*
1467 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1468 */
1469 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301470 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1471 ((u32) scp->cmnd[7] << 8));
1472 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1473 ((u32) scp->cmnd[3] << 16) |
1474 ((u32) scp->cmnd[4] << 8) |
1475 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001476 }
1477
1478 /*
1479 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1480 */
1481 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301482 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1483 ((u32) scp->cmnd[7] << 16) |
1484 ((u32) scp->cmnd[8] << 8) |
1485 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001486
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301487 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1488 ((u32) scp->cmnd[3] << 16) |
1489 ((u32) scp->cmnd[4] << 8) |
1490 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001491 }
1492
1493 /*
1494 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1495 */
1496 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301497 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1498 ((u32) scp->cmnd[11] << 16) |
1499 ((u32) scp->cmnd[12] << 8) |
1500 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001501
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301502 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1503 ((u32) scp->cmnd[7] << 16) |
1504 ((u32) scp->cmnd[8] << 8) |
1505 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001506
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301507 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1508 ((u32) scp->cmnd[3] << 16) |
1509 ((u32) scp->cmnd[4] << 8) |
1510 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001511
1512 }
1513
1514 /*
1515 * Construct SGL
1516 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001517 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301518 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001519 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1520 &ldio->sgl);
1521 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301522 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001523 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1524 } else
1525 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1526
Yang, Bobdc6fb82009-12-06 08:30:19 -07001527 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001528 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001529 ldio->sge_count);
1530 return 0;
1531 }
1532
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001533 /*
1534 * Sense info specific
1535 */
1536 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1537 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301538 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001539
Sumant Patrob1df99d2006-10-03 12:40:47 -07001540 /*
1541 * Compute the total number of frames this command consumes. FW uses
1542 * this number to pull sufficient number of frames from host memory.
1543 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001544 cmd->frame_count = megasas_get_frame_count(instance,
1545 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001546
1547 return cmd->frame_count;
1548}
1549
1550/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301551 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1552 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001553 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001554 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001555 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301556inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001557{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301558 int ret;
1559
Sumant Patrocb59aa62006-01-25 11:53:25 -08001560 switch (cmd->cmnd[0]) {
1561 case READ_10:
1562 case WRITE_10:
1563 case READ_12:
1564 case WRITE_12:
1565 case READ_6:
1566 case WRITE_6:
1567 case READ_16:
1568 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001569 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301570 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1571 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001572 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001573 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301574 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001575 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301576 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001577}
1578
Sumant Patro658dced2006-10-03 13:09:14 -07001579 /**
1580 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001581 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001582 * @instance: Adapter soft state
1583 */
1584static inline void
1585megasas_dump_pending_frames(struct megasas_instance *instance)
1586{
1587 struct megasas_cmd *cmd;
1588 int i,n;
1589 union megasas_sgl *mfi_sgl;
1590 struct megasas_io_frame *ldio;
1591 struct megasas_pthru_frame *pthru;
1592 u32 sgcount;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001593 u16 max_cmd = instance->max_fw_cmds;
Sumant Patro658dced2006-10-03 13:09:14 -07001594
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001595 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1596 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 -07001597 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001598 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 -07001599 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001600 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 -07001601
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001602 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001603 for (i = 0; i < max_cmd; i++) {
1604 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001605 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001606 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001607 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 +05301608 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001609 ldio = (struct megasas_io_frame *)cmd->frame;
1610 mfi_sgl = &ldio->sgl;
1611 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001612 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 +05301613 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1614 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1615 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1616 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001617 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001618 pthru = (struct megasas_pthru_frame *) cmd->frame;
1619 mfi_sgl = &pthru->sgl;
1620 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001621 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 +05301622 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1623 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1624 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1625 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001626 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001627 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1628 for (n = 0; n < sgcount; n++) {
1629 if (IS_DMA64)
1630 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1631 le32_to_cpu(mfi_sgl->sge64[n].length),
1632 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1633 else
1634 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1635 le32_to_cpu(mfi_sgl->sge32[n].length),
1636 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001637 }
1638 }
Sumant Patro658dced2006-10-03 13:09:14 -07001639 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001640 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001641 for (i = 0; i < max_cmd; i++) {
1642
1643 cmd = instance->cmd_list[i];
1644
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001645 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001646 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001647 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001648 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001649}
1650
adam radfordcd50ba82010-12-21 10:23:23 -08001651u32
1652megasas_build_and_issue_cmd(struct megasas_instance *instance,
1653 struct scsi_cmnd *scmd)
1654{
1655 struct megasas_cmd *cmd;
1656 u32 frame_count;
1657
1658 cmd = megasas_get_cmd(instance);
1659 if (!cmd)
1660 return SCSI_MLQUEUE_HOST_BUSY;
1661
1662 /*
1663 * Logical drive command
1664 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301665 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001666 frame_count = megasas_build_ldio(instance, scmd, cmd);
1667 else
1668 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1669
1670 if (!frame_count)
1671 goto out_return_cmd;
1672
1673 cmd->scmd = scmd;
1674 scmd->SCp.ptr = (char *)cmd;
1675
1676 /*
1677 * Issue the command to the FW
1678 */
1679 atomic_inc(&instance->fw_outstanding);
1680
1681 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1682 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001683
1684 return 0;
1685out_return_cmd:
1686 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301687 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001688}
1689
1690
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001691/**
1692 * megasas_queue_command - Queue entry point
1693 * @scmd: SCSI command to be queued
1694 * @done: Callback entry point
1695 */
1696static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301697megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001698{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001699 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301700 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001701
1702 instance = (struct megasas_instance *)
1703 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001704
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301705 if (instance->unload == 1) {
1706 scmd->result = DID_NO_CONNECT << 16;
1707 scmd->scsi_done(scmd);
1708 return 0;
1709 }
1710
bo yang39a98552010-09-22 22:36:29 -04001711 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001712 return SCSI_MLQUEUE_HOST_BUSY;
1713
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301714
adam radford229fe472014-03-10 02:51:56 -07001715 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301716 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001717 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001718 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001719 return SCSI_MLQUEUE_HOST_BUSY;
1720 } else {
adam radford229fe472014-03-10 02:51:56 -07001721 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301722 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001723 return 0;
1724 }
1725 }
1726
Sumit Saxena8a01a412016-01-28 21:04:32 +05301727 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001728 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301729 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301730 return 0;
1731 }
1732
Sumit Saxena18365b12016-01-28 21:04:25 +05301733 mr_device_priv_data = scmd->device->hostdata;
1734 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301735 scmd->result = DID_NO_CONNECT << 16;
1736 scmd->scsi_done(scmd);
1737 return 0;
1738 }
1739
Sumit Saxena8a01a412016-01-28 21:04:32 +05301740 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001741 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001742
Sumit Saxena8a01a412016-01-28 21:04:32 +05301743 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301744 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301745
bo yang39a98552010-09-22 22:36:29 -04001746
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001747 scmd->result = 0;
1748
Shivasharan S3cabd162017-02-10 00:59:05 -08001749 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301750 (scmd->device->id >= instance->fw_supported_vd_count ||
1751 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001752 scmd->result = DID_BAD_TARGET << 16;
1753 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001754 }
1755
Shivasharan S3cabd162017-02-10 00:59:05 -08001756 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1757 MEGASAS_IS_LOGICAL(scmd->device) &&
1758 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001759 scmd->result = DID_OK << 16;
1760 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001761 }
1762
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301763 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001764
Sumant Patrocb59aa62006-01-25 11:53:25 -08001765 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301766 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001767 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001768}
1769
Yang, Bo044833b2009-10-06 14:33:06 -06001770static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1771{
1772 int i;
1773
1774 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1775
1776 if ((megasas_mgmt_info.instance[i]) &&
1777 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1778 return megasas_mgmt_info.instance[i];
1779 }
1780
1781 return NULL;
1782}
1783
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301784/*
Shivasharan S15dd0382017-02-10 00:59:10 -08001785* megasas_set_dynamic_target_properties -
1786* Device property set by driver may not be static and it is required to be
1787* updated after OCR
1788*
1789* set tm_capable.
1790* set dma alignment (only for eedp protection enable vd).
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301791*
1792* @sdev: OS provided scsi device
1793*
1794* Returns void
1795*/
Shivasharan S15dd0382017-02-10 00:59:10 -08001796void megasas_set_dynamic_target_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301797{
Shivasharan S15dd0382017-02-10 00:59:10 -08001798 u16 pd_index = 0, ld;
1799 u32 device_id;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301800 struct megasas_instance *instance;
1801 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301802 struct MR_PRIV_DEVICE *mr_device_priv_data;
1803 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301804 struct MR_LD_RAID *raid;
1805 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1806
1807 instance = megasas_lookup_instance(sdev->host->host_no);
1808 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301809 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301810
Shivasharan Sed981b82017-02-10 00:59:06 -08001811 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301812 return;
1813
Shivasharan Sed981b82017-02-10 00:59:06 -08001814 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301815 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1816 + sdev->id;
1817 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1818 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001819 if (ld >= instance->fw_supported_vd_count)
1820 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301821 raid = MR_LdRaidGet(ld, local_map_ptr);
1822
1823 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan S15dd0382017-02-10 00:59:10 -08001824 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
Shivasharan Sed981b82017-02-10 00:59:06 -08001825
Sumit Saxena18365b12016-01-28 21:04:25 +05301826 mr_device_priv_data->is_tm_capable =
1827 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001828 } else if (instance->use_seqnum_jbod_fp) {
1829 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
Shivasharan S15dd0382017-02-10 00:59:10 -08001830 sdev->id;
Shivasharan Sed981b82017-02-10 00:59:06 -08001831 pd_sync = (void *)fusion->pd_seq_sync
1832 [(instance->pd_seq_map_id - 1) & 1];
1833 mr_device_priv_data->is_tm_capable =
Shivasharan S15dd0382017-02-10 00:59:10 -08001834 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301835 }
1836}
1837
Shivasharan S15dd0382017-02-10 00:59:10 -08001838/*
1839 * megasas_set_nvme_device_properties -
1840 * set nomerges=2
1841 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1842 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1843 *
1844 * MR firmware provides value in KB. Caller of this function converts
1845 * kb into bytes.
1846 *
1847 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1848 * MR firmware provides value 128 as (32 * 4K) = 128K.
1849 *
1850 * @sdev: scsi device
1851 * @max_io_size: maximum io transfer size
1852 *
1853 */
1854static inline void
1855megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
Sumit Saxena2216c302016-01-28 21:04:26 +05301856{
Sumit Saxena2216c302016-01-28 21:04:26 +05301857 struct megasas_instance *instance;
Shivasharan S15dd0382017-02-10 00:59:10 -08001858 u32 mr_nvme_pg_size;
1859
1860 instance = (struct megasas_instance *)sdev->host->hostdata;
1861 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1862 MR_DEFAULT_NVME_PAGE_SIZE);
1863
1864 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1865
1866 queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1867 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1868}
1869
1870
1871/*
1872 * megasas_set_static_target_properties -
1873 * Device property set by driver are static and it is not required to be
1874 * updated after OCR.
1875 *
1876 * set io timeout
1877 * set device queue depth
1878 * set nvme device properties. see - megasas_set_nvme_device_properties
1879 *
1880 * @sdev: scsi device
Shivasharan S96188a82017-02-10 00:59:11 -08001881 * @is_target_prop true, if fw provided target properties.
Shivasharan S15dd0382017-02-10 00:59:10 -08001882 */
Shivasharan S96188a82017-02-10 00:59:11 -08001883static void megasas_set_static_target_properties(struct scsi_device *sdev,
1884 bool is_target_prop)
Shivasharan S15dd0382017-02-10 00:59:10 -08001885{
1886 u16 target_index = 0;
1887 u8 interface_type;
1888 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1889 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
Shivasharan S96188a82017-02-10 00:59:11 -08001890 u32 tgt_device_qd;
Shivasharan S15dd0382017-02-10 00:59:10 -08001891 struct megasas_instance *instance;
1892 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit Saxena2216c302016-01-28 21:04:26 +05301893
1894 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S15dd0382017-02-10 00:59:10 -08001895 mr_device_priv_data = sdev->hostdata;
1896 interface_type = mr_device_priv_data->interface_type;
Sumit Saxena2216c302016-01-28 21:04:26 +05301897
Shivasharan S15dd0382017-02-10 00:59:10 -08001898 /*
1899 * The RAID firmware may require extended timeouts.
1900 */
1901 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
Sumit Saxena2216c302016-01-28 21:04:26 +05301902
Shivasharan S15dd0382017-02-10 00:59:10 -08001903 target_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05301904
Shivasharan S15dd0382017-02-10 00:59:10 -08001905 switch (interface_type) {
1906 case SAS_PD:
1907 device_qd = MEGASAS_SAS_QD;
1908 break;
1909 case SATA_PD:
1910 device_qd = MEGASAS_SATA_QD;
1911 break;
1912 case NVME_PD:
1913 device_qd = MEGASAS_NVME_QD;
1914 break;
Sumit Saxena2216c302016-01-28 21:04:26 +05301915 }
Shivasharan S15dd0382017-02-10 00:59:10 -08001916
Shivasharan S96188a82017-02-10 00:59:11 -08001917 if (is_target_prop) {
1918 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1919 if (tgt_device_qd &&
1920 (tgt_device_qd <= instance->host->can_queue))
1921 device_qd = tgt_device_qd;
1922
1923 /* max_io_size_kb will be set to non zero for
1924 * nvme based vd and syspd.
1925 */
1926 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1927 }
1928
Shivasharan S15dd0382017-02-10 00:59:10 -08001929 if (instance->nvme_page_size && max_io_size_kb)
1930 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1931
1932 scsi_change_queue_depth(sdev, device_qd);
1933
Sumit Saxena2216c302016-01-28 21:04:26 +05301934}
1935
Sumit Saxena18365b12016-01-28 21:04:25 +05301936
Christoph Hellwig147aab62006-02-17 12:13:48 +01001937static int megasas_slave_configure(struct scsi_device *sdev)
1938{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301939 u16 pd_index = 0;
1940 struct megasas_instance *instance;
Shivasharan S96188a82017-02-10 00:59:11 -08001941 int ret_target_prop = DCMD_FAILED;
1942 bool is_target_prop = false;
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301943
1944 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001945 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001946 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301947 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1948 sdev->id;
1949 if (instance->pd_list[pd_index].driveState !=
1950 MR_PD_STATE_SYSTEM)
1951 return -ENXIO;
1952 }
1953 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301954
Shivasharan S15dd0382017-02-10 00:59:10 -08001955 mutex_lock(&instance->hba_mutex);
1956 /* Send DCMD to Firmware and cache the information */
1957 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
1958 megasas_get_pd_info(instance, sdev);
1959
Shivasharan S96188a82017-02-10 00:59:11 -08001960 /* Some ventura firmware may not have instance->nvme_page_size set.
1961 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
1962 */
1963 if ((instance->tgt_prop) && (instance->nvme_page_size))
1964 ret_target_prop = megasas_get_target_prop(instance, sdev);
1965
1966 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
1967 megasas_set_static_target_properties(sdev, is_target_prop);
Shivasharan S15dd0382017-02-10 00:59:10 -08001968
1969 mutex_unlock(&instance->hba_mutex);
1970
1971 /* This sdev property may change post OCR */
1972 megasas_set_dynamic_target_properties(sdev);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301973
Yang, Bo044833b2009-10-06 14:33:06 -06001974 return 0;
1975}
1976
1977static int megasas_slave_alloc(struct scsi_device *sdev)
1978{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001979 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001980 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301981 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001982
Yang, Bo044833b2009-10-06 14:33:06 -06001983 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08001984 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001985 /*
1986 * Open the OS scan to the SYSTEM PD
1987 */
1988 pd_index =
1989 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1990 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001991 if ((instance->pd_list_not_supported ||
1992 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301993 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301994 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001995 }
1996 return -ENXIO;
1997 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301998
1999scan_target:
2000 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2001 GFP_KERNEL);
2002 if (!mr_device_priv_data)
2003 return -ENOMEM;
2004 sdev->hostdata = mr_device_priv_data;
Shivasharan S49524b32017-03-10 03:22:13 -08002005
2006 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2007 instance->r1_ldio_hint_default);
Christoph Hellwig147aab62006-02-17 12:13:48 +01002008 return 0;
2009}
2010
Sumit Saxena18365b12016-01-28 21:04:25 +05302011static void megasas_slave_destroy(struct scsi_device *sdev)
2012{
2013 kfree(sdev->hostdata);
2014 sdev->hostdata = NULL;
2015}
2016
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302017/*
2018* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2019* kill adapter
2020* @instance: Adapter soft state
2021*
2022*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05302023static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302024{
2025 int i;
2026 struct megasas_cmd *cmd_mfi;
2027 struct megasas_cmd_fusion *cmd_fusion;
2028 struct fusion_context *fusion = instance->ctrl_context;
2029
2030 /* Find all outstanding ioctls */
2031 if (fusion) {
2032 for (i = 0; i < instance->max_fw_cmds; i++) {
2033 cmd_fusion = fusion->cmd_list[i];
2034 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2035 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2036 if (cmd_mfi->sync_cmd &&
Shivasharan Seb3fe262017-08-23 04:47:04 -07002037 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2038 cmd_mfi->frame->hdr.cmd_status =
2039 MFI_STAT_WRONG_STATE;
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302040 megasas_complete_cmd(instance,
2041 cmd_mfi, DID_OK);
Shivasharan Seb3fe262017-08-23 04:47:04 -07002042 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302043 }
2044 }
2045 } else {
2046 for (i = 0; i < instance->max_fw_cmds; i++) {
2047 cmd_mfi = instance->cmd_list[i];
2048 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2049 MFI_CMD_ABORT)
2050 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2051 }
2052 }
2053}
2054
2055
adam radford9c915a82010-12-21 13:34:31 -08002056void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04002057{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302058 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302059 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302060 /* Wait 1 second to ensure IO or ioctls in build have posted */
2061 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04002062 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302063 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07002064 (instance->adapter_type != MFI_SERIES)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002065 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07002066 /* Flush */
2067 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05302068 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07002069 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04002070 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302071 writel(MFI_STOP_ADP,
2072 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08002073 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05302074 /* Complete outstanding ioctls when adapter is killed */
2075 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08002076}
2077
2078 /**
2079 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2080 * restored to max value
2081 * @instance: Adapter soft state
2082 *
2083 */
2084void
2085megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2086{
2087 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05302088
adam radford9c915a82010-12-21 13:34:31 -08002089 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07002090 && time_after(jiffies, instance->last_time + 5 * HZ)
2091 && atomic_read(&instance->fw_outstanding) <
2092 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08002093
2094 spin_lock_irqsave(instance->host->host_lock, flags);
2095 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002096
Sumit Saxena308ec452016-01-28 21:04:30 +05302097 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08002098 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04002099 }
2100}
2101
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002102/**
bo yang7343eb62007-11-09 04:35:44 -05002103 * megasas_complete_cmd_dpc - Returns FW's controller structure
2104 * @instance_addr: Address of adapter soft state
2105 *
2106 * Tasklet to complete cmds
2107 */
2108static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2109{
2110 u32 producer;
2111 u32 consumer;
2112 u32 context;
2113 struct megasas_cmd *cmd;
2114 struct megasas_instance *instance =
2115 (struct megasas_instance *)instance_addr;
2116 unsigned long flags;
2117
2118 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05302119 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05002120 return;
2121
2122 spin_lock_irqsave(&instance->completion_lock, flags);
2123
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302124 producer = le32_to_cpu(*instance->producer);
2125 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05002126
2127 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302128 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04002129 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002130 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002131 context);
2132 BUG();
2133 }
bo yang7343eb62007-11-09 04:35:44 -05002134
2135 cmd = instance->cmd_list[context];
2136
2137 megasas_complete_cmd(instance, cmd, DID_OK);
2138
2139 consumer++;
2140 if (consumer == (instance->max_fw_cmds + 1)) {
2141 consumer = 0;
2142 }
2143 }
2144
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302145 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002146
2147 spin_unlock_irqrestore(&instance->completion_lock, flags);
2148
2149 /*
2150 * Check if we can restore can_queue
2151 */
adam radford9c915a82010-12-21 13:34:31 -08002152 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002153}
2154
Kees Cookc251a7b2017-10-22 15:30:04 -07002155static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2156
adam radford229fe472014-03-10 02:51:56 -07002157/**
Kees Cookc251a7b2017-10-22 15:30:04 -07002158 * megasas_start_timer - Initializes sriov heartbeat timer object
adam radford229fe472014-03-10 02:51:56 -07002159 * @instance: Adapter soft state
adam radford229fe472014-03-10 02:51:56 -07002160 *
2161 */
Kees Cookc251a7b2017-10-22 15:30:04 -07002162void megasas_start_timer(struct megasas_instance *instance)
adam radford229fe472014-03-10 02:51:56 -07002163{
Kees Cookc251a7b2017-10-22 15:30:04 -07002164 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2165
2166 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2167 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
adam radford229fe472014-03-10 02:51:56 -07002168 add_timer(timer);
2169}
2170
Yang, Bo707e09b2010-10-12 07:20:27 -06002171static void
2172megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2173
2174static void
2175process_fw_state_change_wq(struct work_struct *work);
2176
2177void megasas_do_ocr(struct megasas_instance *instance)
2178{
2179 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2180 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2181 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302182 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002183 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302184 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302185 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002186 instance->issuepend_done = 0;
2187
2188 atomic_set(&instance->fw_outstanding, 0);
2189 megasas_internal_reset_defer_cmds(instance);
2190 process_fw_state_change_wq(&instance->work_init);
2191}
2192
Adam Radford4cbfea82014-07-09 15:17:56 -07002193static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2194 int initial)
adam radford229fe472014-03-10 02:51:56 -07002195{
2196 struct megasas_cmd *cmd;
2197 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002198 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002199 dma_addr_t new_affiliation_111_h;
2200 int ld, retval = 0;
2201 u8 thisVf;
2202
2203 cmd = megasas_get_cmd(instance);
2204
2205 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002206 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2207 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002208 instance->host->host_no);
2209 return -ENOMEM;
2210 }
2211
2212 dcmd = &cmd->frame->dcmd;
2213
Adam Radford4cbfea82014-07-09 15:17:56 -07002214 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002215 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2216 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002217 megasas_return_cmd(instance, cmd);
2218 return -ENOMEM;
2219 }
2220
2221 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002222 memset(instance->vf_affiliation_111, 0,
2223 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002224 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002225 new_affiliation_111 =
2226 pci_alloc_consistent(instance->pdev,
2227 sizeof(struct MR_LD_VF_AFFILIATION_111),
2228 &new_affiliation_111_h);
2229 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002230 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2231 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002232 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002233 megasas_return_cmd(instance, cmd);
2234 return -ENOMEM;
2235 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002236 memset(new_affiliation_111, 0,
2237 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002238 }
2239
2240 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2241
2242 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302243 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002244 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302245 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002246 dcmd->timeout = 0;
2247 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302248 dcmd->data_xfer_len =
2249 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2250 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002251
Adam Radford4cbfea82014-07-09 15:17:56 -07002252 if (initial)
2253 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302254 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002255 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302256 dcmd->sgl.sge32[0].phys_addr =
2257 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002258
Christoph Hellwig2213a462015-04-23 16:33:54 +05302259 dcmd->sgl.sge32[0].length = cpu_to_le32(
2260 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002261
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002262 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002263 "scsi%d\n", instance->host->host_no);
2264
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302265 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002266 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2267 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002268 dcmd->cmd_status, instance->host->host_no);
2269 retval = 1; /* Do a scan if we couldn't get affiliation */
2270 goto out;
2271 }
2272
2273 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002274 thisVf = new_affiliation_111->thisVf;
2275 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2276 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2277 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002278 dev_warn(&instance->pdev->dev, "SR-IOV: "
2279 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002280 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002281 memcpy(instance->vf_affiliation_111,
2282 new_affiliation_111,
2283 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002284 retval = 1;
2285 goto out;
2286 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002287 }
2288out:
2289 if (new_affiliation_111) {
2290 pci_free_consistent(instance->pdev,
2291 sizeof(struct MR_LD_VF_AFFILIATION_111),
2292 new_affiliation_111,
2293 new_affiliation_111_h);
2294 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302295
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302296 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002297
2298 return retval;
2299}
2300
2301static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2302 int initial)
2303{
2304 struct megasas_cmd *cmd;
2305 struct megasas_dcmd_frame *dcmd;
2306 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2307 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2308 dma_addr_t new_affiliation_h;
2309 int i, j, retval = 0, found = 0, doscan = 0;
2310 u8 thisVf;
2311
2312 cmd = megasas_get_cmd(instance);
2313
2314 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002315 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2316 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002317 instance->host->host_no);
2318 return -ENOMEM;
2319 }
2320
2321 dcmd = &cmd->frame->dcmd;
2322
2323 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002324 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2325 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002326 megasas_return_cmd(instance, cmd);
2327 return -ENOMEM;
2328 }
2329
2330 if (initial)
2331 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2332 sizeof(struct MR_LD_VF_AFFILIATION));
2333 else {
2334 new_affiliation =
2335 pci_alloc_consistent(instance->pdev,
2336 (MAX_LOGICAL_DRIVES + 1) *
2337 sizeof(struct MR_LD_VF_AFFILIATION),
2338 &new_affiliation_h);
2339 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002340 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2341 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002342 instance->host->host_no);
2343 megasas_return_cmd(instance, cmd);
2344 return -ENOMEM;
2345 }
2346 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2347 sizeof(struct MR_LD_VF_AFFILIATION));
2348 }
2349
2350 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2351
2352 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302353 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002354 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302355 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002356 dcmd->timeout = 0;
2357 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302358 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2359 sizeof(struct MR_LD_VF_AFFILIATION));
2360 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002361
2362 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302363 dcmd->sgl.sge32[0].phys_addr =
2364 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002365 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302366 dcmd->sgl.sge32[0].phys_addr =
2367 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002368
Christoph Hellwig2213a462015-04-23 16:33:54 +05302369 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2370 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002371
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002372 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002373 "scsi%d\n", instance->host->host_no);
2374
Adam Radford4cbfea82014-07-09 15:17:56 -07002375
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302376 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002377 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2378 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002379 dcmd->cmd_status, instance->host->host_no);
2380 retval = 1; /* Do a scan if we couldn't get affiliation */
2381 goto out;
2382 }
2383
2384 if (!initial) {
2385 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002386 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2387 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002388 instance->host->host_no);
2389 retval = 1;
2390 goto out;
2391 }
2392 newmap = new_affiliation->map;
2393 savedmap = instance->vf_affiliation->map;
2394 thisVf = new_affiliation->thisVf;
2395 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2396 found = 0;
2397 for (j = 0; j < instance->vf_affiliation->ldCount;
2398 j++) {
2399 if (newmap->ref.targetId ==
2400 savedmap->ref.targetId) {
2401 found = 1;
2402 if (newmap->policy[thisVf] !=
2403 savedmap->policy[thisVf]) {
2404 doscan = 1;
2405 goto out;
2406 }
adam radford229fe472014-03-10 02:51:56 -07002407 }
2408 savedmap = (struct MR_LD_VF_MAP *)
2409 ((unsigned char *)savedmap +
2410 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002411 }
2412 if (!found && newmap->policy[thisVf] !=
2413 MR_LD_ACCESS_HIDDEN) {
2414 doscan = 1;
2415 goto out;
2416 }
2417 newmap = (struct MR_LD_VF_MAP *)
2418 ((unsigned char *)newmap + newmap->size);
2419 }
2420
2421 newmap = new_affiliation->map;
2422 savedmap = instance->vf_affiliation->map;
2423
2424 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2425 found = 0;
2426 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2427 if (savedmap->ref.targetId ==
2428 newmap->ref.targetId) {
2429 found = 1;
2430 if (savedmap->policy[thisVf] !=
2431 newmap->policy[thisVf]) {
2432 doscan = 1;
2433 goto out;
2434 }
2435 }
adam radford229fe472014-03-10 02:51:56 -07002436 newmap = (struct MR_LD_VF_MAP *)
2437 ((unsigned char *)newmap +
2438 newmap->size);
2439 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002440 if (!found && savedmap->policy[thisVf] !=
2441 MR_LD_ACCESS_HIDDEN) {
2442 doscan = 1;
2443 goto out;
2444 }
2445 savedmap = (struct MR_LD_VF_MAP *)
2446 ((unsigned char *)savedmap +
2447 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002448 }
2449 }
2450out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002451 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002452 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2453 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002454 memcpy(instance->vf_affiliation, new_affiliation,
2455 new_affiliation->size);
2456 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002457 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002458
2459 if (new_affiliation)
2460 pci_free_consistent(instance->pdev,
2461 (MAX_LOGICAL_DRIVES + 1) *
2462 sizeof(struct MR_LD_VF_AFFILIATION),
2463 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302464 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002465
2466 return retval;
2467}
2468
Adam Radford4cbfea82014-07-09 15:17:56 -07002469/* This function will get the current SR-IOV LD/VF affiliation */
2470static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2471 int initial)
2472{
2473 int retval;
2474
2475 if (instance->PlasmaFW111)
2476 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2477 else
2478 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2479 return retval;
2480}
2481
adam radford229fe472014-03-10 02:51:56 -07002482/* This function will tell FW to start the SR-IOV heartbeat */
2483int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2484 int initial)
2485{
2486 struct megasas_cmd *cmd;
2487 struct megasas_dcmd_frame *dcmd;
2488 int retval = 0;
2489
2490 cmd = megasas_get_cmd(instance);
2491
2492 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002493 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2494 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002495 instance->host->host_no);
2496 return -ENOMEM;
2497 }
2498
2499 dcmd = &cmd->frame->dcmd;
2500
2501 if (initial) {
2502 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002503 pci_zalloc_consistent(instance->pdev,
2504 sizeof(struct MR_CTRL_HB_HOST_MEM),
2505 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002506 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002507 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2508 " memory for heartbeat host memory for scsi%d\n",
2509 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002510 retval = -ENOMEM;
2511 goto out;
2512 }
adam radford229fe472014-03-10 02:51:56 -07002513 }
2514
2515 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2516
Christoph Hellwig2213a462015-04-23 16:33:54 +05302517 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002518 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302519 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002520 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302521 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002522 dcmd->timeout = 0;
2523 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302524 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2525 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
Shivasharan S107a60d2017-10-19 02:49:05 -07002526
2527 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2528 sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002529
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002530 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002531 instance->host->host_no);
2532
Shivasharan Se7d36b82017-10-19 02:48:50 -07002533 if ((instance->adapter_type != MFI_SERIES) &&
2534 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302535 retval = megasas_issue_blocked_cmd(instance, cmd,
2536 MEGASAS_ROUTINE_WAIT_TIME_VF);
2537 else
2538 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002539
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302540 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302541 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2542 "_MEM_ALLOC DCMD %s for scsi%d\n",
2543 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2544 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002545 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002546 }
2547
2548out:
2549 megasas_return_cmd(instance, cmd);
2550
2551 return retval;
2552}
2553
2554/* Handler for SR-IOV heartbeat */
Kees Cookc251a7b2017-10-22 15:30:04 -07002555static void megasas_sriov_heartbeat_handler(struct timer_list *t)
adam radford229fe472014-03-10 02:51:56 -07002556{
2557 struct megasas_instance *instance =
Kees Cookc251a7b2017-10-22 15:30:04 -07002558 from_timer(instance, t, sriov_heartbeat_timer);
adam radford229fe472014-03-10 02:51:56 -07002559
2560 if (instance->hb_host_mem->HB.fwCounter !=
2561 instance->hb_host_mem->HB.driverCounter) {
2562 instance->hb_host_mem->HB.driverCounter =
2563 instance->hb_host_mem->HB.fwCounter;
2564 mod_timer(&instance->sriov_heartbeat_timer,
2565 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2566 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002567 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002568 "completed for scsi%d\n", instance->host->host_no);
2569 schedule_work(&instance->work_init);
2570 }
2571}
2572
bo yang7343eb62007-11-09 04:35:44 -05002573/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002574 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2575 * @instance: Adapter soft state
2576 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002577 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002578 * complete all its outstanding commands. Returns error if one or more IOs
2579 * are pending after this time period. It also marks the controller dead.
2580 */
2581static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2582{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302583 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002584 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002585 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002586 unsigned long flags;
2587 struct list_head clist_local;
2588 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002589 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002590
Sumit Saxenaccc75072016-01-28 21:04:33 +05302591 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2592 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2593 __func__, __LINE__);
2594 return FAILED;
2595 }
bo yang39a98552010-09-22 22:36:29 -04002596
Sumit Saxena8a01a412016-01-28 21:04:32 +05302597 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002598
2599 INIT_LIST_HEAD(&clist_local);
2600 spin_lock_irqsave(&instance->hba_lock, flags);
2601 list_splice_init(&instance->internal_reset_pending_q,
2602 &clist_local);
2603 spin_unlock_irqrestore(&instance->hba_lock, flags);
2604
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002605 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002606 for (i = 0; i < wait_time; i++) {
2607 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302608 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002609 break;
2610 }
2611
Sumit Saxena8a01a412016-01-28 21:04:32 +05302612 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002613 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302614 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002615 return FAILED;
2616 }
2617
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002618 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002619 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002620 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002621 struct megasas_cmd, list);
2622 list_del_init(&reset_cmd->list);
2623 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002624 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002625 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002626 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002627 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002628
2629 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2630 megasas_return_cmd(instance, reset_cmd);
2631 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002632 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002633 "reset queue\n",
2634 reset_cmd);
2635
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302636 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002637 instance->instancet->fire_cmd(instance,
2638 reset_cmd->frame_phys_addr,
2639 0, instance->reg_set);
2640 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002641 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002642 "cmds lst\n",
2643 reset_cmd);
2644 }
2645 reset_index++;
2646 }
2647
2648 return SUCCESS;
2649 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002650
adam radfordc007b8b2012-07-17 18:20:24 -07002651 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302652 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002653
2654 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002655 break;
2656
2657 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002658 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002659 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002660 /*
2661 * Call cmd completion routine. Cmd to be
2662 * be completed directly without depending on isr.
2663 */
2664 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002665 }
2666
2667 msleep(1000);
2668 }
2669
Yang, Bo707e09b2010-10-12 07:20:27 -06002670 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302671 outstanding = atomic_read(&instance->fw_outstanding);
2672 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002673
Sumit Saxenaccc75072016-01-28 21:04:33 +05302674 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2675 goto no_outstanding;
2676
2677 if (instance->disableOnlineCtrlReset)
2678 goto kill_hba_and_failed;
2679 do {
2680 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2681 dev_info(&instance->pdev->dev,
2682 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2683 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2684 if (i == 3)
2685 goto kill_hba_and_failed;
2686 megasas_do_ocr(instance);
2687
2688 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2689 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2690 __func__, __LINE__);
2691 return FAILED;
2692 }
2693 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2694 __func__, __LINE__);
2695
2696 for (sl = 0; sl < 10; sl++)
2697 msleep(500);
2698
2699 outstanding = atomic_read(&instance->fw_outstanding);
2700
2701 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2702 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2703 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002704 }
2705 i++;
2706 } while (i <= 3);
2707
Sumit Saxenaccc75072016-01-28 21:04:33 +05302708no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002709
Sumit Saxenaccc75072016-01-28 21:04:33 +05302710 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2711 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002712 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302713
2714kill_hba_and_failed:
2715
2716 /* Reset not supported, kill adapter */
2717 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2718 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2719 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2720 atomic_read(&instance->fw_outstanding));
2721 megasas_dump_pending_frames(instance);
2722 megaraid_sas_kill_hba(instance);
2723
2724 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002725}
2726
2727/**
2728 * megasas_generic_reset - Generic reset routine
2729 * @scmd: Mid-layer SCSI command
2730 *
2731 * This routine implements a generic reset handler for device, bus and host
2732 * reset requests. Device, bus and host specific reset handlers can use this
2733 * function after they do their specific tasks.
2734 */
2735static int megasas_generic_reset(struct scsi_cmnd *scmd)
2736{
2737 int ret_val;
2738 struct megasas_instance *instance;
2739
2740 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2741
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002742 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2743 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002744
Sumit Saxena8a01a412016-01-28 21:04:32 +05302745 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002746 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002747 return FAILED;
2748 }
2749
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002750 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002751 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002752 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002753 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002754 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002755
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002756 return ret_val;
2757}
2758
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002759/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002760 * megasas_reset_timer - quiesce the adapter if required
2761 * @scmd: scsi cmnd
2762 *
2763 * Sets the FW busy flag and reduces the host->can_queue if the
2764 * cmd has not been completed within the timeout period.
2765 */
2766static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002767blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002768{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002769 struct megasas_instance *instance;
2770 unsigned long flags;
2771
2772 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302773 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002774 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002775 }
2776
adam radfordf575c5d2011-10-13 16:01:12 -07002777 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002778 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2779 /* FW is busy, throttle IO */
2780 spin_lock_irqsave(instance->host->host_lock, flags);
2781
adam radfordc5daa6a2012-07-17 18:20:03 -07002782 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002783 instance->last_time = jiffies;
2784 instance->flag |= MEGASAS_FW_BUSY;
2785
2786 spin_unlock_irqrestore(instance->host->host_lock, flags);
2787 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002788 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002789}
2790
2791/**
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002792 * megasas_dump_frame - This function will dump MPT/MFI frame
2793 */
2794static inline void
2795megasas_dump_frame(void *mpi_request, int sz)
2796{
2797 int i;
2798 __le32 *mfp = (__le32 *)mpi_request;
2799
2800 printk(KERN_INFO "IO request frame:\n\t");
Dan Carpenter40a4c2c2017-02-14 19:38:55 +03002801 for (i = 0; i < sz / sizeof(__le32); i++) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002802 if (i && ((i % 8) == 0))
2803 printk("\n\t");
2804 printk("%08x ", le32_to_cpu(mfp[i]));
2805 }
2806 printk("\n");
2807}
2808
2809/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002810 * megasas_reset_bus_host - Bus & host reset handler entry point
2811 */
2812static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2813{
2814 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002815 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002816
adam radford9c915a82010-12-21 13:34:31 -08002817 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002818
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002819 scmd_printk(KERN_INFO, scmd,
2820 "Controller reset is requested due to IO timeout\n"
2821 "SCSI command pointer: (%p)\t SCSI host state: %d\t"
2822 " SCSI host busy: %d\t FW outstanding: %d\n",
2823 scmd, scmd->device->host->shost_state,
2824 atomic_read((atomic_t *)&scmd->device->host->host_busy),
2825 atomic_read(&instance->fw_outstanding));
2826
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002827 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002828 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002829 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07002830 if (instance->adapter_type == MFI_SERIES) {
2831 ret = megasas_generic_reset(scmd);
2832 } else {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002833 struct megasas_cmd_fusion *cmd;
2834 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2835 if (cmd)
2836 megasas_dump_frame(cmd->io_request,
Shivasharan S5dd977e2017-08-23 04:47:07 -07002837 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08002838 ret = megasas_reset_fusion(scmd->device->host,
2839 SCSIIO_TIMEOUT_OCR);
Shivasharan Se7d36b82017-10-19 02:48:50 -07002840 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002841
2842 return ret;
2843}
2844
2845/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002846 * megasas_task_abort - Issues task abort request to firmware
2847 * (supported only for fusion adapters)
2848 * @scmd: SCSI command pointer
2849 */
2850static int megasas_task_abort(struct scsi_cmnd *scmd)
2851{
2852 int ret;
2853 struct megasas_instance *instance;
2854
2855 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2856
Shivasharan Se7d36b82017-10-19 02:48:50 -07002857 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002858 ret = megasas_task_abort_fusion(scmd);
2859 else {
2860 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2861 ret = FAILED;
2862 }
2863
2864 return ret;
2865}
2866
2867/**
2868 * megasas_reset_target: Issues target reset request to firmware
2869 * (supported only for fusion adapters)
2870 * @scmd: SCSI command pointer
2871 */
2872static int megasas_reset_target(struct scsi_cmnd *scmd)
2873{
2874 int ret;
2875 struct megasas_instance *instance;
2876
2877 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2878
Shivasharan Se7d36b82017-10-19 02:48:50 -07002879 if (instance->adapter_type != MFI_SERIES)
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002880 ret = megasas_reset_target_fusion(scmd);
2881 else {
2882 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2883 ret = FAILED;
2884 }
2885
2886 return ret;
2887}
2888
2889/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002890 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002891 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002892 * @bdev: block device
2893 * @capacity: drive capacity
2894 * @geom: geometry parameters
2895 */
2896static int
2897megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2898 sector_t capacity, int geom[])
2899{
2900 int heads;
2901 int sectors;
2902 sector_t cylinders;
2903 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002904
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002905 /* Default heads (64) & sectors (32) */
2906 heads = 64;
2907 sectors = 32;
2908
2909 tmp = heads * sectors;
2910 cylinders = capacity;
2911
2912 sector_div(cylinders, tmp);
2913
2914 /*
2915 * Handle extended translation size for logical drives > 1Gb
2916 */
2917
2918 if (capacity >= 0x200000) {
2919 heads = 255;
2920 sectors = 63;
2921 tmp = heads*sectors;
2922 cylinders = capacity;
2923 sector_div(cylinders, tmp);
2924 }
2925
2926 geom[0] = heads;
2927 geom[1] = sectors;
2928 geom[2] = cylinders;
2929
2930 return 0;
2931}
2932
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002933static void megasas_aen_polling(struct work_struct *work);
2934
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002935/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002936 * megasas_service_aen - Processes an event notification
2937 * @instance: Adapter soft state
2938 * @cmd: AEN command completed by the ISR
2939 *
2940 * For AEN, driver sends a command down to FW that is held by the FW till an
2941 * event occurs. When an event of interest occurs, FW completes the command
2942 * that it was previously holding.
2943 *
2944 * This routines sends SIGIO signal to processes that have registered with the
2945 * driver for AEN.
2946 */
2947static void
2948megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2949{
Yang, Boc3518832009-10-06 14:18:02 -06002950 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002951
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002952 /*
2953 * Don't signal app if it is just an aborted previously registered aen
2954 */
Yang, Boc3518832009-10-06 14:18:02 -06002955 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2956 spin_lock_irqsave(&poll_aen_lock, flags);
2957 megasas_poll_wait_aen = 1;
2958 spin_unlock_irqrestore(&poll_aen_lock, flags);
2959 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002960 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002961 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002962 else
2963 cmd->abort_aen = 0;
2964
2965 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302966
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302967 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002968
bo yang39a98552010-09-22 22:36:29 -04002969 if ((instance->unload == 0) &&
2970 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002971 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002972
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002973 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2974 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002975 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002976 } else {
2977 ev->instance = instance;
2978 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002979 INIT_DELAYED_WORK(&ev->hotplug_work,
2980 megasas_aen_polling);
2981 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002982 }
2983 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002984}
2985
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302986static ssize_t
2987megasas_fw_crash_buffer_store(struct device *cdev,
2988 struct device_attribute *attr, const char *buf, size_t count)
2989{
2990 struct Scsi_Host *shost = class_to_shost(cdev);
2991 struct megasas_instance *instance =
2992 (struct megasas_instance *) shost->hostdata;
2993 int val = 0;
2994 unsigned long flags;
2995
2996 if (kstrtoint(buf, 0, &val) != 0)
2997 return -EINVAL;
2998
2999 spin_lock_irqsave(&instance->crashdump_lock, flags);
3000 instance->fw_crash_buffer_offset = val;
3001 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3002 return strlen(buf);
3003}
3004
3005static ssize_t
3006megasas_fw_crash_buffer_show(struct device *cdev,
3007 struct device_attribute *attr, char *buf)
3008{
3009 struct Scsi_Host *shost = class_to_shost(cdev);
3010 struct megasas_instance *instance =
3011 (struct megasas_instance *) shost->hostdata;
3012 u32 size;
3013 unsigned long buff_addr;
3014 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3015 unsigned long src_addr;
3016 unsigned long flags;
3017 u32 buff_offset;
3018
3019 spin_lock_irqsave(&instance->crashdump_lock, flags);
3020 buff_offset = instance->fw_crash_buffer_offset;
3021 if (!instance->crash_dump_buf &&
3022 !((instance->fw_crash_state == AVAILABLE) ||
3023 (instance->fw_crash_state == COPYING))) {
3024 dev_err(&instance->pdev->dev,
3025 "Firmware crash dump is not available\n");
3026 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3027 return -EINVAL;
3028 }
3029
3030 buff_addr = (unsigned long) buf;
3031
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003032 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303033 dev_err(&instance->pdev->dev,
3034 "Firmware crash dump offset is out of range\n");
3035 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3036 return 0;
3037 }
3038
3039 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3040 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3041
3042 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3043 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003044 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303045 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3046
3047 return size;
3048}
3049
3050static ssize_t
3051megasas_fw_crash_buffer_size_show(struct device *cdev,
3052 struct device_attribute *attr, char *buf)
3053{
3054 struct Scsi_Host *shost = class_to_shost(cdev);
3055 struct megasas_instance *instance =
3056 (struct megasas_instance *) shost->hostdata;
3057
3058 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3059 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3060}
3061
3062static ssize_t
3063megasas_fw_crash_state_store(struct device *cdev,
3064 struct device_attribute *attr, const char *buf, size_t count)
3065{
3066 struct Scsi_Host *shost = class_to_shost(cdev);
3067 struct megasas_instance *instance =
3068 (struct megasas_instance *) shost->hostdata;
3069 int val = 0;
3070 unsigned long flags;
3071
3072 if (kstrtoint(buf, 0, &val) != 0)
3073 return -EINVAL;
3074
3075 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3076 dev_err(&instance->pdev->dev, "application updates invalid "
3077 "firmware crash state\n");
3078 return -EINVAL;
3079 }
3080
3081 instance->fw_crash_state = val;
3082
3083 if ((val == COPIED) || (val == COPY_ERROR)) {
3084 spin_lock_irqsave(&instance->crashdump_lock, flags);
3085 megasas_free_host_crash_buffer(instance);
3086 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3087 if (val == COPY_ERROR)
3088 dev_info(&instance->pdev->dev, "application failed to "
3089 "copy Firmware crash dump\n");
3090 else
3091 dev_info(&instance->pdev->dev, "Firmware crash dump "
3092 "copied successfully\n");
3093 }
3094 return strlen(buf);
3095}
3096
3097static ssize_t
3098megasas_fw_crash_state_show(struct device *cdev,
3099 struct device_attribute *attr, char *buf)
3100{
3101 struct Scsi_Host *shost = class_to_shost(cdev);
3102 struct megasas_instance *instance =
3103 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003104
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303105 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3106}
3107
3108static ssize_t
3109megasas_page_size_show(struct device *cdev,
3110 struct device_attribute *attr, char *buf)
3111{
3112 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3113}
3114
Sumit Saxena308ec452016-01-28 21:04:30 +05303115static ssize_t
3116megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3117 char *buf)
3118{
3119 struct Scsi_Host *shost = class_to_shost(cdev);
3120 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3121
3122 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3123}
3124
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303125static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3126 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3127static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3128 megasas_fw_crash_buffer_size_show, NULL);
3129static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3130 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3131static DEVICE_ATTR(page_size, S_IRUGO,
3132 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05303133static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3134 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303135
3136struct device_attribute *megaraid_host_attrs[] = {
3137 &dev_attr_fw_crash_buffer_size,
3138 &dev_attr_fw_crash_buffer,
3139 &dev_attr_fw_crash_state,
3140 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05303141 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303142 NULL,
3143};
3144
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003145/*
3146 * Scsi host template for megaraid_sas driver
3147 */
3148static struct scsi_host_template megasas_template = {
3149
3150 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05303151 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003152 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01003153 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06003154 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05303155 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003156 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07003157 .eh_target_reset_handler = megasas_reset_target,
3158 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003159 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003160 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303161 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003162 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003163 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003164 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003165 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003166};
3167
3168/**
3169 * megasas_complete_int_cmd - Completes an internal command
3170 * @instance: Adapter soft state
3171 * @cmd: Command to be completed
3172 *
3173 * The megasas_issue_blocked_cmd() function waits for a command to complete
3174 * after it issues a command. This function wakes up that waiting routine by
3175 * calling wake_up() on the wait queue.
3176 */
3177static void
3178megasas_complete_int_cmd(struct megasas_instance *instance,
3179 struct megasas_cmd *cmd)
3180{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303181 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003182 wake_up(&instance->int_cmd_wait_q);
3183}
3184
3185/**
3186 * megasas_complete_abort - Completes aborting a command
3187 * @instance: Adapter soft state
3188 * @cmd: Cmd that was issued to abort another cmd
3189 *
adam radford0d490162010-12-14 19:17:17 -08003190 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3191 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003192 * wakes up all functions waiting on the same wait queue.
3193 */
3194static void
3195megasas_complete_abort(struct megasas_instance *instance,
3196 struct megasas_cmd *cmd)
3197{
3198 if (cmd->sync_cmd) {
3199 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303200 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003201 wake_up(&instance->abort_cmd_wait_q);
3202 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003203}
3204
3205/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003206 * megasas_complete_cmd - Completes a command
3207 * @instance: Adapter soft state
3208 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003209 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003210 * SCSI mid-layer instead of the value returned
3211 * by the FW. This should be used if caller wants
3212 * an alternate status (as in the case of aborted
3213 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003214 */
adam radford9c915a82010-12-21 13:34:31 -08003215void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003216megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3217 u8 alt_status)
3218{
3219 int exception = 0;
3220 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003221 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003222 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303223 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003224
bo yang39a98552010-09-22 22:36:29 -04003225 /* flag for the retry reset */
3226 cmd->retry_for_fw_reset = 0;
3227
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003228 if (cmd->scmd)
3229 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003230
3231 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003232 case MFI_CMD_INVALID:
3233 /* Some older 1068 controller FW may keep a pended
3234 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3235 when booting the kdump kernel. Ignore this command to
3236 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003237 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3238 "completed\n");
3239 dev_warn(&instance->pdev->dev, "If you have a controller "
3240 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003241 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003242 case MFI_CMD_PD_SCSI_IO:
3243 case MFI_CMD_LD_SCSI_IO:
3244
3245 /*
3246 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3247 * issued either through an IO path or an IOCTL path. If it
3248 * was via IOCTL, we will send it to internal completion.
3249 */
3250 if (cmd->sync_cmd) {
3251 cmd->sync_cmd = 0;
3252 megasas_complete_int_cmd(instance, cmd);
3253 break;
3254 }
3255
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003256 case MFI_CMD_LD_READ:
3257 case MFI_CMD_LD_WRITE:
3258
3259 if (alt_status) {
3260 cmd->scmd->result = alt_status << 16;
3261 exception = 1;
3262 }
3263
3264 if (exception) {
3265
Sumant Patroe4a082c2006-05-30 12:03:37 -07003266 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003267
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003268 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003269 cmd->scmd->scsi_done(cmd->scmd);
3270 megasas_return_cmd(instance, cmd);
3271
3272 break;
3273 }
3274
3275 switch (hdr->cmd_status) {
3276
3277 case MFI_STAT_OK:
3278 cmd->scmd->result = DID_OK << 16;
3279 break;
3280
3281 case MFI_STAT_SCSI_IO_FAILED:
3282 case MFI_STAT_LD_INIT_IN_PROGRESS:
3283 cmd->scmd->result =
3284 (DID_ERROR << 16) | hdr->scsi_status;
3285 break;
3286
3287 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3288
3289 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3290
3291 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3292 memset(cmd->scmd->sense_buffer, 0,
3293 SCSI_SENSE_BUFFERSIZE);
3294 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3295 hdr->sense_len);
3296
3297 cmd->scmd->result |= DRIVER_SENSE << 24;
3298 }
3299
3300 break;
3301
3302 case MFI_STAT_LD_OFFLINE:
3303 case MFI_STAT_DEVICE_NOT_FOUND:
3304 cmd->scmd->result = DID_BAD_TARGET << 16;
3305 break;
3306
3307 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003308 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003309 hdr->cmd_status);
3310 cmd->scmd->result = DID_ERROR << 16;
3311 break;
3312 }
3313
Sumant Patroe4a082c2006-05-30 12:03:37 -07003314 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003315
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003316 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003317 cmd->scmd->scsi_done(cmd->scmd);
3318 megasas_return_cmd(instance, cmd);
3319
3320 break;
3321
3322 case MFI_CMD_SMP:
3323 case MFI_CMD_STP:
Shivasharan S82add4e2017-10-19 02:49:02 -07003324 megasas_complete_int_cmd(instance, cmd);
3325 break;
3326
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003327 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303328 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003329 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303330 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3331 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303332 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003333 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303334 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003335 if (cmd->frame->hdr.cmd_status != 0) {
3336 if (cmd->frame->hdr.cmd_status !=
3337 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003338 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003339 cmd->frame->hdr.cmd_status);
3340 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303341 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003342 spin_unlock_irqrestore(
3343 instance->host->host_lock,
3344 flags);
3345 break;
3346 }
3347 } else
3348 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303349 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303350
3351 /*
3352 * Set fast path IO to ZERO.
3353 * Validate Map will set proper value.
3354 * Meanwhile all IOs will go as LD IO.
3355 */
3356 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003357 fusion->fast_path_io = 1;
3358 else
3359 fusion->fast_path_io = 0;
3360 megasas_sync_map_info(instance);
3361 spin_unlock_irqrestore(instance->host->host_lock,
3362 flags);
3363 break;
3364 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303365 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3366 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003367 spin_lock_irqsave(&poll_aen_lock, flags);
3368 megasas_poll_wait_aen = 0;
3369 spin_unlock_irqrestore(&poll_aen_lock, flags);
3370 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003371
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303372 /* FW has an updated PD sequence */
3373 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3374 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3375
3376 spin_lock_irqsave(instance->host->host_lock, flags);
3377 status = cmd->frame->hdr.cmd_status;
3378 instance->jbod_seq_cmd = NULL;
3379 megasas_return_cmd(instance, cmd);
3380
3381 if (status == MFI_STAT_OK) {
3382 instance->pd_seq_map_id++;
3383 /* Re-register a pd sync seq num cmd */
3384 if (megasas_sync_pd_seq_num(instance, true))
3385 instance->use_seqnum_jbod_fp = false;
3386 } else
3387 instance->use_seqnum_jbod_fp = false;
3388
3389 spin_unlock_irqrestore(instance->host->host_lock, flags);
3390 break;
3391 }
3392
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003393 /*
3394 * See if got an event notification
3395 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303396 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003397 megasas_service_aen(instance, cmd);
3398 else
3399 megasas_complete_int_cmd(instance, cmd);
3400
3401 break;
3402
3403 case MFI_CMD_ABORT:
3404 /*
3405 * Cmd issued to abort another cmd returned
3406 */
3407 megasas_complete_abort(instance, cmd);
3408 break;
3409
3410 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003411 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003412 hdr->cmd);
Shivasharan S82add4e2017-10-19 02:49:02 -07003413 megasas_complete_int_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003414 break;
3415 }
3416}
3417
3418/**
bo yang39a98552010-09-22 22:36:29 -04003419 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003420 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003421 * @instance: Adapter soft state
3422 */
3423static inline void
3424megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3425{
3426 struct megasas_cmd *cmd;
3427 struct list_head clist_local;
3428 union megasas_evt_class_locale class_locale;
3429 unsigned long flags;
3430 u32 seq_num;
3431
3432 INIT_LIST_HEAD(&clist_local);
3433 spin_lock_irqsave(&instance->hba_lock, flags);
3434 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3435 spin_unlock_irqrestore(&instance->hba_lock, flags);
3436
3437 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003438 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003439 struct megasas_cmd, list);
3440 list_del_init(&cmd->list);
3441
3442 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003443 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3444 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003445 cmd, cmd->scmd, cmd->sync_cmd);
3446
3447 cmd->retry_for_fw_reset++;
3448
3449 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003450 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003451 "was tried multiple times during reset."
3452 "Shutting down the HBA\n",
3453 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303454 instance->instancet->disable_intr(instance);
3455 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003456 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003457 return;
3458 }
3459 }
3460
3461 if (cmd->sync_cmd == 1) {
3462 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003463 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003464 "cmd attached to internal command!\n");
3465 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003466 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003467 "on the internal reset queue,"
3468 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303469 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003470 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003471 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003472 0, instance->reg_set);
3473 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003474 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003475 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003476 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003477
3478 atomic_inc(&instance->fw_outstanding);
3479 instance->instancet->fire_cmd(instance,
3480 cmd->frame_phys_addr,
3481 cmd->frame_count-1, instance->reg_set);
3482 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003483 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003484 "internal reset defer list while re-issue!!\n",
3485 cmd);
3486 }
3487 }
3488
3489 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003490 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003491 megasas_return_cmd(instance, instance->aen_cmd);
3492
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003493 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003494 }
3495
3496 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003497 * Initiate AEN (Asynchronous Event Notification)
3498 */
bo yang39a98552010-09-22 22:36:29 -04003499 seq_num = instance->last_seq_num;
3500 class_locale.members.reserved = 0;
3501 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3502 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3503
3504 megasas_register_aen(instance, seq_num, class_locale.word);
3505}
3506
3507/**
3508 * Move the internal reset pending commands to a deferred queue.
3509 *
3510 * We move the commands pending at internal reset time to a
3511 * pending queue. This queue would be flushed after successful
3512 * completion of the internal reset sequence. if the internal reset
3513 * did not complete in time, the kernel reset handler would flush
3514 * these commands.
3515 **/
3516static void
3517megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3518{
3519 struct megasas_cmd *cmd;
3520 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003521 u16 max_cmd = instance->max_fw_cmds;
bo yang39a98552010-09-22 22:36:29 -04003522 u32 defer_index;
3523 unsigned long flags;
3524
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003525 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303526 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003527 for (i = 0; i < max_cmd; i++) {
3528 cmd = instance->cmd_list[i];
3529 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003530 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003531 "on the defer queue as internal\n",
3532 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3533
3534 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003535 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003536 " moving this cmd:%p, %d %p, it was"
3537 "discovered on some list?\n",
3538 cmd, cmd->sync_cmd, cmd->scmd);
3539
3540 list_del_init(&cmd->list);
3541 }
3542 defer_index++;
3543 list_add_tail(&cmd->list,
3544 &instance->internal_reset_pending_q);
3545 }
3546 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303547 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003548}
3549
3550
3551static void
3552process_fw_state_change_wq(struct work_struct *work)
3553{
3554 struct megasas_instance *instance =
3555 container_of(work, struct megasas_instance, work_init);
3556 u32 wait;
3557 unsigned long flags;
3558
Sumit Saxena8a01a412016-01-28 21:04:32 +05303559 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003560 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303561 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003562 return ;
3563 }
3564
Sumit Saxena8a01a412016-01-28 21:04:32 +05303565 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003566 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003567 "state, restarting it...\n");
3568
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303569 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003570 atomic_set(&instance->fw_outstanding, 0);
3571
3572 atomic_set(&instance->fw_reset_no_pci_access, 1);
3573 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003574 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003575
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003576 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003577 "initiating next stage...\n");
3578
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003579 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003580 "state 2 starting...\n");
3581
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003582 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003583 for (wait = 0; wait < 30; wait++) {
3584 msleep(1000);
3585 }
3586
adam radford058a8fa2011-10-08 18:14:27 -07003587 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003588 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003589
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303590 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003591 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003592 return ;
3593 }
3594
3595 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3596 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3597 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3598 ) {
3599 *instance->consumer = *instance->producer;
3600 } else {
3601 *instance->consumer = 0;
3602 *instance->producer = 0;
3603 }
3604
3605 megasas_issue_init_mfi(instance);
3606
3607 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303608 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003609 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303610 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003611
3612 megasas_issue_pending_cmds_again(instance);
3613 instance->issuepend_done = 1;
3614 }
bo yang39a98552010-09-22 22:36:29 -04003615}
3616
3617/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003618 * megasas_deplete_reply_queue - Processes all completed commands
3619 * @instance: Adapter soft state
3620 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003621 * SCSI mid-layer instead of the status
3622 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003623 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003624 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003625static int
bo yang39a98552010-09-22 22:36:29 -04003626megasas_deplete_reply_queue(struct megasas_instance *instance,
3627 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003628{
bo yang39a98552010-09-22 22:36:29 -04003629 u32 mfiStatus;
3630 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003631
bo yang39a98552010-09-22 22:36:29 -04003632 if ((mfiStatus = instance->instancet->check_reset(instance,
3633 instance->reg_set)) == 1) {
3634 return IRQ_HANDLED;
3635 }
3636
3637 if ((mfiStatus = instance->instancet->clear_intr(
3638 instance->reg_set)
3639 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003640 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003641 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003642 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003643 }
3644
3645 instance->mfiStatus = mfiStatus;
3646
3647 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3648 fw_state = instance->instancet->read_fw_status_reg(
3649 instance->reg_set) & MFI_STATE_MASK;
3650
3651 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003652 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003653 fw_state);
3654 }
3655
3656 if ((fw_state == MFI_STATE_FAULT) &&
3657 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003658 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003659
3660 if ((instance->pdev->device ==
3661 PCI_DEVICE_ID_LSI_SAS1064R) ||
3662 (instance->pdev->device ==
3663 PCI_DEVICE_ID_DELL_PERC5) ||
3664 (instance->pdev->device ==
3665 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3666
3667 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303668 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003669 }
3670
3671
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303672 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303673 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003674 instance->issuepend_done = 0;
3675
3676 atomic_set(&instance->fw_outstanding, 0);
3677 megasas_internal_reset_defer_cmds(instance);
3678
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003679 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303680 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003681
3682 schedule_work(&instance->work_init);
3683 return IRQ_HANDLED;
3684
3685 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003686 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003687 fw_state, instance->disableOnlineCtrlReset);
3688 }
3689 }
3690
Sumant Patro5d018ad2006-10-03 13:13:18 -07003691 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003692 return IRQ_HANDLED;
3693}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003694/**
3695 * megasas_isr - isr entry point
3696 */
David Howells7d12e782006-10-05 14:55:46 +01003697static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003698{
adam radfordc8e858f2011-10-08 18:15:13 -07003699 struct megasas_irq_context *irq_context = devp;
3700 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003701 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003702 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003703
adam radfordc8e858f2011-10-08 18:15:13 -07003704 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003705 return IRQ_HANDLED;
3706
bo yang39a98552010-09-22 22:36:29 -04003707 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003708 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003709 spin_unlock_irqrestore(&instance->hba_lock, flags);
3710
3711 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003712}
3713
3714/**
3715 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003716 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003717 *
3718 * During the initialization, FW passes can potentially be in any one of
3719 * several possible states. If the FW in operational, waiting-for-handshake
3720 * states, driver must take steps to bring it to ready state. Otherwise, it
3721 * has to wait for the ready state.
3722 */
adam radford9c915a82010-12-21 13:34:31 -08003723int
adam radford058a8fa2011-10-08 18:14:27 -07003724megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003725{
3726 int i;
3727 u8 max_wait;
3728 u32 fw_state;
3729 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003730 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003731
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003732 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3733 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003734
Sumant Patroe3bbff92006-10-03 12:28:49 -07003735 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003736 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003737 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003738
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003739 while (fw_state != MFI_STATE_READY) {
3740
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003741 switch (fw_state) {
3742
3743 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003744 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003745 if (ocr) {
3746 max_wait = MEGASAS_RESET_WAIT_TIME;
3747 cur_state = MFI_STATE_FAULT;
3748 break;
3749 } else
3750 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003751
3752 case MFI_STATE_WAIT_HANDSHAKE:
3753 /*
3754 * Set the CLR bit in inbound doorbell
3755 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003756 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003757 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3758 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003759 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003760 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003761 writel(
3762 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003763 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303764 else
Yang, Bo87911122009-10-06 14:31:54 -06003765 writel(
3766 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3767 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003768
Yang, Bo7218df62009-10-06 14:52:20 -06003769 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003770 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3771 break;
3772
Sumant Patroe3bbff92006-10-03 12:28:49 -07003773 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003774 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003775 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3776 (instance->pdev->device ==
3777 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003778 (instance->adapter_type != MFI_SERIES))
Yang, Bo87911122009-10-06 14:31:54 -06003779 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003780 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303781 else
Yang, Bo87911122009-10-06 14:31:54 -06003782 writel(MFI_INIT_HOTPLUG,
3783 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003784
Yang, Bo7218df62009-10-06 14:52:20 -06003785 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003786 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3787 break;
3788
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003789 case MFI_STATE_OPERATIONAL:
3790 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003791 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003792 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303793 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003794 if ((instance->pdev->device ==
3795 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3796 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003797 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
Shivasharan Se7d36b82017-10-19 02:48:50 -07003798 (instance->adapter_type != MFI_SERIES)) {
Yang, Bo87911122009-10-06 14:31:54 -06003799 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003800 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303801
Shivasharan Se7d36b82017-10-19 02:48:50 -07003802 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08003803 for (i = 0; i < (10 * 1000); i += 20) {
3804 if (readl(
3805 &instance->
3806 reg_set->
3807 doorbell) & 1)
3808 msleep(20);
3809 else
3810 break;
3811 }
3812 }
Yang, Bo87911122009-10-06 14:31:54 -06003813 } else
3814 writel(MFI_RESET_FLAGS,
3815 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003816
Yang, Bo7218df62009-10-06 14:52:20 -06003817 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003818 cur_state = MFI_STATE_OPERATIONAL;
3819 break;
3820
3821 case MFI_STATE_UNDEFINED:
3822 /*
3823 * This state should not last for more than 2 seconds
3824 */
Yang, Bo7218df62009-10-06 14:52:20 -06003825 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003826 cur_state = MFI_STATE_UNDEFINED;
3827 break;
3828
3829 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003830 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003831 cur_state = MFI_STATE_BB_INIT;
3832 break;
3833
3834 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003835 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003836 cur_state = MFI_STATE_FW_INIT;
3837 break;
3838
3839 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003840 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003841 cur_state = MFI_STATE_FW_INIT_2;
3842 break;
3843
3844 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003845 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003846 cur_state = MFI_STATE_DEVICE_SCAN;
3847 break;
3848
3849 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003850 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003851 cur_state = MFI_STATE_FLUSH_CACHE;
3852 break;
3853
3854 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003855 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003856 fw_state);
3857 return -ENODEV;
3858 }
3859
3860 /*
3861 * The cur_state should not last for more than max_wait secs
3862 */
3863 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003864 curr_abs_state = instance->instancet->
3865 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003866
Yang, Bo7218df62009-10-06 14:52:20 -06003867 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003868 msleep(1);
3869 } else
3870 break;
3871 }
3872
3873 /*
3874 * Return error if fw_state hasn't changed after max_wait
3875 */
Yang, Bo7218df62009-10-06 14:52:20 -06003876 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003877 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003878 "in %d secs\n", fw_state, max_wait);
3879 return -ENODEV;
3880 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003881
3882 abs_state = curr_abs_state;
3883 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003884 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003885 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003886
3887 return 0;
3888}
3889
3890/**
3891 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3892 * @instance: Adapter soft state
3893 */
3894static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3895{
3896 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003897 u16 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003898 struct megasas_cmd *cmd;
3899
3900 if (!instance->frame_dma_pool)
3901 return;
3902
3903 /*
3904 * Return all frames to pool
3905 */
3906 for (i = 0; i < max_cmd; i++) {
3907
3908 cmd = instance->cmd_list[i];
3909
3910 if (cmd->frame)
Romain Perierfc69d862017-07-06 10:13:06 +02003911 dma_pool_free(instance->frame_dma_pool, cmd->frame,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912 cmd->frame_phys_addr);
3913
3914 if (cmd->sense)
Romain Perierfc69d862017-07-06 10:13:06 +02003915 dma_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003916 cmd->sense_phys_addr);
3917 }
3918
3919 /*
3920 * Now destroy the pool itself
3921 */
Romain Perierfc69d862017-07-06 10:13:06 +02003922 dma_pool_destroy(instance->frame_dma_pool);
3923 dma_pool_destroy(instance->sense_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003924
3925 instance->frame_dma_pool = NULL;
3926 instance->sense_dma_pool = NULL;
3927}
3928
3929/**
3930 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3931 * @instance: Adapter soft state
3932 *
3933 * Each command packet has an embedded DMA memory buffer that is used for
3934 * filling MFI frame and the SG list that immediately follows the frame. This
3935 * function creates those DMA memory buffers for each command packet by using
3936 * PCI pool facility.
3937 */
3938static int megasas_create_frame_pool(struct megasas_instance *instance)
3939{
3940 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08003941 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003942 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003943 u32 frame_count;
3944 struct megasas_cmd *cmd;
3945
adam radford9c915a82010-12-21 13:34:31 -08003946 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003947
3948 /*
3949 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3950 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3951 */
3952 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3953 sizeof(struct megasas_sge32);
3954
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003955 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003956 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003957
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003958 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303959 * For MFI controllers.
3960 * max_num_sge = 60
3961 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3962 * Total 960 byte (15 MFI frame of 64 byte)
3963 *
3964 * Fusion adapter require only 3 extra frame.
3965 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3966 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3967 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003968 */
Shivasharan Se7d36b82017-10-19 02:48:50 -07003969 frame_count = (instance->adapter_type == MFI_SERIES) ?
3970 (15 + 1) : (3 + 1);
Shivasharan S21c34002017-02-10 00:59:28 -08003971 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003972 /*
3973 * Use DMA pool facility provided by PCI layer
3974 */
Romain Perierfc69d862017-07-06 10:13:06 +02003975 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
3976 &instance->pdev->dev,
3977 instance->mfi_frame_size, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003978
3979 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003980 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003981 return -ENOMEM;
3982 }
3983
Romain Perierfc69d862017-07-06 10:13:06 +02003984 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
3985 &instance->pdev->dev, 128,
3986 4, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003987
3988 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003989 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003990
Romain Perierfc69d862017-07-06 10:13:06 +02003991 dma_pool_destroy(instance->frame_dma_pool);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003992 instance->frame_dma_pool = NULL;
3993
3994 return -ENOMEM;
3995 }
3996
3997 /*
3998 * Allocate and attach a frame to each of the commands in cmd_list.
3999 * By making cmd->index as the context instead of the &cmd, we can
4000 * always use 32bit context regardless of the architecture
4001 */
4002 for (i = 0; i < max_cmd; i++) {
4003
4004 cmd = instance->cmd_list[i];
4005
Romain Perierfc69d862017-07-06 10:13:06 +02004006 cmd->frame = dma_pool_alloc(instance->frame_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004007 GFP_KERNEL, &cmd->frame_phys_addr);
4008
Romain Perierfc69d862017-07-06 10:13:06 +02004009 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004010 GFP_KERNEL, &cmd->sense_phys_addr);
4011
4012 /*
4013 * megasas_teardown_frame_pool() takes care of freeing
4014 * whatever has been allocated
4015 */
4016 if (!cmd->frame || !cmd->sense) {
Romain Perierfc69d862017-07-06 10:13:06 +02004017 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004018 megasas_teardown_frame_pool(instance);
4019 return -ENOMEM;
4020 }
4021
Shivasharan S21c34002017-02-10 00:59:28 -08004022 memset(cmd->frame, 0, instance->mfi_frame_size);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304023 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06004024 cmd->frame->io.pad_0 = 0;
Shivasharan Se7d36b82017-10-19 02:48:50 -07004025 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07004026 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004027 }
4028
4029 return 0;
4030}
4031
4032/**
4033 * megasas_free_cmds - Free all the cmds in the free cmd pool
4034 * @instance: Adapter soft state
4035 */
adam radford9c915a82010-12-21 13:34:31 -08004036void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004037{
4038 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05004039
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004040 /* First free the MFI frame pool */
4041 megasas_teardown_frame_pool(instance);
4042
4043 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08004044 for (i = 0; i < instance->max_mfi_cmds; i++)
4045
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004046 kfree(instance->cmd_list[i]);
4047
4048 /* Free the cmd_list buffer itself */
4049 kfree(instance->cmd_list);
4050 instance->cmd_list = NULL;
4051
4052 INIT_LIST_HEAD(&instance->cmd_pool);
4053}
4054
4055/**
4056 * megasas_alloc_cmds - Allocates the command packets
4057 * @instance: Adapter soft state
4058 *
4059 * Each command that is issued to the FW, whether IO commands from the OS or
4060 * internal commands like IOCTLs, are wrapped in local data structure called
4061 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4062 * the FW.
4063 *
4064 * Each frame has a 32-bit field called context (tag). This context is used
4065 * to get back the megasas_cmd from the frame when a frame gets completed in
4066 * the ISR. Typically the address of the megasas_cmd itself would be used as
4067 * the context. But we wanted to keep the differences between 32 and 64 bit
4068 * systems to the mininum. We always use 32 bit integers for the context. In
4069 * this driver, the 32 bit values are the indices into an array cmd_list.
4070 * This array is used only to look up the megasas_cmd given the context. The
4071 * free commands themselves are maintained in a linked list called cmd_pool.
4072 */
adam radford9c915a82010-12-21 13:34:31 -08004073int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004074{
4075 int i;
4076 int j;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08004077 u16 max_cmd;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004078 struct megasas_cmd *cmd;
4079
adam radford9c915a82010-12-21 13:34:31 -08004080 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004081
4082 /*
4083 * instance->cmd_list is an array of struct megasas_cmd pointers.
4084 * Allocate the dynamic array first and then allocate individual
4085 * commands.
4086 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07004087 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004088
4089 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004090 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004091 return -ENOMEM;
4092 }
4093
adam radford9c915a82010-12-21 13:34:31 -08004094 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004095
4096 for (i = 0; i < max_cmd; i++) {
4097 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4098 GFP_KERNEL);
4099
4100 if (!instance->cmd_list[i]) {
4101
4102 for (j = 0; j < i; j++)
4103 kfree(instance->cmd_list[j]);
4104
4105 kfree(instance->cmd_list);
4106 instance->cmd_list = NULL;
4107
4108 return -ENOMEM;
4109 }
4110 }
4111
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004112 for (i = 0; i < max_cmd; i++) {
4113 cmd = instance->cmd_list[i];
4114 memset(cmd, 0, sizeof(struct megasas_cmd));
4115 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04004116 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004117 cmd->instance = instance;
4118
4119 list_add_tail(&cmd->list, &instance->cmd_pool);
4120 }
4121
4122 /*
4123 * Create a frame pool and assign one frame to each cmd
4124 */
4125 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004126 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004127 megasas_free_cmds(instance);
4128 }
4129
4130 return 0;
4131}
4132
Yang, Bo81e403c2009-10-06 14:27:54 -06004133/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304134 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4135 * @instance: Adapter soft state
4136 *
4137 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4138 * or FW is not under OCR.
4139 */
4140inline int
4141dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4142
Shivasharan Se7d36b82017-10-19 02:48:50 -07004143 if (instance->adapter_type == MFI_SERIES)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304144 return KILL_ADAPTER;
4145 else if (instance->unload ||
4146 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4147 return IGNORE_TIMEOUT;
4148 else
4149 return INITIATE_OCR;
4150}
4151
Shivasharan S15dd0382017-02-10 00:59:10 -08004152static void
4153megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
Sumit Saxena2216c302016-01-28 21:04:26 +05304154{
4155 int ret;
4156 struct megasas_cmd *cmd;
4157 struct megasas_dcmd_frame *dcmd;
4158
Shivasharan S15dd0382017-02-10 00:59:10 -08004159 struct MR_PRIV_DEVICE *mr_device_priv_data;
4160 u16 device_id = 0;
4161
4162 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
Sumit Saxena2216c302016-01-28 21:04:26 +05304163 cmd = megasas_get_cmd(instance);
4164
4165 if (!cmd) {
4166 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
Shivasharan S15dd0382017-02-10 00:59:10 -08004167 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304168 }
4169
4170 dcmd = &cmd->frame->dcmd;
4171
4172 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4173 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4174
4175 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4176 dcmd->cmd = MFI_CMD_DCMD;
4177 dcmd->cmd_status = 0xFF;
4178 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004179 dcmd->flags = MFI_FRAME_DIR_READ;
Sumit Saxena2216c302016-01-28 21:04:26 +05304180 dcmd->timeout = 0;
4181 dcmd->pad_0 = 0;
4182 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4183 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
Sumit Saxena2216c302016-01-28 21:04:26 +05304184
Shivasharan S107a60d2017-10-19 02:49:05 -07004185 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4186 sizeof(struct MR_PD_INFO));
Sumit Saxena2216c302016-01-28 21:04:26 +05304187
Shivasharan Se7d36b82017-10-19 02:48:50 -07004188 if ((instance->adapter_type != MFI_SERIES) &&
4189 !instance->mask_interrupts)
Sumit Saxena2216c302016-01-28 21:04:26 +05304190 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4191 else
4192 ret = megasas_issue_polled(instance, cmd);
4193
4194 switch (ret) {
4195 case DCMD_SUCCESS:
Shivasharan S15dd0382017-02-10 00:59:10 -08004196 mr_device_priv_data = sdev->hostdata;
4197 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4198 mr_device_priv_data->interface_type =
Sumit Saxena2216c302016-01-28 21:04:26 +05304199 instance->pd_info->state.ddf.pdType.intf;
4200 break;
4201
4202 case DCMD_TIMEOUT:
4203
4204 switch (dcmd_timeout_ocr_possible(instance)) {
4205 case INITIATE_OCR:
4206 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4207 megasas_reset_fusion(instance->host,
4208 MFI_IO_TIMEOUT_OCR);
4209 break;
4210 case KILL_ADAPTER:
4211 megaraid_sas_kill_hba(instance);
4212 break;
4213 case IGNORE_TIMEOUT:
4214 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4215 __func__, __LINE__);
4216 break;
4217 }
4218
4219 break;
4220 }
4221
4222 if (ret != DCMD_TIMEOUT)
4223 megasas_return_cmd(instance, cmd);
4224
Shivasharan S15dd0382017-02-10 00:59:10 -08004225 return;
Sumit Saxena2216c302016-01-28 21:04:26 +05304226}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304227/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004228 * megasas_get_pd_list_info - Returns FW's pd_list structure
4229 * @instance: Adapter soft state
4230 * @pd_list: pd_list structure
4231 *
4232 * Issues an internal command (DCMD) to get the FW's controller PD
4233 * list structure. This information is mainly used to find out SYSTEM
4234 * supported by the FW.
4235 */
4236static int
4237megasas_get_pd_list(struct megasas_instance *instance)
4238{
4239 int ret = 0, pd_index = 0;
4240 struct megasas_cmd *cmd;
4241 struct megasas_dcmd_frame *dcmd;
4242 struct MR_PD_LIST *ci;
4243 struct MR_PD_ADDRESS *pd_addr;
4244 dma_addr_t ci_h = 0;
4245
Sumit Saxenad9083162016-07-08 03:30:16 -07004246 if (instance->pd_list_not_supported) {
4247 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4248 "not supported by firmware\n");
4249 return ret;
4250 }
4251
Shivasharan S9b3d0282017-10-19 02:48:56 -07004252 ci = instance->pd_list_buf;
4253 ci_h = instance->pd_list_buf_h;
4254
Yang, Bo81e403c2009-10-06 14:27:54 -06004255 cmd = megasas_get_cmd(instance);
4256
4257 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004258 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004259 return -ENOMEM;
4260 }
4261
4262 dcmd = &cmd->frame->dcmd;
4263
Yang, Bo81e403c2009-10-06 14:27:54 -06004264 memset(ci, 0, sizeof(*ci));
4265 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4266
4267 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4268 dcmd->mbox.b[1] = 0;
4269 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304270 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004271 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004272 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bo81e403c2009-10-06 14:27:54 -06004273 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004274 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304275 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4276 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
Yang, Bo81e403c2009-10-06 14:27:54 -06004277
Shivasharan S107a60d2017-10-19 02:49:05 -07004278 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4279 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
Yang, Bo81e403c2009-10-06 14:27:54 -06004280
Shivasharan Se7d36b82017-10-19 02:48:50 -07004281 if ((instance->adapter_type != MFI_SERIES) &&
4282 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304283 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304284 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304285 else
4286 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004287
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304288 switch (ret) {
4289 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004290 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4291 "failed/not supported by firmware\n");
4292
Shivasharan Se7d36b82017-10-19 02:48:50 -07004293 if (instance->adapter_type != MFI_SERIES)
Sumit Saxena30845582016-03-10 02:14:37 -08004294 megaraid_sas_kill_hba(instance);
4295 else
4296 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304297 break;
4298 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004299
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304300 switch (dcmd_timeout_ocr_possible(instance)) {
4301 case INITIATE_OCR:
4302 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4303 /*
4304 * DCMD failed from AEN path.
4305 * AEN path already hold reset_mutex to avoid PCI access
4306 * while OCR is in progress.
4307 */
4308 mutex_unlock(&instance->reset_mutex);
4309 megasas_reset_fusion(instance->host,
4310 MFI_IO_TIMEOUT_OCR);
4311 mutex_lock(&instance->reset_mutex);
4312 break;
4313 case KILL_ADAPTER:
4314 megaraid_sas_kill_hba(instance);
4315 break;
4316 case IGNORE_TIMEOUT:
4317 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4318 __func__, __LINE__);
4319 break;
4320 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004321
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304322 break;
4323
4324 case DCMD_SUCCESS:
4325 pd_addr = ci->addr;
4326
4327 if ((le32_to_cpu(ci->count) >
4328 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4329 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004330
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304331 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304332 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004333
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304334 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304335 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304336 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304337 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304338 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304339 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304340 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004341 pd_addr++;
4342 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304343
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304344 memcpy(instance->pd_list, instance->local_pd_list,
4345 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304346 break;
4347
Yang, Bo81e403c2009-10-06 14:27:54 -06004348 }
4349
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304350 if (ret != DCMD_TIMEOUT)
4351 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004352
4353 return ret;
4354}
4355
Yang, Bobdc6fb82009-12-06 08:30:19 -07004356/*
4357 * megasas_get_ld_list_info - Returns FW's ld_list structure
4358 * @instance: Adapter soft state
4359 * @ld_list: ld_list structure
4360 *
4361 * Issues an internal command (DCMD) to get the FW's controller PD
4362 * list structure. This information is mainly used to find out SYSTEM
4363 * supported by the FW.
4364 */
4365static int
4366megasas_get_ld_list(struct megasas_instance *instance)
4367{
4368 int ret = 0, ld_index = 0, ids = 0;
4369 struct megasas_cmd *cmd;
4370 struct megasas_dcmd_frame *dcmd;
4371 struct MR_LD_LIST *ci;
4372 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304373 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004374
Shivasharan S9b3d0282017-10-19 02:48:56 -07004375 ci = instance->ld_list_buf;
4376 ci_h = instance->ld_list_buf_h;
4377
Yang, Bobdc6fb82009-12-06 08:30:19 -07004378 cmd = megasas_get_cmd(instance);
4379
4380 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004381 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004382 return -ENOMEM;
4383 }
4384
4385 dcmd = &cmd->frame->dcmd;
4386
Yang, Bobdc6fb82009-12-06 08:30:19 -07004387 memset(ci, 0, sizeof(*ci));
4388 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4389
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304390 if (instance->supportmax256vd)
4391 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004392 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304393 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004394 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004395 dcmd->flags = MFI_FRAME_DIR_READ;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004396 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304397 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4398 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004399 dcmd->pad_0 = 0;
4400
Shivasharan S107a60d2017-10-19 02:49:05 -07004401 megasas_set_dma_settings(instance, dcmd, ci_h,
4402 sizeof(struct MR_LD_LIST));
4403
Shivasharan Se7d36b82017-10-19 02:48:50 -07004404 if ((instance->adapter_type != MFI_SERIES) &&
4405 !instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304406 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304407 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304408 else
4409 ret = megasas_issue_polled(instance, cmd);
4410
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304411 ld_count = le32_to_cpu(ci->ldCount);
4412
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304413 switch (ret) {
4414 case DCMD_FAILED:
4415 megaraid_sas_kill_hba(instance);
4416 break;
4417 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004418
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304419 switch (dcmd_timeout_ocr_possible(instance)) {
4420 case INITIATE_OCR:
4421 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4422 /*
4423 * DCMD failed from AEN path.
4424 * AEN path already hold reset_mutex to avoid PCI access
4425 * while OCR is in progress.
4426 */
4427 mutex_unlock(&instance->reset_mutex);
4428 megasas_reset_fusion(instance->host,
4429 MFI_IO_TIMEOUT_OCR);
4430 mutex_lock(&instance->reset_mutex);
4431 break;
4432 case KILL_ADAPTER:
4433 megaraid_sas_kill_hba(instance);
4434 break;
4435 case IGNORE_TIMEOUT:
4436 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4437 __func__, __LINE__);
4438 break;
4439 }
4440
4441 break;
4442
4443 case DCMD_SUCCESS:
4444 if (ld_count > instance->fw_supported_vd_count)
4445 break;
4446
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304447 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004448
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304449 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004450 if (ci->ldList[ld_index].state != 0) {
4451 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304452 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004453 }
4454 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304455
4456 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004457 }
4458
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304459 if (ret != DCMD_TIMEOUT)
4460 megasas_return_cmd(instance, cmd);
4461
Yang, Bobdc6fb82009-12-06 08:30:19 -07004462 return ret;
4463}
4464
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004465/**
adam radford21c9e162013-09-06 15:27:14 -07004466 * megasas_ld_list_query - Returns FW's ld_list structure
4467 * @instance: Adapter soft state
4468 * @ld_list: ld_list structure
4469 *
4470 * Issues an internal command (DCMD) to get the FW's controller PD
4471 * list structure. This information is mainly used to find out SYSTEM
4472 * supported by the FW.
4473 */
4474static int
4475megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4476{
4477 int ret = 0, ld_index = 0, ids = 0;
4478 struct megasas_cmd *cmd;
4479 struct megasas_dcmd_frame *dcmd;
4480 struct MR_LD_TARGETID_LIST *ci;
4481 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304482 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004483
Shivasharan S9b3d0282017-10-19 02:48:56 -07004484 ci = instance->ld_targetid_list_buf;
4485 ci_h = instance->ld_targetid_list_buf_h;
4486
adam radford21c9e162013-09-06 15:27:14 -07004487 cmd = megasas_get_cmd(instance);
4488
4489 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004490 dev_warn(&instance->pdev->dev,
4491 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004492 return -ENOMEM;
4493 }
4494
4495 dcmd = &cmd->frame->dcmd;
4496
adam radford21c9e162013-09-06 15:27:14 -07004497 memset(ci, 0, sizeof(*ci));
4498 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4499
4500 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304501 if (instance->supportmax256vd)
4502 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004503
4504 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304505 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004506 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004507 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford21c9e162013-09-06 15:27:14 -07004508 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304509 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4510 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
adam radford21c9e162013-09-06 15:27:14 -07004511 dcmd->pad_0 = 0;
4512
Shivasharan S107a60d2017-10-19 02:49:05 -07004513 megasas_set_dma_settings(instance, dcmd, ci_h,
4514 sizeof(struct MR_LD_TARGETID_LIST));
4515
Shivasharan Se7d36b82017-10-19 02:48:50 -07004516 if ((instance->adapter_type != MFI_SERIES) &&
4517 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304518 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304519 else
4520 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004521
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304522 switch (ret) {
4523 case DCMD_FAILED:
4524 dev_info(&instance->pdev->dev,
4525 "DCMD not supported by firmware - %s %d\n",
4526 __func__, __LINE__);
4527 ret = megasas_get_ld_list(instance);
4528 break;
4529 case DCMD_TIMEOUT:
4530 switch (dcmd_timeout_ocr_possible(instance)) {
4531 case INITIATE_OCR:
4532 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4533 /*
4534 * DCMD failed from AEN path.
4535 * AEN path already hold reset_mutex to avoid PCI access
4536 * while OCR is in progress.
4537 */
4538 mutex_unlock(&instance->reset_mutex);
4539 megasas_reset_fusion(instance->host,
4540 MFI_IO_TIMEOUT_OCR);
4541 mutex_lock(&instance->reset_mutex);
4542 break;
4543 case KILL_ADAPTER:
4544 megaraid_sas_kill_hba(instance);
4545 break;
4546 case IGNORE_TIMEOUT:
4547 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4548 __func__, __LINE__);
4549 break;
4550 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304551
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304552 break;
4553 case DCMD_SUCCESS:
4554 tgtid_count = le32_to_cpu(ci->count);
4555
4556 if ((tgtid_count > (instance->fw_supported_vd_count)))
4557 break;
4558
adam radford21c9e162013-09-06 15:27:14 -07004559 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304560 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004561 ids = ci->targetId[ld_index];
4562 instance->ld_ids[ids] = ci->targetId[ld_index];
4563 }
4564
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304565 break;
adam radford21c9e162013-09-06 15:27:14 -07004566 }
4567
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304568 if (ret != DCMD_TIMEOUT)
4569 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004570
4571 return ret;
4572}
4573
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304574/*
4575 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4576 * instance : Controller's instance
4577*/
4578static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4579{
4580 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004581 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304582
4583 fusion = instance->ctrl_context;
4584 /* For MFI based controllers return dummy success */
4585 if (!fusion)
4586 return;
4587
4588 instance->supportmax256vd =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004589 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304590 /* Below is additional check to address future FW enhancement */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004591 if (instance->ctrl_info_buf->max_lds > 64)
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304592 instance->supportmax256vd = 1;
4593
4594 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4595 * MEGASAS_MAX_DEV_PER_CHANNEL;
4596 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4597 * MEGASAS_MAX_DEV_PER_CHANNEL;
4598 if (instance->supportmax256vd) {
4599 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4600 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4601 } else {
4602 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4603 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4604 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304605
4606 dev_info(&instance->pdev->dev,
4607 "firmware type\t: %s\n",
4608 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4609 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304610
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004611 if (instance->max_raid_mapsize) {
4612 ventura_map_sz = instance->max_raid_mapsize *
4613 MR_MIN_MAP_SIZE; /* 64k */
4614 fusion->current_map_sz = ventura_map_sz;
4615 fusion->max_map_sz = ventura_map_sz;
4616 } else {
4617 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4618 (sizeof(struct MR_LD_SPAN_MAP) *
4619 (instance->fw_supported_vd_count - 1));
4620 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304621
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004622 fusion->max_map_sz =
4623 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304624
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004625 if (instance->supportmax256vd)
4626 fusion->current_map_sz = fusion->new_map_sz;
4627 else
4628 fusion->current_map_sz = fusion->old_map_sz;
4629 }
4630 /* irrespective of FW raid maps, driver raid map is constant */
4631 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304632}
4633
adam radford21c9e162013-09-06 15:27:14 -07004634/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004635 * megasas_get_controller_info - Returns FW's controller structure
4636 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004637 *
4638 * Issues an internal command (DCMD) to get the FW's controller structure.
4639 * This information is mainly used to find out the maximum IO transfer per
4640 * command supported by the FW.
4641 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304642int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304643megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004644{
4645 int ret = 0;
4646 struct megasas_cmd *cmd;
4647 struct megasas_dcmd_frame *dcmd;
4648 struct megasas_ctrl_info *ci;
4649 dma_addr_t ci_h = 0;
4650
Shivasharan S9b3d0282017-10-19 02:48:56 -07004651 ci = instance->ctrl_info_buf;
4652 ci_h = instance->ctrl_info_buf_h;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304653
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004654 cmd = megasas_get_cmd(instance);
4655
4656 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004657 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004658 return -ENOMEM;
4659 }
4660
4661 dcmd = &cmd->frame->dcmd;
4662
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004663 memset(ci, 0, sizeof(*ci));
4664 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4665
4666 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304667 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004668 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004669 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004670 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004671 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304672 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4673 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304674 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004675
Shivasharan S107a60d2017-10-19 02:49:05 -07004676 megasas_set_dma_settings(instance, dcmd, ci_h,
4677 sizeof(struct megasas_ctrl_info));
4678
Shivasharan Se7d36b82017-10-19 02:48:50 -07004679 if ((instance->adapter_type != MFI_SERIES) &&
4680 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304681 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304682 else
4683 ret = megasas_issue_polled(instance, cmd);
4684
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304685 switch (ret) {
4686 case DCMD_SUCCESS:
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304687 /* Save required controller information in
4688 * CPU endianness format.
4689 */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004690 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
4691 le32_to_cpus((u32 *)&ci->adapterOperations2);
4692 le32_to_cpus((u32 *)&ci->adapterOperations3);
4693 le16_to_cpus((u16 *)&ci->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304694
4695 /* Update the latest Ext VD info.
4696 * From Init path, store current firmware details.
4697 * From OCR path, detect any firmware properties changes.
4698 * in case of Firmware upgrade without system reboot.
4699 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304700 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304701 instance->use_seqnum_jbod_fp =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004702 ci->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004703 instance->support_morethan256jbod =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004704 ci->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304705
4706 /*Check whether controller is iMR or MR */
Shivasharan S9ad18a92017-10-19 02:48:57 -07004707 instance->is_imr = (ci->memory_size ? 0 : 1);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304708 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304709 "controller type\t: %s(%dMB)\n",
4710 instance->is_imr ? "iMR" : "MR",
Shivasharan S9ad18a92017-10-19 02:48:57 -07004711 le16_to_cpu(ci->memory_size));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304712
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304713 instance->disableOnlineCtrlReset =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004714 ci->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304715 instance->secure_jbod_support =
Shivasharan S9ad18a92017-10-19 02:48:57 -07004716 ci->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304717 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4718 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304719 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4720 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304721 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 case DCMD_FAILED:
4739 megaraid_sas_kill_hba(instance);
4740 break;
4741
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304742 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004743
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304744 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304745
4746
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004747 return ret;
4748}
4749
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304750/*
4751 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4752 * to firmware
4753 *
4754 * @instance: Adapter soft state
4755 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4756 MR_CRASH_BUF_TURN_OFF = 0
4757 MR_CRASH_BUF_TURN_ON = 1
4758 * @return 0 on success non-zero on failure.
4759 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4760 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4761 * that driver supports crash dump feature. This DCMD will be sent only if
4762 * crash dump feature is supported by the FW.
4763 *
4764 */
4765int megasas_set_crash_dump_params(struct megasas_instance *instance,
4766 u8 crash_buf_state)
4767{
4768 int ret = 0;
4769 struct megasas_cmd *cmd;
4770 struct megasas_dcmd_frame *dcmd;
4771
4772 cmd = megasas_get_cmd(instance);
4773
4774 if (!cmd) {
4775 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4776 return -ENOMEM;
4777 }
4778
4779
4780 dcmd = &cmd->frame->dcmd;
4781
4782 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4783 dcmd->mbox.b[0] = crash_buf_state;
4784 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304785 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304786 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07004787 dcmd->flags = MFI_FRAME_DIR_NONE;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304788 dcmd->timeout = 0;
4789 dcmd->pad_0 = 0;
4790 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4791 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304792
Shivasharan S107a60d2017-10-19 02:49:05 -07004793 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
4794 CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304795
Shivasharan Se7d36b82017-10-19 02:48:50 -07004796 if ((instance->adapter_type != MFI_SERIES) &&
4797 !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304798 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304799 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304800 ret = megasas_issue_polled(instance, cmd);
4801
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304802 if (ret == DCMD_TIMEOUT) {
4803 switch (dcmd_timeout_ocr_possible(instance)) {
4804 case INITIATE_OCR:
4805 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4806 megasas_reset_fusion(instance->host,
4807 MFI_IO_TIMEOUT_OCR);
4808 break;
4809 case KILL_ADAPTER:
4810 megaraid_sas_kill_hba(instance);
4811 break;
4812 case IGNORE_TIMEOUT:
4813 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4814 __func__, __LINE__);
4815 break;
4816 }
4817 } else
4818 megasas_return_cmd(instance, cmd);
4819
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304820 return ret;
4821}
4822
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004823/**
bo yang31ea7082007-11-07 12:09:50 -05004824 * megasas_issue_init_mfi - Initializes the FW
4825 * @instance: Adapter soft state
4826 *
4827 * Issues the INIT MFI cmd
4828 */
4829static int
4830megasas_issue_init_mfi(struct megasas_instance *instance)
4831{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304832 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004833 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004834 struct megasas_init_frame *init_frame;
4835 struct megasas_init_queue_info *initq_info;
4836 dma_addr_t init_frame_h;
4837 dma_addr_t initq_info_h;
4838
4839 /*
4840 * Prepare a init frame. Note the init frame points to queue info
4841 * structure. Each frame has SGL allocated after first 64 bytes. For
4842 * this frame - since we don't need any SGL - we use SGL's space as
4843 * queue info structure
4844 *
4845 * We will not get a NULL command below. We just created the pool.
4846 */
4847 cmd = megasas_get_cmd(instance);
4848
4849 init_frame = (struct megasas_init_frame *)cmd->frame;
4850 initq_info = (struct megasas_init_queue_info *)
4851 ((unsigned long)init_frame + 64);
4852
4853 init_frame_h = cmd->frame_phys_addr;
4854 initq_info_h = init_frame_h + 64;
4855
4856 context = init_frame->context;
4857 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4858 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4859 init_frame->context = context;
4860
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304861 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4862 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004863
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304864 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4865 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004866
4867 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304868 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304869 init_frame->queue_info_new_phys_addr_lo =
4870 cpu_to_le32(lower_32_bits(initq_info_h));
4871 init_frame->queue_info_new_phys_addr_hi =
4872 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004873
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304874 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004875
4876 /*
4877 * disable the intr before firing the init frame to FW
4878 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304879 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004880
4881 /*
4882 * Issue the init frame in polled mode
4883 */
4884
4885 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004886 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004887 megasas_return_cmd(instance, cmd);
4888 goto fail_fw_init;
4889 }
4890
4891 megasas_return_cmd(instance, cmd);
4892
4893 return 0;
4894
4895fail_fw_init:
4896 return -EINVAL;
4897}
4898
adam radfordcd50ba82010-12-21 10:23:23 -08004899static u32
4900megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004901{
adam radfordcd50ba82010-12-21 10:23:23 -08004902 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004903 u32 context_sz;
4904 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004905
4906 reg_set = instance->reg_set;
4907
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004908 /*
4909 * Get various operational parameters from status register
4910 */
Sumant Patro1341c932006-01-25 12:02:40 -08004911 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004912 /*
4913 * Reduce the max supported cmds by 1. This is to ensure that the
4914 * reply_q_sz (1 more than the max cmd that driver may send)
4915 * does not exceed max cmds that the FW can support
4916 */
4917 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004918 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004919 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004920 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004921 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304922 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4923 * are reserved for IOCTL + driver's internal DCMDs.
4924 */
4925 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4926 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4927 instance->max_scsi_cmds = (instance->max_fw_cmds -
4928 MEGASAS_SKINNY_INT_CMDS);
4929 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4930 } else {
4931 instance->max_scsi_cmds = (instance->max_fw_cmds -
4932 MEGASAS_INT_CMDS);
4933 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4934 }
4935
Sumit Saxena308ec452016-01-28 21:04:30 +05304936 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304937 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004938 * Create a pool of commands
4939 */
4940 if (megasas_alloc_cmds(instance))
4941 goto fail_alloc_cmds;
4942
4943 /*
4944 * Allocate memory for reply queue. Length of reply queue should
4945 * be _one_ more than the maximum commands handled by the firmware.
4946 *
4947 * Note: When FW completes commands, it places corresponding contex
4948 * values in this circular reply queue. This circular queue is a fairly
4949 * typical producer-consumer queue. FW is the producer (of completed
4950 * commands) and the driver is the consumer.
4951 */
4952 context_sz = sizeof(u32);
4953 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4954
4955 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4956 reply_q_sz,
4957 &instance->reply_queue_h);
4958
4959 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004960 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004961 goto fail_reply_queue;
4962 }
4963
bo yang31ea7082007-11-07 12:09:50 -05004964 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004965 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004966
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304967 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304968 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4969 "Fail from %s %d\n", instance->unique_id,
4970 __func__, __LINE__);
4971 goto fail_fw_init;
4972 }
4973
bo yang39a98552010-09-22 22:36:29 -04004974 instance->fw_support_ieee = 0;
4975 instance->fw_support_ieee =
4976 (instance->instancet->read_fw_status_reg(reg_set) &
4977 0x04000000);
4978
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004979 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004980 instance->fw_support_ieee);
4981
4982 if (instance->fw_support_ieee)
4983 instance->flag_ieee = 1;
4984
adam radfordcd50ba82010-12-21 10:23:23 -08004985 return 0;
4986
4987fail_fw_init:
4988
4989 pci_free_consistent(instance->pdev, reply_q_sz,
4990 instance->reply_queue, instance->reply_queue_h);
4991fail_reply_queue:
4992 megasas_free_cmds(instance);
4993
4994fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004995 return 1;
4996}
4997
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304998/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004999 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305000 * @instance: Adapter soft state
5001 *
5002 * Do not enable interrupt, only setup ISRs.
5003 *
5004 * Return 0 on success.
5005 */
5006static int
5007megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5008{
5009 struct pci_dev *pdev;
5010
5011 pdev = instance->pdev;
5012 instance->irq_context[0].instance = instance;
5013 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005014 if (request_irq(pci_irq_vector(pdev, 0),
5015 instance->instancet->service_isr, IRQF_SHARED,
5016 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305017 dev_err(&instance->pdev->dev,
5018 "Failed to register IRQ from %s %d\n",
5019 __func__, __LINE__);
5020 return -1;
5021 }
5022 return 0;
5023}
5024
5025/**
5026 * megasas_setup_irqs_msix - register MSI-x interrupts.
5027 * @instance: Adapter soft state
5028 * @is_probe: Driver probe check
5029 *
5030 * Do not enable interrupt, only setup ISRs.
5031 *
5032 * Return 0 on success.
5033 */
5034static int
5035megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5036{
Hannes Reineckefad119b2016-12-02 12:52:23 +01005037 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305038 struct pci_dev *pdev;
5039
5040 pdev = instance->pdev;
5041
5042 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305043 for (i = 0; i < instance->msix_vectors; i++) {
5044 instance->irq_context[i].instance = instance;
5045 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01005046 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305047 instance->instancet->service_isr, 0, "megasas",
5048 &instance->irq_context[i])) {
5049 dev_err(&instance->pdev->dev,
5050 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01005051 for (j = 0; j < i; j++)
5052 free_irq(pci_irq_vector(pdev, j),
5053 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305054 /* Retry irq register for IO_APIC*/
5055 instance->msix_vectors = 0;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005056 if (is_probe) {
5057 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305058 return megasas_setup_irqs_ioapic(instance);
Shivasharan S64ff64b2017-03-10 03:22:12 -08005059 } else {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305060 return -1;
Shivasharan S64ff64b2017-03-10 03:22:12 -08005061 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305062 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305063 }
5064 return 0;
5065}
5066
5067/*
5068 * megasas_destroy_irqs- unregister interrupts.
5069 * @instance: Adapter soft state
5070 * return: void
5071 */
5072static void
5073megasas_destroy_irqs(struct megasas_instance *instance) {
5074
5075 int i;
5076
5077 if (instance->msix_vectors)
5078 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005079 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305080 &instance->irq_context[i]);
5081 }
5082 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01005083 free_irq(pci_irq_vector(instance->pdev, 0),
5084 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305085}
5086
adam radfordcd50ba82010-12-21 10:23:23 -08005087/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305088 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5089 * @instance: Adapter soft state
5090 * @is_probe: Driver probe check
5091 *
5092 * Return 0 on success.
5093 */
5094void
5095megasas_setup_jbod_map(struct megasas_instance *instance)
5096{
5097 int i;
5098 struct fusion_context *fusion = instance->ctrl_context;
5099 u32 pd_seq_map_sz;
5100
5101 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5102 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5103
5104 if (reset_devices || !fusion ||
Shivasharan S9ad18a92017-10-19 02:48:57 -07005105 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305106 dev_info(&instance->pdev->dev,
5107 "Jbod map is not supported %s %d\n",
5108 __func__, __LINE__);
5109 instance->use_seqnum_jbod_fp = false;
5110 return;
5111 }
5112
5113 if (fusion->pd_seq_sync[0])
5114 goto skip_alloc;
5115
5116 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5117 fusion->pd_seq_sync[i] = dma_alloc_coherent
5118 (&instance->pdev->dev, pd_seq_map_sz,
5119 &fusion->pd_seq_phys[i], GFP_KERNEL);
5120 if (!fusion->pd_seq_sync[i]) {
5121 dev_err(&instance->pdev->dev,
5122 "Failed to allocate memory from %s %d\n",
5123 __func__, __LINE__);
5124 if (i == 1) {
5125 dma_free_coherent(&instance->pdev->dev,
5126 pd_seq_map_sz, fusion->pd_seq_sync[0],
5127 fusion->pd_seq_phys[0]);
5128 fusion->pd_seq_sync[0] = NULL;
5129 }
5130 instance->use_seqnum_jbod_fp = false;
5131 return;
5132 }
5133 }
5134
5135skip_alloc:
5136 if (!megasas_sync_pd_seq_num(instance, false) &&
5137 !megasas_sync_pd_seq_num(instance, true))
5138 instance->use_seqnum_jbod_fp = true;
5139 else
5140 instance->use_seqnum_jbod_fp = false;
5141}
5142
5143/**
adam radfordcd50ba82010-12-21 10:23:23 -08005144 * megasas_init_fw - Initializes the FW
5145 * @instance: Adapter soft state
5146 *
5147 * This is the main function for initializing firmware
5148 */
5149
5150static int megasas_init_fw(struct megasas_instance *instance)
5151{
5152 u32 max_sectors_1;
Shivasharan S15dd0382017-02-10 00:59:10 -08005153 u32 max_sectors_2, tmp_sectors, msix_enable;
5154 u32 scratch_pad_2, scratch_pad_3, scratch_pad_4;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005155 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005156 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305157 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005158 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005159 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005160 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305161 struct fusion_context *fusion;
5162
5163 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005164
5165 /* Find first memory bar */
5166 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005167 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005168 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005169 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005170 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005171 return -EBUSY;
5172 }
5173
Ben Collins11f8a7b2013-09-13 12:46:44 -04005174 base_addr = pci_resource_start(instance->pdev, instance->bar);
5175 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005176
5177 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005178 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005179 goto fail_ioremap;
5180 }
5181
5182 reg_set = instance->reg_set;
5183
Shivasharan Se7d36b82017-10-19 02:48:50 -07005184 if (instance->adapter_type != MFI_SERIES)
adam radford9c915a82010-12-21 13:34:31 -08005185 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005186 else {
5187 switch (instance->pdev->device) {
5188 case PCI_DEVICE_ID_LSI_SAS1078R:
5189 case PCI_DEVICE_ID_LSI_SAS1078DE:
5190 instance->instancet = &megasas_instance_template_ppc;
5191 break;
5192 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5193 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5194 instance->instancet = &megasas_instance_template_gen2;
5195 break;
5196 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5197 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5198 instance->instancet = &megasas_instance_template_skinny;
5199 break;
5200 case PCI_DEVICE_ID_LSI_SAS1064R:
5201 case PCI_DEVICE_ID_DELL_PERC5:
5202 default:
5203 instance->instancet = &megasas_instance_template_xscale;
5204 instance->pd_list_not_supported = 1;
5205 break;
5206 }
adam radfordcd50ba82010-12-21 10:23:23 -08005207 }
5208
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305209 if (megasas_transition_to_ready(instance, 0)) {
5210 atomic_set(&instance->fw_reset_no_pci_access, 1);
5211 instance->instancet->adp_reset
5212 (instance, instance->reg_set);
5213 atomic_set(&instance->fw_reset_no_pci_access, 0);
5214 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005215 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305216 __func__);
5217
5218 /*waitting for about 30 second before retry*/
5219 ssleep(30);
5220
5221 if (megasas_transition_to_ready(instance, 0))
5222 goto fail_ready_state;
5223 }
adam radfordcd50ba82010-12-21 10:23:23 -08005224
Shivasharan Se5d65b42017-10-19 02:48:59 -07005225 megasas_init_ctrl_params(instance);
5226
Shivasharan S107a60d2017-10-19 02:49:05 -07005227 if (megasas_set_dma_mask(instance))
Shivasharan Se5d65b42017-10-19 02:48:59 -07005228 goto fail_ready_state;
5229
5230 if (megasas_alloc_ctrl_mem(instance))
5231 goto fail_alloc_dma_buf;
5232
5233 if (megasas_alloc_ctrl_dma_buffers(instance))
5234 goto fail_alloc_dma_buf;
5235
5236 fusion = instance->ctrl_context;
5237
Shivasharan Sf369a312017-10-19 02:48:52 -07005238 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005239 scratch_pad_3 =
5240 readl(&instance->reg_set->outbound_scratch_pad_3);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005241 instance->max_raid_mapsize = ((scratch_pad_3 >>
5242 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5243 MR_MAX_RAID_MAP_SIZE_MASK);
5244 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305245
adam radford3f1abce2011-05-11 18:33:47 -07005246 /* Check if MSI-X is supported while in ready state */
5247 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5248 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005249 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005250 int irq_flags = PCI_IRQ_MSIX;
5251
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305252 scratch_pad_2 = readl
5253 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005254 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305255 if (fusion) {
Shivasharan Sc3651782017-10-19 02:48:48 -07005256 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5257 /* Thunderbolt Series*/
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305258 instance->msix_vectors = (scratch_pad_2
5259 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5260 fw_msix_count = instance->msix_vectors;
5261 } else { /* Invader series supports more than 8 MSI-x vectors*/
5262 instance->msix_vectors = ((scratch_pad_2
5263 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5264 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005265 if (instance->msix_vectors > 16)
5266 instance->msix_combined = true;
5267
Sumit Saxena179ac142016-01-28 21:04:28 +05305268 if (rdpq_enable)
5269 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5270 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305271 fw_msix_count = instance->msix_vectors;
5272 /* Save 1-15 reply post index address to local memory
5273 * Index 0 is already saved from reg offset
5274 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5275 */
5276 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5277 instance->reply_post_host_index_addr[loop] =
5278 (u32 __iomem *)
5279 ((u8 __iomem *)instance->reg_set +
5280 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5281 + (loop * 0x10));
5282 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305283 }
5284 if (msix_vectors)
5285 instance->msix_vectors = min(msix_vectors,
5286 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305287 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005288 instance->msix_vectors = 1;
5289 /* Don't bother allocating more MSI-X vectors than cpus */
5290 instance->msix_vectors = min(instance->msix_vectors,
5291 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005292 if (smp_affinity_enable)
5293 irq_flags |= PCI_IRQ_AFFINITY;
5294 i = pci_alloc_irq_vectors(instance->pdev, 1,
5295 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005296 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005297 instance->msix_vectors = i;
5298 else
adam radfordc8e858f2011-10-08 18:15:13 -07005299 instance->msix_vectors = 0;
5300 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005301 /*
5302 * MSI-X host index 0 is common for all adapter.
5303 * It is used for all MPT based Adapters.
5304 */
5305 if (instance->msix_combined) {
5306 instance->reply_post_host_index_addr[0] =
5307 (u32 *)((u8 *)instance->reg_set +
5308 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5309 } else {
5310 instance->reply_post_host_index_addr[0] =
5311 (u32 *)((u8 *)instance->reg_set +
5312 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5313 }
5314
Shivasharan S64ff64b2017-03-10 03:22:12 -08005315 if (!instance->msix_vectors) {
5316 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5317 if (i < 0)
5318 goto fail_setup_irqs;
5319 }
adam radford3f1abce2011-05-11 18:33:47 -07005320
Tomas Henzl258c3af2015-06-02 16:09:46 +05305321 dev_info(&instance->pdev->dev,
5322 "firmware supports msix\t: (%d)", fw_msix_count);
5323 dev_info(&instance->pdev->dev,
5324 "current msix/online cpus\t: (%d/%d)\n",
5325 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305326 dev_info(&instance->pdev->dev,
5327 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305328
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305329 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5330 (unsigned long)instance);
5331
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305332 /*
5333 * Below are default value for legacy Firmware.
5334 * non-fusion based controllers
5335 */
5336 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5337 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005338 /* Get operational params, sge flags, send init cmd to controller */
5339 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005340 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005341
Shivasharan Sf369a312017-10-19 02:48:52 -07005342 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S15dd0382017-02-10 00:59:10 -08005343 scratch_pad_4 =
5344 readl(&instance->reg_set->outbound_scratch_pad_4);
5345 if ((scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK) >=
5346 MR_DEFAULT_NVME_PAGE_SHIFT)
5347 instance->nvme_page_size =
5348 (1 << (scratch_pad_4 & MR_NVME_PAGE_SIZE_MASK));
5349
5350 dev_info(&instance->pdev->dev,
5351 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5352 }
5353
Tomas Henzl18103ef2016-11-01 17:32:02 +01005354 if (instance->msix_vectors ?
5355 megasas_setup_irqs_msix(instance, 1) :
5356 megasas_setup_irqs_ioapic(instance))
5357 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305358
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305359 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005360
Andy Lutomirski13f30772016-05-03 10:24:31 -07005361 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005362
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305363 megasas_setup_jbod_map(instance);
5364
bo yang39a98552010-09-22 22:36:29 -04005365 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005366 * the following function will get the PD LIST.
5367 */
5368 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005369 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005370 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005371 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Shivasharan S72bff2d2017-02-10 00:59:33 -08005372 goto fail_get_ld_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005373 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005374
Yang, Bobdc6fb82009-12-06 08:30:19 -07005375 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005376
5377 /* stream detection initialization */
Shivasharan Sf369a312017-10-19 02:48:52 -07005378 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005379 fusion->stream_detect_by_ld =
Shivasharan S41064f12017-02-10 00:59:37 -08005380 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5381 * MAX_LOGICAL_DRIVES_EXT,
5382 GFP_KERNEL);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005383 if (!fusion->stream_detect_by_ld) {
5384 dev_err(&instance->pdev->dev,
Shivasharan S41064f12017-02-10 00:59:37 -08005385 "unable to allocate stream detection for pool of LDs\n");
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005386 goto fail_get_ld_pd_list;
5387 }
5388 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5389 fusion->stream_detect_by_ld[i] =
Shivasharan Se05ee4e2018-01-05 05:27:36 -08005390 kzalloc(sizeof(struct LD_STREAM_DETECT),
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005391 GFP_KERNEL);
5392 if (!fusion->stream_detect_by_ld[i]) {
5393 dev_err(&instance->pdev->dev,
5394 "unable to allocate stream detect by LD\n ");
5395 for (j = 0; j < i; ++j)
5396 kfree(fusion->stream_detect_by_ld[j]);
5397 kfree(fusion->stream_detect_by_ld);
5398 fusion->stream_detect_by_ld = NULL;
5399 goto fail_get_ld_pd_list;
5400 }
5401 fusion->stream_detect_by_ld[i]->mru_bit_map
5402 = MR_STREAM_BITMAP;
5403 }
5404 }
5405
adam radford21c9e162013-09-06 15:27:14 -07005406 if (megasas_ld_list_query(instance,
5407 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
Shivasharan S72bff2d2017-02-10 00:59:33 -08005408 goto fail_get_ld_pd_list;
Yang, Bobdc6fb82009-12-06 08:30:19 -07005409
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005410 /*
5411 * Compute the max allowed sectors per IO: The controller info has two
5412 * limits on max sectors. Driver should use the minimum of these two.
5413 *
5414 * 1 << stripe_sz_ops.min = max sectors per strip
5415 *
5416 * Note that older firmwares ( < FW ver 30) didn't report information
5417 * to calculate max_sectors_1. So the number ended up as zero always.
5418 */
bo yang14faea92007-11-09 04:14:00 -05005419 tmp_sectors = 0;
Shivasharan S9ad18a92017-10-19 02:48:57 -07005420 ctrl_info = instance->ctrl_info_buf;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005421
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305422 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5423 le16_to_cpu(ctrl_info->max_strips_per_io);
5424 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005425
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005426 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305427
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305428 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5429 instance->passive = ctrl_info->cluster.passive;
5430 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305431 instance->UnevenSpanSupport =
5432 ctrl_info->adapterOperations2.supportUnevenSpans;
5433 if (instance->UnevenSpanSupport) {
5434 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305435 if (MR_ValidateMapInfo(instance))
5436 fusion->fast_path_io = 1;
5437 else
5438 fusion->fast_path_io = 0;
5439
5440 }
5441 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305442 instance->requestorId = ctrl_info->iov.requestorId;
5443 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5444 if (!ctrl_info->adapterOperations2.activePassive)
5445 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305446
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305447 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5448 instance->PlasmaFW111 ? "1.11" : "new");
5449
5450 if (instance->PlasmaFW111) {
5451 iovPtr = (struct IOV_111 *)
5452 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5453 instance->requestorId = iovPtr->requestorId;
5454 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305455 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305456 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5457 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305458 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305459
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305460 instance->crash_dump_fw_support =
5461 ctrl_info->adapterOperations3.supportCrashDump;
5462 instance->crash_dump_drv_support =
5463 (instance->crash_dump_fw_support &&
5464 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305465 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305466 megasas_set_crash_dump_params(instance,
5467 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005468
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305469 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305470 if (instance->crash_dump_buf)
5471 pci_free_consistent(instance->pdev,
5472 CRASH_DMA_BUF_SIZE,
5473 instance->crash_dump_buf,
5474 instance->crash_dump_h);
5475 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005476 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305477
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305478
5479 dev_info(&instance->pdev->dev,
5480 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5481 le16_to_cpu(ctrl_info->pci.vendor_id),
5482 le16_to_cpu(ctrl_info->pci.device_id),
5483 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5484 le16_to_cpu(ctrl_info->pci.sub_device_id));
5485 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5486 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305487 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5488 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305489 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5490 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305491
5492
bo yang14faea92007-11-09 04:14:00 -05005493 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305494 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005495 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5496 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005497
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305498 /* Check for valid throttlequeuedepth module parameter */
5499 if (throttlequeuedepth &&
5500 throttlequeuedepth <= instance->max_scsi_cmds)
5501 instance->throttlequeuedepth = throttlequeuedepth;
5502 else
5503 instance->throttlequeuedepth =
5504 MEGASAS_THROTTLE_QUEUE_DEPTH;
5505
Shivasharan Se636a7a2017-08-23 04:46:56 -07005506 if ((resetwaittime < 1) ||
5507 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305508 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5509
5510 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5511 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005512
adam radford229fe472014-03-10 02:51:56 -07005513 /* Launch SR-IOV heartbeat timer */
5514 if (instance->requestorId) {
5515 if (!megasas_sriov_start_heartbeat(instance, 1))
Kees Cookc251a7b2017-10-22 15:30:04 -07005516 megasas_start_timer(instance);
adam radford229fe472014-03-10 02:51:56 -07005517 else
5518 instance->skip_heartbeat_timer_del = 1;
5519 }
5520
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005521 return 0;
5522
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005523fail_get_ld_pd_list:
5524 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005525fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005526 megasas_destroy_irqs(instance);
5527fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305528 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005529 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305530 instance->msix_vectors = 0;
Shivasharan Se5d65b42017-10-19 02:48:59 -07005531fail_alloc_dma_buf:
5532 megasas_free_ctrl_dma_buffers(instance);
5533 megasas_free_ctrl_mem(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005534fail_ready_state:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005535 iounmap(instance->reg_set);
5536
Shivasharan S72bff2d2017-02-10 00:59:33 -08005537fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005538 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005539
Shivasharan S72bff2d2017-02-10 00:59:33 -08005540 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5541 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005542 return -EINVAL;
5543}
5544
5545/**
5546 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005547 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005548 */
5549static void megasas_release_mfi(struct megasas_instance *instance)
5550{
adam radford9c915a82010-12-21 13:34:31 -08005551 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005552
adam radford9c915a82010-12-21 13:34:31 -08005553 if (instance->reply_queue)
5554 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005555 instance->reply_queue, instance->reply_queue_h);
5556
5557 megasas_free_cmds(instance);
5558
5559 iounmap(instance->reg_set);
5560
Yinghai Lue7f85162016-08-05 23:37:34 -07005561 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005562}
5563
5564/**
5565 * megasas_get_seq_num - Gets latest event sequence numbers
5566 * @instance: Adapter soft state
5567 * @eli: FW event log sequence numbers information
5568 *
5569 * FW maintains a log of all events in a non-volatile area. Upper layers would
5570 * usually find out the latest sequence number of the events, the seq number at
5571 * the boot etc. They would "read" all the events below the latest seq number
5572 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5573 * number), they would subsribe to AEN (asynchronous event notification) and
5574 * wait for the events to happen.
5575 */
5576static int
5577megasas_get_seq_num(struct megasas_instance *instance,
5578 struct megasas_evt_log_info *eli)
5579{
5580 struct megasas_cmd *cmd;
5581 struct megasas_dcmd_frame *dcmd;
5582 struct megasas_evt_log_info *el_info;
5583 dma_addr_t el_info_h = 0;
Shivasharan Sb051cc62018-01-05 05:27:38 -08005584 int ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005585
5586 cmd = megasas_get_cmd(instance);
5587
5588 if (!cmd) {
5589 return -ENOMEM;
5590 }
5591
5592 dcmd = &cmd->frame->dcmd;
5593 el_info = pci_alloc_consistent(instance->pdev,
5594 sizeof(struct megasas_evt_log_info),
5595 &el_info_h);
5596
5597 if (!el_info) {
5598 megasas_return_cmd(instance, cmd);
5599 return -ENOMEM;
5600 }
5601
5602 memset(el_info, 0, sizeof(*el_info));
5603 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5604
5605 dcmd->cmd = MFI_CMD_DCMD;
5606 dcmd->cmd_status = 0x0;
5607 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005608 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005609 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005610 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305611 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5612 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07005613
5614 megasas_set_dma_settings(instance, dcmd, el_info_h,
5615 sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005616
Shivasharan Sb051cc62018-01-05 05:27:38 -08005617 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5618 if (ret != DCMD_SUCCESS) {
5619 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5620 __func__, __LINE__);
5621 goto dcmd_failed;
5622 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005623
Shivasharan Sb051cc62018-01-05 05:27:38 -08005624 /*
5625 * Copy the data back into callers buffer
5626 */
5627 eli->newest_seq_num = el_info->newest_seq_num;
5628 eli->oldest_seq_num = el_info->oldest_seq_num;
5629 eli->clear_seq_num = el_info->clear_seq_num;
5630 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5631 eli->boot_seq_num = el_info->boot_seq_num;
5632
5633dcmd_failed:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005634 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5635 el_info, el_info_h);
5636
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305637 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005638
Shivasharan Sb051cc62018-01-05 05:27:38 -08005639 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005640}
5641
5642/**
5643 * megasas_register_aen - Registers for asynchronous event notification
5644 * @instance: Adapter soft state
5645 * @seq_num: The starting sequence number
5646 * @class_locale: Class of the event
5647 *
5648 * This function subscribes for AEN for events beyond the @seq_num. It requests
5649 * to be notified if and only if the event is of type @class_locale
5650 */
5651static int
5652megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5653 u32 class_locale_word)
5654{
5655 int ret_val;
5656 struct megasas_cmd *cmd;
5657 struct megasas_dcmd_frame *dcmd;
5658 union megasas_evt_class_locale curr_aen;
5659 union megasas_evt_class_locale prev_aen;
5660
5661 /*
5662 * If there an AEN pending already (aen_cmd), check if the
5663 * class_locale of that pending AEN is inclusive of the new
5664 * AEN request we currently have. If it is, then we don't have
5665 * to do anything. In other words, whichever events the current
5666 * AEN request is subscribing to, have already been subscribed
5667 * to.
5668 *
5669 * If the old_cmd is _not_ inclusive, then we have to abort
5670 * that command, form a class_locale that is superset of both
5671 * old and current and re-issue to the FW
5672 */
5673
5674 curr_aen.word = class_locale_word;
5675
5676 if (instance->aen_cmd) {
5677
Christoph Hellwiga9555532015-04-23 16:34:24 +05305678 prev_aen.word =
5679 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005680
Shivasharan S91b3d9f2017-08-23 04:47:01 -07005681 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
5682 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
5683 dev_info(&instance->pdev->dev,
5684 "%s %d out of range class %d send by application\n",
5685 __func__, __LINE__, curr_aen.members.class);
5686 return 0;
5687 }
5688
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005689 /*
5690 * A class whose enum value is smaller is inclusive of all
5691 * higher values. If a PROGRESS (= -1) was previously
5692 * registered, then a new registration requests for higher
5693 * classes need not be sent to FW. They are automatically
5694 * included.
5695 *
5696 * Locale numbers don't have such hierarchy. They are bitmap
5697 * values
5698 */
5699 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305700 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005701 curr_aen.members.locale)) {
5702 /*
5703 * Previously issued event registration includes
5704 * current request. Nothing to do.
5705 */
5706 return 0;
5707 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305708 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005709
5710 if (prev_aen.members.class < curr_aen.members.class)
5711 curr_aen.members.class = prev_aen.members.class;
5712
5713 instance->aen_cmd->abort_aen = 1;
5714 ret_val = megasas_issue_blocked_abort_cmd(instance,
5715 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305716 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005717
5718 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005719 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005720 "previous AEN command\n");
5721 return ret_val;
5722 }
5723 }
5724 }
5725
5726 cmd = megasas_get_cmd(instance);
5727
5728 if (!cmd)
5729 return -ENOMEM;
5730
5731 dcmd = &cmd->frame->dcmd;
5732
5733 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5734
5735 /*
5736 * Prepare DCMD for aen registration
5737 */
5738 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5739
5740 dcmd->cmd = MFI_CMD_DCMD;
5741 dcmd->cmd_status = 0x0;
5742 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005743 dcmd->flags = MFI_FRAME_DIR_READ;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005744 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005745 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305746 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5747 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5748 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005749 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305750 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
Shivasharan S107a60d2017-10-19 02:49:05 -07005751
5752 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
5753 sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005754
Yang, Bof4c9a132009-10-06 14:43:28 -06005755 if (instance->aen_cmd != NULL) {
5756 megasas_return_cmd(instance, cmd);
5757 return 0;
5758 }
5759
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005760 /*
5761 * Store reference to the cmd used to register for AEN. When an
5762 * application wants us to register for AEN, we have to abort this
5763 * cmd and re-register with a new EVENT LOCALE supplied by that app
5764 */
5765 instance->aen_cmd = cmd;
5766
5767 /*
5768 * Issue the aen registration frame
5769 */
adam radford9c915a82010-12-21 13:34:31 -08005770 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005771
5772 return 0;
5773}
5774
Shivasharan S96188a82017-02-10 00:59:11 -08005775/* megasas_get_target_prop - Send DCMD with below details to firmware.
5776 *
5777 * This DCMD will fetch few properties of LD/system PD defined
5778 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
5779 *
5780 * DCMD send by drivers whenever new target is added to the OS.
5781 *
5782 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
5783 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
5784 * 0 = system PD, 1 = LD.
5785 * dcmd.mbox.s[1] - TargetID for LD/system PD.
5786 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
5787 *
5788 * @instance: Adapter soft state
5789 * @sdev: OS provided scsi device
5790 *
5791 * Returns 0 on success non-zero on failure.
5792 */
5793static int
5794megasas_get_target_prop(struct megasas_instance *instance,
5795 struct scsi_device *sdev)
5796{
5797 int ret;
5798 struct megasas_cmd *cmd;
5799 struct megasas_dcmd_frame *dcmd;
5800 u16 targetId = (sdev->channel % 2) + sdev->id;
5801
5802 cmd = megasas_get_cmd(instance);
5803
5804 if (!cmd) {
5805 dev_err(&instance->pdev->dev,
5806 "Failed to get cmd %s\n", __func__);
5807 return -ENOMEM;
5808 }
5809
5810 dcmd = &cmd->frame->dcmd;
5811
5812 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
5813 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5814 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
5815
5816 dcmd->mbox.s[1] = cpu_to_le16(targetId);
5817 dcmd->cmd = MFI_CMD_DCMD;
5818 dcmd->cmd_status = 0xFF;
5819 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07005820 dcmd->flags = MFI_FRAME_DIR_READ;
Shivasharan S96188a82017-02-10 00:59:11 -08005821 dcmd->timeout = 0;
5822 dcmd->pad_0 = 0;
5823 dcmd->data_xfer_len =
5824 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
5825 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
Shivasharan S96188a82017-02-10 00:59:11 -08005826
Shivasharan S107a60d2017-10-19 02:49:05 -07005827 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
5828 sizeof(struct MR_TARGET_PROPERTIES));
Shivasharan S96188a82017-02-10 00:59:11 -08005829
Shivasharan Se7d36b82017-10-19 02:48:50 -07005830 if ((instance->adapter_type != MFI_SERIES) &&
5831 !instance->mask_interrupts)
Shivasharan S96188a82017-02-10 00:59:11 -08005832 ret = megasas_issue_blocked_cmd(instance,
5833 cmd, MFI_IO_TIMEOUT_SECS);
5834 else
5835 ret = megasas_issue_polled(instance, cmd);
5836
5837 switch (ret) {
5838 case DCMD_TIMEOUT:
5839 switch (dcmd_timeout_ocr_possible(instance)) {
5840 case INITIATE_OCR:
5841 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5842 megasas_reset_fusion(instance->host,
5843 MFI_IO_TIMEOUT_OCR);
5844 break;
5845 case KILL_ADAPTER:
5846 megaraid_sas_kill_hba(instance);
5847 break;
5848 case IGNORE_TIMEOUT:
5849 dev_info(&instance->pdev->dev,
5850 "Ignore DCMD timeout: %s %d\n",
5851 __func__, __LINE__);
5852 break;
5853 }
5854 break;
5855
5856 default:
5857 megasas_return_cmd(instance, cmd);
5858 }
5859 if (ret != DCMD_SUCCESS)
5860 dev_err(&instance->pdev->dev,
5861 "return from %s %d return value %d\n",
5862 __func__, __LINE__, ret);
5863
5864 return ret;
5865}
5866
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005867/**
5868 * megasas_start_aen - Subscribes to AEN during driver load time
5869 * @instance: Adapter soft state
5870 */
5871static int megasas_start_aen(struct megasas_instance *instance)
5872{
5873 struct megasas_evt_log_info eli;
5874 union megasas_evt_class_locale class_locale;
5875
5876 /*
5877 * Get the latest sequence number from FW
5878 */
5879 memset(&eli, 0, sizeof(eli));
5880
5881 if (megasas_get_seq_num(instance, &eli))
5882 return -1;
5883
5884 /*
5885 * Register AEN with FW for latest sequence number plus 1
5886 */
5887 class_locale.members.reserved = 0;
5888 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5889 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5890
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305891 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305892 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305893 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005894}
5895
5896/**
5897 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5898 * @instance: Adapter soft state
5899 */
5900static int megasas_io_attach(struct megasas_instance *instance)
5901{
5902 struct Scsi_Host *host = instance->host;
5903
5904 /*
5905 * Export parameters required by SCSI mid-layer
5906 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005907 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305908 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005909 host->this_id = instance->init_id;
5910 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005911
5912 if (instance->fw_support_ieee)
5913 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5914
Yang, Bo1fd10682010-10-12 07:18:50 -06005915 /*
5916 * Check if the module parameter value for max_sectors can be used
5917 */
5918 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5919 instance->max_sectors_per_req = max_sectors;
5920 else {
5921 if (max_sectors) {
5922 if (((instance->pdev->device ==
5923 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5924 (instance->pdev->device ==
5925 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5926 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5927 instance->max_sectors_per_req = max_sectors;
5928 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005929 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005930 "and <= %d (or < 1MB for GEN2 controller)\n",
5931 instance->max_sectors_per_req);
5932 }
5933 }
5934 }
5935
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005936 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005937 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005938 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5939 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5940 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005941 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005942
5943 /*
5944 * Notify the mid-layer about the new controller
5945 */
5946 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305947 dev_err(&instance->pdev->dev,
5948 "Failed to add host from %s %d\n",
5949 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005950 return -ENODEV;
5951 }
5952
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005953 return 0;
5954}
5955
Shivasharan S107a60d2017-10-19 02:49:05 -07005956/**
5957 * megasas_set_dma_mask - Set DMA mask for supported controllers
5958 *
5959 * @instance: Adapter soft state
5960 * Description:
5961 *
5962 * For Ventura, driver/FW will operate in 64bit DMA addresses.
5963 *
5964 * For invader-
5965 * By default, driver/FW will operate in 32bit DMA addresses
5966 * for consistent DMA mapping but if 32 bit consistent
5967 * DMA mask fails, driver will try with 64 bit consistent
5968 * mask provided FW is true 64bit DMA capable
5969 *
5970 * For older controllers(Thunderbolt and MFI based adapters)-
5971 * driver/FW will operate in 32 bit consistent DMA addresses.
5972 */
bo yang31ea7082007-11-07 12:09:50 -05005973static int
Shivasharan S107a60d2017-10-19 02:49:05 -07005974megasas_set_dma_mask(struct megasas_instance *instance)
bo yang31ea7082007-11-07 12:09:50 -05005975{
Shivasharan S107a60d2017-10-19 02:49:05 -07005976 u64 consistent_mask;
5977 struct pci_dev *pdev;
5978 u32 scratch_pad_2;
bo yang31ea7082007-11-07 12:09:50 -05005979
Shivasharan S107a60d2017-10-19 02:49:05 -07005980 pdev = instance->pdev;
5981 consistent_mask = (instance->adapter_type == VENTURA_SERIES) ?
5982 DMA_BIT_MASK(64) : DMA_BIT_MASK(32);
5983
5984 if (IS_DMA64) {
5985 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)) &&
5986 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
5987 goto fail_set_dma_mask;
5988
5989 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(64)) &&
5990 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
5991 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
5992 /*
5993 * If 32 bit DMA mask fails, then try for 64 bit mask
5994 * for FW capable of handling 64 bit DMA.
5995 */
5996 scratch_pad_2 = readl
5997 (&instance->reg_set->outbound_scratch_pad_2);
5998
5999 if (!(scratch_pad_2 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6000 goto fail_set_dma_mask;
6001 else if (dma_set_mask_and_coherent(&pdev->dev,
6002 DMA_BIT_MASK(64)))
bo yang31ea7082007-11-07 12:09:50 -05006003 goto fail_set_dma_mask;
6004 }
Shivasharan S107a60d2017-10-19 02:49:05 -07006005 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6006 goto fail_set_dma_mask;
6007
6008 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6009 instance->consistent_mask_64bit = false;
6010 else
6011 instance->consistent_mask_64bit = true;
6012
6013 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6014 ((*pdev->dev.dma_mask == DMA_BIT_MASK(64)) ? "64" : "32"),
6015 (instance->consistent_mask_64bit ? "64" : "32"));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306016
bo yang31ea7082007-11-07 12:09:50 -05006017 return 0;
6018
6019fail_set_dma_mask:
Shivasharan S107a60d2017-10-19 02:49:05 -07006020 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6021 return -1;
6022
bo yang31ea7082007-11-07 12:09:50 -05006023}
6024
Shivasharan Sc3651782017-10-19 02:48:48 -07006025/*
6026 * megasas_set_adapter_type - Set adapter type.
6027 * Supported controllers can be divided in
6028 * 4 categories- enum MR_ADAPTER_TYPE {
6029 * MFI_SERIES = 1,
6030 * THUNDERBOLT_SERIES = 2,
6031 * INVADER_SERIES = 3,
6032 * VENTURA_SERIES = 4,
6033 * };
6034 * @instance: Adapter soft state
6035 * return: void
6036 */
6037static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6038{
Shivasharan S754f1ba2017-10-19 02:48:49 -07006039 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6040 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
Shivasharan Sc3651782017-10-19 02:48:48 -07006041 instance->adapter_type = MFI_SERIES;
Shivasharan S754f1ba2017-10-19 02:48:49 -07006042 } else {
6043 switch (instance->pdev->device) {
6044 case PCI_DEVICE_ID_LSI_VENTURA:
6045 case PCI_DEVICE_ID_LSI_CRUSADER:
6046 case PCI_DEVICE_ID_LSI_HARPOON:
6047 case PCI_DEVICE_ID_LSI_TOMCAT:
6048 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6049 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6050 instance->adapter_type = VENTURA_SERIES;
6051 break;
6052 case PCI_DEVICE_ID_LSI_FUSION:
6053 case PCI_DEVICE_ID_LSI_PLASMA:
6054 instance->adapter_type = THUNDERBOLT_SERIES;
6055 break;
6056 case PCI_DEVICE_ID_LSI_INVADER:
6057 case PCI_DEVICE_ID_LSI_INTRUDER:
6058 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6059 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6060 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6061 case PCI_DEVICE_ID_LSI_FURY:
6062 instance->adapter_type = INVADER_SERIES;
6063 break;
6064 default: /* For all other supported controllers */
6065 instance->adapter_type = MFI_SERIES;
6066 break;
6067 }
Shivasharan Sc3651782017-10-19 02:48:48 -07006068 }
6069}
6070
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006071static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6072{
6073 instance->producer = pci_alloc_consistent(instance->pdev, sizeof(u32),
6074 &instance->producer_h);
6075 instance->consumer = pci_alloc_consistent(instance->pdev, sizeof(u32),
6076 &instance->consumer_h);
6077
6078 if (!instance->producer || !instance->consumer) {
6079 dev_err(&instance->pdev->dev,
6080 "Failed to allocate memory for producer, consumer\n");
6081 return -1;
6082 }
6083
6084 *instance->producer = 0;
6085 *instance->consumer = 0;
6086 return 0;
6087}
6088
6089/**
6090 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6091 * structures which are not common across MFI
6092 * adapters and fusion adapters.
6093 * For MFI based adapters, allocate producer and
6094 * consumer buffers. For fusion adapters, allocate
6095 * memory for fusion context.
6096 * @instance: Adapter soft state
6097 * return: 0 for SUCCESS
6098 */
6099static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6100{
6101 switch (instance->adapter_type) {
6102 case MFI_SERIES:
6103 if (megasas_alloc_mfi_ctrl_mem(instance))
6104 return -ENOMEM;
6105 break;
6106 case VENTURA_SERIES:
6107 case THUNDERBOLT_SERIES:
6108 case INVADER_SERIES:
6109 if (megasas_alloc_fusion_context(instance))
6110 return -ENOMEM;
6111 break;
6112 }
6113
6114 return 0;
6115}
6116
6117/*
6118 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6119 * producer, consumer buffers for MFI adapters
6120 *
6121 * @instance - Adapter soft instance
6122 *
6123 */
6124static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6125{
6126 if (instance->adapter_type == MFI_SERIES) {
6127 if (instance->producer)
6128 pci_free_consistent(instance->pdev, sizeof(u32),
6129 instance->producer,
6130 instance->producer_h);
6131 if (instance->consumer)
6132 pci_free_consistent(instance->pdev, sizeof(u32),
6133 instance->consumer,
6134 instance->consumer_h);
6135 } else {
6136 megasas_free_fusion_context(instance);
6137 }
6138}
6139
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006140/**
Shivasharan S1b4bed22017-10-19 02:48:55 -07006141 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6142 * driver load time
6143 *
6144 * @instance- Adapter soft instance
6145 * @return- O for SUCCESS
6146 */
6147static inline
6148int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6149{
6150 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006151 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006152
6153 instance->evt_detail =
6154 pci_alloc_consistent(pdev,
6155 sizeof(struct megasas_evt_detail),
6156 &instance->evt_detail_h);
6157
6158 if (!instance->evt_detail) {
6159 dev_err(&instance->pdev->dev,
6160 "Failed to allocate event detail buffer\n");
6161 return -ENOMEM;
6162 }
6163
Shivasharan S9b3d0282017-10-19 02:48:56 -07006164 if (fusion) {
6165 fusion->ioc_init_request =
6166 dma_alloc_coherent(&pdev->dev,
6167 sizeof(struct MPI2_IOC_INIT_REQUEST),
6168 &fusion->ioc_init_request_phys,
6169 GFP_KERNEL);
6170
6171 if (!fusion->ioc_init_request) {
6172 dev_err(&pdev->dev,
6173 "Failed to allocate PD list buffer\n");
6174 return -ENOMEM;
6175 }
6176 }
6177
6178 instance->pd_list_buf =
6179 pci_alloc_consistent(pdev,
6180 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6181 &instance->pd_list_buf_h);
6182
6183 if (!instance->pd_list_buf) {
6184 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6185 return -ENOMEM;
6186 }
6187
6188 instance->ctrl_info_buf =
6189 pci_alloc_consistent(pdev,
6190 sizeof(struct megasas_ctrl_info),
6191 &instance->ctrl_info_buf_h);
6192
6193 if (!instance->ctrl_info_buf) {
6194 dev_err(&pdev->dev,
6195 "Failed to allocate controller info buffer\n");
6196 return -ENOMEM;
6197 }
6198
6199 instance->ld_list_buf =
6200 pci_alloc_consistent(pdev,
6201 sizeof(struct MR_LD_LIST),
6202 &instance->ld_list_buf_h);
6203
6204 if (!instance->ld_list_buf) {
6205 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6206 return -ENOMEM;
6207 }
6208
6209 instance->ld_targetid_list_buf =
6210 pci_alloc_consistent(pdev,
6211 sizeof(struct MR_LD_TARGETID_LIST),
6212 &instance->ld_targetid_list_buf_h);
6213
6214 if (!instance->ld_targetid_list_buf) {
6215 dev_err(&pdev->dev,
6216 "Failed to allocate LD targetid list buffer\n");
6217 return -ENOMEM;
6218 }
6219
Shivasharan S1b4bed22017-10-19 02:48:55 -07006220 if (!reset_devices) {
6221 instance->system_info_buf =
6222 pci_alloc_consistent(pdev,
6223 sizeof(struct MR_DRV_SYSTEM_INFO),
6224 &instance->system_info_h);
6225 instance->pd_info =
6226 pci_alloc_consistent(pdev,
6227 sizeof(struct MR_PD_INFO),
6228 &instance->pd_info_h);
6229 instance->tgt_prop =
6230 pci_alloc_consistent(pdev,
6231 sizeof(struct MR_TARGET_PROPERTIES),
6232 &instance->tgt_prop_h);
6233 instance->crash_dump_buf =
6234 pci_alloc_consistent(pdev,
6235 CRASH_DMA_BUF_SIZE,
6236 &instance->crash_dump_h);
6237
6238 if (!instance->system_info_buf)
6239 dev_err(&instance->pdev->dev,
6240 "Failed to allocate system info buffer\n");
6241
6242 if (!instance->pd_info)
6243 dev_err(&instance->pdev->dev,
6244 "Failed to allocate pd_info buffer\n");
6245
6246 if (!instance->tgt_prop)
6247 dev_err(&instance->pdev->dev,
6248 "Failed to allocate tgt_prop buffer\n");
6249
6250 if (!instance->crash_dump_buf)
6251 dev_err(&instance->pdev->dev,
6252 "Failed to allocate crash dump buffer\n");
6253 }
6254
6255 return 0;
6256}
6257
6258/*
6259 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6260 * during driver load time
6261 *
6262 * @instance- Adapter soft instance
6263 *
6264 */
6265static inline
6266void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6267{
6268 struct pci_dev *pdev = instance->pdev;
Shivasharan S9b3d0282017-10-19 02:48:56 -07006269 struct fusion_context *fusion = instance->ctrl_context;
Shivasharan S1b4bed22017-10-19 02:48:55 -07006270
6271 if (instance->evt_detail)
6272 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6273 instance->evt_detail,
6274 instance->evt_detail_h);
6275
Shivasharan S9b3d0282017-10-19 02:48:56 -07006276 if (fusion && fusion->ioc_init_request)
6277 dma_free_coherent(&pdev->dev,
6278 sizeof(struct MPI2_IOC_INIT_REQUEST),
6279 fusion->ioc_init_request,
6280 fusion->ioc_init_request_phys);
6281
6282 if (instance->pd_list_buf)
6283 pci_free_consistent(pdev,
6284 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6285 instance->pd_list_buf,
6286 instance->pd_list_buf_h);
6287
6288 if (instance->ld_list_buf)
6289 pci_free_consistent(pdev, sizeof(struct MR_LD_LIST),
6290 instance->ld_list_buf,
6291 instance->ld_list_buf_h);
6292
6293 if (instance->ld_targetid_list_buf)
6294 pci_free_consistent(pdev, sizeof(struct MR_LD_TARGETID_LIST),
6295 instance->ld_targetid_list_buf,
6296 instance->ld_targetid_list_buf_h);
6297
6298 if (instance->ctrl_info_buf)
6299 pci_free_consistent(pdev, sizeof(struct megasas_ctrl_info),
6300 instance->ctrl_info_buf,
6301 instance->ctrl_info_buf_h);
6302
Shivasharan S1b4bed22017-10-19 02:48:55 -07006303 if (instance->system_info_buf)
6304 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6305 instance->system_info_buf,
6306 instance->system_info_h);
6307
6308 if (instance->pd_info)
6309 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6310 instance->pd_info, instance->pd_info_h);
6311
6312 if (instance->tgt_prop)
6313 pci_free_consistent(pdev, sizeof(struct MR_TARGET_PROPERTIES),
6314 instance->tgt_prop, instance->tgt_prop_h);
6315
6316 if (instance->crash_dump_buf)
6317 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6318 instance->crash_dump_buf,
6319 instance->crash_dump_h);
6320}
6321
Shivasharan S7535f272017-10-19 02:48:58 -07006322/*
6323 * megasas_init_ctrl_params - Initialize controller's instance
6324 * parameters before FW init
6325 * @instance - Adapter soft instance
6326 * @return - void
6327 */
6328static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6329{
6330 instance->fw_crash_state = UNAVAILABLE;
6331
6332 megasas_poll_wait_aen = 0;
6333 instance->issuepend_done = 1;
6334 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6335
6336 /*
6337 * Initialize locks and queues
6338 */
6339 INIT_LIST_HEAD(&instance->cmd_pool);
6340 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6341
6342 atomic_set(&instance->fw_outstanding, 0);
6343
6344 init_waitqueue_head(&instance->int_cmd_wait_q);
6345 init_waitqueue_head(&instance->abort_cmd_wait_q);
6346
6347 spin_lock_init(&instance->crashdump_lock);
6348 spin_lock_init(&instance->mfi_pool_lock);
6349 spin_lock_init(&instance->hba_lock);
6350 spin_lock_init(&instance->stream_lock);
6351 spin_lock_init(&instance->completion_lock);
6352
6353 mutex_init(&instance->hba_mutex);
6354 mutex_init(&instance->reset_mutex);
6355
6356 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6357 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6358 instance->flag_ieee = 1;
6359
6360 megasas_dbg_lvl = 0;
6361 instance->flag = 0;
6362 instance->unload = 1;
6363 instance->last_time = 0;
6364 instance->disableOnlineCtrlReset = 1;
6365 instance->UnevenSpanSupport = 0;
6366
6367 if (instance->adapter_type != MFI_SERIES) {
6368 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
6369 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
6370 } else {
6371 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
6372 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006373}
6374
6375/**
6376 * megasas_probe_one - PCI hotplug entry point
6377 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08006378 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006379 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006380static int megasas_probe_one(struct pci_dev *pdev,
6381 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006382{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306383 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006384 struct Scsi_Host *host;
6385 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08006386 u16 control = 0;
6387
6388 /* Reset MSI-X in the kdump kernel */
6389 if (reset_devices) {
6390 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6391 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06006392 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006393 &control);
6394 if (control & PCI_MSIX_FLAGS_ENABLE) {
6395 dev_info(&pdev->dev, "resetting MSI-X\n");
6396 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06006397 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08006398 control &
6399 ~PCI_MSIX_FLAGS_ENABLE);
6400 }
6401 }
6402 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006403
6404 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006405 * PCI prepping: enable device set bus mastering and dma mask
6406 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006407 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006408
6409 if (rval) {
6410 return rval;
6411 }
6412
6413 pci_set_master(pdev);
6414
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006415 host = scsi_host_alloc(&megasas_template,
6416 sizeof(struct megasas_instance));
6417
6418 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006419 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006420 goto fail_alloc_instance;
6421 }
6422
6423 instance = (struct megasas_instance *)host->hostdata;
6424 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006425 atomic_set(&instance->fw_reset_no_pci_access, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006426
6427 /*
6428 * Initialize PCI related and misc parameters
6429 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006430 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006431 instance->host = host;
6432 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6433 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6434
Shivasharan Sc3651782017-10-19 02:48:48 -07006435 megasas_set_adapter_type(instance);
Sumant Patro658dced2006-10-03 13:09:14 -07006436
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006437 /*
adam radford0a770662011-02-24 20:56:12 -08006438 * Initialize MFI Firmware
6439 */
6440 if (megasas_init_fw(instance))
6441 goto fail_init_mfi;
6442
adam radford229fe472014-03-10 02:51:56 -07006443 if (instance->requestorId) {
6444 if (instance->PlasmaFW111) {
6445 instance->vf_affiliation_111 =
6446 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
6447 &instance->vf_affiliation_111_h);
6448 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006449 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006450 "memory for VF affiliation buffer\n");
6451 } else {
6452 instance->vf_affiliation =
6453 pci_alloc_consistent(pdev,
6454 (MAX_LOGICAL_DRIVES + 1) *
6455 sizeof(struct MR_LD_VF_AFFILIATION),
6456 &instance->vf_affiliation_h);
6457 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006458 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07006459 "memory for VF affiliation buffer\n");
6460 }
6461 }
6462
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006463 /*
6464 * Store instance in PCI softstate
6465 */
6466 pci_set_drvdata(pdev, instance);
6467
6468 /*
6469 * Add this controller to megasas_mgmt_info structure so that it
6470 * can be exported to management applications
6471 */
6472 megasas_mgmt_info.count++;
6473 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6474 megasas_mgmt_info.max_index++;
6475
6476 /*
adam radford541f90b2011-05-11 18:34:29 -07006477 * Register with SCSI mid-layer
6478 */
6479 if (megasas_io_attach(instance))
6480 goto fail_io_attach;
6481
6482 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306483 /*
6484 * Trigger SCSI to scan our drives
6485 */
6486 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07006487
6488 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006489 * Initiate AEN (Asynchronous Event Notification)
6490 */
6491 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006492 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006493 goto fail_start_aen;
6494 }
6495
Adam Radford9ea81f82014-07-09 15:17:57 -07006496 /* Get current SR-IOV LD/VF affiliation */
6497 if (instance->requestorId)
6498 megasas_get_ld_vf_affiliation(instance, 1);
6499
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006500 return 0;
6501
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006502fail_start_aen:
6503fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006504 megasas_mgmt_info.count--;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006505 megasas_mgmt_info.max_index--;
weiping zhang61f0c3c2017-08-08 01:26:57 +08006506 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006507
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306508 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306509 megasas_destroy_irqs(instance);
6510
Shivasharan Se7d36b82017-10-19 02:48:50 -07006511 if (instance->adapter_type != MFI_SERIES)
adam radfordeb1b1232011-02-24 20:55:56 -08006512 megasas_release_fusion(instance);
6513 else
6514 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006515 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006516 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306517fail_init_mfi:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006518 scsi_host_put(host);
6519
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006520fail_alloc_instance:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006521 pci_disable_device(pdev);
6522
6523 return -ENODEV;
6524}
6525
6526/**
6527 * megasas_flush_cache - Requests FW to flush all its caches
6528 * @instance: Adapter soft state
6529 */
6530static void megasas_flush_cache(struct megasas_instance *instance)
6531{
6532 struct megasas_cmd *cmd;
6533 struct megasas_dcmd_frame *dcmd;
6534
Sumit Saxena8a01a412016-01-28 21:04:32 +05306535 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006536 return;
6537
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006538 cmd = megasas_get_cmd(instance);
6539
6540 if (!cmd)
6541 return;
6542
6543 dcmd = &cmd->frame->dcmd;
6544
6545 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6546
6547 dcmd->cmd = MFI_CMD_DCMD;
6548 dcmd->cmd_status = 0x0;
6549 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306550 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006551 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006552 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006553 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306554 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006555 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6556
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306557 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6558 != DCMD_SUCCESS) {
6559 dev_err(&instance->pdev->dev,
6560 "return from %s %d\n", __func__, __LINE__);
6561 return;
6562 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006563
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306564 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006565}
6566
6567/**
6568 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6569 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006570 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006571 */
bo yang31ea7082007-11-07 12:09:50 -05006572static void megasas_shutdown_controller(struct megasas_instance *instance,
6573 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006574{
6575 struct megasas_cmd *cmd;
6576 struct megasas_dcmd_frame *dcmd;
6577
Sumit Saxena8a01a412016-01-28 21:04:32 +05306578 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006579 return;
6580
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006581 cmd = megasas_get_cmd(instance);
6582
6583 if (!cmd)
6584 return;
6585
6586 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306587 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306588 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006589 if (instance->map_update_cmd)
6590 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306591 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306592 if (instance->jbod_seq_cmd)
6593 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306594 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306595
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006596 dcmd = &cmd->frame->dcmd;
6597
6598 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6599
6600 dcmd->cmd = MFI_CMD_DCMD;
6601 dcmd->cmd_status = 0x0;
6602 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306603 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006604 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006605 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006606 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306607 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006608
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306609 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6610 != DCMD_SUCCESS) {
6611 dev_err(&instance->pdev->dev,
6612 "return from %s %d\n", __func__, __LINE__);
6613 return;
6614 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006615
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306616 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006617}
6618
Jiri Slaby33139b22008-05-01 17:56:02 +02006619#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006620/**
bo yangad84db22007-11-09 04:40:16 -05006621 * megasas_suspend - driver suspend entry point
6622 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006623 * @state: PCI power state to suspend routine
6624 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006625static int
bo yang31ea7082007-11-07 12:09:50 -05006626megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6627{
6628 struct Scsi_Host *host;
6629 struct megasas_instance *instance;
6630
6631 instance = pci_get_drvdata(pdev);
6632 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006633 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006634
adam radford229fe472014-03-10 02:51:56 -07006635 /* Shutdown SR-IOV heartbeat timer */
6636 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6637 del_timer_sync(&instance->sriov_heartbeat_timer);
6638
bo yang31ea7082007-11-07 12:09:50 -05006639 megasas_flush_cache(instance);
6640 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006641
6642 /* cancel the delayed work if this work still in queue */
6643 if (instance->ev != NULL) {
6644 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006645 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006646 instance->ev = NULL;
6647 }
6648
bo yang31ea7082007-11-07 12:09:50 -05006649 tasklet_kill(&instance->isr_tasklet);
6650
6651 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306652 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006653
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306654 megasas_destroy_irqs(instance);
6655
adam radfordc8e858f2011-10-08 18:15:13 -07006656 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006657 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006658
6659 pci_save_state(pdev);
6660 pci_disable_device(pdev);
6661
6662 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6663
6664 return 0;
6665}
6666
6667/**
6668 * megasas_resume- driver resume entry point
6669 * @pdev: PCI device structure
6670 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006671static int
bo yang31ea7082007-11-07 12:09:50 -05006672megasas_resume(struct pci_dev *pdev)
6673{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306674 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006675 struct Scsi_Host *host;
6676 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006677 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006678
6679 instance = pci_get_drvdata(pdev);
6680 host = instance->host;
6681 pci_set_power_state(pdev, PCI_D0);
6682 pci_enable_wake(pdev, PCI_D0, 0);
6683 pci_restore_state(pdev);
6684
6685 /*
6686 * PCI prepping: enable device set bus mastering and dma mask
6687 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006688 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006689
6690 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006691 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006692 return rval;
6693 }
6694
6695 pci_set_master(pdev);
6696
Shivasharan S107a60d2017-10-19 02:49:05 -07006697 /*
6698 * We expect the FW state to be READY
6699 */
6700 if (megasas_transition_to_ready(instance, 0))
6701 goto fail_ready_state;
6702
6703 if (megasas_set_dma_mask(instance))
bo yang31ea7082007-11-07 12:09:50 -05006704 goto fail_set_dma_mask;
6705
6706 /*
6707 * Initialize MFI Firmware
6708 */
6709
bo yang31ea7082007-11-07 12:09:50 -05006710 atomic_set(&instance->fw_outstanding, 0);
Shivasharan S41fae9a2018-01-05 05:27:39 -08006711 atomic_set(&instance->ldio_outstanding, 0);
bo yang31ea7082007-11-07 12:09:50 -05006712
adam radford3f1abce2011-05-11 18:33:47 -07006713 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006714 if (instance->msix_vectors) {
6715 irq_flags = PCI_IRQ_MSIX;
6716 if (smp_affinity_enable)
6717 irq_flags |= PCI_IRQ_AFFINITY;
6718 }
6719 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6720 instance->msix_vectors ?
6721 instance->msix_vectors : 1, irq_flags);
6722 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006723 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006724
Shivasharan Se7d36b82017-10-19 02:48:50 -07006725 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006726 megasas_reset_reply_desc(instance);
6727 if (megasas_ioc_init_fusion(instance)) {
6728 megasas_free_cmds(instance);
6729 megasas_free_cmds_fusion(instance);
6730 goto fail_init_mfi;
6731 }
6732 if (!megasas_get_map_info(instance))
6733 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306734 } else {
adam radford9c915a82010-12-21 13:34:31 -08006735 *instance->producer = 0;
6736 *instance->consumer = 0;
6737 if (megasas_issue_init_mfi(instance))
6738 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006739 }
bo yang31ea7082007-11-07 12:09:50 -05006740
adam radford9c915a82010-12-21 13:34:31 -08006741 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6742 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006743
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306744 if (instance->msix_vectors ?
6745 megasas_setup_irqs_msix(instance, 0) :
6746 megasas_setup_irqs_ioapic(instance))
6747 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006748
adam radford229fe472014-03-10 02:51:56 -07006749 /* Re-launch SR-IOV heartbeat timer */
6750 if (instance->requestorId) {
6751 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07006752 megasas_start_timer(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306753 else {
adam radford229fe472014-03-10 02:51:56 -07006754 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306755 goto fail_init_mfi;
6756 }
adam radford229fe472014-03-10 02:51:56 -07006757 }
6758
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306759 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306760 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006761 instance->unload = 0;
6762
adam radford541f90b2011-05-11 18:34:29 -07006763 /*
6764 * Initiate AEN (Asynchronous Event Notification)
6765 */
6766 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006767 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006768
bo yang31ea7082007-11-07 12:09:50 -05006769 return 0;
6770
bo yang31ea7082007-11-07 12:09:50 -05006771fail_init_mfi:
Shivasharan S1b4bed22017-10-19 02:48:55 -07006772 megasas_free_ctrl_dma_buffers(instance);
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006773 megasas_free_ctrl_mem(instance);
bo yang31ea7082007-11-07 12:09:50 -05006774 scsi_host_put(host);
6775
Shivasharan S107a60d2017-10-19 02:49:05 -07006776fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006777fail_set_dma_mask:
6778fail_ready_state:
6779
6780 pci_disable_device(pdev);
6781
6782 return -ENODEV;
6783}
Jiri Slaby33139b22008-05-01 17:56:02 +02006784#else
6785#define megasas_suspend NULL
6786#define megasas_resume NULL
6787#endif
bo yang31ea7082007-11-07 12:09:50 -05006788
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006789static inline int
6790megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6791{
6792 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6793 int i;
6794
6795 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6796 return 1;
6797
6798 for (i = 0; i < wait_time; i++) {
6799 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6800 break;
6801
6802 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6803 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6804
6805 msleep(1000);
6806 }
6807
6808 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6809 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6810 __func__);
6811 return 1;
6812 }
6813
6814 return 0;
6815}
6816
bo yang31ea7082007-11-07 12:09:50 -05006817/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006818 * megasas_detach_one - PCI hot"un"plug entry point
6819 * @pdev: PCI device structure
6820 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006821static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006822{
6823 int i;
6824 struct Scsi_Host *host;
6825 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006826 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306827 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006828
6829 instance = pci_get_drvdata(pdev);
6830 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006831 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006832
adam radford229fe472014-03-10 02:51:56 -07006833 /* Shutdown SR-IOV heartbeat timer */
6834 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6835 del_timer_sync(&instance->sriov_heartbeat_timer);
6836
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306837 if (instance->fw_crash_state != UNAVAILABLE)
6838 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006839 scsi_remove_host(instance->host);
Shivasharan Sf3f79202018-01-05 05:27:41 -08006840 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006841
6842 if (megasas_wait_for_adapter_operational(instance))
6843 goto skip_firing_dcmds;
6844
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006845 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006846 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006847
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006848skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006849 /* cancel the delayed work if this work still in queue*/
6850 if (instance->ev != NULL) {
6851 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006852 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006853 instance->ev = NULL;
6854 }
6855
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306856 /* cancel all wait events */
6857 wake_up_all(&instance->int_cmd_wait_q);
6858
Sumant Patro5d018ad2006-10-03 13:13:18 -07006859 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006860
6861 /*
6862 * Take the instance off the instance array. Note that we will not
6863 * decrement the max_index. We let this array be sparse array
6864 */
6865 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6866 if (megasas_mgmt_info.instance[i] == instance) {
6867 megasas_mgmt_info.count--;
6868 megasas_mgmt_info.instance[i] = NULL;
6869
6870 break;
6871 }
6872 }
6873
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306874 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006875
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306876 megasas_destroy_irqs(instance);
6877
adam radfordc8e858f2011-10-08 18:15:13 -07006878 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006879 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006880
Shivasharan Sf369a312017-10-19 02:48:52 -07006881 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006882 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6883 kfree(fusion->stream_detect_by_ld[i]);
6884 kfree(fusion->stream_detect_by_ld);
6885 fusion->stream_detect_by_ld = NULL;
6886 }
6887
6888
Shivasharan Se7d36b82017-10-19 02:48:50 -07006889 if (instance->adapter_type != MFI_SERIES) {
adam radford9c915a82010-12-21 13:34:31 -08006890 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306891 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6892 (sizeof(struct MR_PD_CFG_SEQ) *
6893 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306894 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006895 if (fusion->ld_map[i])
6896 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306897 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006898 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306899 fusion->ld_map_phys[i]);
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07006900 if (fusion->ld_drv_map[i]) {
6901 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
6902 vfree(fusion->ld_drv_map[i]);
6903 else
6904 free_pages((ulong)fusion->ld_drv_map[i],
6905 fusion->drv_map_pages);
6906 }
6907
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006908 if (fusion->pd_seq_sync[i])
6909 dma_free_coherent(&instance->pdev->dev,
6910 pd_seq_map_sz,
6911 fusion->pd_seq_sync[i],
6912 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306913 }
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306914 } else {
adam radford9c915a82010-12-21 13:34:31 -08006915 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006916 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006917
adam radford229fe472014-03-10 02:51:56 -07006918 if (instance->vf_affiliation)
6919 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6920 sizeof(struct MR_LD_VF_AFFILIATION),
6921 instance->vf_affiliation,
6922 instance->vf_affiliation_h);
6923
6924 if (instance->vf_affiliation_111)
6925 pci_free_consistent(pdev,
6926 sizeof(struct MR_LD_VF_AFFILIATION_111),
6927 instance->vf_affiliation_111,
6928 instance->vf_affiliation_111_h);
6929
6930 if (instance->hb_host_mem)
6931 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6932 instance->hb_host_mem,
6933 instance->hb_host_mem_h);
6934
Shivasharan S1b4bed22017-10-19 02:48:55 -07006935 megasas_free_ctrl_dma_buffers(instance);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306936
Shivasharan S49a7a4a2017-10-19 02:48:54 -07006937 megasas_free_ctrl_mem(instance);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306938
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006939 scsi_host_put(host);
6940
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006941 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006942}
6943
6944/**
6945 * megasas_shutdown - Shutdown entry point
6946 * @device: Generic device structure
6947 */
6948static void megasas_shutdown(struct pci_dev *pdev)
6949{
6950 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006951
Yang, Bo0c79e682009-10-06 14:47:35 -06006952 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006953
6954 if (megasas_wait_for_adapter_operational(instance))
6955 goto skip_firing_dcmds;
6956
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006957 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006958 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006959
6960skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306961 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306962 megasas_destroy_irqs(instance);
6963
adam radfordc8e858f2011-10-08 18:15:13 -07006964 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006965 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006966}
6967
6968/**
6969 * megasas_mgmt_open - char node "open" entry point
6970 */
6971static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6972{
6973 /*
6974 * Allow only those users with admin rights
6975 */
6976 if (!capable(CAP_SYS_ADMIN))
6977 return -EACCES;
6978
6979 return 0;
6980}
6981
6982/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006983 * megasas_mgmt_fasync - Async notifier registration from applications
6984 *
6985 * This function adds the calling process to a driver global queue. When an
6986 * event occurs, SIGIO will be sent to all processes in this queue.
6987 */
6988static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6989{
6990 int rc;
6991
Arjan van de Ven0b950672006-01-11 13:16:10 +01006992 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006993
6994 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6995
Arjan van de Ven0b950672006-01-11 13:16:10 +01006996 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006997
6998 if (rc >= 0) {
6999 /* For sanity check when we get ioctl */
7000 filep->private_data = filep;
7001 return 0;
7002 }
7003
7004 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7005
7006 return rc;
7007}
7008
7009/**
Yang, Boc3518832009-10-06 14:18:02 -06007010 * megasas_mgmt_poll - char node "poll" entry point
7011 * */
7012static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
7013{
7014 unsigned int mask;
7015 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007016
Yang, Boc3518832009-10-06 14:18:02 -06007017 poll_wait(file, &megasas_poll_wait, wait);
7018 spin_lock_irqsave(&poll_aen_lock, flags);
7019 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007020 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06007021 else
7022 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05307023 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06007024 spin_unlock_irqrestore(&poll_aen_lock, flags);
7025 return mask;
7026}
7027
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307028/*
7029 * megasas_set_crash_dump_params_ioctl:
7030 * Send CRASH_DUMP_MODE DCMD to all controllers
7031 * @cmd: MFI command frame
7032 */
7033
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007034static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307035{
7036 struct megasas_instance *local_instance;
7037 int i, error = 0;
7038 int crash_support;
7039
7040 crash_support = cmd->frame->dcmd.mbox.w[0];
7041
7042 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7043 local_instance = megasas_mgmt_info.instance[i];
7044 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05307045 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307046 MEGASAS_HBA_OPERATIONAL) &&
7047 !megasas_set_crash_dump_params(local_instance,
7048 crash_support)) {
7049 local_instance->crash_dump_app_support =
7050 crash_support;
7051 dev_info(&local_instance->pdev->dev,
7052 "Application firmware crash "
7053 "dump mode set success\n");
7054 error = 0;
7055 } else {
7056 dev_info(&local_instance->pdev->dev,
7057 "Application firmware crash "
7058 "dump mode set failed\n");
7059 error = -1;
7060 }
7061 }
7062 }
7063 return error;
7064}
7065
Yang, Boc3518832009-10-06 14:18:02 -06007066/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007067 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7068 * @instance: Adapter soft state
7069 * @argp: User's ioctl packet
7070 */
7071static int
7072megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7073 struct megasas_iocpacket __user * user_ioc,
7074 struct megasas_iocpacket *ioc)
7075{
Shivasharan S107a60d2017-10-19 02:49:05 -07007076 struct megasas_sge64 *kern_sge64 = NULL;
7077 struct megasas_sge32 *kern_sge32 = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007078 struct megasas_cmd *cmd;
7079 void *kbuff_arr[MAX_IOCTL_SGE];
7080 dma_addr_t buf_handle = 0;
7081 int error = 0, i;
7082 void *sense = NULL;
7083 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06007084 unsigned long *sense_ptr;
Shivasharan S82add4e2017-10-19 02:49:02 -07007085 u32 opcode = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007086
7087 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7088
7089 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007090 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007091 ioc->sge_count, MAX_IOCTL_SGE);
7092 return -EINVAL;
7093 }
7094
Shivasharan S82add4e2017-10-19 02:49:02 -07007095 if (ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) {
7096 dev_err(&instance->pdev->dev,
7097 "Received invalid ioctl command 0x%x\n",
7098 ioc->frame.hdr.cmd);
7099 return -ENOTSUPP;
7100 }
7101
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007102 cmd = megasas_get_cmd(instance);
7103 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007104 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007105 return -ENOMEM;
7106 }
7107
7108 /*
7109 * User's IOCTL packet has 2 frames (maximum). Copy those two
7110 * frames into our cmd's frames. cmd->frame's context will get
7111 * overwritten when we copy from user's frames. So set that value
7112 * alone separately
7113 */
7114 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307115 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06007116 cmd->frame->hdr.pad_0 = 0;
Shivasharan S107a60d2017-10-19 02:49:05 -07007117
7118 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7119
7120 if (instance->consistent_mask_64bit)
7121 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7122 MFI_FRAME_SENSE64));
7123 else
7124 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307125 MFI_FRAME_SENSE64));
Shivasharan S82add4e2017-10-19 02:49:02 -07007126
7127 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7128 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007129
Shivasharan S8823abe2017-08-23 04:47:00 -07007130 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
Shivasharan S95c06082017-02-10 00:59:23 -08007131 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7132 megasas_return_cmd(instance, cmd);
7133 return -1;
7134 }
7135 }
7136
Shivasharan S8823abe2017-08-23 04:47:00 -07007137 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05307138 error = megasas_set_crash_dump_params_ioctl(cmd);
7139 megasas_return_cmd(instance, cmd);
7140 return error;
7141 }
7142
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007143 /*
7144 * The management interface between applications and the fw uses
7145 * MFI frames. E.g, RAID configuration changes, LD property changes
7146 * etc are accomplishes through different kinds of MFI frames. The
7147 * driver needs to care only about substituting user buffers with
7148 * kernel buffers in SGLs. The location of SGL is embedded in the
7149 * struct iocpacket itself.
7150 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007151 if (instance->consistent_mask_64bit)
7152 kern_sge64 = (struct megasas_sge64 *)
7153 ((unsigned long)cmd->frame + ioc->sgl_off);
7154 else
7155 kern_sge32 = (struct megasas_sge32 *)
7156 ((unsigned long)cmd->frame + ioc->sgl_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007157
7158 /*
7159 * For each user buffer, create a mirror buffer and copy in
7160 */
7161 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01007162 if (!ioc->sgl[i].iov_len)
7163 continue;
7164
Sumant Patro9f35fa82007-02-14 12:55:45 -08007165 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007166 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08007167 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007168 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007169 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7170 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007171 error = -ENOMEM;
7172 goto out;
7173 }
7174
7175 /*
7176 * We don't change the dma_coherent_mask, so
7177 * pci_alloc_consistent only returns 32bit addresses
7178 */
Shivasharan S107a60d2017-10-19 02:49:05 -07007179 if (instance->consistent_mask_64bit) {
7180 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7181 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7182 } else {
7183 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7184 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7185 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007186
7187 /*
7188 * We created a kernel buffer corresponding to the
7189 * user buffer. Now copy in from the user buffer
7190 */
7191 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7192 (u32) (ioc->sgl[i].iov_len))) {
7193 error = -EFAULT;
7194 goto out;
7195 }
7196 }
7197
7198 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007199 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7200 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007201 if (!sense) {
7202 error = -ENOMEM;
7203 goto out;
7204 }
7205
7206 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06007207 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Shivasharan S107a60d2017-10-19 02:49:05 -07007208 if (instance->consistent_mask_64bit)
7209 *sense_ptr = cpu_to_le64(sense_handle);
7210 else
7211 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007212 }
7213
7214 /*
7215 * Set the sync_cmd flag so that the ISR knows not to complete this
7216 * cmd to the SCSI mid-layer
7217 */
7218 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307219 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7220 cmd->sync_cmd = 0;
7221 dev_err(&instance->pdev->dev,
Shivasharan S82add4e2017-10-19 02:49:02 -07007222 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7223 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7224 cmd->cmd_status_drv);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307225 return -EBUSY;
7226 }
7227
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007228 cmd->sync_cmd = 0;
7229
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05307230 if (instance->unload == 1) {
7231 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7232 "don't submit data to application\n");
7233 goto out;
7234 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007235 /*
7236 * copy out the kernel buffers to user buffers
7237 */
7238 for (i = 0; i < ioc->sge_count; i++) {
7239 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7240 ioc->sgl[i].iov_len)) {
7241 error = -EFAULT;
7242 goto out;
7243 }
7244 }
7245
7246 /*
7247 * copy out the sense
7248 */
7249 if (ioc->sense_len) {
7250 /*
bo yangb70a41e2008-03-18 03:13:06 -04007251 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007252 * sense buffer address
7253 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06007254 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7255 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007256
Shivasharan S318aaef2017-02-10 00:59:22 -08007257 if (copy_to_user((void __user *)((unsigned long)
7258 get_unaligned((unsigned long *)sense_ptr)),
bo yangb70a41e2008-03-18 03:13:06 -04007259 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007260 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05007261 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007262 error = -EFAULT;
7263 goto out;
7264 }
7265 }
7266
7267 /*
7268 * copy the status codes returned by the fw
7269 */
7270 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7271 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007272 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007273 error = -EFAULT;
7274 }
7275
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007276out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007277 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08007278 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007279 sense, sense_handle);
7280 }
7281
Bjørn Mork7a6a7312012-11-21 09:54:48 +01007282 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007283 if (kbuff_arr[i]) {
Shivasharan S107a60d2017-10-19 02:49:05 -07007284 if (instance->consistent_mask_64bit)
7285 dma_free_coherent(&instance->pdev->dev,
7286 le32_to_cpu(kern_sge64[i].length),
7287 kbuff_arr[i],
7288 le64_to_cpu(kern_sge64[i].phys_addr));
7289 else
7290 dma_free_coherent(&instance->pdev->dev,
7291 le32_to_cpu(kern_sge32[i].length),
7292 kbuff_arr[i],
7293 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05307294 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01007295 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007296 }
7297
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05307298 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007299 return error;
7300}
7301
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007302static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7303{
7304 struct megasas_iocpacket __user *user_ioc =
7305 (struct megasas_iocpacket __user *)arg;
7306 struct megasas_iocpacket *ioc;
7307 struct megasas_instance *instance;
7308 int error;
bo yang39a98552010-09-22 22:36:29 -04007309 int i;
7310 unsigned long flags;
7311 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007312
Markus Elfring709ab232016-08-21 10:39:04 +02007313 ioc = memdup_user(user_ioc, sizeof(*ioc));
7314 if (IS_ERR(ioc))
7315 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007316
7317 instance = megasas_lookup_instance(ioc->host_no);
7318 if (!instance) {
7319 error = -ENODEV;
7320 goto out_kfree_ioc;
7321 }
7322
adam radford229fe472014-03-10 02:51:56 -07007323 /* Adjust ioctl wait time for VF mode */
7324 if (instance->requestorId)
7325 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7326
7327 /* Block ioctls in VF mode */
7328 if (instance->requestorId && !allow_vf_ioctls) {
7329 error = -ENODEV;
7330 goto out_kfree_ioc;
7331 }
7332
Sumit Saxena8a01a412016-01-28 21:04:32 +05307333 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007334 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06007335 error = -ENODEV;
7336 goto out_kfree_ioc;
7337 }
7338
7339 if (instance->unload == 1) {
7340 error = -ENODEV;
7341 goto out_kfree_ioc;
7342 }
7343
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007344 if (down_interruptible(&instance->ioctl_sem)) {
7345 error = -ERESTARTSYS;
7346 goto out_kfree_ioc;
7347 }
bo yang39a98552010-09-22 22:36:29 -04007348
7349 for (i = 0; i < wait_time; i++) {
7350
7351 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307352 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007353 spin_unlock_irqrestore(&instance->hba_lock, flags);
7354 break;
7355 }
7356 spin_unlock_irqrestore(&instance->hba_lock, flags);
7357
7358 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007359 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04007360 "for controller reset to finish\n");
7361 }
7362
7363 msleep(1000);
7364 }
7365
7366 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307367 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007368 spin_unlock_irqrestore(&instance->hba_lock, flags);
7369
Colin Ian King644da3c2016-11-12 16:25:24 +00007370 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007371 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03007372 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04007373 }
7374 spin_unlock_irqrestore(&instance->hba_lock, flags);
7375
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007376 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007377out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007378 up(&instance->ioctl_sem);
7379
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007380out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007381 kfree(ioc);
7382 return error;
7383}
7384
7385static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7386{
7387 struct megasas_instance *instance;
7388 struct megasas_aen aen;
7389 int error;
bo yang39a98552010-09-22 22:36:29 -04007390 int i;
7391 unsigned long flags;
7392 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007393
7394 if (file->private_data != file) {
7395 printk(KERN_DEBUG "megasas: fasync_helper was not "
7396 "called first\n");
7397 return -EINVAL;
7398 }
7399
7400 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7401 return -EFAULT;
7402
7403 instance = megasas_lookup_instance(aen.host_no);
7404
7405 if (!instance)
7406 return -ENODEV;
7407
Sumit Saxena8a01a412016-01-28 21:04:32 +05307408 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04007409 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06007410 }
7411
7412 if (instance->unload == 1) {
7413 return -ENODEV;
7414 }
7415
bo yang39a98552010-09-22 22:36:29 -04007416 for (i = 0; i < wait_time; i++) {
7417
7418 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307419 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007420 spin_unlock_irqrestore(&instance->hba_lock,
7421 flags);
7422 break;
7423 }
7424
7425 spin_unlock_irqrestore(&instance->hba_lock, flags);
7426
7427 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05007428 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04007429 "controller reset to finish\n");
7430 }
7431
7432 msleep(1000);
7433 }
7434
7435 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05307436 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04007437 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00007438 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04007439 return -ENODEV;
7440 }
7441 spin_unlock_irqrestore(&instance->hba_lock, flags);
7442
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307443 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007444 error = megasas_register_aen(instance, aen.seq_num,
7445 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307446 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007447 return error;
7448}
7449
7450/**
7451 * megasas_mgmt_ioctl - char node ioctl entry point
7452 */
7453static long
7454megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7455{
7456 switch (cmd) {
7457 case MEGASAS_IOC_FIRMWARE:
7458 return megasas_mgmt_ioctl_fw(file, arg);
7459
7460 case MEGASAS_IOC_GET_AEN:
7461 return megasas_mgmt_ioctl_aen(file, arg);
7462 }
7463
7464 return -ENOTTY;
7465}
7466
7467#ifdef CONFIG_COMPAT
7468static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7469{
7470 struct compat_megasas_iocpacket __user *cioc =
7471 (struct compat_megasas_iocpacket __user *)arg;
7472 struct megasas_iocpacket __user *ioc =
7473 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7474 int i;
7475 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007476 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307477 u32 local_sense_off;
7478 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307479 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007480
Jeff Garzik83aabc12006-10-04 06:34:03 -04007481 if (clear_user(ioc, sizeof(*ioc)))
7482 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007483
7484 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7485 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7486 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7487 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7488 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7489 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7490 return -EFAULT;
7491
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007492 /*
7493 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7494 * sense_len is not null, so prepare the 64bit value under
7495 * the same condition.
7496 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307497 if (get_user(local_sense_off, &ioc->sense_off) ||
7498 get_user(local_sense_len, &ioc->sense_len) ||
7499 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307500 return -EFAULT;
7501
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307502 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007503 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307504 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007505 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307506 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007507 if (get_user(ptr, sense_cioc_ptr) ||
7508 put_user(compat_ptr(ptr), sense_ioc_ptr))
7509 return -EFAULT;
7510 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007511
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007512 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007513 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7514 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7515 copy_in_user(&ioc->sgl[i].iov_len,
7516 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7517 return -EFAULT;
7518 }
7519
7520 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7521
7522 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7523 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7524 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7525 return -EFAULT;
7526 }
7527 return error;
7528}
7529
7530static long
7531megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7532 unsigned long arg)
7533{
7534 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007535 case MEGASAS_IOC_FIRMWARE32:
7536 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007537 case MEGASAS_IOC_GET_AEN:
7538 return megasas_mgmt_ioctl_aen(file, arg);
7539 }
7540
7541 return -ENOTTY;
7542}
7543#endif
7544
7545/*
7546 * File operations structure for management interface
7547 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007548static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007549 .owner = THIS_MODULE,
7550 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007551 .fasync = megasas_mgmt_fasync,
7552 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007553 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007554#ifdef CONFIG_COMPAT
7555 .compat_ioctl = megasas_mgmt_compat_ioctl,
7556#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007557 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007558};
7559
7560/*
7561 * PCI hotplug support registration structure
7562 */
7563static struct pci_driver megasas_pci_driver = {
7564
7565 .name = "megaraid_sas",
7566 .id_table = megasas_pci_table,
7567 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007568 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007569 .suspend = megasas_suspend,
7570 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007571 .shutdown = megasas_shutdown,
7572};
7573
7574/*
7575 * Sysfs driver attributes
7576 */
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007577static ssize_t version_show(struct device_driver *dd, char *buf)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007578{
7579 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7580 MEGASAS_VERSION);
7581}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007582static DRIVER_ATTR_RO(version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007583
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007584static ssize_t release_date_show(struct device_driver *dd, char *buf)
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307585{
7586 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7587 MEGASAS_RELDATE);
7588}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007589static DRIVER_ATTR_RO(release_date);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307590
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007591static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
Yang, Bo72c4fd32009-10-06 14:20:59 -06007592{
7593 return sprintf(buf, "%u\n", support_poll_for_event);
7594}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007595static DRIVER_ATTR_RO(support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007596
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007597static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
Yang, Bo837f5fe2010-10-11 06:59:20 -06007598{
7599 return sprintf(buf, "%u\n", support_device_change);
7600}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007601static DRIVER_ATTR_RO(support_device_change);
Yang, Bo837f5fe2010-10-11 06:59:20 -06007602
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007603static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
Sumant Patro658dced2006-10-03 13:09:14 -07007604{
bo yangad84db22007-11-09 04:40:16 -05007605 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007606}
7607
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007608static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
7609 size_t count)
Sumant Patro658dced2006-10-03 13:09:14 -07007610{
7611 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007612
7613 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007614 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7615 retval = -EINVAL;
7616 }
7617 return retval;
7618}
Greg Kroah-Hartmane14a3962017-07-19 14:50:06 +02007619static DRIVER_ATTR_RW(dbg_lvl);
bo yangad84db22007-11-09 04:40:16 -05007620
Shivasharan Sb4a42212017-02-10 00:59:16 -08007621static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
7622{
7623 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
7624 scsi_remove_device(sdev);
7625 scsi_device_put(sdev);
7626}
7627
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007628static void
7629megasas_aen_polling(struct work_struct *work)
7630{
7631 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007632 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007633 struct megasas_instance *instance = ev->instance;
7634 union megasas_evt_class_locale class_locale;
7635 struct Scsi_Host *host;
7636 struct scsi_device *sdev1;
7637 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007638 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007639 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007640 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007641 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307642 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007643
7644 if (!instance) {
7645 printk(KERN_ERR "invalid instance!\n");
7646 kfree(ev);
7647 return;
7648 }
adam radford229fe472014-03-10 02:51:56 -07007649
7650 /* Adjust event workqueue thread wait time for VF mode */
7651 if (instance->requestorId)
7652 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7653
7654 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307655 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007656
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007657 instance->ev = NULL;
7658 host = instance->host;
7659 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307660 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007661
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307662 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307663
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007664 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307665 case MR_EVT_PD_REMOVED:
7666 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307667 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307668 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007669 break;
7670
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307671 case MR_EVT_LD_OFFLINE:
7672 case MR_EVT_CFG_CLEARED:
7673 case MR_EVT_LD_DELETED:
7674 case MR_EVT_LD_CREATED:
7675 if (!instance->requestorId ||
7676 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7677 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007678
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307679 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307680 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007681
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307682 break;
7683
7684 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7685 case MR_EVT_FOREIGN_CFG_IMPORTED:
7686 case MR_EVT_LD_STATE_CHANGE:
7687 dcmd_ret = megasas_get_pd_list(instance);
7688
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307689 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307690 break;
7691
7692 if (!instance->requestorId ||
7693 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7694 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7695
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307696 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307697 break;
7698
7699 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7700 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7701 instance->host->host_no);
7702 break;
7703
7704 case MR_EVT_CTRL_PROP_CHANGED:
7705 dcmd_ret = megasas_get_ctrl_info(instance);
7706 break;
7707 default:
7708 doscan = 0;
7709 break;
7710 }
7711 } else {
7712 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7713 mutex_unlock(&instance->reset_mutex);
7714 kfree(ev);
7715 return;
7716 }
7717
7718 mutex_unlock(&instance->reset_mutex);
7719
7720 if (doscan & SCAN_PD_CHANNEL) {
7721 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7722 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7723 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007724 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307725 if (instance->pd_list[pd_index].driveState ==
7726 MR_PD_STATE_SYSTEM) {
7727 if (!sdev1)
7728 scsi_add_device(host, i, j, 0);
7729 else
Yang, Boc9786842009-12-06 08:39:25 -07007730 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007731 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007732 if (sdev1)
7733 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007734 }
Yang, Boc9786842009-12-06 08:39:25 -07007735 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007736 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007737 }
7738
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307739 if (doscan & SCAN_VD_CHANNEL) {
7740 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7741 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7742 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7743 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7744 if (instance->ld_ids[ld_index] != 0xff) {
7745 if (!sdev1)
7746 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7747 else
7748 scsi_device_put(sdev1);
7749 } else {
Shivasharan Sb4a42212017-02-10 00:59:16 -08007750 if (sdev1)
7751 megasas_remove_scsi_device(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007752 }
7753 }
7754 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007755 }
7756
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307757 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307758 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7759 else
7760 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007761
7762 /* Register AEN with FW for latest sequence number plus 1 */
7763 class_locale.members.reserved = 0;
7764 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7765 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307766
7767 if (instance->aen_cmd != NULL) {
7768 kfree(ev);
7769 return;
7770 }
7771
7772 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007773 error = megasas_register_aen(instance, seq_num,
7774 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007775 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307776 dev_err(&instance->pdev->dev,
7777 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007778
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307779 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007780 kfree(ev);
7781}
7782
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007783/**
7784 * megasas_init - Driver load entry point
7785 */
7786static int __init megasas_init(void)
7787{
7788 int rval;
7789
7790 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007791 * Booted in kdump kernel, minimize memory footprints by
7792 * disabling few features
7793 */
7794 if (reset_devices) {
7795 msix_vectors = 1;
7796 rdpq_enable = 0;
7797 dual_qdepth_disable = 1;
7798 }
7799
7800 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007801 * Announce driver version and other information
7802 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307803 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007804
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007805 spin_lock_init(&poll_aen_lock);
7806
Yang, Bo72c4fd32009-10-06 14:20:59 -06007807 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007808 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007809
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007810 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7811
7812 /*
7813 * Register character device node
7814 */
7815 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7816
7817 if (rval < 0) {
7818 printk(KERN_DEBUG "megasas: failed to open device node\n");
7819 return rval;
7820 }
7821
7822 megasas_mgmt_majorno = rval;
7823
7824 /*
7825 * Register ourselves as PCI hotplug module
7826 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007827 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007828
7829 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007830 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007831 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007832 }
7833
Jeff Garzik83aabc12006-10-04 06:34:03 -04007834 rval = driver_create_file(&megasas_pci_driver.driver,
7835 &driver_attr_version);
7836 if (rval)
7837 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007838
7839 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307840 &driver_attr_release_date);
7841 if (rval)
7842 goto err_dcf_rel_date;
7843
7844 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007845 &driver_attr_support_poll_for_event);
7846 if (rval)
7847 goto err_dcf_support_poll_for_event;
7848
Jeff Garzik83aabc12006-10-04 06:34:03 -04007849 rval = driver_create_file(&megasas_pci_driver.driver,
7850 &driver_attr_dbg_lvl);
7851 if (rval)
7852 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007853 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007854 &driver_attr_support_device_change);
7855 if (rval)
7856 goto err_dcf_support_device_change;
7857
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007858 return rval;
bo yangad84db22007-11-09 04:40:16 -05007859
Yang, Bo837f5fe2010-10-11 06:59:20 -06007860err_dcf_support_device_change:
7861 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007862 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007863err_dcf_dbg_lvl:
7864 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007865 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007866err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307867 driver_remove_file(&megasas_pci_driver.driver,
7868 &driver_attr_release_date);
7869err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007870 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7871err_dcf_attr_ver:
7872 pci_unregister_driver(&megasas_pci_driver);
7873err_pcidrv:
7874 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007875 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007876}
7877
7878/**
7879 * megasas_exit - Driver unload entry point
7880 */
7881static void __exit megasas_exit(void)
7882{
Sumant Patro658dced2006-10-03 13:09:14 -07007883 driver_remove_file(&megasas_pci_driver.driver,
7884 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007885 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007886 &driver_attr_support_poll_for_event);
7887 driver_remove_file(&megasas_pci_driver.driver,
7888 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307889 driver_remove_file(&megasas_pci_driver.driver,
7890 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007891 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007892
7893 pci_unregister_driver(&megasas_pci_driver);
7894 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7895}
7896
7897module_init(megasas_init);
7898module_exit(megasas_exit);