blob: 80fcdf52eea6c380b8446bbcf19613201ed7f00c [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>
Al Viro43399232005-10-04 17:36:04 +010046#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040047#include <linux/compat.h>
Sumant Patrocf62a0a2007-02-14 12:41:55 -080048#include <linux/blkdev.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010049#include <linux/mutex.h>
Yang, Boc3518832009-10-06 14:18:02 -060050#include <linux/poll.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
adam radford4bcde502011-07-26 15:42:52 -070056#include <scsi/scsi_tcq.h>
adam radford9c915a82010-12-21 13:34:31 -080057#include "megaraid_sas_fusion.h"
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040058#include "megaraid_sas.h"
59
bo yangad84db22007-11-09 04:40:16 -050060/*
Yang, Bo1fd10682010-10-12 07:18:50 -060061 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
63 */
64static unsigned int max_sectors;
65module_param_named(max_sectors, max_sectors, int, 0);
66MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
68
adam radford80d9da92010-12-21 10:17:40 -080069static int msix_disable;
70module_param(msix_disable, int, S_IRUGO);
71MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72
adam radford079eadd2012-10-01 19:26:59 -070073static unsigned int msix_vectors;
74module_param(msix_vectors, int, S_IRUGO);
75MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76
adam radford229fe472014-03-10 02:51:56 -070077static int allow_vf_ioctls;
78module_param(allow_vf_ioctls, int, S_IRUGO);
79MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +053081static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
adam radfordc5daa6a2012-07-17 18:20:03 -070082module_param(throttlequeuedepth, int, S_IRUGO);
83MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
85
Sumit Saxenae3d178c2016-01-28 21:04:34 +053086unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
adam radfordc007b8b2012-07-17 18:20:24 -070087module_param(resetwaittime, int, S_IRUGO);
88MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
90
Sumit.Saxena@avagotech.comac951362014-09-12 18:57:48 +053091int smp_affinity_enable = 1;
92module_param(smp_affinity_enable, int, S_IRUGO);
93MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
94
Sumit Saxena179ac142016-01-28 21:04:28 +053095int rdpq_enable = 1;
96module_param(rdpq_enable, int, S_IRUGO);
97MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
98
Sumit Saxena308ec452016-01-28 21:04:30 +053099unsigned int dual_qdepth_disable;
100module_param(dual_qdepth_disable, int, S_IRUGO);
101MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
102
Sumit Saxenae3d178c2016-01-28 21:04:34 +0530103unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104module_param(scmd_timeout, int, S_IRUGO);
105MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
106
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400107MODULE_LICENSE("GPL");
108MODULE_VERSION(MEGASAS_VERSION);
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +0530109MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
110MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400111
adam radford058a8fa2011-10-08 18:14:27 -0700112int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
bo yang39a98552010-09-22 22:36:29 -0400113static int megasas_get_pd_list(struct megasas_instance *instance);
adam radford21c9e162013-09-06 15:27:14 -0700114static int megasas_ld_list_query(struct megasas_instance *instance,
115 u8 query_type);
bo yang39a98552010-09-22 22:36:29 -0400116static int megasas_issue_init_mfi(struct megasas_instance *instance);
117static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
Sumit Saxena2216c302016-01-28 21:04:26 +0530119static int
120megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400121/*
122 * PCI ID table for all supported controllers
123 */
124static struct pci_device_id megasas_pci_table[] = {
125
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
127 /* xscale IOP */
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
129 /* ppc IOP */
bo yangaf7a5642008-03-17 04:13:07 -0400130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
131 /* ppc IOP */
Yang, Bo6610a6b2008-08-10 12:42:38 -0700132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
133 /* gen2*/
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
135 /* gen2*/
Yang, Bo87911122009-10-06 14:31:54 -0600136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
137 /* skinny*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
139 /* skinny*/
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
141 /* xscale IOP, vega */
142 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
143 /* xscale IOP */
adam radford9c915a82010-12-21 13:34:31 -0800144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
145 /* Fusion */
adam radford229fe472014-03-10 02:51:56 -0700146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
147 /* Plasma */
adam radford36807e62011-10-08 18:15:06 -0700148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
149 /* Invader */
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +0530150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
151 /* Fury */
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +0530152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
153 /* Intruder */
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
155 /* Intruder 24 port*/
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +0530156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500158 /* VENTURA */
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
Henrik Kretzschmarf3d72712006-08-15 11:17:21 +0200164 {}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400165};
166
167MODULE_DEVICE_TABLE(pci, megasas_pci_table);
168
169static int megasas_mgmt_majorno;
adam radford229fe472014-03-10 02:51:56 -0700170struct megasas_mgmt_info megasas_mgmt_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400171static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +0100172static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400173
Yang, Boc3518832009-10-06 14:18:02 -0600174static int megasas_poll_wait_aen;
175static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
Yang, Bo72c4fd32009-10-06 14:20:59 -0600176static u32 support_poll_for_event;
adam radford9c915a82010-12-21 13:34:31 -0800177u32 megasas_dbg_lvl;
Yang, Bo837f5fe2010-10-11 06:59:20 -0600178static u32 support_device_change;
Sumant Patro658dced2006-10-03 13:09:14 -0700179
Yang, Boc3518832009-10-06 14:18:02 -0600180/* define lock for aen poll */
181spinlock_t poll_aen_lock;
182
adam radford9c915a82010-12-21 13:34:31 -0800183void
bo yang7343eb62007-11-09 04:35:44 -0500184megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
185 u8 alt_status);
adam radfordebf054b2011-02-24 20:57:15 -0800186static u32
187megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
188static int
189megasas_adp_reset_gen2(struct megasas_instance *instance,
190 struct megasas_register_set __iomem *reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -0800191static irqreturn_t megasas_isr(int irq, void *devp);
192static u32
193megasas_init_adapter_mfi(struct megasas_instance *instance);
194u32
195megasas_build_and_issue_cmd(struct megasas_instance *instance,
196 struct scsi_cmnd *scmd);
197static void megasas_complete_cmd_dpc(unsigned long instance_addr);
adam radford9c915a82010-12-21 13:34:31 -0800198int
adam radford229fe472014-03-10 02:51:56 -0700199wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
200 int seconds);
adam radford9c915a82010-12-21 13:34:31 -0800201void megasas_fusion_ocr_wq(struct work_struct *work);
adam radford229fe472014-03-10 02:51:56 -0700202static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
203 int initial);
adam radfordcd50ba82010-12-21 10:23:23 -0800204
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530205int
adam radfordcd50ba82010-12-21 10:23:23 -0800206megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
207{
208 instance->instancet->fire_cmd(instance,
209 cmd->frame_phys_addr, 0, instance->reg_set);
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530210 return 0;
adam radfordcd50ba82010-12-21 10:23:23 -0800211}
bo yang7343eb62007-11-09 04:35:44 -0500212
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400213/**
214 * megasas_get_cmd - Get a command from the free pool
215 * @instance: Adapter soft state
216 *
217 * Returns a free command from the pool
218 */
adam radford9c915a82010-12-21 13:34:31 -0800219struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400220 *instance)
221{
222 unsigned long flags;
223 struct megasas_cmd *cmd = NULL;
224
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530225 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400226
227 if (!list_empty(&instance->cmd_pool)) {
228 cmd = list_entry((&instance->cmd_pool)->next,
229 struct megasas_cmd, list);
230 list_del_init(&cmd->list);
231 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500232 dev_err(&instance->pdev->dev, "Command pool empty!\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400233 }
234
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530235 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400236 return cmd;
237}
238
239/**
240 * megasas_return_cmd - Return a cmd to free command pool
241 * @instance: Adapter soft state
242 * @cmd: Command packet to be returned to free command pool
243 */
adam radford9c915a82010-12-21 13:34:31 -0800244inline void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400245megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
246{
247 unsigned long flags;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530248 u32 blk_tags;
249 struct megasas_cmd_fusion *cmd_fusion;
250 struct fusion_context *fusion = instance->ctrl_context;
251
252 /* This flag is used only for fusion adapter.
253 * Wait for Interrupt for Polled mode DCMD
254 */
255 if (cmd->flags & DRV_DCMD_POLLED_MODE)
256 return;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400257
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530258 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400259
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530260 if (fusion) {
261 blk_tags = instance->max_scsi_cmds + cmd->index;
262 cmd_fusion = fusion->cmd_list[blk_tags];
263 megasas_return_cmd_fusion(instance, cmd_fusion);
264 }
265 cmd->scmd = NULL;
266 cmd->frame_count = 0;
267 cmd->flags = 0;
268 if (!fusion && reset_devices)
269 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
270 list_add(&cmd->list, (&instance->cmd_pool)->next);
271
272 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
273
274}
Sumant Patro1341c932006-01-25 12:02:40 -0800275
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +0530276static const char *
277format_timestamp(uint32_t timestamp)
278{
279 static char buffer[32];
280
281 if ((timestamp & 0xff000000) == 0xff000000)
282 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
283 0x00ffffff);
284 else
285 snprintf(buffer, sizeof(buffer), "%us", timestamp);
286 return buffer;
287}
288
289static const char *
290format_class(int8_t class)
291{
292 static char buffer[6];
293
294 switch (class) {
295 case MFI_EVT_CLASS_DEBUG:
296 return "debug";
297 case MFI_EVT_CLASS_PROGRESS:
298 return "progress";
299 case MFI_EVT_CLASS_INFO:
300 return "info";
301 case MFI_EVT_CLASS_WARNING:
302 return "WARN";
303 case MFI_EVT_CLASS_CRITICAL:
304 return "CRIT";
305 case MFI_EVT_CLASS_FATAL:
306 return "FATAL";
307 case MFI_EVT_CLASS_DEAD:
308 return "DEAD";
309 default:
310 snprintf(buffer, sizeof(buffer), "%d", class);
311 return buffer;
312 }
313}
314
315/**
316 * megasas_decode_evt: Decode FW AEN event and print critical event
317 * for information.
318 * @instance: Adapter soft state
319 */
320static void
321megasas_decode_evt(struct megasas_instance *instance)
322{
323 struct megasas_evt_detail *evt_detail = instance->evt_detail;
324 union megasas_evt_class_locale class_locale;
325 class_locale.word = le32_to_cpu(evt_detail->cl.word);
326
327 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
328 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
329 le32_to_cpu(evt_detail->seq_num),
330 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
331 (class_locale.members.locale),
332 format_class(class_locale.members.class),
333 evt_detail->description);
334}
335
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400336/**
adam radford0d490162010-12-14 19:17:17 -0800337* The following functions are defined for xscale
Sumant Patro1341c932006-01-25 12:02:40 -0800338* (deviceid : 1064R, PERC5) controllers
339*/
340
341/**
342 * megasas_enable_intr_xscale - Enables interrupts
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400343 * @regs: MFI register set
344 */
345static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530346megasas_enable_intr_xscale(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400347{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530348 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500349
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530350 regs = instance->reg_set;
bo yang39a98552010-09-22 22:36:29 -0400351 writel(0, &(regs)->outbound_intr_mask);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400352
353 /* Dummy readl to force pci flush */
354 readl(&regs->outbound_intr_mask);
355}
356
357/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700358 * megasas_disable_intr_xscale -Disables interrupt
359 * @regs: MFI register set
360 */
361static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530362megasas_disable_intr_xscale(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700363{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530364 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700365 u32 mask = 0x1f;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500366
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530367 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700368 writel(mask, &regs->outbound_intr_mask);
369 /* Dummy readl to force pci flush */
370 readl(&regs->outbound_intr_mask);
371}
372
373/**
Sumant Patro1341c932006-01-25 12:02:40 -0800374 * megasas_read_fw_status_reg_xscale - returns the current FW status value
375 * @regs: MFI register set
376 */
377static u32
378megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
379{
380 return readl(&(regs)->outbound_msg_0);
381}
382/**
383 * megasas_clear_interrupt_xscale - Check & clear interrupt
384 * @regs: MFI register set
385 */
adam radford0d490162010-12-14 19:17:17 -0800386static int
Sumant Patro1341c932006-01-25 12:02:40 -0800387megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
388{
389 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400390 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500391
Sumant Patro1341c932006-01-25 12:02:40 -0800392 /*
393 * Check if it is our interrupt
394 */
395 status = readl(&regs->outbound_intr_status);
396
bo yang39a98552010-09-22 22:36:29 -0400397 if (status & MFI_OB_INTR_STATUS_MASK)
398 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
399 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
400 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patro1341c932006-01-25 12:02:40 -0800401
402 /*
403 * Clear the interrupt by writing back the same value
404 */
bo yang39a98552010-09-22 22:36:29 -0400405 if (mfiStatus)
406 writel(status, &regs->outbound_intr_status);
Sumant Patro1341c932006-01-25 12:02:40 -0800407
Yang, Bo06f579d2008-08-10 12:42:37 -0700408 /* Dummy readl to force pci flush */
409 readl(&regs->outbound_intr_status);
410
bo yang39a98552010-09-22 22:36:29 -0400411 return mfiStatus;
Sumant Patro1341c932006-01-25 12:02:40 -0800412}
413
414/**
415 * megasas_fire_cmd_xscale - Sends command to the FW
416 * @frame_phys_addr : Physical address of cmd
417 * @frame_count : Number of frames for the command
418 * @regs : MFI register set
419 */
adam radford0d490162010-12-14 19:17:17 -0800420static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600421megasas_fire_cmd_xscale(struct megasas_instance *instance,
422 dma_addr_t frame_phys_addr,
423 u32 frame_count,
424 struct megasas_register_set __iomem *regs)
Sumant Patro1341c932006-01-25 12:02:40 -0800425{
bo yang39a98552010-09-22 22:36:29 -0400426 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500427
bo yang39a98552010-09-22 22:36:29 -0400428 spin_lock_irqsave(&instance->hba_lock, flags);
Sumant Patro1341c932006-01-25 12:02:40 -0800429 writel((frame_phys_addr >> 3)|(frame_count),
430 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400431 spin_unlock_irqrestore(&instance->hba_lock, flags);
432}
433
434/**
435 * megasas_adp_reset_xscale - For controller reset
436 * @regs: MFI register set
437 */
438static int
439megasas_adp_reset_xscale(struct megasas_instance *instance,
440 struct megasas_register_set __iomem *regs)
441{
442 u32 i;
443 u32 pcidata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500444
bo yang39a98552010-09-22 22:36:29 -0400445 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
446
447 for (i = 0; i < 3; i++)
448 msleep(1000); /* sleep for 3 secs */
449 pcidata = 0;
450 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500451 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400452 if (pcidata & 0x2) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500453 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400454 pcidata &= ~0x2;
455 pci_write_config_dword(instance->pdev,
456 MFI_1068_PCSR_OFFSET, pcidata);
457
458 for (i = 0; i < 2; i++)
459 msleep(1000); /* need to wait 2 secs again */
460
461 pcidata = 0;
462 pci_read_config_dword(instance->pdev,
463 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500464 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400465 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500466 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
bo yang39a98552010-09-22 22:36:29 -0400467 pcidata = 0;
468 pci_write_config_dword(instance->pdev,
469 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
470 }
471 }
472 return 0;
473}
474
475/**
476 * megasas_check_reset_xscale - For controller reset check
477 * @regs: MFI register set
478 */
479static int
480megasas_check_reset_xscale(struct megasas_instance *instance,
481 struct megasas_register_set __iomem *regs)
482{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530483 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530484 (le32_to_cpu(*instance->consumer) ==
485 MEGASAS_ADPRESET_INPROG_SIGN))
bo yang39a98552010-09-22 22:36:29 -0400486 return 1;
bo yang39a98552010-09-22 22:36:29 -0400487 return 0;
Sumant Patro1341c932006-01-25 12:02:40 -0800488}
489
490static struct megasas_instance_template megasas_instance_template_xscale = {
491
492 .fire_cmd = megasas_fire_cmd_xscale,
493 .enable_intr = megasas_enable_intr_xscale,
Sumant Patrob274cab2006-10-03 12:52:12 -0700494 .disable_intr = megasas_disable_intr_xscale,
Sumant Patro1341c932006-01-25 12:02:40 -0800495 .clear_intr = megasas_clear_intr_xscale,
496 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
bo yang39a98552010-09-22 22:36:29 -0400497 .adp_reset = megasas_adp_reset_xscale,
498 .check_reset = megasas_check_reset_xscale,
adam radfordcd50ba82010-12-21 10:23:23 -0800499 .service_isr = megasas_isr,
500 .tasklet = megasas_complete_cmd_dpc,
501 .init_adapter = megasas_init_adapter_mfi,
502 .build_and_issue_cmd = megasas_build_and_issue_cmd,
503 .issue_dcmd = megasas_issue_dcmd,
Sumant Patro1341c932006-01-25 12:02:40 -0800504};
505
506/**
adam radford0d490162010-12-14 19:17:17 -0800507* This is the end of set of functions & definitions specific
Sumant Patro1341c932006-01-25 12:02:40 -0800508* to xscale (deviceid : 1064R, PERC5) controllers
509*/
510
511/**
adam radford0d490162010-12-14 19:17:17 -0800512* The following functions are defined for ppc (deviceid : 0x60)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500513* controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800514*/
515
516/**
517 * megasas_enable_intr_ppc - Enables interrupts
518 * @regs: MFI register set
519 */
520static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530521megasas_enable_intr_ppc(struct megasas_instance *instance)
Sumant Patrof9876f02006-02-03 15:34:35 -0800522{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530523 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500524
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530525 regs = instance->reg_set;
Sumant Patrof9876f02006-02-03 15:34:35 -0800526 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
adam radford0d490162010-12-14 19:17:17 -0800527
bo yang39a98552010-09-22 22:36:29 -0400528 writel(~0x80000000, &(regs)->outbound_intr_mask);
Sumant Patrof9876f02006-02-03 15:34:35 -0800529
530 /* Dummy readl to force pci flush */
531 readl(&regs->outbound_intr_mask);
532}
533
534/**
Sumant Patrob274cab2006-10-03 12:52:12 -0700535 * megasas_disable_intr_ppc - Disable interrupt
536 * @regs: MFI register set
537 */
538static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530539megasas_disable_intr_ppc(struct megasas_instance *instance)
Sumant Patrob274cab2006-10-03 12:52:12 -0700540{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530541 struct megasas_register_set __iomem *regs;
Sumant Patrob274cab2006-10-03 12:52:12 -0700542 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500543
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530544 regs = instance->reg_set;
Sumant Patrob274cab2006-10-03 12:52:12 -0700545 writel(mask, &regs->outbound_intr_mask);
546 /* Dummy readl to force pci flush */
547 readl(&regs->outbound_intr_mask);
548}
549
550/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800551 * megasas_read_fw_status_reg_ppc - returns the current FW status value
552 * @regs: MFI register set
553 */
554static u32
555megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
556{
557 return readl(&(regs)->outbound_scratch_pad);
558}
559
560/**
561 * megasas_clear_interrupt_ppc - Check & clear interrupt
562 * @regs: MFI register set
563 */
adam radford0d490162010-12-14 19:17:17 -0800564static int
Sumant Patrof9876f02006-02-03 15:34:35 -0800565megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
566{
adam radford3cc6851f92011-05-11 18:34:52 -0700567 u32 status, mfiStatus = 0;
568
Sumant Patrof9876f02006-02-03 15:34:35 -0800569 /*
570 * Check if it is our interrupt
571 */
572 status = readl(&regs->outbound_intr_status);
573
adam radford3cc6851f92011-05-11 18:34:52 -0700574 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
575 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
576
577 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
578 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
Sumant Patrof9876f02006-02-03 15:34:35 -0800579
580 /*
581 * Clear the interrupt by writing back the same value
582 */
583 writel(status, &regs->outbound_doorbell_clear);
584
Yang, Bo06f579d2008-08-10 12:42:37 -0700585 /* Dummy readl to force pci flush */
586 readl(&regs->outbound_doorbell_clear);
587
adam radford3cc6851f92011-05-11 18:34:52 -0700588 return mfiStatus;
Sumant Patrof9876f02006-02-03 15:34:35 -0800589}
adam radford3cc6851f92011-05-11 18:34:52 -0700590
Sumant Patrof9876f02006-02-03 15:34:35 -0800591/**
592 * megasas_fire_cmd_ppc - Sends command to the FW
593 * @frame_phys_addr : Physical address of cmd
594 * @frame_count : Number of frames for the command
595 * @regs : MFI register set
596 */
adam radford0d490162010-12-14 19:17:17 -0800597static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600598megasas_fire_cmd_ppc(struct megasas_instance *instance,
599 dma_addr_t frame_phys_addr,
600 u32 frame_count,
601 struct megasas_register_set __iomem *regs)
Sumant Patrof9876f02006-02-03 15:34:35 -0800602{
bo yang39a98552010-09-22 22:36:29 -0400603 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500604
bo yang39a98552010-09-22 22:36:29 -0400605 spin_lock_irqsave(&instance->hba_lock, flags);
adam radford0d490162010-12-14 19:17:17 -0800606 writel((frame_phys_addr | (frame_count<<1))|1,
Sumant Patrof9876f02006-02-03 15:34:35 -0800607 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400608 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumant Patrof9876f02006-02-03 15:34:35 -0800609}
610
bo yang39a98552010-09-22 22:36:29 -0400611/**
bo yang39a98552010-09-22 22:36:29 -0400612 * megasas_check_reset_ppc - For controller reset check
613 * @regs: MFI register set
614 */
615static int
616megasas_check_reset_ppc(struct megasas_instance *instance,
617 struct megasas_register_set __iomem *regs)
618{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530619 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700620 return 1;
621
bo yang39a98552010-09-22 22:36:29 -0400622 return 0;
623}
adam radford3cc6851f92011-05-11 18:34:52 -0700624
Sumant Patrof9876f02006-02-03 15:34:35 -0800625static struct megasas_instance_template megasas_instance_template_ppc = {
adam radford0d490162010-12-14 19:17:17 -0800626
Sumant Patrof9876f02006-02-03 15:34:35 -0800627 .fire_cmd = megasas_fire_cmd_ppc,
628 .enable_intr = megasas_enable_intr_ppc,
Sumant Patrob274cab2006-10-03 12:52:12 -0700629 .disable_intr = megasas_disable_intr_ppc,
Sumant Patrof9876f02006-02-03 15:34:35 -0800630 .clear_intr = megasas_clear_intr_ppc,
631 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
adam radford3cc6851f92011-05-11 18:34:52 -0700632 .adp_reset = megasas_adp_reset_xscale,
bo yang39a98552010-09-22 22:36:29 -0400633 .check_reset = megasas_check_reset_ppc,
adam radfordcd50ba82010-12-21 10:23:23 -0800634 .service_isr = megasas_isr,
635 .tasklet = megasas_complete_cmd_dpc,
636 .init_adapter = megasas_init_adapter_mfi,
637 .build_and_issue_cmd = megasas_build_and_issue_cmd,
638 .issue_dcmd = megasas_issue_dcmd,
Sumant Patrof9876f02006-02-03 15:34:35 -0800639};
640
641/**
Yang, Bo87911122009-10-06 14:31:54 -0600642 * megasas_enable_intr_skinny - Enables interrupts
643 * @regs: MFI register set
644 */
645static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530646megasas_enable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600647{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530648 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500649
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530650 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600651 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
652
653 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
654
655 /* Dummy readl to force pci flush */
656 readl(&regs->outbound_intr_mask);
657}
658
659/**
660 * megasas_disable_intr_skinny - Disables interrupt
661 * @regs: MFI register set
662 */
663static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530664megasas_disable_intr_skinny(struct megasas_instance *instance)
Yang, Bo87911122009-10-06 14:31:54 -0600665{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530666 struct megasas_register_set __iomem *regs;
Yang, Bo87911122009-10-06 14:31:54 -0600667 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500668
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530669 regs = instance->reg_set;
Yang, Bo87911122009-10-06 14:31:54 -0600670 writel(mask, &regs->outbound_intr_mask);
671 /* Dummy readl to force pci flush */
672 readl(&regs->outbound_intr_mask);
673}
674
675/**
676 * megasas_read_fw_status_reg_skinny - returns the current FW status value
677 * @regs: MFI register set
678 */
679static u32
680megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
681{
682 return readl(&(regs)->outbound_scratch_pad);
683}
684
685/**
686 * megasas_clear_interrupt_skinny - Check & clear interrupt
687 * @regs: MFI register set
688 */
689static int
690megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
691{
692 u32 status;
adam radfordebf054b2011-02-24 20:57:15 -0800693 u32 mfiStatus = 0;
694
Yang, Bo87911122009-10-06 14:31:54 -0600695 /*
696 * Check if it is our interrupt
697 */
698 status = readl(&regs->outbound_intr_status);
699
700 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
bo yang39a98552010-09-22 22:36:29 -0400701 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600702 }
703
704 /*
adam radfordebf054b2011-02-24 20:57:15 -0800705 * Check if it is our interrupt
706 */
James Georgasa3fda7d2013-06-26 12:03:19 -0600707 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
adam radfordebf054b2011-02-24 20:57:15 -0800708 MFI_STATE_FAULT) {
709 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
710 } else
711 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
712
713 /*
Yang, Bo87911122009-10-06 14:31:54 -0600714 * Clear the interrupt by writing back the same value
715 */
716 writel(status, &regs->outbound_intr_status);
717
718 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500719 * dummy read to flush PCI
720 */
Yang, Bo87911122009-10-06 14:31:54 -0600721 readl(&regs->outbound_intr_status);
722
adam radfordebf054b2011-02-24 20:57:15 -0800723 return mfiStatus;
Yang, Bo87911122009-10-06 14:31:54 -0600724}
725
726/**
727 * megasas_fire_cmd_skinny - Sends command to the FW
728 * @frame_phys_addr : Physical address of cmd
729 * @frame_count : Number of frames for the command
730 * @regs : MFI register set
731 */
732static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600733megasas_fire_cmd_skinny(struct megasas_instance *instance,
734 dma_addr_t frame_phys_addr,
735 u32 frame_count,
Yang, Bo87911122009-10-06 14:31:54 -0600736 struct megasas_register_set __iomem *regs)
737{
Yang, Bo0c79e682009-10-06 14:47:35 -0600738 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500739
bo yang39a98552010-09-22 22:36:29 -0400740 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530741 writel(upper_32_bits(frame_phys_addr),
742 &(regs)->inbound_high_queue_port);
743 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
744 &(regs)->inbound_low_queue_port);
Tomas Henzlb99dbe52016-02-01 15:12:04 +0100745 mmiowb();
bo yang39a98552010-09-22 22:36:29 -0400746 spin_unlock_irqrestore(&instance->hba_lock, flags);
747}
748
749/**
bo yang39a98552010-09-22 22:36:29 -0400750 * megasas_check_reset_skinny - For controller reset check
751 * @regs: MFI register set
752 */
753static int
754megasas_check_reset_skinny(struct megasas_instance *instance,
755 struct megasas_register_set __iomem *regs)
756{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530757 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
adam radford3cc6851f92011-05-11 18:34:52 -0700758 return 1;
759
bo yang39a98552010-09-22 22:36:29 -0400760 return 0;
Yang, Bo87911122009-10-06 14:31:54 -0600761}
762
763static struct megasas_instance_template megasas_instance_template_skinny = {
764
765 .fire_cmd = megasas_fire_cmd_skinny,
766 .enable_intr = megasas_enable_intr_skinny,
767 .disable_intr = megasas_disable_intr_skinny,
768 .clear_intr = megasas_clear_intr_skinny,
769 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
adam radfordebf054b2011-02-24 20:57:15 -0800770 .adp_reset = megasas_adp_reset_gen2,
bo yang39a98552010-09-22 22:36:29 -0400771 .check_reset = megasas_check_reset_skinny,
adam radfordcd50ba82010-12-21 10:23:23 -0800772 .service_isr = megasas_isr,
773 .tasklet = megasas_complete_cmd_dpc,
774 .init_adapter = megasas_init_adapter_mfi,
775 .build_and_issue_cmd = megasas_build_and_issue_cmd,
776 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo87911122009-10-06 14:31:54 -0600777};
778
779
780/**
Yang, Bo6610a6b2008-08-10 12:42:38 -0700781* The following functions are defined for gen2 (deviceid : 0x78 0x79)
782* controllers
783*/
784
785/**
786 * megasas_enable_intr_gen2 - Enables interrupts
787 * @regs: MFI register set
788 */
789static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530790megasas_enable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700791{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530792 struct megasas_register_set __iomem *regs;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500793
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530794 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700795 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
796
797 /* write ~0x00000005 (4 & 1) to the intr mask*/
798 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
799
800 /* Dummy readl to force pci flush */
801 readl(&regs->outbound_intr_mask);
802}
803
804/**
805 * megasas_disable_intr_gen2 - Disables interrupt
806 * @regs: MFI register set
807 */
808static inline void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530809megasas_disable_intr_gen2(struct megasas_instance *instance)
Yang, Bo6610a6b2008-08-10 12:42:38 -0700810{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530811 struct megasas_register_set __iomem *regs;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700812 u32 mask = 0xFFFFFFFF;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500813
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530814 regs = instance->reg_set;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700815 writel(mask, &regs->outbound_intr_mask);
816 /* Dummy readl to force pci flush */
817 readl(&regs->outbound_intr_mask);
818}
819
820/**
821 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
822 * @regs: MFI register set
823 */
824static u32
825megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
826{
827 return readl(&(regs)->outbound_scratch_pad);
828}
829
830/**
831 * megasas_clear_interrupt_gen2 - Check & clear interrupt
832 * @regs: MFI register set
833 */
834static int
835megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
836{
837 u32 status;
bo yang39a98552010-09-22 22:36:29 -0400838 u32 mfiStatus = 0;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500839
Yang, Bo6610a6b2008-08-10 12:42:38 -0700840 /*
841 * Check if it is our interrupt
842 */
843 status = readl(&regs->outbound_intr_status);
844
Sumit.Saxena@lsi.comb5bccad2013-05-22 12:29:54 +0530845 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
bo yang39a98552010-09-22 22:36:29 -0400846 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
847 }
848 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
849 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
850 }
Yang, Bo6610a6b2008-08-10 12:42:38 -0700851
852 /*
853 * Clear the interrupt by writing back the same value
854 */
bo yang39a98552010-09-22 22:36:29 -0400855 if (mfiStatus)
856 writel(status, &regs->outbound_doorbell_clear);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700857
858 /* Dummy readl to force pci flush */
859 readl(&regs->outbound_intr_status);
860
bo yang39a98552010-09-22 22:36:29 -0400861 return mfiStatus;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700862}
863/**
864 * megasas_fire_cmd_gen2 - Sends command to the FW
865 * @frame_phys_addr : Physical address of cmd
866 * @frame_count : Number of frames for the command
867 * @regs : MFI register set
868 */
869static inline void
Yang, Bo0c79e682009-10-06 14:47:35 -0600870megasas_fire_cmd_gen2(struct megasas_instance *instance,
871 dma_addr_t frame_phys_addr,
872 u32 frame_count,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700873 struct megasas_register_set __iomem *regs)
874{
bo yang39a98552010-09-22 22:36:29 -0400875 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500876
bo yang39a98552010-09-22 22:36:29 -0400877 spin_lock_irqsave(&instance->hba_lock, flags);
Yang, Bo6610a6b2008-08-10 12:42:38 -0700878 writel((frame_phys_addr | (frame_count<<1))|1,
879 &(regs)->inbound_queue_port);
bo yang39a98552010-09-22 22:36:29 -0400880 spin_unlock_irqrestore(&instance->hba_lock, flags);
881}
882
883/**
884 * megasas_adp_reset_gen2 - For controller reset
885 * @regs: MFI register set
886 */
887static int
888megasas_adp_reset_gen2(struct megasas_instance *instance,
889 struct megasas_register_set __iomem *reg_set)
890{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500891 u32 retry = 0 ;
892 u32 HostDiag;
893 u32 __iomem *seq_offset = &reg_set->seq_offset;
894 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
bo yang39a98552010-09-22 22:36:29 -0400895
adam radfordebf054b2011-02-24 20:57:15 -0800896 if (instance->instancet == &megasas_instance_template_skinny) {
897 seq_offset = &reg_set->fusion_seq_offset;
898 hostdiag_offset = &reg_set->fusion_host_diag;
899 }
900
901 writel(0, seq_offset);
902 writel(4, seq_offset);
903 writel(0xb, seq_offset);
904 writel(2, seq_offset);
905 writel(7, seq_offset);
906 writel(0xd, seq_offset);
907
bo yang39a98552010-09-22 22:36:29 -0400908 msleep(1000);
909
adam radfordebf054b2011-02-24 20:57:15 -0800910 HostDiag = (u32)readl(hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400911
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500912 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
bo yang39a98552010-09-22 22:36:29 -0400913 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800914 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500915 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400916 retry, HostDiag);
917
918 if (retry++ >= 100)
919 return 1;
920
921 }
922
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500923 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
bo yang39a98552010-09-22 22:36:29 -0400924
adam radfordebf054b2011-02-24 20:57:15 -0800925 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
bo yang39a98552010-09-22 22:36:29 -0400926
927 ssleep(10);
928
adam radfordebf054b2011-02-24 20:57:15 -0800929 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -0500930 while (HostDiag & DIAG_RESET_ADAPTER) {
bo yang39a98552010-09-22 22:36:29 -0400931 msleep(100);
adam radfordebf054b2011-02-24 20:57:15 -0800932 HostDiag = (u32)readl(hostdiag_offset);
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500933 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
bo yang39a98552010-09-22 22:36:29 -0400934 retry, HostDiag);
935
936 if (retry++ >= 1000)
937 return 1;
938
939 }
940 return 0;
941}
942
943/**
944 * megasas_check_reset_gen2 - For controller reset check
945 * @regs: MFI register set
946 */
947static int
948megasas_check_reset_gen2(struct megasas_instance *instance,
949 struct megasas_register_set __iomem *regs)
950{
Sumit Saxena8a01a412016-01-28 21:04:32 +0530951 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
Yang, Bo707e09b2010-10-12 07:20:27 -0600952 return 1;
Yang, Bo707e09b2010-10-12 07:20:27 -0600953
bo yang39a98552010-09-22 22:36:29 -0400954 return 0;
Yang, Bo6610a6b2008-08-10 12:42:38 -0700955}
956
957static struct megasas_instance_template megasas_instance_template_gen2 = {
958
959 .fire_cmd = megasas_fire_cmd_gen2,
960 .enable_intr = megasas_enable_intr_gen2,
961 .disable_intr = megasas_disable_intr_gen2,
962 .clear_intr = megasas_clear_intr_gen2,
963 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
bo yang39a98552010-09-22 22:36:29 -0400964 .adp_reset = megasas_adp_reset_gen2,
965 .check_reset = megasas_check_reset_gen2,
adam radfordcd50ba82010-12-21 10:23:23 -0800966 .service_isr = megasas_isr,
967 .tasklet = megasas_complete_cmd_dpc,
968 .init_adapter = megasas_init_adapter_mfi,
969 .build_and_issue_cmd = megasas_build_and_issue_cmd,
970 .issue_dcmd = megasas_issue_dcmd,
Yang, Bo6610a6b2008-08-10 12:42:38 -0700971};
972
973/**
Sumant Patrof9876f02006-02-03 15:34:35 -0800974* This is the end of set of functions & definitions
bo yang39a98552010-09-22 22:36:29 -0400975* specific to gen2 (deviceid : 0x78, 0x79) controllers
Sumant Patrof9876f02006-02-03 15:34:35 -0800976*/
977
adam radford9c915a82010-12-21 13:34:31 -0800978/*
979 * Template added for TB (Fusion)
980 */
981extern struct megasas_instance_template megasas_instance_template_fusion;
982
Sumant Patrof9876f02006-02-03 15:34:35 -0800983/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400984 * megasas_issue_polled - Issues a polling command
985 * @instance: Adapter soft state
adam radford0d490162010-12-14 19:17:17 -0800986 * @cmd: Command packet to be issued
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400987 *
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530988 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400989 */
adam radford9c915a82010-12-21 13:34:31 -0800990int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400991megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
992{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400993 struct megasas_header *frame_hdr = &cmd->frame->hdr;
994
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530995 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530996 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400997
Sumit Saxena8a01a412016-01-28 21:04:32 +0530998 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530999 (instance->instancet->issue_dcmd(instance, cmd))) {
1000 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1001 __func__, __LINE__);
1002 return DCMD_NOT_FIRED;
1003 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001004
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301005 return wait_and_poll(instance, cmd, instance->requestorId ?
1006 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001007}
1008
1009/**
1010 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1011 * @instance: Adapter soft state
1012 * @cmd: Command to be issued
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301013 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001014 *
1015 * This function waits on an event for the command to be returned from ISR.
Sumant Patro2a3681e2006-10-03 13:19:21 -07001016 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001017 * Used to issue ioctl commands.
1018 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301019int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001020megasas_issue_blocked_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301021 struct megasas_cmd *cmd, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001022{
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301023 int ret = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301024 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001025
Sumit Saxena8a01a412016-01-28 21:04:32 +05301026 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301027 (instance->instancet->issue_dcmd(instance, cmd))) {
1028 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1029 __func__, __LINE__);
1030 return DCMD_NOT_FIRED;
1031 }
1032
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301033 if (timeout) {
1034 ret = wait_event_timeout(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301035 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301036 if (!ret) {
1037 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1038 __func__, __LINE__);
1039 return DCMD_TIMEOUT;
1040 }
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301041 } else
1042 wait_event(instance->int_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301043 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001044
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301045 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301046 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001047}
1048
1049/**
1050 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1051 * @instance: Adapter soft state
1052 * @cmd_to_abort: Previously issued cmd to be aborted
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301053 * @timeout: Timeout in seconds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001054 *
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301055 * MFI firmware can abort previously issued AEN comamnd (automatic event
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001056 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
Sumant Patro2a3681e2006-10-03 13:19:21 -07001057 * cmd and waits for return status.
1058 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001059 */
1060static int
1061megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301062 struct megasas_cmd *cmd_to_abort, int timeout)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001063{
1064 struct megasas_cmd *cmd;
1065 struct megasas_abort_frame *abort_fr;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301066 int ret = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001067
1068 cmd = megasas_get_cmd(instance);
1069
1070 if (!cmd)
1071 return -1;
1072
1073 abort_fr = &cmd->frame->abort;
1074
1075 /*
1076 * Prepare and issue the abort frame
1077 */
1078 abort_fr->cmd = MFI_CMD_ABORT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301079 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301080 abort_fr->flags = cpu_to_le16(0);
1081 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1082 abort_fr->abort_mfi_phys_addr_lo =
1083 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1084 abort_fr->abort_mfi_phys_addr_hi =
1085 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001086
1087 cmd->sync_cmd = 1;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301088 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001089
Sumit Saxena8a01a412016-01-28 21:04:32 +05301090 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301091 (instance->instancet->issue_dcmd(instance, cmd))) {
1092 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1093 __func__, __LINE__);
1094 return DCMD_NOT_FIRED;
1095 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001096
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301097 if (timeout) {
1098 ret = wait_event_timeout(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301099 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301100 if (!ret) {
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301101 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1102 __func__, __LINE__);
1103 return DCMD_TIMEOUT;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301104 }
1105 } else
1106 wait_event(instance->abort_cmd_wait_q,
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05301107 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05301108
bo yang39a98552010-09-22 22:36:29 -04001109 cmd->sync_cmd = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001110
1111 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301112 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1113 DCMD_SUCCESS : DCMD_FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001114}
1115
1116/**
1117 * megasas_make_sgl32 - Prepares 32-bit SGL
1118 * @instance: Adapter soft state
1119 * @scp: SCSI command from the mid-layer
1120 * @mfi_sgl: SGL to be filled in
1121 *
1122 * If successful, this function returns the number of SG elements. Otherwise,
1123 * it returnes -1.
1124 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001125static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001126megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1127 union megasas_sgl *mfi_sgl)
1128{
1129 int i;
1130 int sge_count;
1131 struct scatterlist *os_sgl;
1132
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001133 sge_count = scsi_dma_map(scp);
1134 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001135
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001136 if (sge_count) {
1137 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301138 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1139 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001140 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001141 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001142 return sge_count;
1143}
1144
1145/**
1146 * megasas_make_sgl64 - Prepares 64-bit SGL
1147 * @instance: Adapter soft state
1148 * @scp: SCSI command from the mid-layer
1149 * @mfi_sgl: SGL to be filled in
1150 *
1151 * If successful, this function returns the number of SG elements. Otherwise,
1152 * it returnes -1.
1153 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001154static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001155megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1156 union megasas_sgl *mfi_sgl)
1157{
1158 int i;
1159 int sge_count;
1160 struct scatterlist *os_sgl;
1161
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001162 sge_count = scsi_dma_map(scp);
1163 BUG_ON(sge_count < 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001164
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001165 if (sge_count) {
1166 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301167 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1168 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09001169 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001170 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001171 return sge_count;
1172}
1173
Yang, Bof4c9a132009-10-06 14:43:28 -06001174/**
1175 * megasas_make_sgl_skinny - Prepares IEEE SGL
1176 * @instance: Adapter soft state
1177 * @scp: SCSI command from the mid-layer
1178 * @mfi_sgl: SGL to be filled in
1179 *
1180 * If successful, this function returns the number of SG elements. Otherwise,
1181 * it returnes -1.
1182 */
1183static int
1184megasas_make_sgl_skinny(struct megasas_instance *instance,
1185 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1186{
1187 int i;
1188 int sge_count;
1189 struct scatterlist *os_sgl;
1190
1191 sge_count = scsi_dma_map(scp);
1192
1193 if (sge_count) {
1194 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301195 mfi_sgl->sge_skinny[i].length =
1196 cpu_to_le32(sg_dma_len(os_sgl));
Yang, Bof4c9a132009-10-06 14:43:28 -06001197 mfi_sgl->sge_skinny[i].phys_addr =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301198 cpu_to_le64(sg_dma_address(os_sgl));
1199 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
Yang, Bof4c9a132009-10-06 14:43:28 -06001200 }
1201 }
1202 return sge_count;
1203}
1204
Sumant Patrob1df99d2006-10-03 12:40:47 -07001205 /**
1206 * megasas_get_frame_count - Computes the number of frames
bo yangd532dbe2008-03-17 03:36:43 -04001207 * @frame_type : type of frame- io or pthru frame
Sumant Patrob1df99d2006-10-03 12:40:47 -07001208 * @sge_count : number of sg elements
1209 *
1210 * Returns the number of frames required for numnber of sge's (sge_count)
1211 */
1212
Yang, Bof4c9a132009-10-06 14:43:28 -06001213static u32 megasas_get_frame_count(struct megasas_instance *instance,
1214 u8 sge_count, u8 frame_type)
Sumant Patrob1df99d2006-10-03 12:40:47 -07001215{
1216 int num_cnt;
1217 int sge_bytes;
1218 u32 sge_sz;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001219 u32 frame_count = 0;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001220
1221 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1222 sizeof(struct megasas_sge32);
1223
Yang, Bof4c9a132009-10-06 14:43:28 -06001224 if (instance->flag_ieee) {
1225 sge_sz = sizeof(struct megasas_sge_skinny);
1226 }
1227
Sumant Patrob1df99d2006-10-03 12:40:47 -07001228 /*
bo yangd532dbe2008-03-17 03:36:43 -04001229 * Main frame can contain 2 SGEs for 64-bit SGLs and
1230 * 3 SGEs for 32-bit SGLs for ldio &
1231 * 1 SGEs for 64-bit SGLs and
1232 * 2 SGEs for 32-bit SGLs for pthru frame
1233 */
1234 if (unlikely(frame_type == PTHRU_FRAME)) {
Yang, Bof4c9a132009-10-06 14:43:28 -06001235 if (instance->flag_ieee == 1) {
1236 num_cnt = sge_count - 1;
1237 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001238 num_cnt = sge_count - 1;
1239 else
1240 num_cnt = sge_count - 2;
1241 } else {
Yang, Bof4c9a132009-10-06 14:43:28 -06001242 if (instance->flag_ieee == 1) {
1243 num_cnt = sge_count - 1;
1244 } else if (IS_DMA64)
bo yangd532dbe2008-03-17 03:36:43 -04001245 num_cnt = sge_count - 2;
1246 else
1247 num_cnt = sge_count - 3;
1248 }
Sumant Patrob1df99d2006-10-03 12:40:47 -07001249
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001250 if (num_cnt > 0) {
Sumant Patrob1df99d2006-10-03 12:40:47 -07001251 sge_bytes = sge_sz * num_cnt;
1252
1253 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1254 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1255 }
1256 /* Main frame */
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001257 frame_count += 1;
Sumant Patrob1df99d2006-10-03 12:40:47 -07001258
1259 if (frame_count > 7)
1260 frame_count = 8;
1261 return frame_count;
1262}
1263
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001264/**
1265 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1266 * @instance: Adapter soft state
1267 * @scp: SCSI command
1268 * @cmd: Command to be prepared in
1269 *
1270 * This function prepares CDB commands. These are typcially pass-through
1271 * commands to the devices.
1272 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001273static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001274megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1275 struct megasas_cmd *cmd)
1276{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001277 u32 is_logical;
1278 u32 device_id;
1279 u16 flags = 0;
1280 struct megasas_pthru_frame *pthru;
1281
Shivasharan S3cabd162017-02-10 00:59:05 -08001282 is_logical = MEGASAS_IS_LOGICAL(scp->device);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301283 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001284 pthru = (struct megasas_pthru_frame *)cmd->frame;
1285
1286 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1287 flags = MFI_FRAME_DIR_WRITE;
1288 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1289 flags = MFI_FRAME_DIR_READ;
1290 else if (scp->sc_data_direction == PCI_DMA_NONE)
1291 flags = MFI_FRAME_DIR_NONE;
1292
Yang, Bof4c9a132009-10-06 14:43:28 -06001293 if (instance->flag_ieee == 1) {
1294 flags |= MFI_FRAME_IEEE;
1295 }
1296
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001297 /*
1298 * Prepare the DCDB frame
1299 */
1300 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1301 pthru->cmd_status = 0x0;
1302 pthru->scsi_status = 0x0;
1303 pthru->target_id = device_id;
1304 pthru->lun = scp->device->lun;
1305 pthru->cdb_len = scp->cmd_len;
1306 pthru->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07001307 pthru->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301308 pthru->flags = cpu_to_le16(flags);
1309 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001310
1311 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1312
1313 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001314 * If the command is for the tape device, set the
1315 * pthru timeout to the os layer timeout value.
1316 */
Yang, Bo8d568252009-10-06 14:12:21 -06001317 if (scp->device->type == TYPE_TAPE) {
1318 if ((scp->request->timeout / HZ) > 0xFFFF)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05301319 pthru->timeout = cpu_to_le16(0xFFFF);
Yang, Bo8d568252009-10-06 14:12:21 -06001320 else
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301321 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
Yang, Bo8d568252009-10-06 14:12:21 -06001322 }
1323
1324 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001325 * Construct SGL
1326 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001327 if (instance->flag_ieee == 1) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301328 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001329 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1330 &pthru->sgl);
1331 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301332 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001333 pthru->sge_count = megasas_make_sgl64(instance, scp,
1334 &pthru->sgl);
1335 } else
1336 pthru->sge_count = megasas_make_sgl32(instance, scp,
1337 &pthru->sgl);
1338
Yang, Bobdc6fb82009-12-06 08:30:19 -07001339 if (pthru->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001340 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001341 pthru->sge_count);
1342 return 0;
1343 }
1344
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001345 /*
1346 * Sense info specific
1347 */
1348 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301349 pthru->sense_buf_phys_addr_hi =
1350 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1351 pthru->sense_buf_phys_addr_lo =
1352 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001353
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001354 /*
1355 * Compute the total number of frames this command consumes. FW uses
1356 * this number to pull sufficient number of frames from host memory.
1357 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001358 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
bo yangd532dbe2008-03-17 03:36:43 -04001359 PTHRU_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001360
1361 return cmd->frame_count;
1362}
1363
1364/**
1365 * megasas_build_ldio - Prepares IOs to logical devices
1366 * @instance: Adapter soft state
1367 * @scp: SCSI command
Anand Gadiyarfd589a82009-07-16 17:13:03 +02001368 * @cmd: Command to be prepared
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001369 *
1370 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1371 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001372static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001373megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1374 struct megasas_cmd *cmd)
1375{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001376 u32 device_id;
1377 u8 sc = scp->cmnd[0];
1378 u16 flags = 0;
1379 struct megasas_io_frame *ldio;
1380
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301381 device_id = MEGASAS_DEV_INDEX(scp);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001382 ldio = (struct megasas_io_frame *)cmd->frame;
1383
1384 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1385 flags = MFI_FRAME_DIR_WRITE;
1386 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1387 flags = MFI_FRAME_DIR_READ;
1388
Yang, Bof4c9a132009-10-06 14:43:28 -06001389 if (instance->flag_ieee == 1) {
1390 flags |= MFI_FRAME_IEEE;
1391 }
1392
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001393 /*
Sumant Patrob1df99d2006-10-03 12:40:47 -07001394 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001395 */
1396 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1397 ldio->cmd_status = 0x0;
1398 ldio->scsi_status = 0x0;
1399 ldio->target_id = device_id;
1400 ldio->timeout = 0;
1401 ldio->reserved_0 = 0;
1402 ldio->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301403 ldio->flags = cpu_to_le16(flags);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001404 ldio->start_lba_hi = 0;
1405 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1406
1407 /*
1408 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1409 */
1410 if (scp->cmd_len == 6) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301411 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1412 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1413 ((u32) scp->cmnd[2] << 8) |
1414 (u32) scp->cmnd[3]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001415
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301416 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001417 }
1418
1419 /*
1420 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1421 */
1422 else if (scp->cmd_len == 10) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301423 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1424 ((u32) scp->cmnd[7] << 8));
1425 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1426 ((u32) scp->cmnd[3] << 16) |
1427 ((u32) scp->cmnd[4] << 8) |
1428 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001429 }
1430
1431 /*
1432 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1433 */
1434 else if (scp->cmd_len == 12) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301435 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1436 ((u32) scp->cmnd[7] << 16) |
1437 ((u32) scp->cmnd[8] << 8) |
1438 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001439
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301440 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1441 ((u32) scp->cmnd[3] << 16) |
1442 ((u32) scp->cmnd[4] << 8) |
1443 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001444 }
1445
1446 /*
1447 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1448 */
1449 else if (scp->cmd_len == 16) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301450 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1451 ((u32) scp->cmnd[11] << 16) |
1452 ((u32) scp->cmnd[12] << 8) |
1453 (u32) scp->cmnd[13]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001454
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301455 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1456 ((u32) scp->cmnd[7] << 16) |
1457 ((u32) scp->cmnd[8] << 8) |
1458 (u32) scp->cmnd[9]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001459
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301460 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1461 ((u32) scp->cmnd[3] << 16) |
1462 ((u32) scp->cmnd[4] << 8) |
1463 (u32) scp->cmnd[5]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001464
1465 }
1466
1467 /*
1468 * Construct SGL
1469 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001470 if (instance->flag_ieee) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301471 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Yang, Bof4c9a132009-10-06 14:43:28 -06001472 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1473 &ldio->sgl);
1474 } else if (IS_DMA64) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301475 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001476 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1477 } else
1478 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1479
Yang, Bobdc6fb82009-12-06 08:30:19 -07001480 if (ldio->sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001481 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
Yang, Bobdc6fb82009-12-06 08:30:19 -07001482 ldio->sge_count);
1483 return 0;
1484 }
1485
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001486 /*
1487 * Sense info specific
1488 */
1489 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1490 ldio->sense_buf_phys_addr_hi = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301491 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001492
Sumant Patrob1df99d2006-10-03 12:40:47 -07001493 /*
1494 * Compute the total number of frames this command consumes. FW uses
1495 * this number to pull sufficient number of frames from host memory.
1496 */
Yang, Bof4c9a132009-10-06 14:43:28 -06001497 cmd->frame_count = megasas_get_frame_count(instance,
1498 ldio->sge_count, IO_FRAME);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001499
1500 return cmd->frame_count;
1501}
1502
1503/**
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301504 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1505 * and whether it's RW or non RW
Sumant Patrocb59aa62006-01-25 11:53:25 -08001506 * @scmd: SCSI command
adam radford0d490162010-12-14 19:17:17 -08001507 *
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001508 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301509inline int megasas_cmd_type(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001510{
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301511 int ret;
1512
Sumant Patrocb59aa62006-01-25 11:53:25 -08001513 switch (cmd->cmnd[0]) {
1514 case READ_10:
1515 case WRITE_10:
1516 case READ_12:
1517 case WRITE_12:
1518 case READ_6:
1519 case WRITE_6:
1520 case READ_16:
1521 case WRITE_16:
Shivasharan S3cabd162017-02-10 00:59:05 -08001522 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301523 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1524 break;
Sumant Patrocb59aa62006-01-25 11:53:25 -08001525 default:
Shivasharan S3cabd162017-02-10 00:59:05 -08001526 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301527 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001528 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301529 return ret;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001530}
1531
Sumant Patro658dced2006-10-03 13:09:14 -07001532 /**
1533 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001534 * in FW
Sumant Patro658dced2006-10-03 13:09:14 -07001535 * @instance: Adapter soft state
1536 */
1537static inline void
1538megasas_dump_pending_frames(struct megasas_instance *instance)
1539{
1540 struct megasas_cmd *cmd;
1541 int i,n;
1542 union megasas_sgl *mfi_sgl;
1543 struct megasas_io_frame *ldio;
1544 struct megasas_pthru_frame *pthru;
1545 u32 sgcount;
1546 u32 max_cmd = instance->max_fw_cmds;
1547
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001548 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1549 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 -07001550 if (IS_DMA64)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001551 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 -07001552 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001553 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 -07001554
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001555 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001556 for (i = 0; i < max_cmd; i++) {
1557 cmd = instance->cmd_list[i];
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001558 if (!cmd->scmd)
Sumant Patro658dced2006-10-03 13:09:14 -07001559 continue;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001560 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 +05301561 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
Sumant Patro658dced2006-10-03 13:09:14 -07001562 ldio = (struct megasas_io_frame *)cmd->frame;
1563 mfi_sgl = &ldio->sgl;
1564 sgcount = ldio->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001565 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 +05301566 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1567 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1568 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1569 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001570 } else {
Sumant Patro658dced2006-10-03 13:09:14 -07001571 pthru = (struct megasas_pthru_frame *) cmd->frame;
1572 mfi_sgl = &pthru->sgl;
1573 sgcount = pthru->sge_count;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001574 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 +05301575 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1576 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1577 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1578 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
Sumant Patro658dced2006-10-03 13:09:14 -07001579 }
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001580 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1581 for (n = 0; n < sgcount; n++) {
1582 if (IS_DMA64)
1583 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1584 le32_to_cpu(mfi_sgl->sge64[n].length),
1585 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1586 else
1587 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1588 le32_to_cpu(mfi_sgl->sge32[n].length),
1589 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
Sumant Patro658dced2006-10-03 13:09:14 -07001590 }
1591 }
Sumant Patro658dced2006-10-03 13:09:14 -07001592 } /*for max_cmd*/
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001593 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001594 for (i = 0; i < max_cmd; i++) {
1595
1596 cmd = instance->cmd_list[i];
1597
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001598 if (cmd->sync_cmd == 1)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001599 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
Sumant Patro658dced2006-10-03 13:09:14 -07001600 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001601 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
Sumant Patro658dced2006-10-03 13:09:14 -07001602}
1603
adam radfordcd50ba82010-12-21 10:23:23 -08001604u32
1605megasas_build_and_issue_cmd(struct megasas_instance *instance,
1606 struct scsi_cmnd *scmd)
1607{
1608 struct megasas_cmd *cmd;
1609 u32 frame_count;
1610
1611 cmd = megasas_get_cmd(instance);
1612 if (!cmd)
1613 return SCSI_MLQUEUE_HOST_BUSY;
1614
1615 /*
1616 * Logical drive command
1617 */
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301618 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
adam radfordcd50ba82010-12-21 10:23:23 -08001619 frame_count = megasas_build_ldio(instance, scmd, cmd);
1620 else
1621 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1622
1623 if (!frame_count)
1624 goto out_return_cmd;
1625
1626 cmd->scmd = scmd;
1627 scmd->SCp.ptr = (char *)cmd;
1628
1629 /*
1630 * Issue the command to the FW
1631 */
1632 atomic_inc(&instance->fw_outstanding);
1633
1634 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1635 cmd->frame_count-1, instance->reg_set);
adam radfordcd50ba82010-12-21 10:23:23 -08001636
1637 return 0;
1638out_return_cmd:
1639 megasas_return_cmd(instance, cmd);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301640 return SCSI_MLQUEUE_HOST_BUSY;
adam radfordcd50ba82010-12-21 10:23:23 -08001641}
1642
1643
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001644/**
1645 * megasas_queue_command - Queue entry point
1646 * @scmd: SCSI command to be queued
1647 * @done: Callback entry point
1648 */
1649static int
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301650megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001651{
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001652 struct megasas_instance *instance;
Sumit Saxena18365b12016-01-28 21:04:25 +05301653 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001654
1655 instance = (struct megasas_instance *)
1656 scmd->device->host->hostdata;
Sumant Patroaf37acf2007-02-14 12:34:46 -08001657
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05301658 if (instance->unload == 1) {
1659 scmd->result = DID_NO_CONNECT << 16;
1660 scmd->scsi_done(scmd);
1661 return 0;
1662 }
1663
bo yang39a98552010-09-22 22:36:29 -04001664 if (instance->issuepend_done == 0)
Sumant Patroaf37acf2007-02-14 12:34:46 -08001665 return SCSI_MLQUEUE_HOST_BUSY;
1666
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301667
adam radford229fe472014-03-10 02:51:56 -07001668 /* Check for an mpio path and adjust behavior */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301669 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
adam radford229fe472014-03-10 02:51:56 -07001670 if (megasas_check_mpio_paths(instance, scmd) ==
Shivasharan Sf55cf472017-02-10 00:59:07 -08001671 (DID_REQUEUE << 16)) {
adam radford229fe472014-03-10 02:51:56 -07001672 return SCSI_MLQUEUE_HOST_BUSY;
1673 } else {
adam radford229fe472014-03-10 02:51:56 -07001674 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301675 scmd->scsi_done(scmd);
adam radford229fe472014-03-10 02:51:56 -07001676 return 0;
1677 }
1678 }
1679
Sumit Saxena8a01a412016-01-28 21:04:32 +05301680 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford229fe472014-03-10 02:51:56 -07001681 scmd->result = DID_NO_CONNECT << 16;
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301682 scmd->scsi_done(scmd);
Sumit.Saxena@lsi.comb09e66d2013-05-22 12:29:28 +05301683 return 0;
1684 }
1685
Sumit Saxena18365b12016-01-28 21:04:25 +05301686 mr_device_priv_data = scmd->device->hostdata;
1687 if (!mr_device_priv_data) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301688 scmd->result = DID_NO_CONNECT << 16;
1689 scmd->scsi_done(scmd);
1690 return 0;
1691 }
1692
Sumit Saxena8a01a412016-01-28 21:04:32 +05301693 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04001694 return SCSI_MLQUEUE_HOST_BUSY;
bo yang39a98552010-09-22 22:36:29 -04001695
Sumit Saxena8a01a412016-01-28 21:04:32 +05301696 if (mr_device_priv_data->tm_busy)
Sumit Saxena18365b12016-01-28 21:04:25 +05301697 return SCSI_MLQUEUE_DEVICE_BUSY;
Sumit Saxena18365b12016-01-28 21:04:25 +05301698
bo yang39a98552010-09-22 22:36:29 -04001699
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001700 scmd->result = 0;
1701
Shivasharan S3cabd162017-02-10 00:59:05 -08001702 if (MEGASAS_IS_LOGICAL(scmd->device) &&
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301703 (scmd->device->id >= instance->fw_supported_vd_count ||
1704 scmd->device->lun)) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08001705 scmd->result = DID_BAD_TARGET << 16;
1706 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001707 }
1708
Shivasharan S3cabd162017-02-10 00:59:05 -08001709 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1710 MEGASAS_IS_LOGICAL(scmd->device) &&
1711 (!instance->fw_sync_cache_support)) {
Sumant Patro02b01e02007-02-14 13:00:55 -08001712 scmd->result = DID_OK << 16;
1713 goto out_done;
Sumant Patro02b01e02007-02-14 13:00:55 -08001714 }
1715
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05301716 return instance->instancet->build_and_issue_cmd(instance, scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001717
Sumant Patrocb59aa62006-01-25 11:53:25 -08001718 out_done:
Sumit.Saxena@avagotech.comfb1a24f2014-09-12 18:57:38 +05301719 scmd->scsi_done(scmd);
Sumant Patrocb59aa62006-01-25 11:53:25 -08001720 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001721}
1722
Yang, Bo044833b2009-10-06 14:33:06 -06001723static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1724{
1725 int i;
1726
1727 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1728
1729 if ((megasas_mgmt_info.instance[i]) &&
1730 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1731 return megasas_mgmt_info.instance[i];
1732 }
1733
1734 return NULL;
1735}
1736
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301737/*
Sumit Saxena18365b12016-01-28 21:04:25 +05301738* megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301739*
1740* @sdev: OS provided scsi device
1741*
1742* Returns void
1743*/
Sumit Saxena18365b12016-01-28 21:04:25 +05301744void megasas_update_sdev_properties(struct scsi_device *sdev)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301745{
Sumit Saxena18365b12016-01-28 21:04:25 +05301746 u16 pd_index = 0;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301747 u32 device_id, ld;
1748 struct megasas_instance *instance;
1749 struct fusion_context *fusion;
Sumit Saxena18365b12016-01-28 21:04:25 +05301750 struct MR_PRIV_DEVICE *mr_device_priv_data;
1751 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301752 struct MR_LD_RAID *raid;
1753 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1754
1755 instance = megasas_lookup_instance(sdev->host->host_no);
1756 fusion = instance->ctrl_context;
Sumit Saxena18365b12016-01-28 21:04:25 +05301757 mr_device_priv_data = sdev->hostdata;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301758
Shivasharan Sed981b82017-02-10 00:59:06 -08001759 if (!fusion || !mr_device_priv_data)
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301760 return;
1761
Shivasharan Sed981b82017-02-10 00:59:06 -08001762 if (MEGASAS_IS_LOGICAL(sdev)) {
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301763 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1764 + sdev->id;
1765 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1766 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Shivasharan Sed981b82017-02-10 00:59:06 -08001767 if (ld >= instance->fw_supported_vd_count)
1768 return;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301769 raid = MR_LdRaidGet(ld, local_map_ptr);
1770
1771 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
Shivasharan Sed981b82017-02-10 00:59:06 -08001772 blk_queue_update_dma_alignment(sdev->request_queue,
1773 0x7);
1774
Sumit Saxena18365b12016-01-28 21:04:25 +05301775 mr_device_priv_data->is_tm_capable =
1776 raid->capability.tmCapable;
Shivasharan Sed981b82017-02-10 00:59:06 -08001777 } else if (instance->use_seqnum_jbod_fp) {
1778 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1779 sdev->id;
1780 pd_sync = (void *)fusion->pd_seq_sync
1781 [(instance->pd_seq_map_id - 1) & 1];
1782 mr_device_priv_data->is_tm_capable =
1783 pd_sync->seq[pd_index].capability.tmCapable;
sumit.saxena@avagotech.com0b48d122015-10-15 13:40:44 +05301784 }
1785}
1786
Sumit Saxena2216c302016-01-28 21:04:26 +05301787static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1788{
1789 u16 pd_index = 0;
1790 int ret = DCMD_FAILED;
1791 struct megasas_instance *instance;
1792
1793 instance = megasas_lookup_instance(sdev->host->host_no);
1794
1795 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1796 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1797
1798 if (instance->pd_info) {
1799 mutex_lock(&instance->hba_mutex);
1800 ret = megasas_get_pd_info(instance, pd_index);
1801 mutex_unlock(&instance->hba_mutex);
1802 }
1803
1804 if (ret != DCMD_SUCCESS)
1805 return;
1806
1807 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1808
1809 switch (instance->pd_list[pd_index].interface) {
1810 case SAS_PD:
1811 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1812 break;
1813
1814 case SATA_PD:
1815 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1816 break;
1817
1818 default:
1819 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1820 }
1821 }
1822 }
1823}
1824
Sumit Saxena18365b12016-01-28 21:04:25 +05301825
Christoph Hellwig147aab62006-02-17 12:13:48 +01001826static int megasas_slave_configure(struct scsi_device *sdev)
1827{
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301828 u16 pd_index = 0;
1829 struct megasas_instance *instance;
1830
1831 instance = megasas_lookup_instance(sdev->host->host_no);
Sumit Saxena30845582016-03-10 02:14:37 -08001832 if (instance->pd_list_not_supported) {
Shivasharan S3cabd162017-02-10 00:59:05 -08001833 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301834 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1835 sdev->id;
1836 if (instance->pd_list[pd_index].driveState !=
1837 MR_PD_STATE_SYSTEM)
1838 return -ENXIO;
1839 }
1840 }
Sumit Saxena2216c302016-01-28 21:04:26 +05301841 megasas_set_device_queue_depth(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05301842 megasas_update_sdev_properties(sdev);
1843
Yang, Bo044833b2009-10-06 14:33:06 -06001844 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001845 * The RAID firmware may require extended timeouts.
1846 */
Yang, Bo044833b2009-10-06 14:33:06 -06001847 blk_queue_rq_timeout(sdev->request_queue,
Sumit Saxenae3d178c2016-01-28 21:04:34 +05301848 scmd_timeout * HZ);
Sumit.Saxena@avagotech.com07e38d92014-09-12 18:57:13 +05301849
Yang, Bo044833b2009-10-06 14:33:06 -06001850 return 0;
1851}
1852
1853static int megasas_slave_alloc(struct scsi_device *sdev)
1854{
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001855 u16 pd_index = 0;
Yang, Bo044833b2009-10-06 14:33:06 -06001856 struct megasas_instance *instance ;
Sumit Saxena18365b12016-01-28 21:04:25 +05301857 struct MR_PRIV_DEVICE *mr_device_priv_data;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001858
Yang, Bo044833b2009-10-06 14:33:06 -06001859 instance = megasas_lookup_instance(sdev->host->host_no);
Shivasharan S3cabd162017-02-10 00:59:05 -08001860 if (!MEGASAS_IS_LOGICAL(sdev)) {
Yang, Bo044833b2009-10-06 14:33:06 -06001861 /*
1862 * Open the OS scan to the SYSTEM PD
1863 */
1864 pd_index =
1865 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1866 sdev->id;
Sumit Saxena30845582016-03-10 02:14:37 -08001867 if ((instance->pd_list_not_supported ||
1868 instance->pd_list[pd_index].driveState ==
Sumit Saxenaaed335e2015-11-05 21:17:37 +05301869 MR_PD_STATE_SYSTEM)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05301870 goto scan_target;
Yang, Bo044833b2009-10-06 14:33:06 -06001871 }
1872 return -ENXIO;
1873 }
Sumit Saxena18365b12016-01-28 21:04:25 +05301874
1875scan_target:
1876 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1877 GFP_KERNEL);
1878 if (!mr_device_priv_data)
1879 return -ENOMEM;
1880 sdev->hostdata = mr_device_priv_data;
Christoph Hellwig147aab62006-02-17 12:13:48 +01001881 return 0;
1882}
1883
Sumit Saxena18365b12016-01-28 21:04:25 +05301884static void megasas_slave_destroy(struct scsi_device *sdev)
1885{
1886 kfree(sdev->hostdata);
1887 sdev->hostdata = NULL;
1888}
1889
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301890/*
1891* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1892* kill adapter
1893* @instance: Adapter soft state
1894*
1895*/
kbuild test robot6a6981f2015-04-23 16:33:23 +05301896static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301897{
1898 int i;
1899 struct megasas_cmd *cmd_mfi;
1900 struct megasas_cmd_fusion *cmd_fusion;
1901 struct fusion_context *fusion = instance->ctrl_context;
1902
1903 /* Find all outstanding ioctls */
1904 if (fusion) {
1905 for (i = 0; i < instance->max_fw_cmds; i++) {
1906 cmd_fusion = fusion->cmd_list[i];
1907 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1908 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1909 if (cmd_mfi->sync_cmd &&
1910 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1911 megasas_complete_cmd(instance,
1912 cmd_mfi, DID_OK);
1913 }
1914 }
1915 } else {
1916 for (i = 0; i < instance->max_fw_cmds; i++) {
1917 cmd_mfi = instance->cmd_list[i];
1918 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1919 MFI_CMD_ABORT)
1920 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1921 }
1922 }
1923}
1924
1925
adam radford9c915a82010-12-21 13:34:31 -08001926void megaraid_sas_kill_hba(struct megasas_instance *instance)
bo yang39a98552010-09-22 22:36:29 -04001927{
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301928 /* Set critical error to block I/O & ioctls in case caller didn't */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301929 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301930 /* Wait 1 second to ensure IO or ioctls in build have posted */
1931 msleep(1000);
bo yang39a98552010-09-22 22:36:29 -04001932 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301933 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301934 (instance->ctrl_context)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05001935 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
adam radford229fe472014-03-10 02:51:56 -07001936 /* Flush */
1937 readl(&instance->reg_set->doorbell);
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05301938 if (instance->requestorId && instance->peerIsPresent)
adam radford229fe472014-03-10 02:51:56 -07001939 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
bo yang39a98552010-09-22 22:36:29 -04001940 } else {
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301941 writel(MFI_STOP_ADP,
1942 &instance->reg_set->inbound_doorbell);
adam radford9c915a82010-12-21 13:34:31 -08001943 }
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05301944 /* Complete outstanding ioctls when adapter is killed */
1945 megasas_complete_outstanding_ioctls(instance);
adam radford9c915a82010-12-21 13:34:31 -08001946}
1947
1948 /**
1949 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1950 * restored to max value
1951 * @instance: Adapter soft state
1952 *
1953 */
1954void
1955megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1956{
1957 unsigned long flags;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301958
adam radford9c915a82010-12-21 13:34:31 -08001959 if (instance->flag & MEGASAS_FW_BUSY
adam radfordc5daa6a2012-07-17 18:20:03 -07001960 && time_after(jiffies, instance->last_time + 5 * HZ)
1961 && atomic_read(&instance->fw_outstanding) <
1962 instance->throttlequeuedepth + 1) {
adam radford9c915a82010-12-21 13:34:31 -08001963
1964 spin_lock_irqsave(instance->host->host_lock, flags);
1965 instance->flag &= ~MEGASAS_FW_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08001966
Sumit Saxena308ec452016-01-28 21:04:30 +05301967 instance->host->can_queue = instance->cur_can_queue;
adam radford9c915a82010-12-21 13:34:31 -08001968 spin_unlock_irqrestore(instance->host->host_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04001969 }
1970}
1971
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001972/**
bo yang7343eb62007-11-09 04:35:44 -05001973 * megasas_complete_cmd_dpc - Returns FW's controller structure
1974 * @instance_addr: Address of adapter soft state
1975 *
1976 * Tasklet to complete cmds
1977 */
1978static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1979{
1980 u32 producer;
1981 u32 consumer;
1982 u32 context;
1983 struct megasas_cmd *cmd;
1984 struct megasas_instance *instance =
1985 (struct megasas_instance *)instance_addr;
1986 unsigned long flags;
1987
1988 /* If we have already declared adapter dead, donot complete cmds */
Sumit Saxena8a01a412016-01-28 21:04:32 +05301989 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang7343eb62007-11-09 04:35:44 -05001990 return;
1991
1992 spin_lock_irqsave(&instance->completion_lock, flags);
1993
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301994 producer = le32_to_cpu(*instance->producer);
1995 consumer = le32_to_cpu(*instance->consumer);
bo yang7343eb62007-11-09 04:35:44 -05001996
1997 while (consumer != producer) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301998 context = le32_to_cpu(instance->reply_queue[consumer]);
bo yang39a98552010-09-22 22:36:29 -04001999 if (context >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002000 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
bo yang39a98552010-09-22 22:36:29 -04002001 context);
2002 BUG();
2003 }
bo yang7343eb62007-11-09 04:35:44 -05002004
2005 cmd = instance->cmd_list[context];
2006
2007 megasas_complete_cmd(instance, cmd, DID_OK);
2008
2009 consumer++;
2010 if (consumer == (instance->max_fw_cmds + 1)) {
2011 consumer = 0;
2012 }
2013 }
2014
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302015 *instance->consumer = cpu_to_le32(producer);
bo yang7343eb62007-11-09 04:35:44 -05002016
2017 spin_unlock_irqrestore(&instance->completion_lock, flags);
2018
2019 /*
2020 * Check if we can restore can_queue
2021 */
adam radford9c915a82010-12-21 13:34:31 -08002022 megasas_check_and_restore_queue_depth(instance);
bo yang7343eb62007-11-09 04:35:44 -05002023}
2024
adam radford229fe472014-03-10 02:51:56 -07002025/**
2026 * megasas_start_timer - Initializes a timer object
2027 * @instance: Adapter soft state
2028 * @timer: timer object to be initialized
2029 * @fn: timer function
2030 * @interval: time interval between timer function call
2031 *
2032 */
2033void megasas_start_timer(struct megasas_instance *instance,
2034 struct timer_list *timer,
2035 void *fn, unsigned long interval)
2036{
2037 init_timer(timer);
2038 timer->expires = jiffies + interval;
2039 timer->data = (unsigned long)instance;
2040 timer->function = fn;
2041 add_timer(timer);
2042}
2043
Yang, Bo707e09b2010-10-12 07:20:27 -06002044static void
2045megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2046
2047static void
2048process_fw_state_change_wq(struct work_struct *work);
2049
2050void megasas_do_ocr(struct megasas_instance *instance)
2051{
2052 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2053 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2054 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302055 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
Yang, Bo707e09b2010-10-12 07:20:27 -06002056 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302057 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302058 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Yang, Bo707e09b2010-10-12 07:20:27 -06002059 instance->issuepend_done = 0;
2060
2061 atomic_set(&instance->fw_outstanding, 0);
2062 megasas_internal_reset_defer_cmds(instance);
2063 process_fw_state_change_wq(&instance->work_init);
2064}
2065
Adam Radford4cbfea82014-07-09 15:17:56 -07002066static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2067 int initial)
adam radford229fe472014-03-10 02:51:56 -07002068{
2069 struct megasas_cmd *cmd;
2070 struct megasas_dcmd_frame *dcmd;
adam radford229fe472014-03-10 02:51:56 -07002071 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
adam radford229fe472014-03-10 02:51:56 -07002072 dma_addr_t new_affiliation_111_h;
2073 int ld, retval = 0;
2074 u8 thisVf;
2075
2076 cmd = megasas_get_cmd(instance);
2077
2078 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002079 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2080 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002081 instance->host->host_no);
2082 return -ENOMEM;
2083 }
2084
2085 dcmd = &cmd->frame->dcmd;
2086
Adam Radford4cbfea82014-07-09 15:17:56 -07002087 if (!instance->vf_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002088 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2089 "affiliation for scsi%d\n", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002090 megasas_return_cmd(instance, cmd);
2091 return -ENOMEM;
2092 }
2093
2094 if (initial)
adam radford229fe472014-03-10 02:51:56 -07002095 memset(instance->vf_affiliation_111, 0,
2096 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002097 else {
Adam Radford4cbfea82014-07-09 15:17:56 -07002098 new_affiliation_111 =
2099 pci_alloc_consistent(instance->pdev,
2100 sizeof(struct MR_LD_VF_AFFILIATION_111),
2101 &new_affiliation_111_h);
2102 if (!new_affiliation_111) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002103 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2104 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002105 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002106 megasas_return_cmd(instance, cmd);
2107 return -ENOMEM;
2108 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002109 memset(new_affiliation_111, 0,
2110 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002111 }
2112
2113 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2114
2115 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302116 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002117 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302118 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002119 dcmd->timeout = 0;
2120 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302121 dcmd->data_xfer_len =
2122 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2123 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
adam radford229fe472014-03-10 02:51:56 -07002124
Adam Radford4cbfea82014-07-09 15:17:56 -07002125 if (initial)
2126 dcmd->sgl.sge32[0].phys_addr =
Christoph Hellwig2213a462015-04-23 16:33:54 +05302127 cpu_to_le32(instance->vf_affiliation_111_h);
adam radford229fe472014-03-10 02:51:56 -07002128 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302129 dcmd->sgl.sge32[0].phys_addr =
2130 cpu_to_le32(new_affiliation_111_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002131
Christoph Hellwig2213a462015-04-23 16:33:54 +05302132 dcmd->sgl.sge32[0].length = cpu_to_le32(
2133 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002134
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002135 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
adam radford229fe472014-03-10 02:51:56 -07002136 "scsi%d\n", instance->host->host_no);
2137
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302138 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002139 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2140 " failed with status 0x%x for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002141 dcmd->cmd_status, instance->host->host_no);
2142 retval = 1; /* Do a scan if we couldn't get affiliation */
2143 goto out;
2144 }
2145
2146 if (!initial) {
Adam Radford4cbfea82014-07-09 15:17:56 -07002147 thisVf = new_affiliation_111->thisVf;
2148 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2149 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2150 new_affiliation_111->map[ld].policy[thisVf]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002151 dev_warn(&instance->pdev->dev, "SR-IOV: "
2152 "Got new LD/VF affiliation for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002153 instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002154 memcpy(instance->vf_affiliation_111,
2155 new_affiliation_111,
2156 sizeof(struct MR_LD_VF_AFFILIATION_111));
adam radford229fe472014-03-10 02:51:56 -07002157 retval = 1;
2158 goto out;
2159 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002160 }
2161out:
2162 if (new_affiliation_111) {
2163 pci_free_consistent(instance->pdev,
2164 sizeof(struct MR_LD_VF_AFFILIATION_111),
2165 new_affiliation_111,
2166 new_affiliation_111_h);
2167 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302168
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302169 megasas_return_cmd(instance, cmd);
Adam Radford4cbfea82014-07-09 15:17:56 -07002170
2171 return retval;
2172}
2173
2174static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2175 int initial)
2176{
2177 struct megasas_cmd *cmd;
2178 struct megasas_dcmd_frame *dcmd;
2179 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2180 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2181 dma_addr_t new_affiliation_h;
2182 int i, j, retval = 0, found = 0, doscan = 0;
2183 u8 thisVf;
2184
2185 cmd = megasas_get_cmd(instance);
2186
2187 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002188 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2189 "Failed to get cmd for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002190 instance->host->host_no);
2191 return -ENOMEM;
2192 }
2193
2194 dcmd = &cmd->frame->dcmd;
2195
2196 if (!instance->vf_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002197 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2198 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002199 megasas_return_cmd(instance, cmd);
2200 return -ENOMEM;
2201 }
2202
2203 if (initial)
2204 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2205 sizeof(struct MR_LD_VF_AFFILIATION));
2206 else {
2207 new_affiliation =
2208 pci_alloc_consistent(instance->pdev,
2209 (MAX_LOGICAL_DRIVES + 1) *
2210 sizeof(struct MR_LD_VF_AFFILIATION),
2211 &new_affiliation_h);
2212 if (!new_affiliation) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002213 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2214 "memory for new affiliation for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002215 instance->host->host_no);
2216 megasas_return_cmd(instance, cmd);
2217 return -ENOMEM;
2218 }
2219 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2220 sizeof(struct MR_LD_VF_AFFILIATION));
2221 }
2222
2223 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2224
2225 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302226 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Adam Radford4cbfea82014-07-09 15:17:56 -07002227 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302228 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
Adam Radford4cbfea82014-07-09 15:17:56 -07002229 dcmd->timeout = 0;
2230 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302231 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2232 sizeof(struct MR_LD_VF_AFFILIATION));
2233 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
Adam Radford4cbfea82014-07-09 15:17:56 -07002234
2235 if (initial)
Christoph Hellwig2213a462015-04-23 16:33:54 +05302236 dcmd->sgl.sge32[0].phys_addr =
2237 cpu_to_le32(instance->vf_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002238 else
Christoph Hellwig2213a462015-04-23 16:33:54 +05302239 dcmd->sgl.sge32[0].phys_addr =
2240 cpu_to_le32(new_affiliation_h);
Adam Radford4cbfea82014-07-09 15:17:56 -07002241
Christoph Hellwig2213a462015-04-23 16:33:54 +05302242 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2243 sizeof(struct MR_LD_VF_AFFILIATION));
Adam Radford4cbfea82014-07-09 15:17:56 -07002244
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002245 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
Adam Radford4cbfea82014-07-09 15:17:56 -07002246 "scsi%d\n", instance->host->host_no);
2247
Adam Radford4cbfea82014-07-09 15:17:56 -07002248
Sumit Saxena6d40afb2016-01-28 21:04:23 +05302249 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002250 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2251 " failed with status 0x%x for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002252 dcmd->cmd_status, instance->host->host_no);
2253 retval = 1; /* Do a scan if we couldn't get affiliation */
2254 goto out;
2255 }
2256
2257 if (!initial) {
2258 if (!new_affiliation->ldCount) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002259 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2260 "affiliation for passive path for scsi%d\n",
Adam Radford4cbfea82014-07-09 15:17:56 -07002261 instance->host->host_no);
2262 retval = 1;
2263 goto out;
2264 }
2265 newmap = new_affiliation->map;
2266 savedmap = instance->vf_affiliation->map;
2267 thisVf = new_affiliation->thisVf;
2268 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2269 found = 0;
2270 for (j = 0; j < instance->vf_affiliation->ldCount;
2271 j++) {
2272 if (newmap->ref.targetId ==
2273 savedmap->ref.targetId) {
2274 found = 1;
2275 if (newmap->policy[thisVf] !=
2276 savedmap->policy[thisVf]) {
2277 doscan = 1;
2278 goto out;
2279 }
adam radford229fe472014-03-10 02:51:56 -07002280 }
2281 savedmap = (struct MR_LD_VF_MAP *)
2282 ((unsigned char *)savedmap +
2283 savedmap->size);
Adam Radford4cbfea82014-07-09 15:17:56 -07002284 }
2285 if (!found && newmap->policy[thisVf] !=
2286 MR_LD_ACCESS_HIDDEN) {
2287 doscan = 1;
2288 goto out;
2289 }
2290 newmap = (struct MR_LD_VF_MAP *)
2291 ((unsigned char *)newmap + newmap->size);
2292 }
2293
2294 newmap = new_affiliation->map;
2295 savedmap = instance->vf_affiliation->map;
2296
2297 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2298 found = 0;
2299 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2300 if (savedmap->ref.targetId ==
2301 newmap->ref.targetId) {
2302 found = 1;
2303 if (savedmap->policy[thisVf] !=
2304 newmap->policy[thisVf]) {
2305 doscan = 1;
2306 goto out;
2307 }
2308 }
adam radford229fe472014-03-10 02:51:56 -07002309 newmap = (struct MR_LD_VF_MAP *)
2310 ((unsigned char *)newmap +
2311 newmap->size);
2312 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002313 if (!found && savedmap->policy[thisVf] !=
2314 MR_LD_ACCESS_HIDDEN) {
2315 doscan = 1;
2316 goto out;
2317 }
2318 savedmap = (struct MR_LD_VF_MAP *)
2319 ((unsigned char *)savedmap +
2320 savedmap->size);
adam radford229fe472014-03-10 02:51:56 -07002321 }
2322 }
2323out:
Adam Radford4cbfea82014-07-09 15:17:56 -07002324 if (doscan) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002325 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2326 "affiliation for scsi%d\n", instance->host->host_no);
Adam Radford4cbfea82014-07-09 15:17:56 -07002327 memcpy(instance->vf_affiliation, new_affiliation,
2328 new_affiliation->size);
2329 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002330 }
Adam Radford4cbfea82014-07-09 15:17:56 -07002331
2332 if (new_affiliation)
2333 pci_free_consistent(instance->pdev,
2334 (MAX_LOGICAL_DRIVES + 1) *
2335 sizeof(struct MR_LD_VF_AFFILIATION),
2336 new_affiliation, new_affiliation_h);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302337 megasas_return_cmd(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002338
2339 return retval;
2340}
2341
Adam Radford4cbfea82014-07-09 15:17:56 -07002342/* This function will get the current SR-IOV LD/VF affiliation */
2343static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2344 int initial)
2345{
2346 int retval;
2347
2348 if (instance->PlasmaFW111)
2349 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2350 else
2351 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2352 return retval;
2353}
2354
adam radford229fe472014-03-10 02:51:56 -07002355/* This function will tell FW to start the SR-IOV heartbeat */
2356int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2357 int initial)
2358{
2359 struct megasas_cmd *cmd;
2360 struct megasas_dcmd_frame *dcmd;
2361 int retval = 0;
2362
2363 cmd = megasas_get_cmd(instance);
2364
2365 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002366 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2367 "Failed to get cmd for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002368 instance->host->host_no);
2369 return -ENOMEM;
2370 }
2371
2372 dcmd = &cmd->frame->dcmd;
2373
2374 if (initial) {
2375 instance->hb_host_mem =
Joe Perches7c845eb2014-08-08 14:24:46 -07002376 pci_zalloc_consistent(instance->pdev,
2377 sizeof(struct MR_CTRL_HB_HOST_MEM),
2378 &instance->hb_host_mem_h);
adam radford229fe472014-03-10 02:51:56 -07002379 if (!instance->hb_host_mem) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002380 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2381 " memory for heartbeat host memory for scsi%d\n",
2382 instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002383 retval = -ENOMEM;
2384 goto out;
2385 }
adam radford229fe472014-03-10 02:51:56 -07002386 }
2387
2388 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2389
Christoph Hellwig2213a462015-04-23 16:33:54 +05302390 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002391 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302392 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford229fe472014-03-10 02:51:56 -07002393 dcmd->sge_count = 1;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302394 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
adam radford229fe472014-03-10 02:51:56 -07002395 dcmd->timeout = 0;
2396 dcmd->pad_0 = 0;
Christoph Hellwig2213a462015-04-23 16:33:54 +05302397 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2398 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2399 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2400 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
adam radford229fe472014-03-10 02:51:56 -07002401
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002402 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
adam radford229fe472014-03-10 02:51:56 -07002403 instance->host->host_no);
2404
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302405 if (instance->ctrl_context && !instance->mask_interrupts)
2406 retval = megasas_issue_blocked_cmd(instance, cmd,
2407 MEGASAS_ROUTINE_WAIT_TIME_VF);
2408 else
2409 retval = megasas_issue_polled(instance, cmd);
adam radford229fe472014-03-10 02:51:56 -07002410
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302411 if (retval) {
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302412 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2413 "_MEM_ALLOC DCMD %s for scsi%d\n",
2414 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2415 "timed out" : "failed", instance->host->host_no);
adam radford229fe472014-03-10 02:51:56 -07002416 retval = 1;
adam radford229fe472014-03-10 02:51:56 -07002417 }
2418
2419out:
2420 megasas_return_cmd(instance, cmd);
2421
2422 return retval;
2423}
2424
2425/* Handler for SR-IOV heartbeat */
2426void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2427{
2428 struct megasas_instance *instance =
2429 (struct megasas_instance *)instance_addr;
2430
2431 if (instance->hb_host_mem->HB.fwCounter !=
2432 instance->hb_host_mem->HB.driverCounter) {
2433 instance->hb_host_mem->HB.driverCounter =
2434 instance->hb_host_mem->HB.fwCounter;
2435 mod_timer(&instance->sriov_heartbeat_timer,
2436 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2437 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002438 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
adam radford229fe472014-03-10 02:51:56 -07002439 "completed for scsi%d\n", instance->host->host_no);
2440 schedule_work(&instance->work_init);
2441 }
2442}
2443
bo yang7343eb62007-11-09 04:35:44 -05002444/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002445 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2446 * @instance: Adapter soft state
2447 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002448 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002449 * complete all its outstanding commands. Returns error if one or more IOs
2450 * are pending after this time period. It also marks the controller dead.
2451 */
2452static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2453{
Sumit Saxenaccc75072016-01-28 21:04:33 +05302454 int i, sl, outstanding;
bo yang39a98552010-09-22 22:36:29 -04002455 u32 reset_index;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002456 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
bo yang39a98552010-09-22 22:36:29 -04002457 unsigned long flags;
2458 struct list_head clist_local;
2459 struct megasas_cmd *reset_cmd;
Yang, Bo707e09b2010-10-12 07:20:27 -06002460 u32 fw_state;
bo yang39a98552010-09-22 22:36:29 -04002461
Sumit Saxenaccc75072016-01-28 21:04:33 +05302462 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2463 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2464 __func__, __LINE__);
2465 return FAILED;
2466 }
bo yang39a98552010-09-22 22:36:29 -04002467
Sumit Saxena8a01a412016-01-28 21:04:32 +05302468 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04002469
2470 INIT_LIST_HEAD(&clist_local);
2471 spin_lock_irqsave(&instance->hba_lock, flags);
2472 list_splice_init(&instance->internal_reset_pending_q,
2473 &clist_local);
2474 spin_unlock_irqrestore(&instance->hba_lock, flags);
2475
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002476 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
bo yang39a98552010-09-22 22:36:29 -04002477 for (i = 0; i < wait_time; i++) {
2478 msleep(1000);
Sumit Saxena8a01a412016-01-28 21:04:32 +05302479 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
bo yang39a98552010-09-22 22:36:29 -04002480 break;
2481 }
2482
Sumit Saxena8a01a412016-01-28 21:04:32 +05302483 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002484 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05302485 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
bo yang39a98552010-09-22 22:36:29 -04002486 return FAILED;
2487 }
2488
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002489 reset_index = 0;
bo yang39a98552010-09-22 22:36:29 -04002490 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002491 reset_cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04002492 struct megasas_cmd, list);
2493 list_del_init(&reset_cmd->list);
2494 if (reset_cmd->scmd) {
Shivasharan Sf55cf472017-02-10 00:59:07 -08002495 reset_cmd->scmd->result = DID_REQUEUE << 16;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002496 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
bo yang39a98552010-09-22 22:36:29 -04002497 reset_index, reset_cmd,
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002498 reset_cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04002499
2500 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2501 megasas_return_cmd(instance, reset_cmd);
2502 } else if (reset_cmd->sync_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002503 dev_notice(&instance->pdev->dev, "%p synch cmds"
bo yang39a98552010-09-22 22:36:29 -04002504 "reset queue\n",
2505 reset_cmd);
2506
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05302507 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04002508 instance->instancet->fire_cmd(instance,
2509 reset_cmd->frame_phys_addr,
2510 0, instance->reg_set);
2511 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002512 dev_notice(&instance->pdev->dev, "%p unexpected"
bo yang39a98552010-09-22 22:36:29 -04002513 "cmds lst\n",
2514 reset_cmd);
2515 }
2516 reset_index++;
2517 }
2518
2519 return SUCCESS;
2520 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002521
adam radfordc007b8b2012-07-17 18:20:24 -07002522 for (i = 0; i < resetwaittime; i++) {
Sumit Saxenaccc75072016-01-28 21:04:33 +05302523 outstanding = atomic_read(&instance->fw_outstanding);
Sumant Patroe4a082c2006-05-30 12:03:37 -07002524
2525 if (!outstanding)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002526 break;
2527
2528 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002529 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
Sumant Patroe4a082c2006-05-30 12:03:37 -07002530 "commands to complete\n",i,outstanding);
bo yang7343eb62007-11-09 04:35:44 -05002531 /*
2532 * Call cmd completion routine. Cmd to be
2533 * be completed directly without depending on isr.
2534 */
2535 megasas_complete_cmd_dpc((unsigned long)instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002536 }
2537
2538 msleep(1000);
2539 }
2540
Yang, Bo707e09b2010-10-12 07:20:27 -06002541 i = 0;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302542 outstanding = atomic_read(&instance->fw_outstanding);
2543 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
Yang, Bo707e09b2010-10-12 07:20:27 -06002544
Sumit Saxenaccc75072016-01-28 21:04:33 +05302545 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2546 goto no_outstanding;
2547
2548 if (instance->disableOnlineCtrlReset)
2549 goto kill_hba_and_failed;
2550 do {
2551 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2552 dev_info(&instance->pdev->dev,
2553 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2554 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2555 if (i == 3)
2556 goto kill_hba_and_failed;
2557 megasas_do_ocr(instance);
2558
2559 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2560 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2561 __func__, __LINE__);
2562 return FAILED;
2563 }
2564 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2565 __func__, __LINE__);
2566
2567 for (sl = 0; sl < 10; sl++)
2568 msleep(500);
2569
2570 outstanding = atomic_read(&instance->fw_outstanding);
2571
2572 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2573 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2574 goto no_outstanding;
Yang, Bo707e09b2010-10-12 07:20:27 -06002575 }
2576 i++;
2577 } while (i <= 3);
2578
Sumit Saxenaccc75072016-01-28 21:04:33 +05302579no_outstanding:
Yang, Bo707e09b2010-10-12 07:20:27 -06002580
Sumit Saxenaccc75072016-01-28 21:04:33 +05302581 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2582 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002583 return SUCCESS;
Sumit Saxenaccc75072016-01-28 21:04:33 +05302584
2585kill_hba_and_failed:
2586
2587 /* Reset not supported, kill adapter */
2588 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2589 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2590 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2591 atomic_read(&instance->fw_outstanding));
2592 megasas_dump_pending_frames(instance);
2593 megaraid_sas_kill_hba(instance);
2594
2595 return FAILED;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002596}
2597
2598/**
2599 * megasas_generic_reset - Generic reset routine
2600 * @scmd: Mid-layer SCSI command
2601 *
2602 * This routine implements a generic reset handler for device, bus and host
2603 * reset requests. Device, bus and host specific reset handlers can use this
2604 * function after they do their specific tasks.
2605 */
2606static int megasas_generic_reset(struct scsi_cmnd *scmd)
2607{
2608 int ret_val;
2609 struct megasas_instance *instance;
2610
2611 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2612
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04002613 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2614 scmd->cmnd[0], scmd->retries);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002615
Sumit Saxena8a01a412016-01-28 21:04:32 +05302616 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002617 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002618 return FAILED;
2619 }
2620
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002621 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002622 if (ret_val == SUCCESS)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002623 dev_notice(&instance->pdev->dev, "reset successful\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002624 else
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002625 dev_err(&instance->pdev->dev, "failed to do reset\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002626
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002627 return ret_val;
2628}
2629
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002630/**
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002631 * megasas_reset_timer - quiesce the adapter if required
2632 * @scmd: scsi cmnd
2633 *
2634 * Sets the FW busy flag and reduces the host->can_queue if the
2635 * cmd has not been completed within the timeout period.
2636 */
2637static enum
Jens Axboe242f9dc2008-09-14 05:55:09 -07002638blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002639{
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002640 struct megasas_instance *instance;
2641 unsigned long flags;
2642
2643 if (time_after(jiffies, scmd->jiffies_at_alloc +
Sumit Saxenae3d178c2016-01-28 21:04:34 +05302644 (scmd_timeout * 2) * HZ)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07002645 return BLK_EH_NOT_HANDLED;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002646 }
2647
adam radfordf575c5d2011-10-13 16:01:12 -07002648 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002649 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2650 /* FW is busy, throttle IO */
2651 spin_lock_irqsave(instance->host->host_lock, flags);
2652
adam radfordc5daa6a2012-07-17 18:20:03 -07002653 instance->host->can_queue = instance->throttlequeuedepth;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002654 instance->last_time = jiffies;
2655 instance->flag |= MEGASAS_FW_BUSY;
2656
2657 spin_unlock_irqrestore(instance->host->host_lock, flags);
2658 }
Jens Axboe242f9dc2008-09-14 05:55:09 -07002659 return BLK_EH_RESET_TIMER;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002660}
2661
2662/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002663 * megasas_reset_bus_host - Bus & host reset handler entry point
2664 */
2665static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2666{
2667 int ret;
adam radford9c915a82010-12-21 13:34:31 -08002668 struct megasas_instance *instance;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002669
adam radford9c915a82010-12-21 13:34:31 -08002670 instance = (struct megasas_instance *)scmd->device->host->hostdata;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002671
2672 /*
Uwe Zeisberger80682fa2006-03-22 00:21:33 +01002673 * First wait for all commands to complete
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002674 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302675 if (instance->ctrl_context)
adam radford229fe472014-03-10 02:51:56 -07002676 ret = megasas_reset_fusion(scmd->device->host, 1);
adam radford9c915a82010-12-21 13:34:31 -08002677 else
2678 ret = megasas_generic_reset(scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002679
2680 return ret;
2681}
2682
2683/**
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002684 * megasas_task_abort - Issues task abort request to firmware
2685 * (supported only for fusion adapters)
2686 * @scmd: SCSI command pointer
2687 */
2688static int megasas_task_abort(struct scsi_cmnd *scmd)
2689{
2690 int ret;
2691 struct megasas_instance *instance;
2692
2693 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2694
2695 if (instance->ctrl_context)
2696 ret = megasas_task_abort_fusion(scmd);
2697 else {
2698 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2699 ret = FAILED;
2700 }
2701
2702 return ret;
2703}
2704
2705/**
2706 * megasas_reset_target: Issues target reset request to firmware
2707 * (supported only for fusion adapters)
2708 * @scmd: SCSI command pointer
2709 */
2710static int megasas_reset_target(struct scsi_cmnd *scmd)
2711{
2712 int ret;
2713 struct megasas_instance *instance;
2714
2715 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2716
2717 if (instance->ctrl_context)
2718 ret = megasas_reset_target_fusion(scmd);
2719 else {
2720 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2721 ret = FAILED;
2722 }
2723
2724 return ret;
2725}
2726
2727/**
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002728 * megasas_bios_param - Returns disk geometry for a disk
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002729 * @sdev: device handle
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002730 * @bdev: block device
2731 * @capacity: drive capacity
2732 * @geom: geometry parameters
2733 */
2734static int
2735megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2736 sector_t capacity, int geom[])
2737{
2738 int heads;
2739 int sectors;
2740 sector_t cylinders;
2741 unsigned long tmp;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002742
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002743 /* Default heads (64) & sectors (32) */
2744 heads = 64;
2745 sectors = 32;
2746
2747 tmp = heads * sectors;
2748 cylinders = capacity;
2749
2750 sector_div(cylinders, tmp);
2751
2752 /*
2753 * Handle extended translation size for logical drives > 1Gb
2754 */
2755
2756 if (capacity >= 0x200000) {
2757 heads = 255;
2758 sectors = 63;
2759 tmp = heads*sectors;
2760 cylinders = capacity;
2761 sector_div(cylinders, tmp);
2762 }
2763
2764 geom[0] = heads;
2765 geom[1] = sectors;
2766 geom[2] = cylinders;
2767
2768 return 0;
2769}
2770
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002771static void megasas_aen_polling(struct work_struct *work);
2772
Sumant Patrocf62a0a2007-02-14 12:41:55 -08002773/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002774 * megasas_service_aen - Processes an event notification
2775 * @instance: Adapter soft state
2776 * @cmd: AEN command completed by the ISR
2777 *
2778 * For AEN, driver sends a command down to FW that is held by the FW till an
2779 * event occurs. When an event of interest occurs, FW completes the command
2780 * that it was previously holding.
2781 *
2782 * This routines sends SIGIO signal to processes that have registered with the
2783 * driver for AEN.
2784 */
2785static void
2786megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2787{
Yang, Boc3518832009-10-06 14:18:02 -06002788 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002789
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002790 /*
2791 * Don't signal app if it is just an aborted previously registered aen
2792 */
Yang, Boc3518832009-10-06 14:18:02 -06002793 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2794 spin_lock_irqsave(&poll_aen_lock, flags);
2795 megasas_poll_wait_aen = 1;
2796 spin_unlock_irqrestore(&poll_aen_lock, flags);
2797 wake_up(&megasas_poll_wait);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002798 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
Yang, Boc3518832009-10-06 14:18:02 -06002799 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002800 else
2801 cmd->abort_aen = 0;
2802
2803 instance->aen_cmd = NULL;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302804
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302805 megasas_return_cmd(instance, cmd);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002806
bo yang39a98552010-09-22 22:36:29 -04002807 if ((instance->unload == 0) &&
2808 ((instance->issuepend_done == 1))) {
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002809 struct megasas_aen_event *ev;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002810
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002811 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2812 if (!ev) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002813 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002814 } else {
2815 ev->instance = instance;
2816 instance->ev = ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08002817 INIT_DELAYED_WORK(&ev->hotplug_work,
2818 megasas_aen_polling);
2819 schedule_delayed_work(&ev->hotplug_work, 0);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06002820 }
2821 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002822}
2823
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302824static ssize_t
2825megasas_fw_crash_buffer_store(struct device *cdev,
2826 struct device_attribute *attr, const char *buf, size_t count)
2827{
2828 struct Scsi_Host *shost = class_to_shost(cdev);
2829 struct megasas_instance *instance =
2830 (struct megasas_instance *) shost->hostdata;
2831 int val = 0;
2832 unsigned long flags;
2833
2834 if (kstrtoint(buf, 0, &val) != 0)
2835 return -EINVAL;
2836
2837 spin_lock_irqsave(&instance->crashdump_lock, flags);
2838 instance->fw_crash_buffer_offset = val;
2839 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2840 return strlen(buf);
2841}
2842
2843static ssize_t
2844megasas_fw_crash_buffer_show(struct device *cdev,
2845 struct device_attribute *attr, char *buf)
2846{
2847 struct Scsi_Host *shost = class_to_shost(cdev);
2848 struct megasas_instance *instance =
2849 (struct megasas_instance *) shost->hostdata;
2850 u32 size;
2851 unsigned long buff_addr;
2852 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2853 unsigned long src_addr;
2854 unsigned long flags;
2855 u32 buff_offset;
2856
2857 spin_lock_irqsave(&instance->crashdump_lock, flags);
2858 buff_offset = instance->fw_crash_buffer_offset;
2859 if (!instance->crash_dump_buf &&
2860 !((instance->fw_crash_state == AVAILABLE) ||
2861 (instance->fw_crash_state == COPYING))) {
2862 dev_err(&instance->pdev->dev,
2863 "Firmware crash dump is not available\n");
2864 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2865 return -EINVAL;
2866 }
2867
2868 buff_addr = (unsigned long) buf;
2869
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002870 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302871 dev_err(&instance->pdev->dev,
2872 "Firmware crash dump offset is out of range\n");
2873 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2874 return 0;
2875 }
2876
2877 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2878 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2879
2880 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2881 (buff_offset % dmachunk);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002882 memcpy(buf, (void *)src_addr, size);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302883 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2884
2885 return size;
2886}
2887
2888static ssize_t
2889megasas_fw_crash_buffer_size_show(struct device *cdev,
2890 struct device_attribute *attr, char *buf)
2891{
2892 struct Scsi_Host *shost = class_to_shost(cdev);
2893 struct megasas_instance *instance =
2894 (struct megasas_instance *) shost->hostdata;
2895
2896 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2897 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2898}
2899
2900static ssize_t
2901megasas_fw_crash_state_store(struct device *cdev,
2902 struct device_attribute *attr, const char *buf, size_t count)
2903{
2904 struct Scsi_Host *shost = class_to_shost(cdev);
2905 struct megasas_instance *instance =
2906 (struct megasas_instance *) shost->hostdata;
2907 int val = 0;
2908 unsigned long flags;
2909
2910 if (kstrtoint(buf, 0, &val) != 0)
2911 return -EINVAL;
2912
2913 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2914 dev_err(&instance->pdev->dev, "application updates invalid "
2915 "firmware crash state\n");
2916 return -EINVAL;
2917 }
2918
2919 instance->fw_crash_state = val;
2920
2921 if ((val == COPIED) || (val == COPY_ERROR)) {
2922 spin_lock_irqsave(&instance->crashdump_lock, flags);
2923 megasas_free_host_crash_buffer(instance);
2924 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2925 if (val == COPY_ERROR)
2926 dev_info(&instance->pdev->dev, "application failed to "
2927 "copy Firmware crash dump\n");
2928 else
2929 dev_info(&instance->pdev->dev, "Firmware crash dump "
2930 "copied successfully\n");
2931 }
2932 return strlen(buf);
2933}
2934
2935static ssize_t
2936megasas_fw_crash_state_show(struct device *cdev,
2937 struct device_attribute *attr, char *buf)
2938{
2939 struct Scsi_Host *shost = class_to_shost(cdev);
2940 struct megasas_instance *instance =
2941 (struct megasas_instance *) shost->hostdata;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05002942
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302943 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2944}
2945
2946static ssize_t
2947megasas_page_size_show(struct device *cdev,
2948 struct device_attribute *attr, char *buf)
2949{
2950 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2951}
2952
Sumit Saxena308ec452016-01-28 21:04:30 +05302953static ssize_t
2954megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2955 char *buf)
2956{
2957 struct Scsi_Host *shost = class_to_shost(cdev);
2958 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2959
2960 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2961}
2962
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302963static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2964 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2965static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2966 megasas_fw_crash_buffer_size_show, NULL);
2967static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2968 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2969static DEVICE_ATTR(page_size, S_IRUGO,
2970 megasas_page_size_show, NULL);
Sumit Saxena308ec452016-01-28 21:04:30 +05302971static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2972 megasas_ldio_outstanding_show, NULL);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302973
2974struct device_attribute *megaraid_host_attrs[] = {
2975 &dev_attr_fw_crash_buffer_size,
2976 &dev_attr_fw_crash_buffer,
2977 &dev_attr_fw_crash_state,
2978 &dev_attr_page_size,
Sumit Saxena308ec452016-01-28 21:04:30 +05302979 &dev_attr_ldio_outstanding,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302980 NULL,
2981};
2982
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002983/*
2984 * Scsi host template for megaraid_sas driver
2985 */
2986static struct scsi_host_template megasas_template = {
2987
2988 .module = THIS_MODULE,
Sumit.Saxena@avagotech.com43cd7fe2015-04-23 16:31:39 +05302989 .name = "Avago SAS based MegaRAID driver",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002990 .proc_name = "megaraid_sas",
Christoph Hellwig147aab62006-02-17 12:13:48 +01002991 .slave_configure = megasas_slave_configure,
Yang, Bo044833b2009-10-06 14:33:06 -06002992 .slave_alloc = megasas_slave_alloc,
Sumit Saxena18365b12016-01-28 21:04:25 +05302993 .slave_destroy = megasas_slave_destroy,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002994 .queuecommand = megasas_queue_command,
Sumit Saxenabd23d4a2016-04-15 00:23:32 -07002995 .eh_target_reset_handler = megasas_reset_target,
2996 .eh_abort_handler = megasas_task_abort,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002997 .eh_host_reset_handler = megasas_reset_bus_host,
Sumant Patro05e9ebb2007-05-17 05:47:51 -07002998 .eh_timed_out = megasas_reset_timer,
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302999 .shost_attrs = megaraid_host_attrs,
Sumant Patrocf62a0a2007-02-14 12:41:55 -08003000 .bios_param = megasas_bios_param,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003001 .use_clustering = ENABLE_CLUSTERING,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003002 .change_queue_depth = scsi_change_queue_depth,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04003003 .no_write_same = 1,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003004};
3005
3006/**
3007 * megasas_complete_int_cmd - Completes an internal command
3008 * @instance: Adapter soft state
3009 * @cmd: Command to be completed
3010 *
3011 * The megasas_issue_blocked_cmd() function waits for a command to complete
3012 * after it issues a command. This function wakes up that waiting routine by
3013 * calling wake_up() on the wait queue.
3014 */
3015static void
3016megasas_complete_int_cmd(struct megasas_instance *instance,
3017 struct megasas_cmd *cmd)
3018{
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303019 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003020 wake_up(&instance->int_cmd_wait_q);
3021}
3022
3023/**
3024 * megasas_complete_abort - Completes aborting a command
3025 * @instance: Adapter soft state
3026 * @cmd: Cmd that was issued to abort another cmd
3027 *
adam radford0d490162010-12-14 19:17:17 -08003028 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3029 * after it issues an abort on a previously issued command. This function
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003030 * wakes up all functions waiting on the same wait queue.
3031 */
3032static void
3033megasas_complete_abort(struct megasas_instance *instance,
3034 struct megasas_cmd *cmd)
3035{
3036 if (cmd->sync_cmd) {
3037 cmd->sync_cmd = 0;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303038 cmd->cmd_status_drv = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003039 wake_up(&instance->abort_cmd_wait_q);
3040 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003041}
3042
3043/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003044 * megasas_complete_cmd - Completes a command
3045 * @instance: Adapter soft state
3046 * @cmd: Command to be completed
adam radford0d490162010-12-14 19:17:17 -08003047 * @alt_status: If non-zero, use this value as status to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003048 * SCSI mid-layer instead of the value returned
3049 * by the FW. This should be used if caller wants
3050 * an alternate status (as in the case of aborted
3051 * commands)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003052 */
adam radford9c915a82010-12-21 13:34:31 -08003053void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003054megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3055 u8 alt_status)
3056{
3057 int exception = 0;
3058 struct megasas_header *hdr = &cmd->frame->hdr;
Yang, Boc3518832009-10-06 14:18:02 -06003059 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -08003060 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303061 u32 opcode, status;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003062
bo yang39a98552010-09-22 22:36:29 -04003063 /* flag for the retry reset */
3064 cmd->retry_for_fw_reset = 0;
3065
Sumant Patro05e9ebb2007-05-17 05:47:51 -07003066 if (cmd->scmd)
3067 cmd->scmd->SCp.ptr = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003068
3069 switch (hdr->cmd) {
adam radforde5f93a32011-10-08 18:15:19 -07003070 case MFI_CMD_INVALID:
3071 /* Some older 1068 controller FW may keep a pended
3072 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3073 when booting the kdump kernel. Ignore this command to
3074 prevent a kernel panic on shutdown of the kdump kernel. */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003075 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3076 "completed\n");
3077 dev_warn(&instance->pdev->dev, "If you have a controller "
3078 "other than PERC5, please upgrade your firmware\n");
adam radforde5f93a32011-10-08 18:15:19 -07003079 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003080 case MFI_CMD_PD_SCSI_IO:
3081 case MFI_CMD_LD_SCSI_IO:
3082
3083 /*
3084 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3085 * issued either through an IO path or an IOCTL path. If it
3086 * was via IOCTL, we will send it to internal completion.
3087 */
3088 if (cmd->sync_cmd) {
3089 cmd->sync_cmd = 0;
3090 megasas_complete_int_cmd(instance, cmd);
3091 break;
3092 }
3093
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003094 case MFI_CMD_LD_READ:
3095 case MFI_CMD_LD_WRITE:
3096
3097 if (alt_status) {
3098 cmd->scmd->result = alt_status << 16;
3099 exception = 1;
3100 }
3101
3102 if (exception) {
3103
Sumant Patroe4a082c2006-05-30 12:03:37 -07003104 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003105
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003106 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003107 cmd->scmd->scsi_done(cmd->scmd);
3108 megasas_return_cmd(instance, cmd);
3109
3110 break;
3111 }
3112
3113 switch (hdr->cmd_status) {
3114
3115 case MFI_STAT_OK:
3116 cmd->scmd->result = DID_OK << 16;
3117 break;
3118
3119 case MFI_STAT_SCSI_IO_FAILED:
3120 case MFI_STAT_LD_INIT_IN_PROGRESS:
3121 cmd->scmd->result =
3122 (DID_ERROR << 16) | hdr->scsi_status;
3123 break;
3124
3125 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3126
3127 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3128
3129 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3130 memset(cmd->scmd->sense_buffer, 0,
3131 SCSI_SENSE_BUFFERSIZE);
3132 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3133 hdr->sense_len);
3134
3135 cmd->scmd->result |= DRIVER_SENSE << 24;
3136 }
3137
3138 break;
3139
3140 case MFI_STAT_LD_OFFLINE:
3141 case MFI_STAT_DEVICE_NOT_FOUND:
3142 cmd->scmd->result = DID_BAD_TARGET << 16;
3143 break;
3144
3145 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003146 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003147 hdr->cmd_status);
3148 cmd->scmd->result = DID_ERROR << 16;
3149 break;
3150 }
3151
Sumant Patroe4a082c2006-05-30 12:03:37 -07003152 atomic_dec(&instance->fw_outstanding);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003153
FUJITA Tomonori155d98f2007-05-26 05:04:08 +09003154 scsi_dma_unmap(cmd->scmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003155 cmd->scmd->scsi_done(cmd->scmd);
3156 megasas_return_cmd(instance, cmd);
3157
3158 break;
3159
3160 case MFI_CMD_SMP:
3161 case MFI_CMD_STP:
3162 case MFI_CMD_DCMD:
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303163 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
adam radford9c915a82010-12-21 13:34:31 -08003164 /* Check for LD map update */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303165 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3166 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303167 fusion->fast_path_io = 0;
adam radford9c915a82010-12-21 13:34:31 -08003168 spin_lock_irqsave(instance->host->host_lock, flags);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303169 instance->map_update_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003170 if (cmd->frame->hdr.cmd_status != 0) {
3171 if (cmd->frame->hdr.cmd_status !=
3172 MFI_STAT_NOT_FOUND)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003173 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
adam radford9c915a82010-12-21 13:34:31 -08003174 cmd->frame->hdr.cmd_status);
3175 else {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303176 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003177 spin_unlock_irqrestore(
3178 instance->host->host_lock,
3179 flags);
3180 break;
3181 }
3182 } else
3183 instance->map_id++;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303184 megasas_return_cmd(instance, cmd);
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05303185
3186 /*
3187 * Set fast path IO to ZERO.
3188 * Validate Map will set proper value.
3189 * Meanwhile all IOs will go as LD IO.
3190 */
3191 if (MR_ValidateMapInfo(instance))
adam radford9c915a82010-12-21 13:34:31 -08003192 fusion->fast_path_io = 1;
3193 else
3194 fusion->fast_path_io = 0;
3195 megasas_sync_map_info(instance);
3196 spin_unlock_irqrestore(instance->host->host_lock,
3197 flags);
3198 break;
3199 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303200 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3201 opcode == MR_DCMD_CTRL_EVENT_GET) {
Yang, Boc3518832009-10-06 14:18:02 -06003202 spin_lock_irqsave(&poll_aen_lock, flags);
3203 megasas_poll_wait_aen = 0;
3204 spin_unlock_irqrestore(&poll_aen_lock, flags);
3205 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003206
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303207 /* FW has an updated PD sequence */
3208 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3209 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3210
3211 spin_lock_irqsave(instance->host->host_lock, flags);
3212 status = cmd->frame->hdr.cmd_status;
3213 instance->jbod_seq_cmd = NULL;
3214 megasas_return_cmd(instance, cmd);
3215
3216 if (status == MFI_STAT_OK) {
3217 instance->pd_seq_map_id++;
3218 /* Re-register a pd sync seq num cmd */
3219 if (megasas_sync_pd_seq_num(instance, true))
3220 instance->use_seqnum_jbod_fp = false;
3221 } else
3222 instance->use_seqnum_jbod_fp = false;
3223
3224 spin_unlock_irqrestore(instance->host->host_lock, flags);
3225 break;
3226 }
3227
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003228 /*
3229 * See if got an event notification
3230 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303231 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003232 megasas_service_aen(instance, cmd);
3233 else
3234 megasas_complete_int_cmd(instance, cmd);
3235
3236 break;
3237
3238 case MFI_CMD_ABORT:
3239 /*
3240 * Cmd issued to abort another cmd returned
3241 */
3242 megasas_complete_abort(instance, cmd);
3243 break;
3244
3245 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003246 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003247 hdr->cmd);
3248 break;
3249 }
3250}
3251
3252/**
bo yang39a98552010-09-22 22:36:29 -04003253 * megasas_issue_pending_cmds_again - issue all pending cmds
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003254 * in FW again because of the fw reset
bo yang39a98552010-09-22 22:36:29 -04003255 * @instance: Adapter soft state
3256 */
3257static inline void
3258megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3259{
3260 struct megasas_cmd *cmd;
3261 struct list_head clist_local;
3262 union megasas_evt_class_locale class_locale;
3263 unsigned long flags;
3264 u32 seq_num;
3265
3266 INIT_LIST_HEAD(&clist_local);
3267 spin_lock_irqsave(&instance->hba_lock, flags);
3268 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3269 spin_unlock_irqrestore(&instance->hba_lock, flags);
3270
3271 while (!list_empty(&clist_local)) {
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003272 cmd = list_entry((&clist_local)->next,
bo yang39a98552010-09-22 22:36:29 -04003273 struct megasas_cmd, list);
3274 list_del_init(&cmd->list);
3275
3276 if (cmd->sync_cmd || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003277 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3278 "detected to be pending while HBA reset\n",
bo yang39a98552010-09-22 22:36:29 -04003279 cmd, cmd->scmd, cmd->sync_cmd);
3280
3281 cmd->retry_for_fw_reset++;
3282
3283 if (cmd->retry_for_fw_reset == 3) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003284 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
bo yang39a98552010-09-22 22:36:29 -04003285 "was tried multiple times during reset."
3286 "Shutting down the HBA\n",
3287 cmd, cmd->scmd, cmd->sync_cmd);
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303288 instance->instancet->disable_intr(instance);
3289 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003290 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003291 return;
3292 }
3293 }
3294
3295 if (cmd->sync_cmd == 1) {
3296 if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003297 dev_notice(&instance->pdev->dev, "unexpected"
bo yang39a98552010-09-22 22:36:29 -04003298 "cmd attached to internal command!\n");
3299 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003300 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
bo yang39a98552010-09-22 22:36:29 -04003301 "on the internal reset queue,"
3302 "issue it again.\n", cmd);
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05303303 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
bo yang39a98552010-09-22 22:36:29 -04003304 instance->instancet->fire_cmd(instance,
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003305 cmd->frame_phys_addr,
bo yang39a98552010-09-22 22:36:29 -04003306 0, instance->reg_set);
3307 } else if (cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003308 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
bo yang39a98552010-09-22 22:36:29 -04003309 "detected on the internal queue, issue again.\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04003310 cmd, cmd->scmd->cmnd[0]);
bo yang39a98552010-09-22 22:36:29 -04003311
3312 atomic_inc(&instance->fw_outstanding);
3313 instance->instancet->fire_cmd(instance,
3314 cmd->frame_phys_addr,
3315 cmd->frame_count-1, instance->reg_set);
3316 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003317 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
bo yang39a98552010-09-22 22:36:29 -04003318 "internal reset defer list while re-issue!!\n",
3319 cmd);
3320 }
3321 }
3322
3323 if (instance->aen_cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003324 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
bo yang39a98552010-09-22 22:36:29 -04003325 megasas_return_cmd(instance, instance->aen_cmd);
3326
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003327 instance->aen_cmd = NULL;
bo yang39a98552010-09-22 22:36:29 -04003328 }
3329
3330 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003331 * Initiate AEN (Asynchronous Event Notification)
3332 */
bo yang39a98552010-09-22 22:36:29 -04003333 seq_num = instance->last_seq_num;
3334 class_locale.members.reserved = 0;
3335 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3336 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3337
3338 megasas_register_aen(instance, seq_num, class_locale.word);
3339}
3340
3341/**
3342 * Move the internal reset pending commands to a deferred queue.
3343 *
3344 * We move the commands pending at internal reset time to a
3345 * pending queue. This queue would be flushed after successful
3346 * completion of the internal reset sequence. if the internal reset
3347 * did not complete in time, the kernel reset handler would flush
3348 * these commands.
3349 **/
3350static void
3351megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3352{
3353 struct megasas_cmd *cmd;
3354 int i;
3355 u32 max_cmd = instance->max_fw_cmds;
3356 u32 defer_index;
3357 unsigned long flags;
3358
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003359 defer_index = 0;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303360 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003361 for (i = 0; i < max_cmd; i++) {
3362 cmd = instance->cmd_list[i];
3363 if (cmd->sync_cmd == 1 || cmd->scmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003364 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
bo yang39a98552010-09-22 22:36:29 -04003365 "on the defer queue as internal\n",
3366 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3367
3368 if (!list_empty(&cmd->list)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003369 dev_notice(&instance->pdev->dev, "ERROR while"
bo yang39a98552010-09-22 22:36:29 -04003370 " moving this cmd:%p, %d %p, it was"
3371 "discovered on some list?\n",
3372 cmd, cmd->sync_cmd, cmd->scmd);
3373
3374 list_del_init(&cmd->list);
3375 }
3376 defer_index++;
3377 list_add_tail(&cmd->list,
3378 &instance->internal_reset_pending_q);
3379 }
3380 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303381 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
bo yang39a98552010-09-22 22:36:29 -04003382}
3383
3384
3385static void
3386process_fw_state_change_wq(struct work_struct *work)
3387{
3388 struct megasas_instance *instance =
3389 container_of(work, struct megasas_instance, work_init);
3390 u32 wait;
3391 unsigned long flags;
3392
Sumit Saxena8a01a412016-01-28 21:04:32 +05303393 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003394 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303395 atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003396 return ;
3397 }
3398
Sumit Saxena8a01a412016-01-28 21:04:32 +05303399 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003400 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
bo yang39a98552010-09-22 22:36:29 -04003401 "state, restarting it...\n");
3402
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303403 instance->instancet->disable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003404 atomic_set(&instance->fw_outstanding, 0);
3405
3406 atomic_set(&instance->fw_reset_no_pci_access, 1);
3407 instance->instancet->adp_reset(instance, instance->reg_set);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003408 atomic_set(&instance->fw_reset_no_pci_access, 0);
bo yang39a98552010-09-22 22:36:29 -04003409
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003410 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
bo yang39a98552010-09-22 22:36:29 -04003411 "initiating next stage...\n");
3412
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003413 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
bo yang39a98552010-09-22 22:36:29 -04003414 "state 2 starting...\n");
3415
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003416 /* waiting for about 20 second before start the second init */
bo yang39a98552010-09-22 22:36:29 -04003417 for (wait = 0; wait < 30; wait++) {
3418 msleep(1000);
3419 }
3420
adam radford058a8fa2011-10-08 18:14:27 -07003421 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003422 dev_notice(&instance->pdev->dev, "adapter not ready\n");
bo yang39a98552010-09-22 22:36:29 -04003423
Sumit.Saxena@avagotech.comc8dd61e2015-01-05 20:06:18 +05303424 atomic_set(&instance->fw_reset_no_pci_access, 1);
bo yang39a98552010-09-22 22:36:29 -04003425 megaraid_sas_kill_hba(instance);
bo yang39a98552010-09-22 22:36:29 -04003426 return ;
3427 }
3428
3429 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3430 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3431 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3432 ) {
3433 *instance->consumer = *instance->producer;
3434 } else {
3435 *instance->consumer = 0;
3436 *instance->producer = 0;
3437 }
3438
3439 megasas_issue_init_mfi(instance);
3440
3441 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303442 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
bo yang39a98552010-09-22 22:36:29 -04003443 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303444 instance->instancet->enable_intr(instance);
bo yang39a98552010-09-22 22:36:29 -04003445
3446 megasas_issue_pending_cmds_again(instance);
3447 instance->issuepend_done = 1;
3448 }
bo yang39a98552010-09-22 22:36:29 -04003449}
3450
3451/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003452 * megasas_deplete_reply_queue - Processes all completed commands
3453 * @instance: Adapter soft state
3454 * @alt_status: Alternate status to be returned to
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003455 * SCSI mid-layer instead of the status
3456 * returned by the FW
bo yang39a98552010-09-22 22:36:29 -04003457 * Note: this must be called with hba lock held
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003458 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08003459static int
bo yang39a98552010-09-22 22:36:29 -04003460megasas_deplete_reply_queue(struct megasas_instance *instance,
3461 u8 alt_status)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003462{
bo yang39a98552010-09-22 22:36:29 -04003463 u32 mfiStatus;
3464 u32 fw_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003465
bo yang39a98552010-09-22 22:36:29 -04003466 if ((mfiStatus = instance->instancet->check_reset(instance,
3467 instance->reg_set)) == 1) {
3468 return IRQ_HANDLED;
3469 }
3470
3471 if ((mfiStatus = instance->instancet->clear_intr(
3472 instance->reg_set)
3473 ) == 0) {
adam radforde1419192011-02-24 20:56:21 -08003474 /* Hardware may not set outbound_intr_status in MSI-X mode */
adam radfordc8e858f2011-10-08 18:15:13 -07003475 if (!instance->msix_vectors)
adam radforde1419192011-02-24 20:56:21 -08003476 return IRQ_NONE;
bo yang39a98552010-09-22 22:36:29 -04003477 }
3478
3479 instance->mfiStatus = mfiStatus;
3480
3481 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3482 fw_state = instance->instancet->read_fw_status_reg(
3483 instance->reg_set) & MFI_STATE_MASK;
3484
3485 if (fw_state != MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003486 dev_notice(&instance->pdev->dev, "fw state:%x\n",
bo yang39a98552010-09-22 22:36:29 -04003487 fw_state);
3488 }
3489
3490 if ((fw_state == MFI_STATE_FAULT) &&
3491 (instance->disableOnlineCtrlReset == 0)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003492 dev_notice(&instance->pdev->dev, "wait adp restart\n");
bo yang39a98552010-09-22 22:36:29 -04003493
3494 if ((instance->pdev->device ==
3495 PCI_DEVICE_ID_LSI_SAS1064R) ||
3496 (instance->pdev->device ==
3497 PCI_DEVICE_ID_DELL_PERC5) ||
3498 (instance->pdev->device ==
3499 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3500
3501 *instance->consumer =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303502 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
bo yang39a98552010-09-22 22:36:29 -04003503 }
3504
3505
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303506 instance->instancet->disable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303507 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
bo yang39a98552010-09-22 22:36:29 -04003508 instance->issuepend_done = 0;
3509
3510 atomic_set(&instance->fw_outstanding, 0);
3511 megasas_internal_reset_defer_cmds(instance);
3512
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003513 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
Sumit Saxena8a01a412016-01-28 21:04:32 +05303514 fw_state, atomic_read(&instance->adprecovery));
bo yang39a98552010-09-22 22:36:29 -04003515
3516 schedule_work(&instance->work_init);
3517 return IRQ_HANDLED;
3518
3519 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003520 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
bo yang39a98552010-09-22 22:36:29 -04003521 fw_state, instance->disableOnlineCtrlReset);
3522 }
3523 }
3524
Sumant Patro5d018ad2006-10-03 13:13:18 -07003525 tasklet_schedule(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003526 return IRQ_HANDLED;
3527}
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003528/**
3529 * megasas_isr - isr entry point
3530 */
David Howells7d12e782006-10-05 14:55:46 +01003531static irqreturn_t megasas_isr(int irq, void *devp)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003532{
adam radfordc8e858f2011-10-08 18:15:13 -07003533 struct megasas_irq_context *irq_context = devp;
3534 struct megasas_instance *instance = irq_context->instance;
bo yang39a98552010-09-22 22:36:29 -04003535 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003536 irqreturn_t rc;
bo yang39a98552010-09-22 22:36:29 -04003537
adam radfordc8e858f2011-10-08 18:15:13 -07003538 if (atomic_read(&instance->fw_reset_no_pci_access))
bo yang39a98552010-09-22 22:36:29 -04003539 return IRQ_HANDLED;
3540
bo yang39a98552010-09-22 22:36:29 -04003541 spin_lock_irqsave(&instance->hba_lock, flags);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003542 rc = megasas_deplete_reply_queue(instance, DID_OK);
bo yang39a98552010-09-22 22:36:29 -04003543 spin_unlock_irqrestore(&instance->hba_lock, flags);
3544
3545 return rc;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003546}
3547
3548/**
3549 * megasas_transition_to_ready - Move the FW to READY state
Sumant Patro1341c932006-01-25 12:02:40 -08003550 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003551 *
3552 * During the initialization, FW passes can potentially be in any one of
3553 * several possible states. If the FW in operational, waiting-for-handshake
3554 * states, driver must take steps to bring it to ready state. Otherwise, it
3555 * has to wait for the ready state.
3556 */
adam radford9c915a82010-12-21 13:34:31 -08003557int
adam radford058a8fa2011-10-08 18:14:27 -07003558megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003559{
3560 int i;
3561 u8 max_wait;
3562 u32 fw_state;
3563 u32 cur_state;
Yang, Bo7218df62009-10-06 14:52:20 -06003564 u32 abs_state, curr_abs_state;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003565
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003566 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3567 fw_state = abs_state & MFI_STATE_MASK;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003568
Sumant Patroe3bbff92006-10-03 12:28:49 -07003569 if (fw_state != MFI_STATE_READY)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003570 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
adam radford0d490162010-12-14 19:17:17 -08003571 " state\n");
Sumant Patroe3bbff92006-10-03 12:28:49 -07003572
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003573 while (fw_state != MFI_STATE_READY) {
3574
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003575 switch (fw_state) {
3576
3577 case MFI_STATE_FAULT:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003578 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
adam radford058a8fa2011-10-08 18:14:27 -07003579 if (ocr) {
3580 max_wait = MEGASAS_RESET_WAIT_TIME;
3581 cur_state = MFI_STATE_FAULT;
3582 break;
3583 } else
3584 return -ENODEV;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003585
3586 case MFI_STATE_WAIT_HANDSHAKE:
3587 /*
3588 * Set the CLR bit in inbound doorbell
3589 */
Yang, Bo0c79e682009-10-06 14:47:35 -06003590 if ((instance->pdev->device ==
Yang, Bo87911122009-10-06 14:31:54 -06003591 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3592 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003593 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303594 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003595 writel(
3596 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003597 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303598 else
Yang, Bo87911122009-10-06 14:31:54 -06003599 writel(
3600 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3601 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003602
Yang, Bo7218df62009-10-06 14:52:20 -06003603 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003604 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3605 break;
3606
Sumant Patroe3bbff92006-10-03 12:28:49 -07003607 case MFI_STATE_BOOT_MESSAGE_PENDING:
Yang, Bo87911122009-10-06 14:31:54 -06003608 if ((instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003609 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3610 (instance->pdev->device ==
3611 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303612 (instance->ctrl_context))
Yang, Bo87911122009-10-06 14:31:54 -06003613 writel(MFI_INIT_HOTPLUG,
adam radford9c915a82010-12-21 13:34:31 -08003614 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303615 else
Yang, Bo87911122009-10-06 14:31:54 -06003616 writel(MFI_INIT_HOTPLUG,
3617 &instance->reg_set->inbound_doorbell);
Sumant Patroe3bbff92006-10-03 12:28:49 -07003618
Yang, Bo7218df62009-10-06 14:52:20 -06003619 max_wait = MEGASAS_RESET_WAIT_TIME;
Sumant Patroe3bbff92006-10-03 12:28:49 -07003620 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3621 break;
3622
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003623 case MFI_STATE_OPERATIONAL:
3624 /*
Sumant Patroe3bbff92006-10-03 12:28:49 -07003625 * Bring it to READY state; assuming max wait 10 secs
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003626 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303627 instance->instancet->disable_intr(instance);
Yang, Bo87911122009-10-06 14:31:54 -06003628 if ((instance->pdev->device ==
3629 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3630 (instance->pdev->device ==
adam radford9c915a82010-12-21 13:34:31 -08003631 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303632 (instance->ctrl_context)) {
Yang, Bo87911122009-10-06 14:31:54 -06003633 writel(MFI_RESET_FLAGS,
adam radford9c915a82010-12-21 13:34:31 -08003634 &instance->reg_set->doorbell);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303635
3636 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08003637 for (i = 0; i < (10 * 1000); i += 20) {
3638 if (readl(
3639 &instance->
3640 reg_set->
3641 doorbell) & 1)
3642 msleep(20);
3643 else
3644 break;
3645 }
3646 }
Yang, Bo87911122009-10-06 14:31:54 -06003647 } else
3648 writel(MFI_RESET_FLAGS,
3649 &instance->reg_set->inbound_doorbell);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003650
Yang, Bo7218df62009-10-06 14:52:20 -06003651 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003652 cur_state = MFI_STATE_OPERATIONAL;
3653 break;
3654
3655 case MFI_STATE_UNDEFINED:
3656 /*
3657 * This state should not last for more than 2 seconds
3658 */
Yang, Bo7218df62009-10-06 14:52:20 -06003659 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003660 cur_state = MFI_STATE_UNDEFINED;
3661 break;
3662
3663 case MFI_STATE_BB_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003664 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003665 cur_state = MFI_STATE_BB_INIT;
3666 break;
3667
3668 case MFI_STATE_FW_INIT:
Yang, Bo7218df62009-10-06 14:52:20 -06003669 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003670 cur_state = MFI_STATE_FW_INIT;
3671 break;
3672
3673 case MFI_STATE_FW_INIT_2:
Yang, Bo7218df62009-10-06 14:52:20 -06003674 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003675 cur_state = MFI_STATE_FW_INIT_2;
3676 break;
3677
3678 case MFI_STATE_DEVICE_SCAN:
Yang, Bo7218df62009-10-06 14:52:20 -06003679 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003680 cur_state = MFI_STATE_DEVICE_SCAN;
3681 break;
3682
3683 case MFI_STATE_FLUSH_CACHE:
Yang, Bo7218df62009-10-06 14:52:20 -06003684 max_wait = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003685 cur_state = MFI_STATE_FLUSH_CACHE;
3686 break;
3687
3688 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003689 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003690 fw_state);
3691 return -ENODEV;
3692 }
3693
3694 /*
3695 * The cur_state should not last for more than max_wait secs
3696 */
3697 for (i = 0; i < (max_wait * 1000); i++) {
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003698 curr_abs_state = instance->instancet->
3699 read_fw_status_reg(instance->reg_set);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003700
Yang, Bo7218df62009-10-06 14:52:20 -06003701 if (abs_state == curr_abs_state) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003702 msleep(1);
3703 } else
3704 break;
3705 }
3706
3707 /*
3708 * Return error if fw_state hasn't changed after max_wait
3709 */
Yang, Bo7218df62009-10-06 14:52:20 -06003710 if (curr_abs_state == abs_state) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003711 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003712 "in %d secs\n", fw_state, max_wait);
3713 return -ENODEV;
3714 }
Tomas Henzlbc6ac5e2014-04-01 13:59:50 +02003715
3716 abs_state = curr_abs_state;
3717 fw_state = curr_abs_state & MFI_STATE_MASK;
bo yang39a98552010-09-22 22:36:29 -04003718 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003719 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003720
3721 return 0;
3722}
3723
3724/**
3725 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3726 * @instance: Adapter soft state
3727 */
3728static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3729{
3730 int i;
adam radford9c915a82010-12-21 13:34:31 -08003731 u32 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003732 struct megasas_cmd *cmd;
3733
3734 if (!instance->frame_dma_pool)
3735 return;
3736
3737 /*
3738 * Return all frames to pool
3739 */
3740 for (i = 0; i < max_cmd; i++) {
3741
3742 cmd = instance->cmd_list[i];
3743
3744 if (cmd->frame)
3745 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3746 cmd->frame_phys_addr);
3747
3748 if (cmd->sense)
Sumant Patroe3bbff92006-10-03 12:28:49 -07003749 pci_pool_free(instance->sense_dma_pool, cmd->sense,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003750 cmd->sense_phys_addr);
3751 }
3752
3753 /*
3754 * Now destroy the pool itself
3755 */
3756 pci_pool_destroy(instance->frame_dma_pool);
3757 pci_pool_destroy(instance->sense_dma_pool);
3758
3759 instance->frame_dma_pool = NULL;
3760 instance->sense_dma_pool = NULL;
3761}
3762
3763/**
3764 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3765 * @instance: Adapter soft state
3766 *
3767 * Each command packet has an embedded DMA memory buffer that is used for
3768 * filling MFI frame and the SG list that immediately follows the frame. This
3769 * function creates those DMA memory buffers for each command packet by using
3770 * PCI pool facility.
3771 */
3772static int megasas_create_frame_pool(struct megasas_instance *instance)
3773{
3774 int i;
3775 u32 max_cmd;
3776 u32 sge_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003777 u32 total_sz;
3778 u32 frame_count;
3779 struct megasas_cmd *cmd;
3780
adam radford9c915a82010-12-21 13:34:31 -08003781 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003782
3783 /*
3784 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3785 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3786 */
3787 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3788 sizeof(struct megasas_sge32);
3789
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003790 if (instance->flag_ieee)
Yang, Bof4c9a132009-10-06 14:43:28 -06003791 sge_sz = sizeof(struct megasas_sge_skinny);
Yang, Bof4c9a132009-10-06 14:43:28 -06003792
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003793 /*
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303794 * For MFI controllers.
3795 * max_num_sge = 60
3796 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3797 * Total 960 byte (15 MFI frame of 64 byte)
3798 *
3799 * Fusion adapter require only 3 extra frame.
3800 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3801 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3802 * Total 192 byte (3 MFI frame of 64 byte)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003803 */
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303804 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003805 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3806 /*
3807 * Use DMA pool facility provided by PCI layer
3808 */
3809 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05303810 instance->pdev, total_sz, 256, 0);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003811
3812 if (!instance->frame_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003813 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003814 return -ENOMEM;
3815 }
3816
3817 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3818 instance->pdev, 128, 4, 0);
3819
3820 if (!instance->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003821 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003822
3823 pci_pool_destroy(instance->frame_dma_pool);
3824 instance->frame_dma_pool = NULL;
3825
3826 return -ENOMEM;
3827 }
3828
3829 /*
3830 * Allocate and attach a frame to each of the commands in cmd_list.
3831 * By making cmd->index as the context instead of the &cmd, we can
3832 * always use 32bit context regardless of the architecture
3833 */
3834 for (i = 0; i < max_cmd; i++) {
3835
3836 cmd = instance->cmd_list[i];
3837
3838 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3839 GFP_KERNEL, &cmd->frame_phys_addr);
3840
3841 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3842 GFP_KERNEL, &cmd->sense_phys_addr);
3843
3844 /*
3845 * megasas_teardown_frame_pool() takes care of freeing
3846 * whatever has been allocated
3847 */
3848 if (!cmd->frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003849 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003850 megasas_teardown_frame_pool(instance);
3851 return -ENOMEM;
3852 }
3853
Yang, Bo707e09b2010-10-12 07:20:27 -06003854 memset(cmd->frame, 0, total_sz);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303855 cmd->frame->io.context = cpu_to_le32(cmd->index);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06003856 cmd->frame->io.pad_0 = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05303857 if (!instance->ctrl_context && reset_devices)
adam radforde5f93a32011-10-08 18:15:19 -07003858 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003859 }
3860
3861 return 0;
3862}
3863
3864/**
3865 * megasas_free_cmds - Free all the cmds in the free cmd pool
3866 * @instance: Adapter soft state
3867 */
adam radford9c915a82010-12-21 13:34:31 -08003868void megasas_free_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003869{
3870 int i;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05003871
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003872 /* First free the MFI frame pool */
3873 megasas_teardown_frame_pool(instance);
3874
3875 /* Free all the commands in the cmd_list */
adam radford9c915a82010-12-21 13:34:31 -08003876 for (i = 0; i < instance->max_mfi_cmds; i++)
3877
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003878 kfree(instance->cmd_list[i]);
3879
3880 /* Free the cmd_list buffer itself */
3881 kfree(instance->cmd_list);
3882 instance->cmd_list = NULL;
3883
3884 INIT_LIST_HEAD(&instance->cmd_pool);
3885}
3886
3887/**
3888 * megasas_alloc_cmds - Allocates the command packets
3889 * @instance: Adapter soft state
3890 *
3891 * Each command that is issued to the FW, whether IO commands from the OS or
3892 * internal commands like IOCTLs, are wrapped in local data structure called
3893 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3894 * the FW.
3895 *
3896 * Each frame has a 32-bit field called context (tag). This context is used
3897 * to get back the megasas_cmd from the frame when a frame gets completed in
3898 * the ISR. Typically the address of the megasas_cmd itself would be used as
3899 * the context. But we wanted to keep the differences between 32 and 64 bit
3900 * systems to the mininum. We always use 32 bit integers for the context. In
3901 * this driver, the 32 bit values are the indices into an array cmd_list.
3902 * This array is used only to look up the megasas_cmd given the context. The
3903 * free commands themselves are maintained in a linked list called cmd_pool.
3904 */
adam radford9c915a82010-12-21 13:34:31 -08003905int megasas_alloc_cmds(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003906{
3907 int i;
3908 int j;
3909 u32 max_cmd;
3910 struct megasas_cmd *cmd;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303911 struct fusion_context *fusion;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003912
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303913 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -08003914 max_cmd = instance->max_mfi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003915
3916 /*
3917 * instance->cmd_list is an array of struct megasas_cmd pointers.
3918 * Allocate the dynamic array first and then allocate individual
3919 * commands.
3920 */
Yoann Padioleaudd00cc42007-07-19 01:49:03 -07003921 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003922
3923 if (!instance->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003924 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003925 return -ENOMEM;
3926 }
3927
adam radford9c915a82010-12-21 13:34:31 -08003928 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003929
3930 for (i = 0; i < max_cmd; i++) {
3931 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3932 GFP_KERNEL);
3933
3934 if (!instance->cmd_list[i]) {
3935
3936 for (j = 0; j < i; j++)
3937 kfree(instance->cmd_list[j]);
3938
3939 kfree(instance->cmd_list);
3940 instance->cmd_list = NULL;
3941
3942 return -ENOMEM;
3943 }
3944 }
3945
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003946 for (i = 0; i < max_cmd; i++) {
3947 cmd = instance->cmd_list[i];
3948 memset(cmd, 0, sizeof(struct megasas_cmd));
3949 cmd->index = i;
bo yang39a98552010-09-22 22:36:29 -04003950 cmd->scmd = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003951 cmd->instance = instance;
3952
3953 list_add_tail(&cmd->list, &instance->cmd_pool);
3954 }
3955
3956 /*
3957 * Create a frame pool and assign one frame to each cmd
3958 */
3959 if (megasas_create_frame_pool(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003960 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04003961 megasas_free_cmds(instance);
3962 }
3963
3964 return 0;
3965}
3966
Yang, Bo81e403c2009-10-06 14:27:54 -06003967/*
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303968 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3969 * @instance: Adapter soft state
3970 *
3971 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3972 * or FW is not under OCR.
3973 */
3974inline int
3975dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3976
3977 if (!instance->ctrl_context)
3978 return KILL_ADAPTER;
3979 else if (instance->unload ||
3980 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3981 return IGNORE_TIMEOUT;
3982 else
3983 return INITIATE_OCR;
3984}
3985
Sumit Saxena2216c302016-01-28 21:04:26 +05303986static int
3987megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3988{
3989 int ret;
3990 struct megasas_cmd *cmd;
3991 struct megasas_dcmd_frame *dcmd;
3992
3993 cmd = megasas_get_cmd(instance);
3994
3995 if (!cmd) {
3996 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
3997 return -ENOMEM;
3998 }
3999
4000 dcmd = &cmd->frame->dcmd;
4001
4002 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4003 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4004
4005 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4006 dcmd->cmd = MFI_CMD_DCMD;
4007 dcmd->cmd_status = 0xFF;
4008 dcmd->sge_count = 1;
4009 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4010 dcmd->timeout = 0;
4011 dcmd->pad_0 = 0;
4012 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4013 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4014 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4015 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4016
4017 if (instance->ctrl_context && !instance->mask_interrupts)
4018 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4019 else
4020 ret = megasas_issue_polled(instance, cmd);
4021
4022 switch (ret) {
4023 case DCMD_SUCCESS:
4024 instance->pd_list[device_id].interface =
4025 instance->pd_info->state.ddf.pdType.intf;
4026 break;
4027
4028 case DCMD_TIMEOUT:
4029
4030 switch (dcmd_timeout_ocr_possible(instance)) {
4031 case INITIATE_OCR:
4032 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4033 megasas_reset_fusion(instance->host,
4034 MFI_IO_TIMEOUT_OCR);
4035 break;
4036 case KILL_ADAPTER:
4037 megaraid_sas_kill_hba(instance);
4038 break;
4039 case IGNORE_TIMEOUT:
4040 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4041 __func__, __LINE__);
4042 break;
4043 }
4044
4045 break;
4046 }
4047
4048 if (ret != DCMD_TIMEOUT)
4049 megasas_return_cmd(instance, cmd);
4050
4051 return ret;
4052}
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304053/*
Yang, Bo81e403c2009-10-06 14:27:54 -06004054 * megasas_get_pd_list_info - Returns FW's pd_list structure
4055 * @instance: Adapter soft state
4056 * @pd_list: pd_list structure
4057 *
4058 * Issues an internal command (DCMD) to get the FW's controller PD
4059 * list structure. This information is mainly used to find out SYSTEM
4060 * supported by the FW.
4061 */
4062static int
4063megasas_get_pd_list(struct megasas_instance *instance)
4064{
4065 int ret = 0, pd_index = 0;
4066 struct megasas_cmd *cmd;
4067 struct megasas_dcmd_frame *dcmd;
4068 struct MR_PD_LIST *ci;
4069 struct MR_PD_ADDRESS *pd_addr;
4070 dma_addr_t ci_h = 0;
4071
Sumit Saxenad9083162016-07-08 03:30:16 -07004072 if (instance->pd_list_not_supported) {
4073 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4074 "not supported by firmware\n");
4075 return ret;
4076 }
4077
Yang, Bo81e403c2009-10-06 14:27:54 -06004078 cmd = megasas_get_cmd(instance);
4079
4080 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004081 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004082 return -ENOMEM;
4083 }
4084
4085 dcmd = &cmd->frame->dcmd;
4086
4087 ci = pci_alloc_consistent(instance->pdev,
4088 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4089
4090 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004091 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
Yang, Bo81e403c2009-10-06 14:27:54 -06004092 megasas_return_cmd(instance, cmd);
4093 return -ENOMEM;
4094 }
4095
4096 memset(ci, 0, sizeof(*ci));
4097 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4098
4099 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4100 dcmd->mbox.b[1] = 0;
4101 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304102 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bo81e403c2009-10-06 14:27:54 -06004103 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304104 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bo81e403c2009-10-06 14:27:54 -06004105 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004106 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304107 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4108 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4109 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4110 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
Yang, Bo81e403c2009-10-06 14:27:54 -06004111
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304112 if (instance->ctrl_context && !instance->mask_interrupts)
4113 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304114 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304115 else
4116 ret = megasas_issue_polled(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004117
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304118 switch (ret) {
4119 case DCMD_FAILED:
Sumit Saxena30845582016-03-10 02:14:37 -08004120 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4121 "failed/not supported by firmware\n");
4122
4123 if (instance->ctrl_context)
4124 megaraid_sas_kill_hba(instance);
4125 else
4126 instance->pd_list_not_supported = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304127 break;
4128 case DCMD_TIMEOUT:
Yang, Bo81e403c2009-10-06 14:27:54 -06004129
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304130 switch (dcmd_timeout_ocr_possible(instance)) {
4131 case INITIATE_OCR:
4132 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4133 /*
4134 * DCMD failed from AEN path.
4135 * AEN path already hold reset_mutex to avoid PCI access
4136 * while OCR is in progress.
4137 */
4138 mutex_unlock(&instance->reset_mutex);
4139 megasas_reset_fusion(instance->host,
4140 MFI_IO_TIMEOUT_OCR);
4141 mutex_lock(&instance->reset_mutex);
4142 break;
4143 case KILL_ADAPTER:
4144 megaraid_sas_kill_hba(instance);
4145 break;
4146 case IGNORE_TIMEOUT:
4147 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4148 __func__, __LINE__);
4149 break;
4150 }
Yang, Bo81e403c2009-10-06 14:27:54 -06004151
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304152 break;
4153
4154 case DCMD_SUCCESS:
4155 pd_addr = ci->addr;
4156
4157 if ((le32_to_cpu(ci->count) >
4158 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4159 break;
Yang, Bo81e403c2009-10-06 14:27:54 -06004160
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304161 memset(instance->local_pd_list, 0,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304162 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
Yang, Bo81e403c2009-10-06 14:27:54 -06004163
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304164 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304165 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304166 le16_to_cpu(pd_addr->deviceId);
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304167 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304168 pd_addr->scsiDevType;
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304169 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304170 MR_PD_STATE_SYSTEM;
Yang, Bo81e403c2009-10-06 14:27:54 -06004171 pd_addr++;
4172 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304173
Sumit.Saxena@lsi.com999ece02013-10-18 12:50:37 +05304174 memcpy(instance->pd_list, instance->local_pd_list,
4175 sizeof(instance->pd_list));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304176 break;
4177
Yang, Bo81e403c2009-10-06 14:27:54 -06004178 }
4179
4180 pci_free_consistent(instance->pdev,
4181 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4182 ci, ci_h);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304183
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304184 if (ret != DCMD_TIMEOUT)
4185 megasas_return_cmd(instance, cmd);
Yang, Bo81e403c2009-10-06 14:27:54 -06004186
4187 return ret;
4188}
4189
Yang, Bobdc6fb82009-12-06 08:30:19 -07004190/*
4191 * megasas_get_ld_list_info - Returns FW's ld_list structure
4192 * @instance: Adapter soft state
4193 * @ld_list: ld_list structure
4194 *
4195 * Issues an internal command (DCMD) to get the FW's controller PD
4196 * list structure. This information is mainly used to find out SYSTEM
4197 * supported by the FW.
4198 */
4199static int
4200megasas_get_ld_list(struct megasas_instance *instance)
4201{
4202 int ret = 0, ld_index = 0, ids = 0;
4203 struct megasas_cmd *cmd;
4204 struct megasas_dcmd_frame *dcmd;
4205 struct MR_LD_LIST *ci;
4206 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304207 u32 ld_count;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004208
4209 cmd = megasas_get_cmd(instance);
4210
4211 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004212 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004213 return -ENOMEM;
4214 }
4215
4216 dcmd = &cmd->frame->dcmd;
4217
4218 ci = pci_alloc_consistent(instance->pdev,
4219 sizeof(struct MR_LD_LIST),
4220 &ci_h);
4221
4222 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004223 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
Yang, Bobdc6fb82009-12-06 08:30:19 -07004224 megasas_return_cmd(instance, cmd);
4225 return -ENOMEM;
4226 }
4227
4228 memset(ci, 0, sizeof(*ci));
4229 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4230
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304231 if (instance->supportmax256vd)
4232 dcmd->mbox.b[0] = 1;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004233 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304234 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004235 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304236 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004237 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304238 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4239 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4240 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4241 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
Yang, Bobdc6fb82009-12-06 08:30:19 -07004242 dcmd->pad_0 = 0;
4243
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304244 if (instance->ctrl_context && !instance->mask_interrupts)
4245 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304246 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304247 else
4248 ret = megasas_issue_polled(instance, cmd);
4249
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304250 ld_count = le32_to_cpu(ci->ldCount);
4251
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304252 switch (ret) {
4253 case DCMD_FAILED:
4254 megaraid_sas_kill_hba(instance);
4255 break;
4256 case DCMD_TIMEOUT:
Yang, Bobdc6fb82009-12-06 08:30:19 -07004257
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304258 switch (dcmd_timeout_ocr_possible(instance)) {
4259 case INITIATE_OCR:
4260 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4261 /*
4262 * DCMD failed from AEN path.
4263 * AEN path already hold reset_mutex to avoid PCI access
4264 * while OCR is in progress.
4265 */
4266 mutex_unlock(&instance->reset_mutex);
4267 megasas_reset_fusion(instance->host,
4268 MFI_IO_TIMEOUT_OCR);
4269 mutex_lock(&instance->reset_mutex);
4270 break;
4271 case KILL_ADAPTER:
4272 megaraid_sas_kill_hba(instance);
4273 break;
4274 case IGNORE_TIMEOUT:
4275 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4276 __func__, __LINE__);
4277 break;
4278 }
4279
4280 break;
4281
4282 case DCMD_SUCCESS:
4283 if (ld_count > instance->fw_supported_vd_count)
4284 break;
4285
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304286 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004287
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304288 for (ld_index = 0; ld_index < ld_count; ld_index++) {
Yang, Bobdc6fb82009-12-06 08:30:19 -07004289 if (ci->ldList[ld_index].state != 0) {
4290 ids = ci->ldList[ld_index].ref.targetId;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304291 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004292 }
4293 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304294
4295 break;
Yang, Bobdc6fb82009-12-06 08:30:19 -07004296 }
4297
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304298 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
Yang, Bobdc6fb82009-12-06 08:30:19 -07004299
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304300 if (ret != DCMD_TIMEOUT)
4301 megasas_return_cmd(instance, cmd);
4302
Yang, Bobdc6fb82009-12-06 08:30:19 -07004303 return ret;
4304}
4305
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004306/**
adam radford21c9e162013-09-06 15:27:14 -07004307 * megasas_ld_list_query - Returns FW's ld_list structure
4308 * @instance: Adapter soft state
4309 * @ld_list: ld_list structure
4310 *
4311 * Issues an internal command (DCMD) to get the FW's controller PD
4312 * list structure. This information is mainly used to find out SYSTEM
4313 * supported by the FW.
4314 */
4315static int
4316megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4317{
4318 int ret = 0, ld_index = 0, ids = 0;
4319 struct megasas_cmd *cmd;
4320 struct megasas_dcmd_frame *dcmd;
4321 struct MR_LD_TARGETID_LIST *ci;
4322 dma_addr_t ci_h = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304323 u32 tgtid_count;
adam radford21c9e162013-09-06 15:27:14 -07004324
4325 cmd = megasas_get_cmd(instance);
4326
4327 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004328 dev_warn(&instance->pdev->dev,
4329 "megasas_ld_list_query: Failed to get cmd\n");
adam radford21c9e162013-09-06 15:27:14 -07004330 return -ENOMEM;
4331 }
4332
4333 dcmd = &cmd->frame->dcmd;
4334
4335 ci = pci_alloc_consistent(instance->pdev,
4336 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4337
4338 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004339 dev_warn(&instance->pdev->dev,
4340 "Failed to alloc mem for ld_list_query\n");
adam radford21c9e162013-09-06 15:27:14 -07004341 megasas_return_cmd(instance, cmd);
4342 return -ENOMEM;
4343 }
4344
4345 memset(ci, 0, sizeof(*ci));
4346 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4347
4348 dcmd->mbox.b[0] = query_type;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304349 if (instance->supportmax256vd)
4350 dcmd->mbox.b[2] = 1;
adam radford21c9e162013-09-06 15:27:14 -07004351
4352 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304353 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
adam radford21c9e162013-09-06 15:27:14 -07004354 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304355 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford21c9e162013-09-06 15:27:14 -07004356 dcmd->timeout = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304357 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4358 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4359 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4360 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
adam radford21c9e162013-09-06 15:27:14 -07004361 dcmd->pad_0 = 0;
4362
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304363 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304364 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304365 else
4366 ret = megasas_issue_polled(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004367
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304368 switch (ret) {
4369 case DCMD_FAILED:
4370 dev_info(&instance->pdev->dev,
4371 "DCMD not supported by firmware - %s %d\n",
4372 __func__, __LINE__);
4373 ret = megasas_get_ld_list(instance);
4374 break;
4375 case DCMD_TIMEOUT:
4376 switch (dcmd_timeout_ocr_possible(instance)) {
4377 case INITIATE_OCR:
4378 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4379 /*
4380 * DCMD failed from AEN path.
4381 * AEN path already hold reset_mutex to avoid PCI access
4382 * while OCR is in progress.
4383 */
4384 mutex_unlock(&instance->reset_mutex);
4385 megasas_reset_fusion(instance->host,
4386 MFI_IO_TIMEOUT_OCR);
4387 mutex_lock(&instance->reset_mutex);
4388 break;
4389 case KILL_ADAPTER:
4390 megaraid_sas_kill_hba(instance);
4391 break;
4392 case IGNORE_TIMEOUT:
4393 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4394 __func__, __LINE__);
4395 break;
4396 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304397
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304398 break;
4399 case DCMD_SUCCESS:
4400 tgtid_count = le32_to_cpu(ci->count);
4401
4402 if ((tgtid_count > (instance->fw_supported_vd_count)))
4403 break;
4404
adam radford21c9e162013-09-06 15:27:14 -07004405 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304406 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
adam radford21c9e162013-09-06 15:27:14 -07004407 ids = ci->targetId[ld_index];
4408 instance->ld_ids[ids] = ci->targetId[ld_index];
4409 }
4410
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304411 break;
adam radford21c9e162013-09-06 15:27:14 -07004412 }
4413
4414 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304415 ci, ci_h);
adam radford21c9e162013-09-06 15:27:14 -07004416
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304417 if (ret != DCMD_TIMEOUT)
4418 megasas_return_cmd(instance, cmd);
adam radford21c9e162013-09-06 15:27:14 -07004419
4420 return ret;
4421}
4422
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304423/*
4424 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4425 * instance : Controller's instance
4426*/
4427static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4428{
4429 struct fusion_context *fusion;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004430 u32 ventura_map_sz = 0;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304431
4432 fusion = instance->ctrl_context;
4433 /* For MFI based controllers return dummy success */
4434 if (!fusion)
4435 return;
4436
4437 instance->supportmax256vd =
4438 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4439 /* Below is additional check to address future FW enhancement */
4440 if (instance->ctrl_info->max_lds > 64)
4441 instance->supportmax256vd = 1;
4442
4443 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4444 * MEGASAS_MAX_DEV_PER_CHANNEL;
4445 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4446 * MEGASAS_MAX_DEV_PER_CHANNEL;
4447 if (instance->supportmax256vd) {
4448 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4449 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4450 } else {
4451 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4452 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4453 }
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05304454
4455 dev_info(&instance->pdev->dev,
4456 "firmware type\t: %s\n",
4457 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4458 "Legacy(64 VD) firmware");
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304459
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004460 if (instance->max_raid_mapsize) {
4461 ventura_map_sz = instance->max_raid_mapsize *
4462 MR_MIN_MAP_SIZE; /* 64k */
4463 fusion->current_map_sz = ventura_map_sz;
4464 fusion->max_map_sz = ventura_map_sz;
4465 } else {
4466 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4467 (sizeof(struct MR_LD_SPAN_MAP) *
4468 (instance->fw_supported_vd_count - 1));
4469 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304470
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004471 fusion->max_map_sz =
4472 max(fusion->old_map_sz, fusion->new_map_sz);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304473
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004474 if (instance->supportmax256vd)
4475 fusion->current_map_sz = fusion->new_map_sz;
4476 else
4477 fusion->current_map_sz = fusion->old_map_sz;
4478 }
4479 /* irrespective of FW raid maps, driver raid map is constant */
4480 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304481
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05004482#if VD_EXT_DEBUG
4483 dev_info(&instance->pdev->dev, "instance->max_raid_mapsize 0x%x\n ",
4484 instance->max_raid_mapsize);
4485 dev_info(&instance->pdev->dev, "new_map_sz = 0x%x, old_map_sz = 0x%x\n",
4486 fusion->new_map_sz, fusion->old_map_sz);
4487 dev_info(&instance->pdev->dev, "ventura_map_sz = 0x%x, current_map_sz = 0x%x\n",
4488 ventura_map_sz, fusion->current_map_sz);
4489 dev_info(&instance->pdev->dev, "fusion->drv_map_sz =0x%x, size of driver raid map 0x%lx\n",
4490 fusion->drv_map_sz, sizeof(struct MR_DRV_RAID_MAP_ALL));
4491#endif
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304492}
4493
adam radford21c9e162013-09-06 15:27:14 -07004494/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004495 * megasas_get_controller_info - Returns FW's controller structure
4496 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004497 *
4498 * Issues an internal command (DCMD) to get the FW's controller structure.
4499 * This information is mainly used to find out the maximum IO transfer per
4500 * command supported by the FW.
4501 */
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304502int
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304503megasas_get_ctrl_info(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004504{
4505 int ret = 0;
4506 struct megasas_cmd *cmd;
4507 struct megasas_dcmd_frame *dcmd;
4508 struct megasas_ctrl_info *ci;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304509 struct megasas_ctrl_info *ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004510 dma_addr_t ci_h = 0;
4511
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304512 ctrl_info = instance->ctrl_info;
4513
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004514 cmd = megasas_get_cmd(instance);
4515
4516 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004517 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004518 return -ENOMEM;
4519 }
4520
4521 dcmd = &cmd->frame->dcmd;
4522
4523 ci = pci_alloc_consistent(instance->pdev,
4524 sizeof(struct megasas_ctrl_info), &ci_h);
4525
4526 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004527 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004528 megasas_return_cmd(instance, cmd);
4529 return -ENOMEM;
4530 }
4531
4532 memset(ci, 0, sizeof(*ci));
4533 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4534
4535 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304536 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004537 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304538 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004539 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07004540 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304541 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4542 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4543 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4544 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304545 dcmd->mbox.b[0] = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004546
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304547 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304548 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304549 else
4550 ret = megasas_issue_polled(instance, cmd);
4551
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304552 switch (ret) {
4553 case DCMD_SUCCESS:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004554 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304555 /* Save required controller information in
4556 * CPU endianness format.
4557 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304558 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4559 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4560 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004561 le16_to_cpus((u16 *)&ctrl_info->adapter_operations4);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304562
4563 /* Update the latest Ext VD info.
4564 * From Init path, store current firmware details.
4565 * From OCR path, detect any firmware properties changes.
4566 * in case of Firmware upgrade without system reboot.
4567 */
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304568 megasas_update_ext_vd_details(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304569 instance->use_seqnum_jbod_fp =
4570 ctrl_info->adapterOperations3.useSeqNumJbodFP;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05004571 instance->support_morethan256jbod =
4572 ctrl_info->adapter_operations4.support_pd_map_target_id;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304573
4574 /*Check whether controller is iMR or MR */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304575 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4576 dev_info(&instance->pdev->dev,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304577 "controller type\t: %s(%dMB)\n",
4578 instance->is_imr ? "iMR" : "MR",
4579 le16_to_cpu(ctrl_info->memory_size));
4580
sumit.saxena@avagotech.comc4bd2652015-10-15 13:40:14 +05304581 instance->disableOnlineCtrlReset =
4582 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304583 instance->secure_jbod_support =
4584 ctrl_info->adapterOperations3.supportSecurityonJBOD;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304585 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4586 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
sumit.saxena@avagotech.com32222512015-10-15 13:40:24 +05304587 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4588 instance->secure_jbod_support ? "Yes" : "No");
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304589 break;
4590
4591 case DCMD_TIMEOUT:
4592 switch (dcmd_timeout_ocr_possible(instance)) {
4593 case INITIATE_OCR:
4594 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4595 megasas_reset_fusion(instance->host,
4596 MFI_IO_TIMEOUT_OCR);
4597 break;
4598 case KILL_ADAPTER:
4599 megaraid_sas_kill_hba(instance);
4600 break;
4601 case IGNORE_TIMEOUT:
4602 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4603 __func__, __LINE__);
4604 break;
4605 }
4606 case DCMD_FAILED:
4607 megaraid_sas_kill_hba(instance);
4608 break;
4609
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304610 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004611
4612 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4613 ci, ci_h);
4614
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304615 megasas_return_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304616
4617
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004618 return ret;
4619}
4620
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304621/*
4622 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4623 * to firmware
4624 *
4625 * @instance: Adapter soft state
4626 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4627 MR_CRASH_BUF_TURN_OFF = 0
4628 MR_CRASH_BUF_TURN_ON = 1
4629 * @return 0 on success non-zero on failure.
4630 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4631 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4632 * that driver supports crash dump feature. This DCMD will be sent only if
4633 * crash dump feature is supported by the FW.
4634 *
4635 */
4636int megasas_set_crash_dump_params(struct megasas_instance *instance,
4637 u8 crash_buf_state)
4638{
4639 int ret = 0;
4640 struct megasas_cmd *cmd;
4641 struct megasas_dcmd_frame *dcmd;
4642
4643 cmd = megasas_get_cmd(instance);
4644
4645 if (!cmd) {
4646 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4647 return -ENOMEM;
4648 }
4649
4650
4651 dcmd = &cmd->frame->dcmd;
4652
4653 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4654 dcmd->mbox.b[0] = crash_buf_state;
4655 dcmd->cmd = MFI_CMD_DCMD;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304656 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304657 dcmd->sge_count = 1;
4658 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4659 dcmd->timeout = 0;
4660 dcmd->pad_0 = 0;
4661 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4662 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4663 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4664 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4665
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304666 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304667 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304668 else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05304669 ret = megasas_issue_polled(instance, cmd);
4670
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304671 if (ret == DCMD_TIMEOUT) {
4672 switch (dcmd_timeout_ocr_possible(instance)) {
4673 case INITIATE_OCR:
4674 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4675 megasas_reset_fusion(instance->host,
4676 MFI_IO_TIMEOUT_OCR);
4677 break;
4678 case KILL_ADAPTER:
4679 megaraid_sas_kill_hba(instance);
4680 break;
4681 case IGNORE_TIMEOUT:
4682 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4683 __func__, __LINE__);
4684 break;
4685 }
4686 } else
4687 megasas_return_cmd(instance, cmd);
4688
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304689 return ret;
4690}
4691
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004692/**
bo yang31ea7082007-11-07 12:09:50 -05004693 * megasas_issue_init_mfi - Initializes the FW
4694 * @instance: Adapter soft state
4695 *
4696 * Issues the INIT MFI cmd
4697 */
4698static int
4699megasas_issue_init_mfi(struct megasas_instance *instance)
4700{
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05304701 __le32 context;
bo yang31ea7082007-11-07 12:09:50 -05004702 struct megasas_cmd *cmd;
bo yang31ea7082007-11-07 12:09:50 -05004703 struct megasas_init_frame *init_frame;
4704 struct megasas_init_queue_info *initq_info;
4705 dma_addr_t init_frame_h;
4706 dma_addr_t initq_info_h;
4707
4708 /*
4709 * Prepare a init frame. Note the init frame points to queue info
4710 * structure. Each frame has SGL allocated after first 64 bytes. For
4711 * this frame - since we don't need any SGL - we use SGL's space as
4712 * queue info structure
4713 *
4714 * We will not get a NULL command below. We just created the pool.
4715 */
4716 cmd = megasas_get_cmd(instance);
4717
4718 init_frame = (struct megasas_init_frame *)cmd->frame;
4719 initq_info = (struct megasas_init_queue_info *)
4720 ((unsigned long)init_frame + 64);
4721
4722 init_frame_h = cmd->frame_phys_addr;
4723 initq_info_h = init_frame_h + 64;
4724
4725 context = init_frame->context;
4726 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4727 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4728 init_frame->context = context;
4729
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304730 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4731 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
bo yang31ea7082007-11-07 12:09:50 -05004732
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304733 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4734 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
bo yang31ea7082007-11-07 12:09:50 -05004735
4736 init_frame->cmd = MFI_CMD_INIT;
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +05304737 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304738 init_frame->queue_info_new_phys_addr_lo =
4739 cpu_to_le32(lower_32_bits(initq_info_h));
4740 init_frame->queue_info_new_phys_addr_hi =
4741 cpu_to_le32(upper_32_bits(initq_info_h));
bo yang31ea7082007-11-07 12:09:50 -05004742
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05304743 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
bo yang31ea7082007-11-07 12:09:50 -05004744
4745 /*
4746 * disable the intr before firing the init frame to FW
4747 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304748 instance->instancet->disable_intr(instance);
bo yang31ea7082007-11-07 12:09:50 -05004749
4750 /*
4751 * Issue the init frame in polled mode
4752 */
4753
4754 if (megasas_issue_polled(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004755 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
bo yang31ea7082007-11-07 12:09:50 -05004756 megasas_return_cmd(instance, cmd);
4757 goto fail_fw_init;
4758 }
4759
4760 megasas_return_cmd(instance, cmd);
4761
4762 return 0;
4763
4764fail_fw_init:
4765 return -EINVAL;
4766}
4767
adam radfordcd50ba82010-12-21 10:23:23 -08004768static u32
4769megasas_init_adapter_mfi(struct megasas_instance *instance)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004770{
adam radfordcd50ba82010-12-21 10:23:23 -08004771 struct megasas_register_set __iomem *reg_set;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004772 u32 context_sz;
4773 u32 reply_q_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004774
4775 reg_set = instance->reg_set;
4776
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004777 /*
4778 * Get various operational parameters from status register
4779 */
Sumant Patro1341c932006-01-25 12:02:40 -08004780 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
Sumant Patroe3bbff92006-10-03 12:28:49 -07004781 /*
4782 * Reduce the max supported cmds by 1. This is to ensure that the
4783 * reply_q_sz (1 more than the max cmd that driver may send)
4784 * does not exceed max cmds that the FW can support
4785 */
4786 instance->max_fw_cmds = instance->max_fw_cmds-1;
adam radford9c915a82010-12-21 13:34:31 -08004787 instance->max_mfi_cmds = instance->max_fw_cmds;
adam radford0d490162010-12-14 19:17:17 -08004788 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
Sumant Patro1341c932006-01-25 12:02:40 -08004789 0x10;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004790 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304791 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4792 * are reserved for IOCTL + driver's internal DCMDs.
4793 */
4794 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4795 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4796 instance->max_scsi_cmds = (instance->max_fw_cmds -
4797 MEGASAS_SKINNY_INT_CMDS);
4798 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4799 } else {
4800 instance->max_scsi_cmds = (instance->max_fw_cmds -
4801 MEGASAS_INT_CMDS);
4802 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4803 }
4804
Sumit Saxena308ec452016-01-28 21:04:30 +05304805 instance->cur_can_queue = instance->max_scsi_cmds;
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05304806 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004807 * Create a pool of commands
4808 */
4809 if (megasas_alloc_cmds(instance))
4810 goto fail_alloc_cmds;
4811
4812 /*
4813 * Allocate memory for reply queue. Length of reply queue should
4814 * be _one_ more than the maximum commands handled by the firmware.
4815 *
4816 * Note: When FW completes commands, it places corresponding contex
4817 * values in this circular reply queue. This circular queue is a fairly
4818 * typical producer-consumer queue. FW is the producer (of completed
4819 * commands) and the driver is the consumer.
4820 */
4821 context_sz = sizeof(u32);
4822 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4823
4824 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4825 reply_q_sz,
4826 &instance->reply_queue_h);
4827
4828 if (!instance->reply_queue) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004829 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004830 goto fail_reply_queue;
4831 }
4832
bo yang31ea7082007-11-07 12:09:50 -05004833 if (megasas_issue_init_mfi(instance))
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004834 goto fail_fw_init;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04004835
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304836 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304837 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4838 "Fail from %s %d\n", instance->unique_id,
4839 __func__, __LINE__);
4840 goto fail_fw_init;
4841 }
4842
bo yang39a98552010-09-22 22:36:29 -04004843 instance->fw_support_ieee = 0;
4844 instance->fw_support_ieee =
4845 (instance->instancet->read_fw_status_reg(reg_set) &
4846 0x04000000);
4847
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004848 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
bo yang39a98552010-09-22 22:36:29 -04004849 instance->fw_support_ieee);
4850
4851 if (instance->fw_support_ieee)
4852 instance->flag_ieee = 1;
4853
adam radfordcd50ba82010-12-21 10:23:23 -08004854 return 0;
4855
4856fail_fw_init:
4857
4858 pci_free_consistent(instance->pdev, reply_q_sz,
4859 instance->reply_queue, instance->reply_queue_h);
4860fail_reply_queue:
4861 megasas_free_cmds(instance);
4862
4863fail_alloc_cmds:
adam radfordcd50ba82010-12-21 10:23:23 -08004864 return 1;
4865}
4866
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304867/*
Hannes Reineckefad119b2016-12-02 12:52:23 +01004868 * megasas_setup_irqs_ioapic - register legacy interrupts.
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304869 * @instance: Adapter soft state
4870 *
4871 * Do not enable interrupt, only setup ISRs.
4872 *
4873 * Return 0 on success.
4874 */
4875static int
4876megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4877{
4878 struct pci_dev *pdev;
4879
4880 pdev = instance->pdev;
4881 instance->irq_context[0].instance = instance;
4882 instance->irq_context[0].MSIxIndex = 0;
Hannes Reineckefad119b2016-12-02 12:52:23 +01004883 if (request_irq(pci_irq_vector(pdev, 0),
4884 instance->instancet->service_isr, IRQF_SHARED,
4885 "megasas", &instance->irq_context[0])) {
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304886 dev_err(&instance->pdev->dev,
4887 "Failed to register IRQ from %s %d\n",
4888 __func__, __LINE__);
4889 return -1;
4890 }
4891 return 0;
4892}
4893
4894/**
4895 * megasas_setup_irqs_msix - register MSI-x interrupts.
4896 * @instance: Adapter soft state
4897 * @is_probe: Driver probe check
4898 *
4899 * Do not enable interrupt, only setup ISRs.
4900 *
4901 * Return 0 on success.
4902 */
4903static int
4904megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4905{
Hannes Reineckefad119b2016-12-02 12:52:23 +01004906 int i, j;
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304907 struct pci_dev *pdev;
4908
4909 pdev = instance->pdev;
4910
4911 /* Try MSI-x */
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304912 for (i = 0; i < instance->msix_vectors; i++) {
4913 instance->irq_context[i].instance = instance;
4914 instance->irq_context[i].MSIxIndex = i;
Hannes Reineckefad119b2016-12-02 12:52:23 +01004915 if (request_irq(pci_irq_vector(pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304916 instance->instancet->service_isr, 0, "megasas",
4917 &instance->irq_context[i])) {
4918 dev_err(&instance->pdev->dev,
4919 "Failed to register IRQ for vector %d.\n", i);
Hannes Reineckefad119b2016-12-02 12:52:23 +01004920 for (j = 0; j < i; j++)
4921 free_irq(pci_irq_vector(pdev, j),
4922 &instance->irq_context[j]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304923 /* Retry irq register for IO_APIC*/
4924 instance->msix_vectors = 0;
4925 if (is_probe)
4926 return megasas_setup_irqs_ioapic(instance);
4927 else
4928 return -1;
4929 }
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304930 }
4931 return 0;
4932}
4933
4934/*
4935 * megasas_destroy_irqs- unregister interrupts.
4936 * @instance: Adapter soft state
4937 * return: void
4938 */
4939static void
4940megasas_destroy_irqs(struct megasas_instance *instance) {
4941
4942 int i;
4943
4944 if (instance->msix_vectors)
4945 for (i = 0; i < instance->msix_vectors; i++) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01004946 free_irq(pci_irq_vector(instance->pdev, i),
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304947 &instance->irq_context[i]);
4948 }
4949 else
Hannes Reineckefad119b2016-12-02 12:52:23 +01004950 free_irq(pci_irq_vector(instance->pdev, 0),
4951 &instance->irq_context[0]);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05304952}
4953
adam radfordcd50ba82010-12-21 10:23:23 -08004954/**
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304955 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4956 * @instance: Adapter soft state
4957 * @is_probe: Driver probe check
4958 *
4959 * Return 0 on success.
4960 */
4961void
4962megasas_setup_jbod_map(struct megasas_instance *instance)
4963{
4964 int i;
4965 struct fusion_context *fusion = instance->ctrl_context;
4966 u32 pd_seq_map_sz;
4967
4968 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4969 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4970
4971 if (reset_devices || !fusion ||
4972 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4973 dev_info(&instance->pdev->dev,
4974 "Jbod map is not supported %s %d\n",
4975 __func__, __LINE__);
4976 instance->use_seqnum_jbod_fp = false;
4977 return;
4978 }
4979
4980 if (fusion->pd_seq_sync[0])
4981 goto skip_alloc;
4982
4983 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4984 fusion->pd_seq_sync[i] = dma_alloc_coherent
4985 (&instance->pdev->dev, pd_seq_map_sz,
4986 &fusion->pd_seq_phys[i], GFP_KERNEL);
4987 if (!fusion->pd_seq_sync[i]) {
4988 dev_err(&instance->pdev->dev,
4989 "Failed to allocate memory from %s %d\n",
4990 __func__, __LINE__);
4991 if (i == 1) {
4992 dma_free_coherent(&instance->pdev->dev,
4993 pd_seq_map_sz, fusion->pd_seq_sync[0],
4994 fusion->pd_seq_phys[0]);
4995 fusion->pd_seq_sync[0] = NULL;
4996 }
4997 instance->use_seqnum_jbod_fp = false;
4998 return;
4999 }
5000 }
5001
5002skip_alloc:
5003 if (!megasas_sync_pd_seq_num(instance, false) &&
5004 !megasas_sync_pd_seq_num(instance, true))
5005 instance->use_seqnum_jbod_fp = true;
5006 else
5007 instance->use_seqnum_jbod_fp = false;
5008}
5009
5010/**
adam radfordcd50ba82010-12-21 10:23:23 -08005011 * megasas_init_fw - Initializes the FW
5012 * @instance: Adapter soft state
5013 *
5014 * This is the main function for initializing firmware
5015 */
5016
5017static int megasas_init_fw(struct megasas_instance *instance)
5018{
5019 u32 max_sectors_1;
5020 u32 max_sectors_2;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005021 u32 tmp_sectors, msix_enable, scratch_pad_2, scratch_pad_3;
Ben Collins11f8a7b2013-09-13 12:46:44 -04005022 resource_size_t base_addr;
adam radfordcd50ba82010-12-21 10:23:23 -08005023 struct megasas_register_set __iomem *reg_set;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305024 struct megasas_ctrl_info *ctrl_info = NULL;
adam radfordcd50ba82010-12-21 10:23:23 -08005025 unsigned long bar_list;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005026 int i, j, loop, fw_msix_count = 0;
adam radford229fe472014-03-10 02:51:56 -07005027 struct IOV_111 *iovPtr;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305028 struct fusion_context *fusion;
5029
5030 fusion = instance->ctrl_context;
adam radfordcd50ba82010-12-21 10:23:23 -08005031
5032 /* Find first memory bar */
5033 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
Christophe JAILLET51f90392016-08-21 10:28:25 +02005034 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
Yinghai Lue7f85162016-08-05 23:37:34 -07005035 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
adam radfordcd50ba82010-12-21 10:23:23 -08005036 "megasas: LSI")) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005037 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005038 return -EBUSY;
5039 }
5040
Ben Collins11f8a7b2013-09-13 12:46:44 -04005041 base_addr = pci_resource_start(instance->pdev, instance->bar);
5042 instance->reg_set = ioremap_nocache(base_addr, 8192);
adam radfordcd50ba82010-12-21 10:23:23 -08005043
5044 if (!instance->reg_set) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005045 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005046 goto fail_ioremap;
5047 }
5048
5049 reg_set = instance->reg_set;
5050
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005051 if (fusion)
adam radford9c915a82010-12-21 13:34:31 -08005052 instance->instancet = &megasas_instance_template_fusion;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005053 else {
5054 switch (instance->pdev->device) {
5055 case PCI_DEVICE_ID_LSI_SAS1078R:
5056 case PCI_DEVICE_ID_LSI_SAS1078DE:
5057 instance->instancet = &megasas_instance_template_ppc;
5058 break;
5059 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5060 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5061 instance->instancet = &megasas_instance_template_gen2;
5062 break;
5063 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5064 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5065 instance->instancet = &megasas_instance_template_skinny;
5066 break;
5067 case PCI_DEVICE_ID_LSI_SAS1064R:
5068 case PCI_DEVICE_ID_DELL_PERC5:
5069 default:
5070 instance->instancet = &megasas_instance_template_xscale;
5071 instance->pd_list_not_supported = 1;
5072 break;
5073 }
adam radfordcd50ba82010-12-21 10:23:23 -08005074 }
5075
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305076 if (megasas_transition_to_ready(instance, 0)) {
5077 atomic_set(&instance->fw_reset_no_pci_access, 1);
5078 instance->instancet->adp_reset
5079 (instance, instance->reg_set);
5080 atomic_set(&instance->fw_reset_no_pci_access, 0);
5081 dev_info(&instance->pdev->dev,
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005082 "FW restarted successfully from %s!\n",
Sumit.Saxena@lsi.com6431f5d2013-07-16 02:26:05 +05305083 __func__);
5084
5085 /*waitting for about 30 second before retry*/
5086 ssleep(30);
5087
5088 if (megasas_transition_to_ready(instance, 0))
5089 goto fail_ready_state;
5090 }
adam radfordcd50ba82010-12-21 10:23:23 -08005091
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05005092 if (instance->is_ventura) {
5093 scratch_pad_3 =
5094 readl(&instance->reg_set->outbound_scratch_pad_3);
5095#if VD_EXT_DEBUG
5096 dev_info(&instance->pdev->dev, "scratch_pad3 0x%x\n",
5097 scratch_pad_3);
5098#endif
5099 instance->max_raid_mapsize = ((scratch_pad_3 >>
5100 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5101 MR_MAX_RAID_MAP_SIZE_MASK);
5102 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305103
adam radford3f1abce2011-05-11 18:33:47 -07005104 /* Check if MSI-X is supported while in ready state */
5105 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5106 0x4000000) >> 0x1a;
adam radfordc8e858f2011-10-08 18:15:13 -07005107 if (msix_enable && !msix_disable) {
Hannes Reineckefad119b2016-12-02 12:52:23 +01005108 int irq_flags = PCI_IRQ_MSIX;
5109
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305110 scratch_pad_2 = readl
5111 (&instance->reg_set->outbound_scratch_pad_2);
adam radfordc8e858f2011-10-08 18:15:13 -07005112 /* Check max MSI-X vectors */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305113 if (fusion) {
5114 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5115 instance->msix_vectors = (scratch_pad_2
5116 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5117 fw_msix_count = instance->msix_vectors;
5118 } else { /* Invader series supports more than 8 MSI-x vectors*/
5119 instance->msix_vectors = ((scratch_pad_2
5120 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5121 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005122 if (instance->msix_vectors > 16)
5123 instance->msix_combined = true;
5124
Sumit Saxena179ac142016-01-28 21:04:28 +05305125 if (rdpq_enable)
5126 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5127 1 : 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305128 fw_msix_count = instance->msix_vectors;
5129 /* Save 1-15 reply post index address to local memory
5130 * Index 0 is already saved from reg offset
5131 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5132 */
5133 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5134 instance->reply_post_host_index_addr[loop] =
5135 (u32 __iomem *)
5136 ((u8 __iomem *)instance->reg_set +
5137 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5138 + (loop * 0x10));
5139 }
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05305140 }
5141 if (msix_vectors)
5142 instance->msix_vectors = min(msix_vectors,
5143 instance->msix_vectors);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305144 } else /* MFI adapters */
adam radfordc8e858f2011-10-08 18:15:13 -07005145 instance->msix_vectors = 1;
5146 /* Don't bother allocating more MSI-X vectors than cpus */
5147 instance->msix_vectors = min(instance->msix_vectors,
5148 (unsigned int)num_online_cpus());
Hannes Reineckefad119b2016-12-02 12:52:23 +01005149 if (smp_affinity_enable)
5150 irq_flags |= PCI_IRQ_AFFINITY;
5151 i = pci_alloc_irq_vectors(instance->pdev, 1,
5152 instance->msix_vectors, irq_flags);
Jiang Liuc12de882014-11-03 20:44:20 +08005153 if (i > 0)
Alexander Gordeev8ae80ed2014-08-18 08:01:44 +02005154 instance->msix_vectors = i;
5155 else
adam radfordc8e858f2011-10-08 18:15:13 -07005156 instance->msix_vectors = 0;
5157 }
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05005158 /*
5159 * MSI-X host index 0 is common for all adapter.
5160 * It is used for all MPT based Adapters.
5161 */
5162 if (instance->msix_combined) {
5163 instance->reply_post_host_index_addr[0] =
5164 (u32 *)((u8 *)instance->reg_set +
5165 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5166 } else {
5167 instance->reply_post_host_index_addr[0] =
5168 (u32 *)((u8 *)instance->reg_set +
5169 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5170 }
5171
Hannes Reineckefad119b2016-12-02 12:52:23 +01005172 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5173 if (i < 0)
5174 goto fail_setup_irqs;
adam radford3f1abce2011-05-11 18:33:47 -07005175
Tomas Henzl258c3af2015-06-02 16:09:46 +05305176 dev_info(&instance->pdev->dev,
5177 "firmware supports msix\t: (%d)", fw_msix_count);
5178 dev_info(&instance->pdev->dev,
5179 "current msix/online cpus\t: (%d/%d)\n",
5180 instance->msix_vectors, (unsigned int)num_online_cpus());
Sumit Saxena179ac142016-01-28 21:04:28 +05305181 dev_info(&instance->pdev->dev,
5182 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
Tomas Henzl258c3af2015-06-02 16:09:46 +05305183
sumit.saxena@avagotech.com91626c22015-10-15 13:40:34 +05305184 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5185 (unsigned long)instance);
5186
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305187 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5188 GFP_KERNEL);
5189 if (instance->ctrl_info == NULL)
5190 goto fail_init_adapter;
5191
5192 /*
5193 * Below are default value for legacy Firmware.
5194 * non-fusion based controllers
5195 */
5196 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5197 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
adam radfordcd50ba82010-12-21 10:23:23 -08005198 /* Get operational params, sge flags, send init cmd to controller */
5199 if (instance->instancet->init_adapter(instance))
adam radfordeb1b1232011-02-24 20:55:56 -08005200 goto fail_init_adapter;
adam radfordcd50ba82010-12-21 10:23:23 -08005201
Tomas Henzl18103ef2016-11-01 17:32:02 +01005202 if (instance->msix_vectors ?
5203 megasas_setup_irqs_msix(instance, 1) :
5204 megasas_setup_irqs_ioapic(instance))
5205 goto fail_init_adapter;
Tomas Henzl258c3af2015-06-02 16:09:46 +05305206
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305207 instance->instancet->enable_intr(instance);
adam radfordcd50ba82010-12-21 10:23:23 -08005208
Andy Lutomirski13f30772016-05-03 10:24:31 -07005209 dev_info(&instance->pdev->dev, "INIT adapter done\n");
adam radfordcd50ba82010-12-21 10:23:23 -08005210
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305211 megasas_setup_jbod_map(instance);
5212
bo yang39a98552010-09-22 22:36:29 -04005213 /** for passthrough
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005214 * the following function will get the PD LIST.
5215 */
5216 memset(instance->pd_list, 0,
Yang, Bo81e403c2009-10-06 14:27:54 -06005217 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005218 if (megasas_get_pd_list(instance) < 0) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005219 dev_err(&instance->pdev->dev, "failed to get PD list\n");
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305220 goto fail_get_pd_list;
Hannes Reinecke58968fc2014-01-16 11:25:36 +01005221 }
Yang, Bo81e403c2009-10-06 14:27:54 -06005222
Yang, Bobdc6fb82009-12-06 08:30:19 -07005223 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005224
5225 /* stream detection initialization */
5226 if (instance->is_ventura) {
5227 fusion->stream_detect_by_ld =
5228 kzalloc(sizeof(struct LD_STREAM_DETECT *)
5229 * MAX_LOGICAL_DRIVES_EXT,
5230 GFP_KERNEL);
5231 if (!fusion->stream_detect_by_ld) {
5232 dev_err(&instance->pdev->dev,
5233 "unable to allocate stream detection for pool of LDs\n");
5234 goto fail_get_ld_pd_list;
5235 }
5236 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5237 fusion->stream_detect_by_ld[i] =
5238 kmalloc(sizeof(struct LD_STREAM_DETECT),
5239 GFP_KERNEL);
5240 if (!fusion->stream_detect_by_ld[i]) {
5241 dev_err(&instance->pdev->dev,
5242 "unable to allocate stream detect by LD\n ");
5243 for (j = 0; j < i; ++j)
5244 kfree(fusion->stream_detect_by_ld[j]);
5245 kfree(fusion->stream_detect_by_ld);
5246 fusion->stream_detect_by_ld = NULL;
5247 goto fail_get_ld_pd_list;
5248 }
5249 fusion->stream_detect_by_ld[i]->mru_bit_map
5250 = MR_STREAM_BITMAP;
5251 }
5252 }
5253
adam radford21c9e162013-09-06 15:27:14 -07005254 if (megasas_ld_list_query(instance,
5255 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5256 megasas_get_ld_list(instance);
Yang, Bobdc6fb82009-12-06 08:30:19 -07005257
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005258 /*
5259 * Compute the max allowed sectors per IO: The controller info has two
5260 * limits on max sectors. Driver should use the minimum of these two.
5261 *
5262 * 1 << stripe_sz_ops.min = max sectors per strip
5263 *
5264 * Note that older firmwares ( < FW ver 30) didn't report information
5265 * to calculate max_sectors_1. So the number ended up as zero always.
5266 */
bo yang14faea92007-11-09 04:14:00 -05005267 tmp_sectors = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305268 ctrl_info = instance->ctrl_info;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005269
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305270 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5271 le16_to_cpu(ctrl_info->max_strips_per_io);
5272 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005273
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005274 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305275
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05305276 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5277 instance->passive = ctrl_info->cluster.passive;
5278 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305279 instance->UnevenSpanSupport =
5280 ctrl_info->adapterOperations2.supportUnevenSpans;
5281 if (instance->UnevenSpanSupport) {
5282 struct fusion_context *fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305283 if (MR_ValidateMapInfo(instance))
5284 fusion->fast_path_io = 1;
5285 else
5286 fusion->fast_path_io = 0;
5287
5288 }
5289 if (ctrl_info->host_interface.SRIOV) {
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305290 instance->requestorId = ctrl_info->iov.requestorId;
5291 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5292 if (!ctrl_info->adapterOperations2.activePassive)
5293 instance->PlasmaFW111 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305294
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305295 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5296 instance->PlasmaFW111 ? "1.11" : "new");
5297
5298 if (instance->PlasmaFW111) {
5299 iovPtr = (struct IOV_111 *)
5300 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5301 instance->requestorId = iovPtr->requestorId;
5302 }
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305303 }
sumit.saxena@avagotech.com92bb6502015-08-31 17:24:11 +05305304 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5305 instance->requestorId);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305306 }
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305307
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305308 instance->crash_dump_fw_support =
5309 ctrl_info->adapterOperations3.supportCrashDump;
5310 instance->crash_dump_drv_support =
5311 (instance->crash_dump_fw_support &&
5312 instance->crash_dump_buf);
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305313 if (instance->crash_dump_drv_support)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305314 megasas_set_crash_dump_params(instance,
5315 MR_CRASH_BUF_TURN_OFF);
adam radford229fe472014-03-10 02:51:56 -07005316
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305317 else {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305318 if (instance->crash_dump_buf)
5319 pci_free_consistent(instance->pdev,
5320 CRASH_DMA_BUF_SIZE,
5321 instance->crash_dump_buf,
5322 instance->crash_dump_h);
5323 instance->crash_dump_buf = NULL;
bo yang14faea92007-11-09 04:14:00 -05005324 }
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05305325
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305326
5327 dev_info(&instance->pdev->dev,
5328 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5329 le16_to_cpu(ctrl_info->pci.vendor_id),
5330 le16_to_cpu(ctrl_info->pci.device_id),
5331 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5332 le16_to_cpu(ctrl_info->pci.sub_device_id));
5333 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5334 instance->UnevenSpanSupport ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305335 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5336 instance->crash_dump_drv_support ? "yes" : "no");
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05305337 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5338 instance->use_seqnum_jbod_fp ? "yes" : "no");
Sumit.Saxena@avagotech.comd88da092015-04-23 16:31:09 +05305339
5340
bo yang14faea92007-11-09 04:14:00 -05005341 instance->max_sectors_per_req = instance->max_num_sge *
sumit.saxena@avagotech.com357ae962015-10-15 13:40:04 +05305342 SGE_BUFFER_SIZE / 512;
bo yang14faea92007-11-09 04:14:00 -05005343 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5344 instance->max_sectors_per_req = tmp_sectors;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005345
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305346 /* Check for valid throttlequeuedepth module parameter */
5347 if (throttlequeuedepth &&
5348 throttlequeuedepth <= instance->max_scsi_cmds)
5349 instance->throttlequeuedepth = throttlequeuedepth;
5350 else
5351 instance->throttlequeuedepth =
5352 MEGASAS_THROTTLE_QUEUE_DEPTH;
5353
Sumit Saxenae3d178c2016-01-28 21:04:34 +05305354 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5355 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5356
5357 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5358 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
bo yangad84db22007-11-09 04:40:16 -05005359
adam radford229fe472014-03-10 02:51:56 -07005360 /* Launch SR-IOV heartbeat timer */
5361 if (instance->requestorId) {
5362 if (!megasas_sriov_start_heartbeat(instance, 1))
5363 megasas_start_timer(instance,
5364 &instance->sriov_heartbeat_timer,
5365 megasas_sriov_heartbeat_handler,
5366 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5367 else
5368 instance->skip_heartbeat_timer_del = 1;
5369 }
5370
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005371 return 0;
5372
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005373fail_get_ld_pd_list:
5374 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305375fail_get_pd_list:
5376 instance->instancet->disable_intr(instance);
Tomas Henzl18103ef2016-11-01 17:32:02 +01005377fail_init_adapter:
Hannes Reineckefad119b2016-12-02 12:52:23 +01005378 megasas_destroy_irqs(instance);
5379fail_setup_irqs:
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305380 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01005381 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305382 instance->msix_vectors = 0;
adam radfordcd50ba82010-12-21 10:23:23 -08005383fail_ready_state:
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305384 kfree(instance->ctrl_info);
5385 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005386 iounmap(instance->reg_set);
5387
5388 fail_ioremap:
Yinghai Lue7f85162016-08-05 23:37:34 -07005389 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005390
5391 return -EINVAL;
5392}
5393
5394/**
5395 * megasas_release_mfi - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01005396 * @instance: Adapter soft state
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005397 */
5398static void megasas_release_mfi(struct megasas_instance *instance)
5399{
adam radford9c915a82010-12-21 13:34:31 -08005400 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005401
adam radford9c915a82010-12-21 13:34:31 -08005402 if (instance->reply_queue)
5403 pci_free_consistent(instance->pdev, reply_q_sz,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005404 instance->reply_queue, instance->reply_queue_h);
5405
5406 megasas_free_cmds(instance);
5407
5408 iounmap(instance->reg_set);
5409
Yinghai Lue7f85162016-08-05 23:37:34 -07005410 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005411}
5412
5413/**
5414 * megasas_get_seq_num - Gets latest event sequence numbers
5415 * @instance: Adapter soft state
5416 * @eli: FW event log sequence numbers information
5417 *
5418 * FW maintains a log of all events in a non-volatile area. Upper layers would
5419 * usually find out the latest sequence number of the events, the seq number at
5420 * the boot etc. They would "read" all the events below the latest seq number
5421 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5422 * number), they would subsribe to AEN (asynchronous event notification) and
5423 * wait for the events to happen.
5424 */
5425static int
5426megasas_get_seq_num(struct megasas_instance *instance,
5427 struct megasas_evt_log_info *eli)
5428{
5429 struct megasas_cmd *cmd;
5430 struct megasas_dcmd_frame *dcmd;
5431 struct megasas_evt_log_info *el_info;
5432 dma_addr_t el_info_h = 0;
5433
5434 cmd = megasas_get_cmd(instance);
5435
5436 if (!cmd) {
5437 return -ENOMEM;
5438 }
5439
5440 dcmd = &cmd->frame->dcmd;
5441 el_info = pci_alloc_consistent(instance->pdev,
5442 sizeof(struct megasas_evt_log_info),
5443 &el_info_h);
5444
5445 if (!el_info) {
5446 megasas_return_cmd(instance, cmd);
5447 return -ENOMEM;
5448 }
5449
5450 memset(el_info, 0, sizeof(*el_info));
5451 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5452
5453 dcmd->cmd = MFI_CMD_DCMD;
5454 dcmd->cmd_status = 0x0;
5455 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305456 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005457 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005458 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305459 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5460 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5461 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5462 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005463
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305464 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5465 DCMD_SUCCESS) {
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305466 /*
5467 * Copy the data back into callers buffer
5468 */
Christoph Hellwig48100b02015-04-23 16:33:24 +05305469 eli->newest_seq_num = el_info->newest_seq_num;
5470 eli->oldest_seq_num = el_info->oldest_seq_num;
5471 eli->clear_seq_num = el_info->clear_seq_num;
5472 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5473 eli->boot_seq_num = el_info->boot_seq_num;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05305474 } else
5475 dev_err(&instance->pdev->dev, "DCMD failed "
5476 "from %s\n", __func__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005477
5478 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5479 el_info, el_info_h);
5480
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305481 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005482
5483 return 0;
5484}
5485
5486/**
5487 * megasas_register_aen - Registers for asynchronous event notification
5488 * @instance: Adapter soft state
5489 * @seq_num: The starting sequence number
5490 * @class_locale: Class of the event
5491 *
5492 * This function subscribes for AEN for events beyond the @seq_num. It requests
5493 * to be notified if and only if the event is of type @class_locale
5494 */
5495static int
5496megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5497 u32 class_locale_word)
5498{
5499 int ret_val;
5500 struct megasas_cmd *cmd;
5501 struct megasas_dcmd_frame *dcmd;
5502 union megasas_evt_class_locale curr_aen;
5503 union megasas_evt_class_locale prev_aen;
5504
5505 /*
5506 * If there an AEN pending already (aen_cmd), check if the
5507 * class_locale of that pending AEN is inclusive of the new
5508 * AEN request we currently have. If it is, then we don't have
5509 * to do anything. In other words, whichever events the current
5510 * AEN request is subscribing to, have already been subscribed
5511 * to.
5512 *
5513 * If the old_cmd is _not_ inclusive, then we have to abort
5514 * that command, form a class_locale that is superset of both
5515 * old and current and re-issue to the FW
5516 */
5517
5518 curr_aen.word = class_locale_word;
5519
5520 if (instance->aen_cmd) {
5521
Christoph Hellwiga9555532015-04-23 16:34:24 +05305522 prev_aen.word =
5523 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005524
5525 /*
5526 * A class whose enum value is smaller is inclusive of all
5527 * higher values. If a PROGRESS (= -1) was previously
5528 * registered, then a new registration requests for higher
5529 * classes need not be sent to FW. They are automatically
5530 * included.
5531 *
5532 * Locale numbers don't have such hierarchy. They are bitmap
5533 * values
5534 */
5535 if ((prev_aen.members.class <= curr_aen.members.class) &&
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305536 !((prev_aen.members.locale & curr_aen.members.locale) ^
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005537 curr_aen.members.locale)) {
5538 /*
5539 * Previously issued event registration includes
5540 * current request. Nothing to do.
5541 */
5542 return 0;
5543 } else {
Sumit.Saxena@lsi.com3993a862013-09-16 15:18:06 +05305544 curr_aen.members.locale |= prev_aen.members.locale;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005545
5546 if (prev_aen.members.class < curr_aen.members.class)
5547 curr_aen.members.class = prev_aen.members.class;
5548
5549 instance->aen_cmd->abort_aen = 1;
5550 ret_val = megasas_issue_blocked_abort_cmd(instance,
5551 instance->
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05305552 aen_cmd, 30);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005553
5554 if (ret_val) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005555 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005556 "previous AEN command\n");
5557 return ret_val;
5558 }
5559 }
5560 }
5561
5562 cmd = megasas_get_cmd(instance);
5563
5564 if (!cmd)
5565 return -ENOMEM;
5566
5567 dcmd = &cmd->frame->dcmd;
5568
5569 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5570
5571 /*
5572 * Prepare DCMD for aen registration
5573 */
5574 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5575
5576 dcmd->cmd = MFI_CMD_DCMD;
5577 dcmd->cmd_status = 0x0;
5578 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305579 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005580 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07005581 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305582 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5583 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5584 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
bo yang39a98552010-09-22 22:36:29 -04005585 instance->last_seq_num = seq_num;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305586 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5587 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5588 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005589
Yang, Bof4c9a132009-10-06 14:43:28 -06005590 if (instance->aen_cmd != NULL) {
5591 megasas_return_cmd(instance, cmd);
5592 return 0;
5593 }
5594
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005595 /*
5596 * Store reference to the cmd used to register for AEN. When an
5597 * application wants us to register for AEN, we have to abort this
5598 * cmd and re-register with a new EVENT LOCALE supplied by that app
5599 */
5600 instance->aen_cmd = cmd;
5601
5602 /*
5603 * Issue the aen registration frame
5604 */
adam radford9c915a82010-12-21 13:34:31 -08005605 instance->instancet->issue_dcmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005606
5607 return 0;
5608}
5609
5610/**
5611 * megasas_start_aen - Subscribes to AEN during driver load time
5612 * @instance: Adapter soft state
5613 */
5614static int megasas_start_aen(struct megasas_instance *instance)
5615{
5616 struct megasas_evt_log_info eli;
5617 union megasas_evt_class_locale class_locale;
5618
5619 /*
5620 * Get the latest sequence number from FW
5621 */
5622 memset(&eli, 0, sizeof(eli));
5623
5624 if (megasas_get_seq_num(instance, &eli))
5625 return -1;
5626
5627 /*
5628 * Register AEN with FW for latest sequence number plus 1
5629 */
5630 class_locale.members.reserved = 0;
5631 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5632 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5633
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305634 return megasas_register_aen(instance,
Christoph Hellwig48100b02015-04-23 16:33:24 +05305635 le32_to_cpu(eli.newest_seq_num) + 1,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305636 class_locale.word);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005637}
5638
5639/**
5640 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5641 * @instance: Adapter soft state
5642 */
5643static int megasas_io_attach(struct megasas_instance *instance)
5644{
5645 struct Scsi_Host *host = instance->host;
5646
5647 /*
5648 * Export parameters required by SCSI mid-layer
5649 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005650 host->unique_id = instance->unique_id;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305651 host->can_queue = instance->max_scsi_cmds;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005652 host->this_id = instance->init_id;
5653 host->sg_tablesize = instance->max_num_sge;
adam radford42a8d2b2011-02-24 20:57:09 -08005654
5655 if (instance->fw_support_ieee)
5656 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5657
Yang, Bo1fd10682010-10-12 07:18:50 -06005658 /*
5659 * Check if the module parameter value for max_sectors can be used
5660 */
5661 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5662 instance->max_sectors_per_req = max_sectors;
5663 else {
5664 if (max_sectors) {
5665 if (((instance->pdev->device ==
5666 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5667 (instance->pdev->device ==
5668 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5669 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5670 instance->max_sectors_per_req = max_sectors;
5671 } else {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005672 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
Yang, Bo1fd10682010-10-12 07:18:50 -06005673 "and <= %d (or < 1MB for GEN2 controller)\n",
5674 instance->max_sectors_per_req);
5675 }
5676 }
5677 }
5678
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005679 host->max_sectors = instance->max_sectors_per_req;
adam radford9c915a82010-12-21 13:34:31 -08005680 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005681 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5682 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5683 host->max_lun = MEGASAS_MAX_LUN;
Joshua Giles122da302006-02-03 15:34:17 -08005684 host->max_cmd_len = 16;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005685
5686 /*
5687 * Notify the mid-layer about the new controller
5688 */
5689 if (scsi_add_host(host, &instance->pdev->dev)) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05305690 dev_err(&instance->pdev->dev,
5691 "Failed to add host from %s %d\n",
5692 __func__, __LINE__);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005693 return -ENODEV;
5694 }
5695
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005696 return 0;
5697}
5698
bo yang31ea7082007-11-07 12:09:50 -05005699static int
5700megasas_set_dma_mask(struct pci_dev *pdev)
5701{
5702 /*
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005703 * All our controllers are capable of performing 64-bit DMA
bo yang31ea7082007-11-07 12:09:50 -05005704 */
5705 if (IS_DMA64) {
Yang Hongyang6a355282009-04-06 19:01:13 -07005706 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
bo yang31ea7082007-11-07 12:09:50 -05005707
Yang Hongyang284901a2009-04-06 19:01:15 -07005708 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005709 goto fail_set_dma_mask;
5710 }
5711 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07005712 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
bo yang31ea7082007-11-07 12:09:50 -05005713 goto fail_set_dma_mask;
5714 }
Sumit.Saxena@lsi.com46de63e22014-02-12 23:38:44 +05305715 /*
5716 * Ensure that all data structures are allocated in 32-bit
5717 * memory.
5718 */
5719 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5720 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5721 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5722 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5723 dev_info(&pdev->dev, "set 32bit DMA mask"
5724 "and 32 bit consistent mask\n");
5725 else
5726 goto fail_set_dma_mask;
5727 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05305728
bo yang31ea7082007-11-07 12:09:50 -05005729 return 0;
5730
5731fail_set_dma_mask:
5732 return 1;
5733}
5734
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005735/**
5736 * megasas_probe_one - PCI hotplug entry point
5737 * @pdev: PCI device structure
adam radford0d490162010-12-14 19:17:17 -08005738 * @id: PCI ids of supported hotplugged adapter
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005739 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005740static int megasas_probe_one(struct pci_dev *pdev,
5741 const struct pci_device_id *id)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005742{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05305743 int rval, pos;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005744 struct Scsi_Host *host;
5745 struct megasas_instance *instance;
adam radford66192dfe2011-02-24 20:56:28 -08005746 u16 control = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305747 struct fusion_context *fusion = NULL;
adam radford66192dfe2011-02-24 20:56:28 -08005748
5749 /* Reset MSI-X in the kdump kernel */
5750 if (reset_devices) {
5751 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5752 if (pos) {
Bjorn Helgaas99369062013-04-17 18:08:44 -06005753 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005754 &control);
5755 if (control & PCI_MSIX_FLAGS_ENABLE) {
5756 dev_info(&pdev->dev, "resetting MSI-X\n");
5757 pci_write_config_word(pdev,
Bjorn Helgaas99369062013-04-17 18:08:44 -06005758 pos + PCI_MSIX_FLAGS,
adam radford66192dfe2011-02-24 20:56:28 -08005759 control &
5760 ~PCI_MSIX_FLAGS_ENABLE);
5761 }
5762 }
5763 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005764
5765 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005766 * PCI prepping: enable device set bus mastering and dma mask
5767 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09005768 rval = pci_enable_device_mem(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005769
5770 if (rval) {
5771 return rval;
5772 }
5773
5774 pci_set_master(pdev);
5775
bo yang31ea7082007-11-07 12:09:50 -05005776 if (megasas_set_dma_mask(pdev))
5777 goto fail_set_dma_mask;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005778
5779 host = scsi_host_alloc(&megasas_template,
5780 sizeof(struct megasas_instance));
5781
5782 if (!host) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005783 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005784 goto fail_alloc_instance;
5785 }
5786
5787 instance = (struct megasas_instance *)host->hostdata;
5788 memset(instance, 0, sizeof(*instance));
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05005789 atomic_set(&instance->fw_reset_no_pci_access, 0);
adam radford9c915a82010-12-21 13:34:31 -08005790 instance->pdev = pdev;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005791
adam radford9c915a82010-12-21 13:34:31 -08005792 switch (instance->pdev->device) {
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -05005793 case PCI_DEVICE_ID_LSI_VENTURA:
5794 case PCI_DEVICE_ID_LSI_HARPOON:
5795 case PCI_DEVICE_ID_LSI_TOMCAT:
5796 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
5797 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
5798 instance->is_ventura = true;
adam radford9c915a82010-12-21 13:34:31 -08005799 case PCI_DEVICE_ID_LSI_FUSION:
adam radford229fe472014-03-10 02:51:56 -07005800 case PCI_DEVICE_ID_LSI_PLASMA:
adam radford36807e62011-10-08 18:15:06 -07005801 case PCI_DEVICE_ID_LSI_INVADER:
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05305802 case PCI_DEVICE_ID_LSI_FURY:
sumit.saxena@avagotech.com90c204b2015-10-15 13:39:44 +05305803 case PCI_DEVICE_ID_LSI_INTRUDER:
5804 case PCI_DEVICE_ID_LSI_INTRUDER_24:
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05305805 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5806 case PCI_DEVICE_ID_LSI_CUTLASS_53:
adam radford9c915a82010-12-21 13:34:31 -08005807 {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305808 instance->ctrl_context_pages =
5809 get_order(sizeof(struct fusion_context));
5810 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5811 instance->ctrl_context_pages);
adam radford9c915a82010-12-21 13:34:31 -08005812 if (!instance->ctrl_context) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005813 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005814 "memory for Fusion context info\n");
5815 goto fail_alloc_dma_buf;
5816 }
5817 fusion = instance->ctrl_context;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05305818 memset(fusion, 0,
5819 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305820 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5821 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5822 fusion->adapter_type = THUNDERBOLT_SERIES;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05005823 else if (instance->is_ventura)
5824 fusion->adapter_type = VENTURA_SERIES;
5825 else
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305826 fusion->adapter_type = INVADER_SERIES;
adam radford9c915a82010-12-21 13:34:31 -08005827 }
5828 break;
5829 default: /* For all other supported controllers */
5830
5831 instance->producer =
5832 pci_alloc_consistent(pdev, sizeof(u32),
5833 &instance->producer_h);
5834 instance->consumer =
5835 pci_alloc_consistent(pdev, sizeof(u32),
5836 &instance->consumer_h);
5837
5838 if (!instance->producer || !instance->consumer) {
Colin Ian King4086eae52016-09-12 13:42:12 +01005839 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
adam radford9c915a82010-12-21 13:34:31 -08005840 "memory for producer, consumer\n");
5841 goto fail_alloc_dma_buf;
5842 }
5843
5844 *instance->producer = 0;
5845 *instance->consumer = 0;
5846 break;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005847 }
5848
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305849 /* Crash dump feature related initialisation*/
5850 instance->drv_buf_index = 0;
5851 instance->drv_buf_alloc = 0;
5852 instance->crash_dump_fw_support = 0;
5853 instance->crash_dump_app_support = 0;
5854 instance->fw_crash_state = UNAVAILABLE;
5855 spin_lock_init(&instance->crashdump_lock);
5856 instance->crash_dump_buf = NULL;
5857
Yang, Boc3518832009-10-06 14:18:02 -06005858 megasas_poll_wait_aen = 0;
Yang, Bof4c9a132009-10-06 14:43:28 -06005859 instance->flag_ieee = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06005860 instance->ev = NULL;
bo yang39a98552010-09-22 22:36:29 -04005861 instance->issuepend_done = 1;
Sumit Saxena8a01a412016-01-28 21:04:32 +05305862 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@lsi.com404a8a12013-05-22 12:35:33 +05305863 instance->is_imr = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005864
5865 instance->evt_detail = pci_alloc_consistent(pdev,
5866 sizeof(struct
5867 megasas_evt_detail),
5868 &instance->evt_detail_h);
5869
5870 if (!instance->evt_detail) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005871 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005872 "event detail structure\n");
5873 goto fail_alloc_dma_buf;
5874 }
5875
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005876 if (!reset_devices) {
5877 instance->system_info_buf = pci_zalloc_consistent(pdev,
5878 sizeof(struct MR_DRV_SYSTEM_INFO),
5879 &instance->system_info_h);
5880 if (!instance->system_info_buf)
5881 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
Sumit Saxena2216c302016-01-28 21:04:26 +05305882
Sumit Saxenac3e385a2016-04-15 00:23:30 -07005883 instance->pd_info = pci_alloc_consistent(pdev,
5884 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5885
5886 if (!instance->pd_info)
5887 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5888
5889 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5890 CRASH_DMA_BUF_SIZE,
5891 &instance->crash_dump_h);
5892 if (!instance->crash_dump_buf)
5893 dev_err(&pdev->dev, "Can't allocate Firmware "
5894 "crash dump DMA buffer\n");
5895 }
Sumit Saxena2216c302016-01-28 21:04:26 +05305896
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005897 /*
5898 * Initialize locks and queues
5899 */
5900 INIT_LIST_HEAD(&instance->cmd_pool);
bo yang39a98552010-09-22 22:36:29 -04005901 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005902
Sumant Patroe4a082c2006-05-30 12:03:37 -07005903 atomic_set(&instance->fw_outstanding,0);
5904
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005905 init_waitqueue_head(&instance->int_cmd_wait_q);
5906 init_waitqueue_head(&instance->abort_cmd_wait_q);
5907
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05305908 spin_lock_init(&instance->mfi_pool_lock);
bo yang39a98552010-09-22 22:36:29 -04005909 spin_lock_init(&instance->hba_lock);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05005910 spin_lock_init(&instance->stream_lock);
bo yang7343eb62007-11-09 04:35:44 -05005911 spin_lock_init(&instance->completion_lock);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005912
adam radford9c915a82010-12-21 13:34:31 -08005913 mutex_init(&instance->reset_mutex);
Sumit Saxena2216c302016-01-28 21:04:26 +05305914 mutex_init(&instance->hba_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005915
5916 /*
5917 * Initialize PCI related and misc parameters
5918 */
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005919 instance->host = host;
5920 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5921 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05305922 instance->ctrl_info = NULL;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005923
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305924
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005925 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05305926 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
Yang, Bof4c9a132009-10-06 14:43:28 -06005927 instance->flag_ieee = 1;
Yang, Bo7bebf5c2009-10-06 14:40:58 -06005928
Sumant Patro658dced2006-10-03 13:09:14 -07005929 megasas_dbg_lvl = 0;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005930 instance->flag = 0;
Yang, Bo0c79e682009-10-06 14:47:35 -06005931 instance->unload = 1;
Sumant Patro05e9ebb2007-05-17 05:47:51 -07005932 instance->last_time = 0;
bo yang39a98552010-09-22 22:36:29 -04005933 instance->disableOnlineCtrlReset = 1;
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05305934 instance->UnevenSpanSupport = 0;
bo yang39a98552010-09-22 22:36:29 -04005935
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05305936 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08005937 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05305938 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5939 } else
adam radford9c915a82010-12-21 13:34:31 -08005940 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
Sumant Patro658dced2006-10-03 13:09:14 -07005941
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005942 /*
adam radford0a770662011-02-24 20:56:12 -08005943 * Initialize MFI Firmware
5944 */
5945 if (megasas_init_fw(instance))
5946 goto fail_init_mfi;
5947
adam radford229fe472014-03-10 02:51:56 -07005948 if (instance->requestorId) {
5949 if (instance->PlasmaFW111) {
5950 instance->vf_affiliation_111 =
5951 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5952 &instance->vf_affiliation_111_h);
5953 if (!instance->vf_affiliation_111)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005954 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005955 "memory for VF affiliation buffer\n");
5956 } else {
5957 instance->vf_affiliation =
5958 pci_alloc_consistent(pdev,
5959 (MAX_LOGICAL_DRIVES + 1) *
5960 sizeof(struct MR_LD_VF_AFFILIATION),
5961 &instance->vf_affiliation_h);
5962 if (!instance->vf_affiliation)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005963 dev_warn(&pdev->dev, "Can't allocate "
adam radford229fe472014-03-10 02:51:56 -07005964 "memory for VF affiliation buffer\n");
5965 }
5966 }
5967
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005968 /*
5969 * Store instance in PCI softstate
5970 */
5971 pci_set_drvdata(pdev, instance);
5972
5973 /*
5974 * Add this controller to megasas_mgmt_info structure so that it
5975 * can be exported to management applications
5976 */
5977 megasas_mgmt_info.count++;
5978 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5979 megasas_mgmt_info.max_index++;
5980
5981 /*
adam radford541f90b2011-05-11 18:34:29 -07005982 * Register with SCSI mid-layer
5983 */
5984 if (megasas_io_attach(instance))
5985 goto fail_io_attach;
5986
5987 instance->unload = 0;
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05305988 /*
5989 * Trigger SCSI to scan our drives
5990 */
5991 scsi_scan_host(host);
adam radford541f90b2011-05-11 18:34:29 -07005992
5993 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005994 * Initiate AEN (Asynchronous Event Notification)
5995 */
5996 if (megasas_start_aen(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05005997 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04005998 goto fail_start_aen;
5999 }
6000
Adam Radford9ea81f82014-07-09 15:17:57 -07006001 /* Get current SR-IOV LD/VF affiliation */
6002 if (instance->requestorId)
6003 megasas_get_ld_vf_affiliation(instance, 1);
6004
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006005 return 0;
6006
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006007fail_start_aen:
6008fail_io_attach:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006009 megasas_mgmt_info.count--;
6010 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
6011 megasas_mgmt_info.max_index--;
6012
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306013 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306014 megasas_destroy_irqs(instance);
6015
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306016 if (instance->ctrl_context)
adam radfordeb1b1232011-02-24 20:55:56 -08006017 megasas_release_fusion(instance);
6018 else
6019 megasas_release_mfi(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006020 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006021 pci_free_irq_vectors(instance->pdev);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306022fail_init_mfi:
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006023fail_alloc_dma_buf:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006024 if (instance->evt_detail)
6025 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6026 instance->evt_detail,
6027 instance->evt_detail_h);
6028
Sumit Saxena2216c302016-01-28 21:04:26 +05306029 if (instance->pd_info)
6030 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6031 instance->pd_info,
6032 instance->pd_info_h);
adam radfordeb1b1232011-02-24 20:55:56 -08006033 if (instance->producer)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006034 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6035 instance->producer_h);
6036 if (instance->consumer)
6037 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6038 instance->consumer_h);
6039 scsi_host_put(host);
6040
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006041fail_alloc_instance:
6042fail_set_dma_mask:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006043 pci_disable_device(pdev);
6044
6045 return -ENODEV;
6046}
6047
6048/**
6049 * megasas_flush_cache - Requests FW to flush all its caches
6050 * @instance: Adapter soft state
6051 */
6052static void megasas_flush_cache(struct megasas_instance *instance)
6053{
6054 struct megasas_cmd *cmd;
6055 struct megasas_dcmd_frame *dcmd;
6056
Sumit Saxena8a01a412016-01-28 21:04:32 +05306057 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006058 return;
6059
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006060 cmd = megasas_get_cmd(instance);
6061
6062 if (!cmd)
6063 return;
6064
6065 dcmd = &cmd->frame->dcmd;
6066
6067 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6068
6069 dcmd->cmd = MFI_CMD_DCMD;
6070 dcmd->cmd_status = 0x0;
6071 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306072 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006073 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006074 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006075 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306076 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006077 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6078
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306079 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6080 != DCMD_SUCCESS) {
6081 dev_err(&instance->pdev->dev,
6082 "return from %s %d\n", __func__, __LINE__);
6083 return;
6084 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006085
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306086 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006087}
6088
6089/**
6090 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6091 * @instance: Adapter soft state
bo yang31ea7082007-11-07 12:09:50 -05006092 * @opcode: Shutdown/Hibernate
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006093 */
bo yang31ea7082007-11-07 12:09:50 -05006094static void megasas_shutdown_controller(struct megasas_instance *instance,
6095 u32 opcode)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006096{
6097 struct megasas_cmd *cmd;
6098 struct megasas_dcmd_frame *dcmd;
6099
Sumit Saxena8a01a412016-01-28 21:04:32 +05306100 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
bo yang39a98552010-09-22 22:36:29 -04006101 return;
6102
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006103 cmd = megasas_get_cmd(instance);
6104
6105 if (!cmd)
6106 return;
6107
6108 if (instance->aen_cmd)
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306109 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306110 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08006111 if (instance->map_update_cmd)
6112 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306113 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306114 if (instance->jbod_seq_cmd)
6115 megasas_issue_blocked_abort_cmd(instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306116 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306117
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006118 dcmd = &cmd->frame->dcmd;
6119
6120 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6121
6122 dcmd->cmd = MFI_CMD_DCMD;
6123 dcmd->cmd_status = 0x0;
6124 dcmd->sge_count = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306125 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006126 dcmd->timeout = 0;
Yang, Bo780a3762009-12-06 08:24:21 -07006127 dcmd->pad_0 = 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006128 dcmd->data_xfer_len = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306129 dcmd->opcode = cpu_to_le32(opcode);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006130
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306131 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6132 != DCMD_SUCCESS) {
6133 dev_err(&instance->pdev->dev,
6134 "return from %s %d\n", __func__, __LINE__);
6135 return;
6136 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006137
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306138 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006139}
6140
Jiri Slaby33139b22008-05-01 17:56:02 +02006141#ifdef CONFIG_PM
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006142/**
bo yangad84db22007-11-09 04:40:16 -05006143 * megasas_suspend - driver suspend entry point
6144 * @pdev: PCI device structure
bo yang31ea7082007-11-07 12:09:50 -05006145 * @state: PCI power state to suspend routine
6146 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006147static int
bo yang31ea7082007-11-07 12:09:50 -05006148megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6149{
6150 struct Scsi_Host *host;
6151 struct megasas_instance *instance;
6152
6153 instance = pci_get_drvdata(pdev);
6154 host = instance->host;
Yang, Bo0c79e682009-10-06 14:47:35 -06006155 instance->unload = 1;
bo yang31ea7082007-11-07 12:09:50 -05006156
adam radford229fe472014-03-10 02:51:56 -07006157 /* Shutdown SR-IOV heartbeat timer */
6158 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6159 del_timer_sync(&instance->sriov_heartbeat_timer);
6160
bo yang31ea7082007-11-07 12:09:50 -05006161 megasas_flush_cache(instance);
6162 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006163
6164 /* cancel the delayed work if this work still in queue */
6165 if (instance->ev != NULL) {
6166 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006167 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006168 instance->ev = NULL;
6169 }
6170
bo yang31ea7082007-11-07 12:09:50 -05006171 tasklet_kill(&instance->isr_tasklet);
6172
6173 pci_set_drvdata(instance->pdev, instance);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306174 instance->instancet->disable_intr(instance);
adam radfordc8e858f2011-10-08 18:15:13 -07006175
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306176 megasas_destroy_irqs(instance);
6177
adam radfordc8e858f2011-10-08 18:15:13 -07006178 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006179 pci_free_irq_vectors(instance->pdev);
bo yang31ea7082007-11-07 12:09:50 -05006180
6181 pci_save_state(pdev);
6182 pci_disable_device(pdev);
6183
6184 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6185
6186 return 0;
6187}
6188
6189/**
6190 * megasas_resume- driver resume entry point
6191 * @pdev: PCI device structure
6192 */
Jiri Slaby33139b22008-05-01 17:56:02 +02006193static int
bo yang31ea7082007-11-07 12:09:50 -05006194megasas_resume(struct pci_dev *pdev)
6195{
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306196 int rval;
bo yang31ea7082007-11-07 12:09:50 -05006197 struct Scsi_Host *host;
6198 struct megasas_instance *instance;
Hannes Reineckefad119b2016-12-02 12:52:23 +01006199 int irq_flags = PCI_IRQ_LEGACY;
bo yang31ea7082007-11-07 12:09:50 -05006200
6201 instance = pci_get_drvdata(pdev);
6202 host = instance->host;
6203 pci_set_power_state(pdev, PCI_D0);
6204 pci_enable_wake(pdev, PCI_D0, 0);
6205 pci_restore_state(pdev);
6206
6207 /*
6208 * PCI prepping: enable device set bus mastering and dma mask
6209 */
Noriyuki Fujiiaeab3fd2009-11-20 16:27:20 +09006210 rval = pci_enable_device_mem(pdev);
bo yang31ea7082007-11-07 12:09:50 -05006211
6212 if (rval) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006213 dev_err(&pdev->dev, "Enable device failed\n");
bo yang31ea7082007-11-07 12:09:50 -05006214 return rval;
6215 }
6216
6217 pci_set_master(pdev);
6218
6219 if (megasas_set_dma_mask(pdev))
6220 goto fail_set_dma_mask;
6221
6222 /*
6223 * Initialize MFI Firmware
6224 */
6225
bo yang31ea7082007-11-07 12:09:50 -05006226 atomic_set(&instance->fw_outstanding, 0);
6227
6228 /*
6229 * We expect the FW state to be READY
6230 */
adam radford058a8fa2011-10-08 18:14:27 -07006231 if (megasas_transition_to_ready(instance, 0))
bo yang31ea7082007-11-07 12:09:50 -05006232 goto fail_ready_state;
6233
adam radford3f1abce2011-05-11 18:33:47 -07006234 /* Now re-enable MSI-X */
Hannes Reineckefad119b2016-12-02 12:52:23 +01006235 if (instance->msix_vectors) {
6236 irq_flags = PCI_IRQ_MSIX;
6237 if (smp_affinity_enable)
6238 irq_flags |= PCI_IRQ_AFFINITY;
6239 }
6240 rval = pci_alloc_irq_vectors(instance->pdev, 1,
6241 instance->msix_vectors ?
6242 instance->msix_vectors : 1, irq_flags);
6243 if (rval < 0)
Alexander Gordeevdd088122014-08-18 08:01:43 +02006244 goto fail_reenable_msix;
adam radford3f1abce2011-05-11 18:33:47 -07006245
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306246 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006247 megasas_reset_reply_desc(instance);
6248 if (megasas_ioc_init_fusion(instance)) {
6249 megasas_free_cmds(instance);
6250 megasas_free_cmds_fusion(instance);
6251 goto fail_init_mfi;
6252 }
6253 if (!megasas_get_map_info(instance))
6254 megasas_sync_map_info(instance);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306255 } else {
adam radford9c915a82010-12-21 13:34:31 -08006256 *instance->producer = 0;
6257 *instance->consumer = 0;
6258 if (megasas_issue_init_mfi(instance))
6259 goto fail_init_mfi;
adam radford9c915a82010-12-21 13:34:31 -08006260 }
bo yang31ea7082007-11-07 12:09:50 -05006261
adam radford9c915a82010-12-21 13:34:31 -08006262 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6263 (unsigned long)instance);
bo yang31ea7082007-11-07 12:09:50 -05006264
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306265 if (instance->msix_vectors ?
6266 megasas_setup_irqs_msix(instance, 0) :
6267 megasas_setup_irqs_ioapic(instance))
6268 goto fail_init_mfi;
bo yang31ea7082007-11-07 12:09:50 -05006269
adam radford229fe472014-03-10 02:51:56 -07006270 /* Re-launch SR-IOV heartbeat timer */
6271 if (instance->requestorId) {
6272 if (!megasas_sriov_start_heartbeat(instance, 0))
6273 megasas_start_timer(instance,
6274 &instance->sriov_heartbeat_timer,
6275 megasas_sriov_heartbeat_handler,
6276 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306277 else {
adam radford229fe472014-03-10 02:51:56 -07006278 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306279 goto fail_init_mfi;
6280 }
adam radford229fe472014-03-10 02:51:56 -07006281 }
6282
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306283 instance->instancet->enable_intr(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306284 megasas_setup_jbod_map(instance);
Yang, Bo0c79e682009-10-06 14:47:35 -06006285 instance->unload = 0;
6286
adam radford541f90b2011-05-11 18:34:29 -07006287 /*
6288 * Initiate AEN (Asynchronous Event Notification)
6289 */
6290 if (megasas_start_aen(instance))
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006291 dev_err(&instance->pdev->dev, "Start AEN failed\n");
adam radford541f90b2011-05-11 18:34:29 -07006292
bo yang31ea7082007-11-07 12:09:50 -05006293 return 0;
6294
bo yang31ea7082007-11-07 12:09:50 -05006295fail_init_mfi:
6296 if (instance->evt_detail)
6297 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6298 instance->evt_detail,
6299 instance->evt_detail_h);
6300
Sumit Saxena2216c302016-01-28 21:04:26 +05306301 if (instance->pd_info)
6302 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6303 instance->pd_info,
6304 instance->pd_info_h);
bo yang31ea7082007-11-07 12:09:50 -05006305 if (instance->producer)
6306 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6307 instance->producer_h);
6308 if (instance->consumer)
6309 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6310 instance->consumer_h);
6311 scsi_host_put(host);
6312
6313fail_set_dma_mask:
6314fail_ready_state:
Alexander Gordeevdd088122014-08-18 08:01:43 +02006315fail_reenable_msix:
bo yang31ea7082007-11-07 12:09:50 -05006316
6317 pci_disable_device(pdev);
6318
6319 return -ENODEV;
6320}
Jiri Slaby33139b22008-05-01 17:56:02 +02006321#else
6322#define megasas_suspend NULL
6323#define megasas_resume NULL
6324#endif
bo yang31ea7082007-11-07 12:09:50 -05006325
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006326static inline int
6327megasas_wait_for_adapter_operational(struct megasas_instance *instance)
6328{
6329 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
6330 int i;
6331
6332 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6333 return 1;
6334
6335 for (i = 0; i < wait_time; i++) {
6336 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
6337 break;
6338
6339 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
6340 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
6341
6342 msleep(1000);
6343 }
6344
6345 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6346 dev_info(&instance->pdev->dev, "%s timed out while waiting for HBA to recover.\n",
6347 __func__);
6348 return 1;
6349 }
6350
6351 return 0;
6352}
6353
bo yang31ea7082007-11-07 12:09:50 -05006354/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006355 * megasas_detach_one - PCI hot"un"plug entry point
6356 * @pdev: PCI device structure
6357 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08006358static void megasas_detach_one(struct pci_dev *pdev)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006359{
6360 int i;
6361 struct Scsi_Host *host;
6362 struct megasas_instance *instance;
adam radford9c915a82010-12-21 13:34:31 -08006363 struct fusion_context *fusion;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306364 u32 pd_seq_map_sz;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006365
6366 instance = pci_get_drvdata(pdev);
Yang, Boc3518832009-10-06 14:18:02 -06006367 instance->unload = 1;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006368 host = instance->host;
adam radford9c915a82010-12-21 13:34:31 -08006369 fusion = instance->ctrl_context;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006370
adam radford229fe472014-03-10 02:51:56 -07006371 /* Shutdown SR-IOV heartbeat timer */
6372 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6373 del_timer_sync(&instance->sriov_heartbeat_timer);
6374
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306375 if (instance->fw_crash_state != UNAVAILABLE)
6376 megasas_free_host_crash_buffer(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006377 scsi_remove_host(instance->host);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006378
6379 if (megasas_wait_for_adapter_operational(instance))
6380 goto skip_firing_dcmds;
6381
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006382 megasas_flush_cache(instance);
bo yang31ea7082007-11-07 12:09:50 -05006383 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006384
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006385skip_firing_dcmds:
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006386 /* cancel the delayed work if this work still in queue*/
6387 if (instance->ev != NULL) {
6388 struct megasas_aen_event *ev = instance->ev;
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08006389 cancel_delayed_work_sync(&ev->hotplug_work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06006390 instance->ev = NULL;
6391 }
6392
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05306393 /* cancel all wait events */
6394 wake_up_all(&instance->int_cmd_wait_q);
6395
Sumant Patro5d018ad2006-10-03 13:13:18 -07006396 tasklet_kill(&instance->isr_tasklet);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006397
6398 /*
6399 * Take the instance off the instance array. Note that we will not
6400 * decrement the max_index. We let this array be sparse array
6401 */
6402 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6403 if (megasas_mgmt_info.instance[i] == instance) {
6404 megasas_mgmt_info.count--;
6405 megasas_mgmt_info.instance[i] = NULL;
6406
6407 break;
6408 }
6409 }
6410
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306411 instance->instancet->disable_intr(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006412
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306413 megasas_destroy_irqs(instance);
6414
adam radfordc8e858f2011-10-08 18:15:13 -07006415 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006416 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006417
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05006418 if (instance->is_ventura) {
6419 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
6420 kfree(fusion->stream_detect_by_ld[i]);
6421 kfree(fusion->stream_detect_by_ld);
6422 fusion->stream_detect_by_ld = NULL;
6423 }
6424
6425
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306426 if (instance->ctrl_context) {
adam radford9c915a82010-12-21 13:34:31 -08006427 megasas_release_fusion(instance);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05306428 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6429 (sizeof(struct MR_PD_CFG_SEQ) *
6430 (MAX_PHYSICAL_DEVICES - 1));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306431 for (i = 0; i < 2 ; i++) {
adam radford9c915a82010-12-21 13:34:31 -08006432 if (fusion->ld_map[i])
6433 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306434 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08006435 fusion->ld_map[i],
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306436 fusion->ld_map_phys[i]);
6437 if (fusion->ld_drv_map[i])
6438 free_pages((ulong)fusion->ld_drv_map[i],
6439 fusion->drv_map_pages);
Maurizio Lombardi546e5592016-01-22 13:41:42 +01006440 if (fusion->pd_seq_sync[i])
6441 dma_free_coherent(&instance->pdev->dev,
6442 pd_seq_map_sz,
6443 fusion->pd_seq_sync[i],
6444 fusion->pd_seq_phys[i]);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306445 }
6446 free_pages((ulong)instance->ctrl_context,
6447 instance->ctrl_context_pages);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05306448 } else {
adam radford9c915a82010-12-21 13:34:31 -08006449 megasas_release_mfi(instance);
adam radford9c915a82010-12-21 13:34:31 -08006450 pci_free_consistent(pdev, sizeof(u32),
6451 instance->producer,
6452 instance->producer_h);
6453 pci_free_consistent(pdev, sizeof(u32),
6454 instance->consumer,
6455 instance->consumer_h);
adam radford9c915a82010-12-21 13:34:31 -08006456 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006457
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306458 kfree(instance->ctrl_info);
6459
Sumit.Saxena@lsi.com105900d2013-05-22 12:30:54 +05306460 if (instance->evt_detail)
6461 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6462 instance->evt_detail, instance->evt_detail_h);
adam radford229fe472014-03-10 02:51:56 -07006463
Sumit Saxena2216c302016-01-28 21:04:26 +05306464 if (instance->pd_info)
6465 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6466 instance->pd_info,
6467 instance->pd_info_h);
adam radford229fe472014-03-10 02:51:56 -07006468 if (instance->vf_affiliation)
6469 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6470 sizeof(struct MR_LD_VF_AFFILIATION),
6471 instance->vf_affiliation,
6472 instance->vf_affiliation_h);
6473
6474 if (instance->vf_affiliation_111)
6475 pci_free_consistent(pdev,
6476 sizeof(struct MR_LD_VF_AFFILIATION_111),
6477 instance->vf_affiliation_111,
6478 instance->vf_affiliation_111_h);
6479
6480 if (instance->hb_host_mem)
6481 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6482 instance->hb_host_mem,
6483 instance->hb_host_mem_h);
6484
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306485 if (instance->crash_dump_buf)
6486 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6487 instance->crash_dump_buf, instance->crash_dump_h);
6488
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05306489 if (instance->system_info_buf)
6490 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6491 instance->system_info_buf, instance->system_info_h);
6492
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006493 scsi_host_put(host);
6494
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006495 pci_disable_device(pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006496}
6497
6498/**
6499 * megasas_shutdown - Shutdown entry point
6500 * @device: Generic device structure
6501 */
6502static void megasas_shutdown(struct pci_dev *pdev)
6503{
6504 struct megasas_instance *instance = pci_get_drvdata(pdev);
adam radfordc8e858f2011-10-08 18:15:13 -07006505
Yang, Bo0c79e682009-10-06 14:47:35 -06006506 instance->unload = 1;
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006507
6508 if (megasas_wait_for_adapter_operational(instance))
6509 goto skip_firing_dcmds;
6510
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006511 megasas_flush_cache(instance);
Yang, Bo530e6fc2008-08-10 12:42:37 -07006512 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07006513
6514skip_firing_dcmds:
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05306515 instance->instancet->disable_intr(instance);
Sumit.Saxena@avagotech.comd3557fc2015-04-23 16:30:09 +05306516 megasas_destroy_irqs(instance);
6517
adam radfordc8e858f2011-10-08 18:15:13 -07006518 if (instance->msix_vectors)
Hannes Reineckefad119b2016-12-02 12:52:23 +01006519 pci_free_irq_vectors(instance->pdev);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006520}
6521
6522/**
6523 * megasas_mgmt_open - char node "open" entry point
6524 */
6525static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6526{
6527 /*
6528 * Allow only those users with admin rights
6529 */
6530 if (!capable(CAP_SYS_ADMIN))
6531 return -EACCES;
6532
6533 return 0;
6534}
6535
6536/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006537 * megasas_mgmt_fasync - Async notifier registration from applications
6538 *
6539 * This function adds the calling process to a driver global queue. When an
6540 * event occurs, SIGIO will be sent to all processes in this queue.
6541 */
6542static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6543{
6544 int rc;
6545
Arjan van de Ven0b950672006-01-11 13:16:10 +01006546 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006547
6548 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6549
Arjan van de Ven0b950672006-01-11 13:16:10 +01006550 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006551
6552 if (rc >= 0) {
6553 /* For sanity check when we get ioctl */
6554 filep->private_data = filep;
6555 return 0;
6556 }
6557
6558 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6559
6560 return rc;
6561}
6562
6563/**
Yang, Boc3518832009-10-06 14:18:02 -06006564 * megasas_mgmt_poll - char node "poll" entry point
6565 * */
6566static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6567{
6568 unsigned int mask;
6569 unsigned long flags;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006570
Yang, Boc3518832009-10-06 14:18:02 -06006571 poll_wait(file, &megasas_poll_wait, wait);
6572 spin_lock_irqsave(&poll_aen_lock, flags);
6573 if (megasas_poll_wait_aen)
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006574 mask = (POLLIN | POLLRDNORM);
Yang, Boc3518832009-10-06 14:18:02 -06006575 else
6576 mask = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05306577 megasas_poll_wait_aen = 0;
Yang, Boc3518832009-10-06 14:18:02 -06006578 spin_unlock_irqrestore(&poll_aen_lock, flags);
6579 return mask;
6580}
6581
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306582/*
6583 * megasas_set_crash_dump_params_ioctl:
6584 * Send CRASH_DUMP_MODE DCMD to all controllers
6585 * @cmd: MFI command frame
6586 */
6587
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006588static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306589{
6590 struct megasas_instance *local_instance;
6591 int i, error = 0;
6592 int crash_support;
6593
6594 crash_support = cmd->frame->dcmd.mbox.w[0];
6595
6596 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6597 local_instance = megasas_mgmt_info.instance[i];
6598 if (local_instance && local_instance->crash_dump_drv_support) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05306599 if ((atomic_read(&local_instance->adprecovery) ==
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306600 MEGASAS_HBA_OPERATIONAL) &&
6601 !megasas_set_crash_dump_params(local_instance,
6602 crash_support)) {
6603 local_instance->crash_dump_app_support =
6604 crash_support;
6605 dev_info(&local_instance->pdev->dev,
6606 "Application firmware crash "
6607 "dump mode set success\n");
6608 error = 0;
6609 } else {
6610 dev_info(&local_instance->pdev->dev,
6611 "Application firmware crash "
6612 "dump mode set failed\n");
6613 error = -1;
6614 }
6615 }
6616 }
6617 return error;
6618}
6619
Yang, Boc3518832009-10-06 14:18:02 -06006620/**
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006621 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6622 * @instance: Adapter soft state
6623 * @argp: User's ioctl packet
6624 */
6625static int
6626megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6627 struct megasas_iocpacket __user * user_ioc,
6628 struct megasas_iocpacket *ioc)
6629{
6630 struct megasas_sge32 *kern_sge32;
6631 struct megasas_cmd *cmd;
6632 void *kbuff_arr[MAX_IOCTL_SGE];
6633 dma_addr_t buf_handle = 0;
6634 int error = 0, i;
6635 void *sense = NULL;
6636 dma_addr_t sense_handle;
Yang, Bo7b2519a2009-10-06 14:52:20 -06006637 unsigned long *sense_ptr;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006638
6639 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6640
6641 if (ioc->sge_count > MAX_IOCTL_SGE) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006642 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006643 ioc->sge_count, MAX_IOCTL_SGE);
6644 return -EINVAL;
6645 }
6646
6647 cmd = megasas_get_cmd(instance);
6648 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006649 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006650 return -ENOMEM;
6651 }
6652
6653 /*
6654 * User's IOCTL packet has 2 frames (maximum). Copy those two
6655 * frames into our cmd's frames. cmd->frame's context will get
6656 * overwritten when we copy from user's frames. So set that value
6657 * alone separately
6658 */
6659 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306660 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
Yang, Boc3518832009-10-06 14:18:02 -06006661 cmd->frame->hdr.pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306662 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6663 MFI_FRAME_SGL64 |
6664 MFI_FRAME_SENSE64));
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006665
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05306666 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6667 error = megasas_set_crash_dump_params_ioctl(cmd);
6668 megasas_return_cmd(instance, cmd);
6669 return error;
6670 }
6671
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006672 /*
6673 * The management interface between applications and the fw uses
6674 * MFI frames. E.g, RAID configuration changes, LD property changes
6675 * etc are accomplishes through different kinds of MFI frames. The
6676 * driver needs to care only about substituting user buffers with
6677 * kernel buffers in SGLs. The location of SGL is embedded in the
6678 * struct iocpacket itself.
6679 */
6680 kern_sge32 = (struct megasas_sge32 *)
6681 ((unsigned long)cmd->frame + ioc->sgl_off);
6682
6683 /*
6684 * For each user buffer, create a mirror buffer and copy in
6685 */
6686 for (i = 0; i < ioc->sge_count; i++) {
Bjørn Mork98cb7e42011-01-19 10:01:14 +01006687 if (!ioc->sgl[i].iov_len)
6688 continue;
6689
Sumant Patro9f35fa82007-02-14 12:55:45 -08006690 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006691 ioc->sgl[i].iov_len,
Sumant Patro9f35fa82007-02-14 12:55:45 -08006692 &buf_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006693 if (!kbuff_arr[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006694 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6695 "kernel SGL buffer for IOCTL\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006696 error = -ENOMEM;
6697 goto out;
6698 }
6699
6700 /*
6701 * We don't change the dma_coherent_mask, so
6702 * pci_alloc_consistent only returns 32bit addresses
6703 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306704 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6705 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006706
6707 /*
6708 * We created a kernel buffer corresponding to the
6709 * user buffer. Now copy in from the user buffer
6710 */
6711 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6712 (u32) (ioc->sgl[i].iov_len))) {
6713 error = -EFAULT;
6714 goto out;
6715 }
6716 }
6717
6718 if (ioc->sense_len) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006719 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6720 &sense_handle, GFP_KERNEL);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006721 if (!sense) {
6722 error = -ENOMEM;
6723 goto out;
6724 }
6725
6726 sense_ptr =
Yang, Bo7b2519a2009-10-06 14:52:20 -06006727 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306728 *sense_ptr = cpu_to_le32(sense_handle);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006729 }
6730
6731 /*
6732 * Set the sync_cmd flag so that the ISR knows not to complete this
6733 * cmd to the SCSI mid-layer
6734 */
6735 cmd->sync_cmd = 1;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05306736 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6737 cmd->sync_cmd = 0;
6738 dev_err(&instance->pdev->dev,
6739 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6740 __func__, __LINE__, cmd->frame->dcmd.opcode,
6741 cmd->cmd_status_drv);
6742 return -EBUSY;
6743 }
6744
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006745 cmd->sync_cmd = 0;
6746
Sumit.Saxena@avagotech.comaa008322014-11-17 15:24:08 +05306747 if (instance->unload == 1) {
6748 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6749 "don't submit data to application\n");
6750 goto out;
6751 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006752 /*
6753 * copy out the kernel buffers to user buffers
6754 */
6755 for (i = 0; i < ioc->sge_count; i++) {
6756 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6757 ioc->sgl[i].iov_len)) {
6758 error = -EFAULT;
6759 goto out;
6760 }
6761 }
6762
6763 /*
6764 * copy out the sense
6765 */
6766 if (ioc->sense_len) {
6767 /*
bo yangb70a41e2008-03-18 03:13:06 -04006768 * sense_ptr points to the location that has the user
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006769 * sense buffer address
6770 */
Yang, Bo7b2519a2009-10-06 14:52:20 -06006771 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6772 ioc->sense_off);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006773
bo yangb70a41e2008-03-18 03:13:06 -04006774 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6775 sense, ioc->sense_len)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006776 dev_err(&instance->pdev->dev, "Failed to copy out to user "
bo yangb10c36a2007-11-09 04:28:47 -05006777 "sense data\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006778 error = -EFAULT;
6779 goto out;
6780 }
6781 }
6782
6783 /*
6784 * copy the status codes returned by the fw
6785 */
6786 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6787 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006788 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006789 error = -EFAULT;
6790 }
6791
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006792out:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006793 if (sense) {
Sumant Patro9f35fa82007-02-14 12:55:45 -08006794 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006795 sense, sense_handle);
6796 }
6797
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006798 for (i = 0; i < ioc->sge_count; i++) {
Arnd Bergmann3deb9432016-03-14 15:29:45 +01006799 if (kbuff_arr[i]) {
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006800 dma_free_coherent(&instance->pdev->dev,
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306801 le32_to_cpu(kern_sge32[i].length),
Bjørn Mork7a6a7312012-11-21 09:54:48 +01006802 kbuff_arr[i],
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05306803 le32_to_cpu(kern_sge32[i].phys_addr));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05306804 kbuff_arr[i] = NULL;
Arnd Bergmann3deb9432016-03-14 15:29:45 +01006805 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006806 }
6807
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05306808 megasas_return_cmd(instance, cmd);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006809 return error;
6810}
6811
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006812static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6813{
6814 struct megasas_iocpacket __user *user_ioc =
6815 (struct megasas_iocpacket __user *)arg;
6816 struct megasas_iocpacket *ioc;
6817 struct megasas_instance *instance;
6818 int error;
bo yang39a98552010-09-22 22:36:29 -04006819 int i;
6820 unsigned long flags;
6821 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006822
Markus Elfring709ab232016-08-21 10:39:04 +02006823 ioc = memdup_user(user_ioc, sizeof(*ioc));
6824 if (IS_ERR(ioc))
6825 return PTR_ERR(ioc);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006826
6827 instance = megasas_lookup_instance(ioc->host_no);
6828 if (!instance) {
6829 error = -ENODEV;
6830 goto out_kfree_ioc;
6831 }
6832
adam radford229fe472014-03-10 02:51:56 -07006833 /* Adjust ioctl wait time for VF mode */
6834 if (instance->requestorId)
6835 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6836
6837 /* Block ioctls in VF mode */
6838 if (instance->requestorId && !allow_vf_ioctls) {
6839 error = -ENODEV;
6840 goto out_kfree_ioc;
6841 }
6842
Sumit Saxena8a01a412016-01-28 21:04:32 +05306843 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006844 dev_err(&instance->pdev->dev, "Controller in crit error\n");
Yang, Bo0c79e682009-10-06 14:47:35 -06006845 error = -ENODEV;
6846 goto out_kfree_ioc;
6847 }
6848
6849 if (instance->unload == 1) {
6850 error = -ENODEV;
6851 goto out_kfree_ioc;
6852 }
6853
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006854 if (down_interruptible(&instance->ioctl_sem)) {
6855 error = -ERESTARTSYS;
6856 goto out_kfree_ioc;
6857 }
bo yang39a98552010-09-22 22:36:29 -04006858
6859 for (i = 0; i < wait_time; i++) {
6860
6861 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306862 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006863 spin_unlock_irqrestore(&instance->hba_lock, flags);
6864 break;
6865 }
6866 spin_unlock_irqrestore(&instance->hba_lock, flags);
6867
6868 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006869 dev_notice(&instance->pdev->dev, "waiting"
bo yang39a98552010-09-22 22:36:29 -04006870 "for controller reset to finish\n");
6871 }
6872
6873 msleep(1000);
6874 }
6875
6876 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306877 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006878 spin_unlock_irqrestore(&instance->hba_lock, flags);
6879
Colin Ian King644da3c2016-11-12 16:25:24 +00006880 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006881 error = -ENODEV;
Dan Carpenterc64e4832013-04-16 10:44:19 +03006882 goto out_up;
bo yang39a98552010-09-22 22:36:29 -04006883 }
6884 spin_unlock_irqrestore(&instance->hba_lock, flags);
6885
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006886 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006887out_up:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006888 up(&instance->ioctl_sem);
6889
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05006890out_kfree_ioc:
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006891 kfree(ioc);
6892 return error;
6893}
6894
6895static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6896{
6897 struct megasas_instance *instance;
6898 struct megasas_aen aen;
6899 int error;
bo yang39a98552010-09-22 22:36:29 -04006900 int i;
6901 unsigned long flags;
6902 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006903
6904 if (file->private_data != file) {
6905 printk(KERN_DEBUG "megasas: fasync_helper was not "
6906 "called first\n");
6907 return -EINVAL;
6908 }
6909
6910 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6911 return -EFAULT;
6912
6913 instance = megasas_lookup_instance(aen.host_no);
6914
6915 if (!instance)
6916 return -ENODEV;
6917
Sumit Saxena8a01a412016-01-28 21:04:32 +05306918 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
bo yang39a98552010-09-22 22:36:29 -04006919 return -ENODEV;
Yang, Bo0c79e682009-10-06 14:47:35 -06006920 }
6921
6922 if (instance->unload == 1) {
6923 return -ENODEV;
6924 }
6925
bo yang39a98552010-09-22 22:36:29 -04006926 for (i = 0; i < wait_time; i++) {
6927
6928 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306929 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006930 spin_unlock_irqrestore(&instance->hba_lock,
6931 flags);
6932 break;
6933 }
6934
6935 spin_unlock_irqrestore(&instance->hba_lock, flags);
6936
6937 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05006938 dev_notice(&instance->pdev->dev, "waiting for"
bo yang39a98552010-09-22 22:36:29 -04006939 "controller reset to finish\n");
6940 }
6941
6942 msleep(1000);
6943 }
6944
6945 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05306946 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
bo yang39a98552010-09-22 22:36:29 -04006947 spin_unlock_irqrestore(&instance->hba_lock, flags);
Colin Ian King644da3c2016-11-12 16:25:24 +00006948 dev_err(&instance->pdev->dev, "timed out while waiting for HBA to recover\n");
bo yang39a98552010-09-22 22:36:29 -04006949 return -ENODEV;
6950 }
6951 spin_unlock_irqrestore(&instance->hba_lock, flags);
6952
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306953 mutex_lock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006954 error = megasas_register_aen(instance, aen.seq_num,
6955 aen.class_locale_word);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05306956 mutex_unlock(&instance->reset_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006957 return error;
6958}
6959
6960/**
6961 * megasas_mgmt_ioctl - char node ioctl entry point
6962 */
6963static long
6964megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6965{
6966 switch (cmd) {
6967 case MEGASAS_IOC_FIRMWARE:
6968 return megasas_mgmt_ioctl_fw(file, arg);
6969
6970 case MEGASAS_IOC_GET_AEN:
6971 return megasas_mgmt_ioctl_aen(file, arg);
6972 }
6973
6974 return -ENOTTY;
6975}
6976
6977#ifdef CONFIG_COMPAT
6978static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6979{
6980 struct compat_megasas_iocpacket __user *cioc =
6981 (struct compat_megasas_iocpacket __user *)arg;
6982 struct megasas_iocpacket __user *ioc =
6983 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6984 int i;
6985 int error = 0;
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01006986 compat_uptr_t ptr;
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05306987 u32 local_sense_off;
6988 u32 local_sense_len;
Sumit Saxenaea1c9282016-01-28 21:14:26 +05306989 u32 user_sense_off;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006990
Jeff Garzik83aabc12006-10-04 06:34:03 -04006991 if (clear_user(ioc, sizeof(*ioc)))
6992 return -EFAULT;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04006993
6994 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6995 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6996 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6997 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6998 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6999 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7000 return -EFAULT;
7001
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007002 /*
7003 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7004 * sense_len is not null, so prepare the 64bit value under
7005 * the same condition.
7006 */
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307007 if (get_user(local_sense_off, &ioc->sense_off) ||
7008 get_user(local_sense_len, &ioc->sense_len) ||
7009 get_user(user_sense_off, &cioc->sense_off))
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307010 return -EFAULT;
7011
sumit.saxena@avagotech.com323c4a02015-10-15 13:40:54 +05307012 if (local_sense_len) {
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007013 void __user **sense_ioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307014 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007015 compat_uptr_t *sense_cioc_ptr =
Sumit Saxenaea1c9282016-01-28 21:14:26 +05307016 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007017 if (get_user(ptr, sense_cioc_ptr) ||
7018 put_user(compat_ptr(ptr), sense_ioc_ptr))
7019 return -EFAULT;
7020 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007021
Tomas Henzlb3dc1a22010-02-11 18:01:50 +01007022 for (i = 0; i < MAX_IOCTL_SGE; i++) {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007023 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
7024 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
7025 copy_in_user(&ioc->sgl[i].iov_len,
7026 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
7027 return -EFAULT;
7028 }
7029
7030 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
7031
7032 if (copy_in_user(&cioc->frame.hdr.cmd_status,
7033 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
7034 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
7035 return -EFAULT;
7036 }
7037 return error;
7038}
7039
7040static long
7041megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
7042 unsigned long arg)
7043{
7044 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08007045 case MEGASAS_IOC_FIRMWARE32:
7046 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007047 case MEGASAS_IOC_GET_AEN:
7048 return megasas_mgmt_ioctl_aen(file, arg);
7049 }
7050
7051 return -ENOTTY;
7052}
7053#endif
7054
7055/*
7056 * File operations structure for management interface
7057 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08007058static const struct file_operations megasas_mgmt_fops = {
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007059 .owner = THIS_MODULE,
7060 .open = megasas_mgmt_open,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007061 .fasync = megasas_mgmt_fasync,
7062 .unlocked_ioctl = megasas_mgmt_ioctl,
Yang, Boc3518832009-10-06 14:18:02 -06007063 .poll = megasas_mgmt_poll,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007064#ifdef CONFIG_COMPAT
7065 .compat_ioctl = megasas_mgmt_compat_ioctl,
7066#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02007067 .llseek = noop_llseek,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007068};
7069
7070/*
7071 * PCI hotplug support registration structure
7072 */
7073static struct pci_driver megasas_pci_driver = {
7074
7075 .name = "megaraid_sas",
7076 .id_table = megasas_pci_table,
7077 .probe = megasas_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007078 .remove = megasas_detach_one,
bo yang31ea7082007-11-07 12:09:50 -05007079 .suspend = megasas_suspend,
7080 .resume = megasas_resume,
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007081 .shutdown = megasas_shutdown,
7082};
7083
7084/*
7085 * Sysfs driver attributes
7086 */
7087static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
7088{
7089 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
7090 MEGASAS_VERSION);
7091}
7092
7093static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
7094
7095static ssize_t
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307096megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
7097{
7098 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
7099 MEGASAS_RELDATE);
7100}
7101
7102static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
7103
7104static ssize_t
Yang, Bo72c4fd32009-10-06 14:20:59 -06007105megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
7106{
7107 return sprintf(buf, "%u\n", support_poll_for_event);
7108}
7109
7110static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
7111 megasas_sysfs_show_support_poll_for_event, NULL);
7112
Yang, Bo837f5fe2010-10-11 06:59:20 -06007113 static ssize_t
7114megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
7115{
7116 return sprintf(buf, "%u\n", support_device_change);
7117}
7118
7119static DRIVER_ATTR(support_device_change, S_IRUGO,
7120 megasas_sysfs_show_support_device_change, NULL);
7121
Yang, Bo72c4fd32009-10-06 14:20:59 -06007122static ssize_t
Sumant Patro658dced2006-10-03 13:09:14 -07007123megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7124{
bo yangad84db22007-11-09 04:40:16 -05007125 return sprintf(buf, "%u\n", megasas_dbg_lvl);
Sumant Patro658dced2006-10-03 13:09:14 -07007126}
7127
7128static ssize_t
7129megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7130{
7131 int retval = count;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007132
7133 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
Sumant Patro658dced2006-10-03 13:09:14 -07007134 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7135 retval = -EINVAL;
7136 }
7137 return retval;
7138}
7139
Joe Malicki66dca9b2008-08-14 17:14:48 -04007140static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
bo yangad84db22007-11-09 04:40:16 -05007141 megasas_sysfs_set_dbg_lvl);
7142
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007143static void
7144megasas_aen_polling(struct work_struct *work)
7145{
7146 struct megasas_aen_event *ev =
Xiaotian Fengc1d390d82012-12-04 19:33:54 +08007147 container_of(work, struct megasas_aen_event, hotplug_work.work);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007148 struct megasas_instance *instance = ev->instance;
7149 union megasas_evt_class_locale class_locale;
7150 struct Scsi_Host *host;
7151 struct scsi_device *sdev1;
7152 u16 pd_index = 0;
Yang, Boc9786842009-12-06 08:39:25 -07007153 u16 ld_index = 0;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007154 int i, j, doscan = 0;
adam radford229fe472014-03-10 02:51:56 -07007155 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007156 int error;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307157 u8 dcmd_ret = DCMD_SUCCESS;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007158
7159 if (!instance) {
7160 printk(KERN_ERR "invalid instance!\n");
7161 kfree(ev);
7162 return;
7163 }
adam radford229fe472014-03-10 02:51:56 -07007164
7165 /* Adjust event workqueue thread wait time for VF mode */
7166 if (instance->requestorId)
7167 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7168
7169 /* Don't run the event workqueue thread if OCR is running */
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307170 mutex_lock(&instance->reset_mutex);
adam radford229fe472014-03-10 02:51:56 -07007171
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007172 instance->ev = NULL;
7173 host = instance->host;
7174 if (instance->evt_detail) {
sumit.saxena@avagotech.com714f5172015-08-31 17:23:51 +05307175 megasas_decode_evt(instance);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007176
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05307177 switch (le32_to_cpu(instance->evt_detail->code)) {
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307178
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007179 case MR_EVT_PD_INSERTED:
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307180 case MR_EVT_PD_REMOVED:
7181 dcmd_ret = megasas_get_pd_list(instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307182 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307183 doscan = SCAN_PD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007184 break;
7185
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307186 case MR_EVT_LD_OFFLINE:
7187 case MR_EVT_CFG_CLEARED:
7188 case MR_EVT_LD_DELETED:
7189 case MR_EVT_LD_CREATED:
7190 if (!instance->requestorId ||
7191 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7192 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
Yang, Boc9786842009-12-06 08:39:25 -07007193
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307194 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307195 doscan = SCAN_VD_CHANNEL;
Yang, Boc9786842009-12-06 08:39:25 -07007196
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307197 break;
7198
7199 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7200 case MR_EVT_FOREIGN_CFG_IMPORTED:
7201 case MR_EVT_LD_STATE_CHANGE:
7202 dcmd_ret = megasas_get_pd_list(instance);
7203
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307204 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307205 break;
7206
7207 if (!instance->requestorId ||
7208 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7209 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7210
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307211 if (dcmd_ret != DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307212 break;
7213
7214 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7215 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7216 instance->host->host_no);
7217 break;
7218
7219 case MR_EVT_CTRL_PROP_CHANGED:
7220 dcmd_ret = megasas_get_ctrl_info(instance);
7221 break;
7222 default:
7223 doscan = 0;
7224 break;
7225 }
7226 } else {
7227 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7228 mutex_unlock(&instance->reset_mutex);
7229 kfree(ev);
7230 return;
7231 }
7232
7233 mutex_unlock(&instance->reset_mutex);
7234
7235 if (doscan & SCAN_PD_CHANNEL) {
7236 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7237 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7238 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
Bjorn Helgaasda0dc9f2015-07-07 15:52:45 -05007239 sdev1 = scsi_device_lookup(host, i, j, 0);
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307240 if (instance->pd_list[pd_index].driveState ==
7241 MR_PD_STATE_SYSTEM) {
7242 if (!sdev1)
7243 scsi_add_device(host, i, j, 0);
7244 else
Yang, Boc9786842009-12-06 08:39:25 -07007245 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007246 } else {
7247 if (sdev1) {
7248 scsi_remove_device(sdev1);
7249 scsi_device_put(sdev1);
7250 }
7251 }
Yang, Boc9786842009-12-06 08:39:25 -07007252 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007253 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007254 }
7255
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307256 if (doscan & SCAN_VD_CHANNEL) {
7257 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7258 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7259 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7260 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7261 if (instance->ld_ids[ld_index] != 0xff) {
7262 if (!sdev1)
7263 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7264 else
7265 scsi_device_put(sdev1);
7266 } else {
7267 if (sdev1) {
7268 scsi_remove_device(sdev1);
7269 scsi_device_put(sdev1);
Yang, Boc9786842009-12-06 08:39:25 -07007270 }
7271 }
7272 }
7273 }
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007274 }
7275
Sumit Saxena6d40afb2016-01-28 21:04:23 +05307276 if (dcmd_ret == DCMD_SUCCESS)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307277 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7278 else
7279 seq_num = instance->last_seq_num;
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007280
7281 /* Register AEN with FW for latest sequence number plus 1 */
7282 class_locale.members.reserved = 0;
7283 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7284 class_locale.members.class = MR_EVT_CLASS_DEBUG;
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307285
7286 if (instance->aen_cmd != NULL) {
7287 kfree(ev);
7288 return;
7289 }
7290
7291 mutex_lock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007292 error = megasas_register_aen(instance, seq_num,
7293 class_locale.word);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007294 if (error)
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307295 dev_err(&instance->pdev->dev,
7296 "register aen failed error %x\n", error);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007297
Sumit Saxena11c71cb2016-01-28 21:04:22 +05307298 mutex_unlock(&instance->reset_mutex);
Yang, Bo7e8a75f2009-10-06 14:50:17 -06007299 kfree(ev);
7300}
7301
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007302/**
7303 * megasas_init - Driver load entry point
7304 */
7305static int __init megasas_init(void)
7306{
7307 int rval;
7308
7309 /*
Sumit Saxenac3e385a2016-04-15 00:23:30 -07007310 * Booted in kdump kernel, minimize memory footprints by
7311 * disabling few features
7312 */
7313 if (reset_devices) {
7314 msix_vectors = 1;
7315 rdpq_enable = 0;
7316 dual_qdepth_disable = 1;
7317 }
7318
7319 /*
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007320 * Announce driver version and other information
7321 */
Sumit.Saxena@avagotech.comd98a6de2014-11-17 15:23:58 +05307322 pr_info("megasas: %s\n", MEGASAS_VERSION);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007323
Kashyap Desaibd8d6dd2012-07-17 18:20:44 -07007324 spin_lock_init(&poll_aen_lock);
7325
Yang, Bo72c4fd32009-10-06 14:20:59 -06007326 support_poll_for_event = 2;
Yang, Bo837f5fe2010-10-11 06:59:20 -06007327 support_device_change = 1;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007328
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007329 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7330
7331 /*
7332 * Register character device node
7333 */
7334 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7335
7336 if (rval < 0) {
7337 printk(KERN_DEBUG "megasas: failed to open device node\n");
7338 return rval;
7339 }
7340
7341 megasas_mgmt_majorno = rval;
7342
7343 /*
7344 * Register ourselves as PCI hotplug module
7345 */
Michal Piotrowski4041b9c2006-08-17 13:28:22 +00007346 rval = pci_register_driver(&megasas_pci_driver);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007347
7348 if (rval) {
Masanari Iida6774def2014-11-05 22:26:48 +09007349 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
Jeff Garzik83aabc12006-10-04 06:34:03 -04007350 goto err_pcidrv;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007351 }
7352
Jeff Garzik83aabc12006-10-04 06:34:03 -04007353 rval = driver_create_file(&megasas_pci_driver.driver,
7354 &driver_attr_version);
7355 if (rval)
7356 goto err_dcf_attr_ver;
Yang, Bo72c4fd32009-10-06 14:20:59 -06007357
7358 rval = driver_create_file(&megasas_pci_driver.driver,
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307359 &driver_attr_release_date);
7360 if (rval)
7361 goto err_dcf_rel_date;
7362
7363 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007364 &driver_attr_support_poll_for_event);
7365 if (rval)
7366 goto err_dcf_support_poll_for_event;
7367
Jeff Garzik83aabc12006-10-04 06:34:03 -04007368 rval = driver_create_file(&megasas_pci_driver.driver,
7369 &driver_attr_dbg_lvl);
7370 if (rval)
7371 goto err_dcf_dbg_lvl;
bo yangad84db22007-11-09 04:40:16 -05007372 rval = driver_create_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007373 &driver_attr_support_device_change);
7374 if (rval)
7375 goto err_dcf_support_device_change;
7376
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007377 return rval;
bo yangad84db22007-11-09 04:40:16 -05007378
Yang, Bo837f5fe2010-10-11 06:59:20 -06007379err_dcf_support_device_change:
7380 driver_remove_file(&megasas_pci_driver.driver,
bo yangad84db22007-11-09 04:40:16 -05007381 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007382err_dcf_dbg_lvl:
7383 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo72c4fd32009-10-06 14:20:59 -06007384 &driver_attr_support_poll_for_event);
Yang, Bo72c4fd32009-10-06 14:20:59 -06007385err_dcf_support_poll_for_event:
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307386 driver_remove_file(&megasas_pci_driver.driver,
7387 &driver_attr_release_date);
7388err_dcf_rel_date:
Jeff Garzik83aabc12006-10-04 06:34:03 -04007389 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7390err_dcf_attr_ver:
7391 pci_unregister_driver(&megasas_pci_driver);
7392err_pcidrv:
7393 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
adam radford0d490162010-12-14 19:17:17 -08007394 return rval;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007395}
7396
7397/**
7398 * megasas_exit - Driver unload entry point
7399 */
7400static void __exit megasas_exit(void)
7401{
Sumant Patro658dced2006-10-03 13:09:14 -07007402 driver_remove_file(&megasas_pci_driver.driver,
7403 &driver_attr_dbg_lvl);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007404 driver_remove_file(&megasas_pci_driver.driver,
Yang, Bo837f5fe2010-10-11 06:59:20 -06007405 &driver_attr_support_poll_for_event);
7406 driver_remove_file(&megasas_pci_driver.driver,
7407 &driver_attr_support_device_change);
Sumit.Saxena@avagotech.com09fced12015-04-23 16:31:54 +05307408 driver_remove_file(&megasas_pci_driver.driver,
7409 &driver_attr_release_date);
Jeff Garzik83aabc12006-10-04 06:34:03 -04007410 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04007411
7412 pci_unregister_driver(&megasas_pci_driver);
7413 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7414}
7415
7416module_init(megasas_init);
7417module_exit(megasas_exit);